From 7615b02eb62aa63917daf924d69b4246d5a30e8d Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 15 Jul 2024 15:26:09 -0400 Subject: [PATCH] Proble with TIME_WAIT still remain --- lib/gestcp.c | 37 ++++++++++++++++++++++++++++-------- lib/gestcp.h | 6 ++++++ lib/modrec.c | 53 ++++++++++++++++++++++++++++++++++++++-------------- lib/unisoc.c | 6 ++++-- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/lib/gestcp.c b/lib/gestcp.c index 89475e4..6594a63 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -38,8 +38,6 @@ struct sockaddr addr; taille=sizeof(addr); contact=accept(binding->handle,&addr,&taille); -(void) rou_alert(0,"JMPDBG, getcontact. acct status='%d' errno=<%s>", - contact,strerror(errno)); if (contact<0) { switch (errno) { case EAGAIN : @@ -60,11 +58,12 @@ else { if ((flags=fcntl(contact,F_GETFL,0))<0) (void) rou_core_dump("%s Unable to get socket descripteur (error='%s')", - OPEP,strerror(errno)); - if ((flags=fcntl(contact,F_SETFL,flags|O_NONBLOCK|O_ASYNC))<0) - (void) rou_core_dump("%s Unable to set socket descripteur (error='%s')", + OPEP,strerror(errno)); + if ((flags=fcntl(contact,F_SETFL,flags|O_NONBLOCK|O_ASYNC))<0) + (void) rou_core_dump("%s Unable to set socket descripteur (error='%s')", OPEP,strerror(errno)); } +(void) rou_alert(0,"JMPDBG got contact channel='%d'",contact); return contact; #undef OPEP } @@ -115,7 +114,6 @@ while (proceed==true) { relax.tv_sec=1; //1 sec relax time relax.tv_usec=0; status=select(binding->handle+1,&reading,(fd_set *)0,(fd_set *)0,&relax); - (void) rou_alert(0,"JMPDBG select status='%d' errno='%d'",status,errno); switch (status) { case -1 : //got a wrong return switch (errno) { @@ -153,14 +151,13 @@ while (proceed==true) { } break; } - (void) rou_alert(0,"JMPDBG exiting from select phase='%d' contact='%d'", - phase,contact); break; default : //SAFE Guard proceed=false; break; } phase++; + (void) rou_alert(0,"JMPDB waitlisten contat='%d' phase='%d'",contact,phase); } return contact; #undef OPEP @@ -170,6 +167,28 @@ return contact; */ /********************************************************/ /* */ +/* Procedure to send data to a tcp socket. */ +/* return the number of character transmitted, is */ +/* unable to send char return -1; */ +/* */ +/********************************************************/ +PUBLIC int tcp_fprintf(CONTYP *contact,const char *format,...) + +{ +int sent; +va_list args; + +sent=0; +va_start(args,format); +sent=vdprintf(contact->channel,format,args); +va_end(args); +return sent; +} +/* +^L +*/ +/********************************************************/ +/* */ /* Procedure to wait for a remote client. */ /* return all reference to the established contact.*/ /* */ @@ -210,6 +229,7 @@ while (proceed==true) { proceed=false; break; } + (void) rou_alert(0,"JMPDB tcp_getcontact contat='%p' phase='%d'",contact,phase); phase++; } return contact; @@ -243,6 +263,7 @@ while (proceed==true) { } break; case 1 : //waiting from contact + (void) rou_alert(0,"JMPDBG shutdown contact channel='%d'",contact->channel); if (shutdown(contact->channel,SHUT_RDWR)<0) { switch (errno) { case ENOTCONN : //already disconnect by other side! diff --git a/lib/gestcp.h b/lib/gestcp.h index 55c3cb5..9a1fd36 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -16,6 +16,12 @@ typedef struct { SOCTYP *binding;//extablised contact context }CONTYP; +//Transmit formated data to the contact channel +extern int tcp_fprintf(CONTYP *contact,const char *format,...); + +//Wait for an input coming from the contact channel +extern int *tcp_fscanf(CONTYP *contact,const char *format,...); + //wait for an incoming contact extern CONTYP *tcp_getcontact(SOCTYP *binding); diff --git a/lib/modrec.c b/lib/modrec.c index 625b7f9..4739ff1 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -53,14 +53,18 @@ while (proceed==true) { break; case 2 : //do contact for (int i=0;ichannel,"JMPDBG pid=%d iteration=%d/%d\n", - getpid(),i,TESTL); - + sent=tcp_fprintf(contact,"JMPDBG pid=%d iter=%d/%d\n",getpid(),i,TESTL); + if (sent<0) { + (void) rou_alert(0,"JMPDBG tcp_fprintf error <%s>",strerror(errno)); + break; + } } break; case 3 : //close contact @@ -121,7 +125,6 @@ while (proceed==true) { (void) docontact(binding); //waiting, handling remote contact (void) rou_alert(0,"Contact Exiting: %s-%s",appname,rou_getversion()); (void) sleep(1); //avoiding avalanche - (void) closelog(); (void) exit(0); break; default : @@ -191,32 +194,52 @@ static void release(SOCTYP **bindings) { #define OPEP "modrec.c:release" +SOCTYP **ptr; int phase; _Bool proceed; +ptr=bindings; phase=0; proceed=true; while (proceed==true) { switch (phase) { case 0 : //making sur the list is defined - if (bindings==(SOCTYP **)0) { + if (ptr==(SOCTYP **)0) { (void) rou_alert(0,"%s bindings list pointer is NULL (Bug!?)",OPEP); phase=999; //not going further } break; case 1 : //check need to kill a dispatched process - while (*bindings!=(SOCTYP *)0) { - for (int i=0;i<(*bindings)->iteration;i++) { - if ((*bindings)->childs[i]==(pid_t)0) + ptr=bindings; + while (*ptr!=(SOCTYP *)0) { + for (int i=0;i<(*ptr)->iteration;i++) { + if ((*ptr)->childs[i]==(pid_t)0) continue; - if (prc_checkprocess((*bindings)->childs[i])==true) { + if (prc_checkprocess((*ptr)->childs[i])==true) { (void) rou_alert(0,"JMPDBG sending a SIGTERM process to '%d'", - (*bindings)->childs[i]); - (void) kill((*bindings)->childs[i],SIGTERM); - (*bindings)->childs[i]=(pid_t)0; + (*ptr)->childs[i]); + (void) kill((*ptr)->childs[i],SIGTERM); + (void) usleep(1000); } } - bindings++; + ptr++; + } + break; + case 2 : //checking if ALL process are now terminated + sleep(1); + (void) prc_nozombie(); + ptr=bindings; + while (*ptr!=(SOCTYP *)0) { + for (int i=0;i<(*ptr)->iteration;i++) { + if ((*ptr)->childs[i]==(pid_t)0) + continue; + if (prc_checkprocess((*ptr)->childs[i])==false) { + (*ptr)->childs[i]=(pid_t)0; + continue; + } + phase=0; //some process still remain killing again? + } + ptr++; } break; default : //SAFE Guard @@ -264,8 +287,10 @@ while (proceed==true) { if ((hangup==false)&&(reload==false)) phase--; //normal cycle, lets proceed to actvate again break; - case 3 : //closing all socket currently opened + case 3 : //release current binding (void) release(bindings); + break; + case 4 : //closing all socket currently opened (void) soc_mulclose(bindings); break; default : //SAFE Guard diff --git a/lib/unisoc.c b/lib/unisoc.c index 867d0dd..6070fc2 100644 --- a/lib/unisoc.c +++ b/lib/unisoc.c @@ -73,7 +73,8 @@ int phase; _Bool proceed; handle=-1; -(void) memset(&slg,'\000',sizeof(struct linger)); +slg.l_onoff=true; +slg.l_linger=0; /*0 sec timeout on close*/ buf=calloc(BFSZ,sizeof(char)); flags=0; phase=0; @@ -103,7 +104,7 @@ while (proceed==true) { phase=999; //trouble cleanup phase } break; - case 3 : //SO_LINGER allow Connection reset by peer" + case 3 : //SO_LINGER allow Connection reset by peer if (setsockopt(handle,SOL_SOCKET,SO_LINGER,&slg,sizeof(slg))<0) { (void) rou_alert(0,"%s Unable to set socket option " "SO_LINGER for <%s> (error='%s')", @@ -155,6 +156,7 @@ while (proceed==true) { phase++; } (void) free(buf); +(void) rou_alert(0,"JMPDBG got handle channel='%d'",handle); return handle; #undef OPEP } -- 2.47.3