]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Signal handling is better
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 31 Jul 2024 19:37:48 +0000 (15:37 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 31 Jul 2024 19:37:48 +0000 (15:37 -0400)
lib/modrec.c
lib/subrou.c
lib/ubrou.c [new file with mode: 0644]
lib/unisoc.c
lib/unisoc.h

index 4851da517633ef90e31d217418511d7fb66cf551..dc622933ad6f3472410bdb416da08685e2bde931 100644 (file)
@@ -163,69 +163,26 @@ while (proceed==true) {
 */
 /********************************************************/
 /*                                                     */
-/*      Procedure to kill all child process if necessary*/
+/*      Procedure to start a binding+waiting process    */
 /*                                                     */
 /********************************************************/
-static void release(SOCTYP **bindings)
+PUBLIC pid_t startwaiter(SOCTYP *binding)
 
 {
-#define OPEP    "modrec.c:release"
+pid_t waiter;
 
-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 (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
-      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])==true) {
-            (void) rou_alert(0,"JMPDBG sending a SIGTERM process to '%d'",
-                                (*ptr)->childs[i]);
-            (void) kill((*ptr)->childs[i],SIGTERM);
-            (void) usleep(1000);
-            }
-          }
-        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
-      proceed=false;
-      break;
-    }
-  phase++;
+if ((waiter=fork())==(pid_t)0) {
+  (void) closelog();
+  (void) openlog("Waiter",LOG_NDELAY|LOG_PID,LOG_DAEMON);
+  (void) prc_settitle("Daemon Waiting on %s:%s (Num of channels='%02d')",
+                       binding->ip,binding->port,binding->iteration);
+  (void) rou_alert(0,"JMPDBG starting waiter");
+  (void) sleep(20); 
+  (void) rou_alert(0,"JMPDBG waiter exiting");
+  (void) closelog();
+  (void) exit(0);
   }
-#undef  OPEP
+return waiter;
 }
 /*
 \f
@@ -239,11 +196,14 @@ PUBLIC void rec_handlesmtp()
 
 {
 #define OPEP    "modrec.c:rec_handlesmtp"
+#define LINE    "---------------------------------"
 
+int maxretry;   //how long checking sub process to be out
 SOCTYP **bindings;
 int phase;
 _Bool proceed;
 
+maxretry=30;     //5 seconds max retry
 bindings=(SOCTYP **)0;
 bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.1","2525",1);
 //bindings=soc_mkbindinf(bindings,pro_smtp,"127.0.0.26","2626",1);
@@ -253,32 +213,72 @@ while (proceed==true) {
   switch (phase) {
     case 0      :       //looping forever email receiving processes
       (void) prc_settitle("Emlrec Daemon");
-      (void) rou_alert(0,"-------------JMPDBG---------------");
+      (void) rou_alert(0,LINE);
+      (void) rou_alert(0,"Starting Emlrec daemon");
+      if (bindings==(SOCTYP **)0) {
+        (void) rou_alert(0,"No bindings definition found!");
+        phase=999;      //No need to go further
+        }
       break;
     case 1      :       //Opening ALL channels
-      if (soc_mulopen(bindings)==false) 
-        (void) rou_alert(0,"%s, Unable to open ALL socket (config?)",OPEP);
+      for (SOCTYP **ptr=bindings;(*ptr)!=(SOCTYP *)0;ptr++) {
+        (void) prc_nozombie();
+        if ((*ptr)->waiter==(pid_t)0)
+          continue;
+        if (prc_checkprocess((*ptr)->waiter)==false)
+          (*ptr)->waiter=(pid_t)0;
+        }
       break;
-    case 2      :       //Terminating all remaining process
-      (void) activate(bindings);
-      if ((hangup==false)&&(reload==false))
-        (void) sleep(5);
+    case 2      :       //starting restarting all waiter process
+      for (SOCTYP **ptr=bindings;(*ptr)!=(SOCTYP *)0;ptr++) {
+        if ((*ptr)->waiter!=(pid_t)0)
+          continue;
+        (*ptr)->waiter=startwaiter(*ptr);
+        }
+      break;
+    case 3      :       //relax time
+      (void) sleep(5);  //Waiting for signal
       if ((hangup==false)&&(reload==false))
-        phase--;        //normal cycle, lets proceed to actvate again
+        phase=0;        //Normal process, lets restart
       break;
-    case 3      :       //release current binding
-      (void) release(bindings);
+    case 4      :       //we got a signal
+      for (SOCTYP **ptr=bindings;(*ptr)!=(SOCTYP *)0;ptr++) {
+        if ((*ptr)->waiter==(pid_t)0)
+          continue;
+        if (prc_checkprocess((*ptr)->waiter)==false) {
+          (*ptr)->waiter=(pid_t)0;
+          continue;
+          }
+        if ((hangup==true)||(reload==true))
+          (void) kill((*ptr)->waiter,SIGTERM);
+        }
       break;
-    case 4      :       //closing all socket currently opened
-      (void) soc_mulclose(bindings);
+    case 5      :       //making sur all waiter procees are gone
+      maxretry--;
+      (void) sleep(1);
+      //(void) rou_alert(0,"JMPDBG cleanup! maxretry='%d'",maxretry);
+      for (SOCTYP **ptr=bindings;(*ptr)!=(SOCTYP *)0;ptr++) {
+       (void)  prc_nozombie();
+        if ((*ptr)->waiter==(pid_t)0)
+          continue;
+        if (prc_checkprocess((*ptr)->waiter)==true) {
+          if (maxretry>0) 
+            phase--;      //retrying
+          }
+        else
+          (*ptr)->waiter=(pid_t)0;
+        }
       break;
     default     :       //SAFE Guard
+      (void) rou_alert(0,"Stopping Emlrec daemon");
+      (void) rou_alert(0,LINE);
       proceed=false;
       break;
     }
   phase++;
   }
 bindings=soc_freebindinf(bindings);
+#undef  LINE
 #undef  OPEP
 }
 /*
index 458ce00e1f32cb553690a773c8bf835fa9f0defc..41807fa1c71484a47727849958d2a2ea81cff5f5 100644 (file)
@@ -235,7 +235,8 @@ if (debug>=dlevel)
       (void) syslog(LOG_INFO,"%s",locline);
       ptr +=DEBMAX;
       } 
-    (void) syslog(LOG_INFO,"%s",ptr);
+    if (strlen(ptr)>0)
+      (void) syslog(LOG_INFO,"%s",ptr);
     }
   }
 }
diff --git a/lib/ubrou.c b/lib/ubrou.c
new file mode 100644 (file)
index 0000000..90ea5bd
--- /dev/null
@@ -0,0 +1,186 @@
+\e[4mSYSLOG\e[24m(3)                   Linux Programmer’s Manual                   \e[4mSYSLOG\e[24m(3)
+
+\e[1mNAME\e[0m
+       closelog, openlog, syslog, vsyslog - send messages to the system logger
+
+\e[1mSYNOPSIS\e[0m
+       \e[1m#include <syslog.h>\e[0m
+
+       \e[1mvoid openlog(const char *\e[4m\e[22mident\e[24m\e[1m, int \e[4m\e[22moption\e[24m\e[1m, int \e[4m\e[22mfacility\e[24m\e[1m);\e[0m
+       \e[1mvoid syslog(int \e[4m\e[22mpriority\e[24m\e[1m, const char *\e[4m\e[22mformat\e[24m\e[1m, ...);\e[0m
+       \e[1mvoid closelog(void);\e[0m
+
+       \e[1mvoid vsyslog(int \e[4m\e[22mpriority\e[24m\e[1m, const char *\e[4m\e[22mformat\e[24m\e[1m, va_list \e[4m\e[22map\e[24m\e[1m);\e[0m
+
+   Feature Test Macro Requirements for glibc (see \e[1mfeature_test_macros\e[22m(7)):
+
+       \e[1mvsyslog\e[22m():
+           Since glibc 2.19:
+               _DEFAULT_SOURCE
+           Glibc 2.19 and earlier:
+               _BSD_SOURCE
+
+\e[1mDESCRIPTION\e[0m
+   \e[1mopenlog()\e[0m
+       \e[1mopenlog\e[22m() opens a connection to the system logger for a program.
+
+       The string pointed to by \e[4mident\e[24m is prepended to every message, and is typi‐
+       cally  set  to  the  program  name.  If \e[4mident\e[24m is NULL, the program name is
+       used.  (POSIX.1‐2008 does not specify the behavior when \e[4mident\e[24m is NULL.)
+
+       The \e[4moption\e[24m argument specifies flags which control the operation  of  \e[1mopen‐\e[0m
+       \e[1mlog\e[22m() and subsequent calls to \e[1msyslog\e[22m().  The \e[4mfacility\e[24m argument establishes
+       a default to be used if none is specified in subsequent calls to \e[1msyslog\e[22m().
+       The values that may be specified for \e[4moption\e[24m and \e[4mfacility\e[24m are described be‐
+       low.
+
+       The  use of \e[1mopenlog\e[22m() is optional; it will automatically be called by \e[1msys‐\e[0m
+       \e[1mlog\e[22m() if necessary, in which case \e[4mident\e[24m will default to NULL.
+
+   \e[1msyslog() and vsyslog()\e[0m
+       \e[1msyslog\e[22m() generates a log message, which will be distributed by \e[1msyslogd\e[22m(8).
+
+       The \e[4mpriority\e[24m argument is formed by ORing together a \e[4mfacility\e[24m value  and  a
+       \e[4mlevel\e[24m  value  (described below).  If no \e[4mfacility\e[24m value is ORed into \e[4mprior‐\e[0m
+       \e[4mity\e[24m, then the default value set by \e[1mopenlog\e[22m() is used, or, if there was  no
+       preceding \e[1mopenlog\e[22m() call, a default of \e[1mLOG_USER \e[22mis employed.
+
+       The  remaining  arguments are a \e[4mformat\e[24m, as in \e[1mprintf\e[22m(3), and any arguments
+       required by the \e[4mformat\e[24m, except that the two‐character sequence \e[1m%m \e[22mwill  be
+       replaced  by  the error message string \e[4mstrerror\e[24m(\e[4merrno\e[24m).  The format string
+       need not include a terminating newline character.
+
+       The function \e[1mvsyslog\e[22m() performs the same task as \e[1msyslog\e[22m() with the differ‐
+       ence that it takes a set of arguments which have been obtained  using  the
+       \e[1mstdarg\e[22m(3) variable argument list macros.
+
+   \e[1mcloselog()\e[0m
+       \e[1mcloselog\e[22m()  closes  the  file descriptor being used to write to the system
+       logger.  The use of \e[1mcloselog\e[22m() is optional.
+
+   \e[1mValues for \e[4moption\e[0m
+       The \e[4moption\e[24m argument to \e[1mopenlog\e[22m() is a bit mask constructed  by  ORing  to‐
+       gether any of the following values:
+
+       \e[1mLOG_CONS       \e[22mWrite  directly  to the system console if there is an error
+                      while sending to the system logger.
+
+       \e[1mLOG_NDELAY     \e[22mOpen the connection immediately (normally,  the  connection
+                      is  opened  when the first message is logged).  This may be
+                      useful, for example, if a subsequent \e[1mchroot\e[22m(2)  would  make
+                      the  pathname  used  internally by the logging facility un‐
+                      reachable.
+
+       \e[1mLOG_NOWAIT     \e[22mDon’t wait for child processes that may have  been  created
+                      while  logging  the  message.   (The GNU C library does not
+                      create a child process, so this option  has  no  effect  on
+                      Linux.)
+
+       \e[1mLOG_ODELAY     \e[22mThe  converse  of  \e[1mLOG_NDELAY\e[22m; opening of the connection is
+                      delayed until \e[1msyslog\e[22m() is called.  (This  is  the  default,
+                      and need not be specified.)
+
+       \e[1mLOG_PERROR     \e[22m(Not  in  POSIX.1‐2001 or POSIX.1‐2008.)  Also log the mes‐
+                      sage to \e[4mstderr\e[24m.
+
+       \e[1mLOG_PID        \e[22mInclude the caller’s PID with each message.
+
+   \e[1mValues for \e[4mfacility\e[0m
+       The \e[4mfacility\e[24m argument is used to specify what type of program  is  logging
+       the  message.  This lets the configuration file specify that messages from
+       different facilities will be handled differently.
+
+       \e[1mLOG_AUTH       \e[22msecurity/authorization messages
+
+       \e[1mLOG_AUTHPRIV   \e[22msecurity/authorization messages (private)
+
+       \e[1mLOG_CRON       \e[22mclock daemon (\e[1mcron \e[22mand \e[1mat\e[22m)
+
+       \e[1mLOG_DAEMON     \e[22msystem daemons without separate facility value
+
+       \e[1mLOG_FTP        \e[22mftp daemon
+
+       \e[1mLOG_KERN       \e[22mkernel  messages  (these  can’t  be  generated  from   user
+                      processes)
+
+       \e[1mLOG_LOCAL0 \e[22mthrough \e[1mLOG_LOCAL7\e[0m
+                      reserved for local use
+
+       \e[1mLOG_LPR        \e[22mline printer subsystem
+
+       \e[1mLOG_MAIL       \e[22mmail subsystem
+
+       \e[1mLOG_NEWS       \e[22mUSENET news subsystem
+
+       \e[1mLOG_SYSLOG     \e[22mmessages generated internally by \e[1msyslogd\e[22m(8)
+
+       \e[1mLOG_USER \e[22m(default)
+                      generic user‐level messages
+
+       \e[1mLOG_UUCP       \e[22mUUCP subsystem
+
+   \e[1mValues for \e[4mlevel\e[0m
+       This  determines  the importance of the message.  The levels are, in order
+       of decreasing importance:
+
+       \e[1mLOG_EMERG      \e[22msystem is unusable
+
+       \e[1mLOG_ALERT      \e[22maction must be taken immediately
+
+       \e[1mLOG_CRIT       \e[22mcritical conditions
+
+       \e[1mLOG_ERR        \e[22merror conditions
+
+       \e[1mLOG_WARNING    \e[22mwarning conditions
+
+       \e[1mLOG_NOTICE     \e[22mnormal, but significant, condition
+
+       \e[1mLOG_INFO       \e[22minformational message
+
+       \e[1mLOG_DEBUG      \e[22mdebug‐level message
+
+       The function \e[1msetlogmask\e[22m(3) can be used to restrict  logging  to  specified
+       levels only.
+
+\e[1mATTRIBUTES\e[0m
+       For an explanation of the terms used in this section, see \e[1mattributes\e[22m(7).
+       ┌───────────────────────┬───────────────┬────────────────────┐
+       │ \e[1mInterface             \e[22m│ \e[1mAttribute     \e[22m│ \e[1mValue              \e[22m│
+       ├───────────────────────┼───────────────┼────────────────────┤
+       │ \e[1mopenlog\e[22m(), \e[1mcloselog\e[22m() │ Thread safety │ MT‐Safe            │
+       ├───────────────────────┼───────────────┼────────────────────┤
+       │ \e[1msyslog\e[22m(), \e[1mvsyslog\e[22m()   │ Thread safety │ MT‐Safe env locale │
+       └───────────────────────┴───────────────┴────────────────────┘
+
+\e[1mCONFORMING TO\e[0m
+       The  functions \e[1mopenlog\e[22m(), \e[1mcloselog\e[22m(), and \e[1msyslog\e[22m() (but not \e[1mvsyslog\e[22m()) are
+       specified in SUSv2, POSIX.1‐2001, and POSIX.1‐2008.
+
+       POSIX.1‐2001 specifies only the \e[1mLOG_USER \e[22mand \e[1mLOG_LOCAL* \e[22mvalues for  \e[4mfacil‐\e[0m
+       \e[4mity\e[24m.   However,  with the exception of \e[1mLOG_AUTHPRIV \e[22mand \e[1mLOG_FTP\e[22m, the other
+       \e[4mfacility\e[24m values appear on most UNIX systems.
+
+       The \e[1mLOG_PERROR \e[22mvalue for  \e[4moption\e[24m  is  not  specified  by  POSIX.1‐2001  or
+       POSIX.1‐2008, but is available in most versions of UNIX.
+
+\e[1mNOTES\e[0m
+       The  argument  \e[4mident\e[24m  in  the  call of \e[1mopenlog\e[22m() is probably stored as‐is.
+       Thus, if the string it points to is changed, \e[1msyslog\e[22m() may start prepending
+       the changed string, and if the string it points to ceases  to  exist,  the
+       results are undefined.  Most portable is to use a string constant.
+
+       Never pass a string with user‐supplied data as a format, use the following
+       instead:
+
+           syslog(priority, "%s", string);
+
+\e[1mSEE ALSO\e[0m
+       \e[1mjournalctl\e[22m(1), \e[1mlogger\e[22m(1), \e[1msetlogmask\e[22m(3), \e[1msyslog.conf\e[22m(5), \e[1msyslogd\e[22m(8)
+
+\e[1mCOLOPHON\e[0m
+       This  page  is part of release 5.09 of the Linux \e[4mman‐pages\e[24m project.  A de‐
+       scription of the project, information about reporting bugs, and the latest
+       version      of      this      page,      can      be       found       at
+       https://www.kernel.org/doc/man-pages/.
+
+Linux                               2017‐09‐15                          \e[4mSYSLOG\e[24m(3)
index 71624c1b226eac88e2ac45be97bfb182b1c78495..cbd05a520a2dc89d3dbce7e2b43f5d7fcc8f2a59 100644 (file)
@@ -44,7 +44,6 @@ 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; 
   }
@@ -179,7 +178,6 @@ 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 d4e386008e97b5a343f775253704e281f4bd02f2..f1235e8e1d2c1e114ac5198d91e0e2634274b28a 100644 (file)
@@ -30,7 +30,7 @@ typedef struct  {
         time_t lasttry; //successful binding last time
         int iteration;  //number of soc slot used on the IP
         int handle;     //connexion handle
-        pid_t *childs;  //all process connected to this handle
+        pid_t waiter;   //binding manager
         }SOCTYP;