int3
int3

Reputation: 658

How to "bypass" pthreads limit number

I have a little problem here. I know Linux limits the number of threads of an user can actually run.

I'm using pthread_create and an array of pthread_t limited with 50 ( pthread_t tid[50]; ). I have a for cycle that each time that limit reaches 50 every thread on pthread_t array is killed.

How? I tested almost everything. with pthread_kill(tid[w],SIGKILL); w is a simple cycle control variable goes from 0 to 50. I already tested pthread_cancel(tid[w]); and the problem keeps.

So what is the problem? Everytime I reach 380 thread number I can't create more. But I'm killing with cancel or kill. So what is happening?

The objective of the program is a network scanner. To be faster I need like 500 threads with like 2 seconds of timeout to test IP's and ports.

Anyone knows how to "go arround" this problem? I thought that I could kill the thread it would solve the problem but I was wrong :(

Without use ulimit or in /proc/sys/kernel/threads_max changing values, I looked at pthread_attr_setstacksize but I'm a bit confused :P

any ideas?

EDIT The code as requested :P I'm going to put ALL code here:

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>

#ifndef SOL_TCP
    #define SOL_TCP 6
#endif
#ifndef TCP_USER_TIMEOUT
    #define TCP_USER_TIMEOUT 18 //retry
#endif
#define MAX_TH 250


struct ar_stc{
                char* ip;
                int port;
};

char* ret[2];
int porar[2];
pthread_t tid[MAX_TH];

void create_port_scan_th(char* host,int p,int j);

//cares about args.
//this is not helpful for the threads post on stackoverflow. skip this function
char** arguments_handle(int argc,char **arg)
{
        char p[]="-p";
        char h[]="-h";
        size_t _p,_h;
        _p=(size_t)strlen(p);
        _h=(size_t)strlen(h);
        if(argc!=5)
        {
                printf("Usage:./file -p PORT-RANGE -h HOST.IP\n");
                exit(1);
        }
        if(strncmp(arg[1],p,_p)==0 || strncmp(arg[1],h,_h)==0 && strncmp(arg[3],p,_p)==0 || strncmp(arg[3],h,_h)==0)
        {
                if(strncmp(arg[1],p,_p)==0)
                {
                        strncpy(ret[0],arg[2],strlen(arg[2]));
                }
                else
                {
                        strncpy(ret[1],arg[2],strlen(arg[2]));
                }
                if(strncmp(arg[3],h,_h)==0)
                {
                        strncpy(ret[1],arg[4],strlen(arg[4]));
                }
                else
                {
                        strncpy(ret[0],arg[4],strlen(arg[4]));
                }
        }
        return ret;
}

int* take_ports(char *arg)
{
        char* ports[2];
        ports[0] = malloc(5);
        ports[1] = malloc(5);

        memset(ports[0],0,5);
        memset(ports[1],0,5);

        char tmp[5];

        int len = strlen(arg);
        int i,j=0,x=0;
        char min_p[5],max_p[5];
        for(i=0;i<len;i++)
        {
                if(arg[i]=='-')
                {
                        min_p[x]='\0';
                        j=1;
                        x=0;
                        continue;
                }
                else
                {
                        if(j==0)
                                min_p[x]=arg[i];
                        else
                                max_p[x]=arg[i];
                }
                x++;
        }
        max_p[x]='\0';

        porar[1]=atoi(max_p);
        porar[0]=atoi(min_p);
        free(ports[0]);
        free(ports[1]);
        return porar;
}

void *check_port(void* ar_p)
{
        struct ar_stc *ar =ar_p;
        char* ip = ar->ip;
        int port = ar->port;

        int s,conexao;
        int timeout = 1000; //1 second timeout

        s=socket(AF_INET,SOCK_STREAM,0);
        struct sockaddr_in dst;
        setsockopt(s,SOL_TCP,TCP_USER_TIMEOUT,(char*)&timeout,sizeof(timeout)); //NOT WORKING :(
        if(s<0)
        {
                printf("\nCouldnt create socket\nPremissions maybe?\n");
                exit(1);
        }

        dst.sin_family = AF_INET;
        dst.sin_port = htons(port);
        dst.sin_addr.s_addr = inet_addr(ip);
        bzero(&(dst.sin_zero),8);
        //printf("\nChecking: %d...",port);
        conexao = connect(s,(struct sockaddr*)&dst,sizeof(dst));
        if(conexao <0)
        {
                printf("TCP/%d:CLOSED!\n",port); //just to make sure the thread is running
                close(s);
                return;
        }
        else
        {
                printf("TCP/%d:OPEN!\n",port);
                close(s);
                return;
        }

}

int main(int argc, char **argv)
{
        int open_ports[65535];
        int open_ports_count=0;
        int min_p,max_p;
        int* p;

        ret[0] = malloc(20);
        ret[1] = malloc(20);

        memset(ret[0],0,20);
        memset(ret[1],0,20);

        char** ipnport;

        ipnport = arguments_handle(argc,argv);
        printf("The IP is :%s and the range is %s\n",ipnport[1],ipnport[0]);
        p=take_ports(ipnport[0]);
        min_p=p[0];
        max_p=p[1];

        printf("Min port:%d e max port:%d\n",min_p,max_p);
        int i;
        int thread_count=-1;
        for(i=min_p;i<=max_p;i++)
        {
                thread_count++;
                create_port_scan_th(ipnport[1],i,thread_count);
                if(thread_count>=MAX_TH)
                {
                        sleep(1);
                        thread_count=0;
                        int w;
                        for(w=0;w<=MAX_TH;w++)
                        {
                                pthread_kill(tid[w],SIGKILL);
                        }
                }
        }

        free(ret[0]);
        free(ret[1]);

        return 0x0;
}
void create_port_scan_th(char* host,int p,int j)
{
        int error;
        struct ar_stc *ar;
        ar = malloc(sizeof(*ar));

        ar->ip=host;
        ar->port=p;

        error = pthread_create(&(tid[j]),NULL,&check_port,(void*)ar);
        if(error!=0)
                printf("\nError creating thread:%s\n",strerror(error));
}

Upvotes: 0

Views: 515

Answers (1)

nos
nos

Reputation: 229058

But I'm killing with cancel or kill.

First of all, pthread_kill does not kill or end a thread. (see more at pthread_kill doesnt kill thread C linux or When to use pthread_cancel and not pthread_kill).

If you send SIGKILL to a thread, the entire process will end.

To end a thread, you need to

  1. Make the thread end.

    • by returning from the thread function, or
    • calling pthread_exit or
    • pthread_cancel the thread
  2. Dispose the resources tied to the thread by:

    • Call pthread_join() on the thread or
    • make the thread a detached thread.

If you opt for the last point by making the thread detached - which will automatically release the thread when it ends , you can call pthread_detach(pthread_Self()) at the start of your thread function. Or supply a pthread_attr_t when you call pthread_create(), where you set the thread to a detached state.

As for the total number of threads you can use, linux have a limit on the total number of threads/processes any user can have running.

You can view this with the command ulimit -u

Upvotes: 1

Related Questions