]> SAFE projects GIT repository - jmp/mailleur/commitdiff
able to start process on the ip:port binding
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 13 Jul 2024 07:15:08 +0000 (03:15 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 13 Jul 2024 07:15:08 +0000 (03:15 -0400)
lib/modrec.c
lib/unisoc.c
lib/unisoc.h

index e0bd28190c076845bc0b1faf22c6db77621dbfe2..1a3b3adb23e9d6ae2a080da5e1aaff0c15502baf 100644 (file)
@@ -6,6 +6,7 @@
 /********************************************************/
 #include        <errno.h>
 #include        <stdio.h>
+#include        <stdlib.h>
 #include        <string.h>
 #include        <unistd.h>
 
@@ -21,6 +22,126 @@ static  _Bool modopen;          //boolean module open/close
 */
 /********************************************************/
 /*                                                     */
+/*      kleenex procedure                               */
+/*                                                     */
+/********************************************************/
+static void kludge(SOCTYP *binding,int attend)
+
+{
+for (int i=0;i<attend;i++) {
+  (void) prc_settitle("Kluge for %d seconds, ip:port=<%s:%s>",
+                     attend-i,binding->ip,binding->port);
+  (void) sleep(1);
+  }
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*      Procedure to dispatch sub process               */
+/*                                                     */
+/********************************************************/
+static void dispatching(SOCTYP *binding)
+
+{
+#define OPEP    "modrec.c:dispatching"
+
+int phase;
+_Bool proceed;
+
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //dispatched  process still alive
+      (void) prc_nozombie();
+      for (int i=0;i<binding->iteration;i++) {
+        if (binding->childs[i]==(pid_t)0)
+          continue;
+        if (prc_checkprocess(binding->childs[i])==false) {
+          (void) rou_alert(3,"%s, Process '%05d' exited",OPEP,binding->childs[i]);
+          binding->childs[i]=(pid_t)0;
+          }
+        }
+      break;
+    case 1      :       //dispating need process
+      for (int i=0;i<binding->iteration;i++) {
+        int attend;
+
+        attend=(random()%60)+10;
+        if (binding->childs[i]!=(pid_t)0)
+          continue;
+        switch(binding->childs[i]=fork()) {
+          case -1       :
+            (void) rou_alert(0,"%s, Unable to start process to "
+                                "handle <%s:%s> socket (config?)",
+                                OPEP,binding->ip,binding->port);
+            break;
+          case 0        :       //the dispatched process
+            (void) kludge(binding,attend);
+            (void) sleep(1);    //avoiding avalanche
+            (void) exit(0);
+            break;
+          default       :
+            (void) usleep(10000);       //wait for next fork
+            break;
+          }
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*      Procedure to active child process if necessary  */
+/*                                                     */
+/********************************************************/
+static void activate(SOCTYP **bindings)
+
+{
+#define OPEP    "modrec.c:activate"
+
+int phase;
+_Bool proceed;
+
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //making sur the list is defined
+       if (bindings==(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 dispatch a process
+      while (*bindings!=(SOCTYP *)0) {
+        (void) dispatching(*bindings);
+        bindings++;
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+#undef  OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
 /*      Waiting and handling smtp request               */
 /*                                                     */
 /********************************************************/
@@ -48,9 +169,10 @@ while (proceed==true) {
         (void) rou_alert(0,"%s, Unable to open ALL socket (config?)",OPEP);
       break;
     case 2      :       //Terminating all remaining process
-      (void) fprintf(stderr,"JMPDBG wait 5 sec\n");
+      (void) activate(bindings);
       (void) sleep(5);
-      (void) fprintf(stderr,"JMPDBG wleep completed\n");
+      if ((hangup==false)&&(reload==false))
+        phase--;        //normal cycle, lets proceed to actvate again
       break;
     case 3      :       //closing all socket openend
       (void) soc_mulclose(bindings);
index f9c5efbdd6de0c50f9e319b570a56db92dece21a..c9affa2cda48dae5da57d2682fe53b3d5134f331 100644 (file)
@@ -44,6 +44,7 @@ static SOCTYP *freebinding(SOCTYP *binding)
 if (binding!=(SOCTYP *)0) {
   binding->port=rou_freestr(binding->port);
   binding->ip=rou_freestr(binding->ip);
+  (void) free(binding->childs);
   (void) free(binding);
   binding=(SOCTYP *)0; 
   }
@@ -200,6 +201,7 @@ bininf->proto=proto;
 bininf->ip=strdup(ip);
 bininf->port=strdup(port);
 bininf->iteration=iteration;
+bininf->childs=(pid_t *)calloc(iteration,sizeof(pid_t));
 bindings=(SOCTYP **)rou_addlist((void **)bindings,(void *)bininf);
 return bindings;
 }
index 975948b5dfcb33c5b81a5fcf6f6ba7680a3e98ec..bb4213585158aa7e27629d9ff8b455699f49993b 100644 (file)
@@ -24,8 +24,9 @@ typedef struct  {
         char *ip;       //Binding IP    //IPV4 or IPV6
         char *port;     //Binding Port
         time_t lasttry; //successful binding last time
-        int iteration;  //number of handle ready on the soc
+        int iteration;  //number of soc slot used on the IP
         int handle;     //connexion handle
+        pid_t *childs;  //all process connected to this handle
         }SOCTYP;
 
 //procedure to free all memory used by a TCP socket