]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding module moderec, testing receiveing email sub process
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 10 Jul 2024 05:04:42 +0000 (01:04 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Wed, 10 Jul 2024 05:04:42 +0000 (01:04 -0400)
app/Makefile
app/maild.c
lib/Makefile
lib/modrec.c [new file with mode: 0644]
lib/modrec.h [new file with mode: 0644]
lib/subrou.c
lib/uniprc.c
lib/uniprc.h

index 6d7ee495036cbc5ff9b1094bac3d024e4f55d898..4311de51e16b179509f4cd86fcc124bf80c1094f 100644 (file)
@@ -42,6 +42,8 @@ maild :  toremake maild.o
 
 
 maild.o        :  maild.c                              \
+          ../lib/modrec.h                      \
+          ../lib/uniprc.h                      \
           ../lib/unipar.h                      \
           ../lib/subrou.h                      \
 
index bea11beaab366f1676db943ea812516fecd1aee6..6067d1aec46d9b80d92f2ee8963fbb4c0e645971 100644 (file)
@@ -4,6 +4,7 @@
 /*     Main SMTP Daemon                                */
 /*                                                     */
 /********************************************************/
+#include       <signal.h>
 #include       <stdlib.h>
 #include       <stdio.h>
 #include       <unistd.h>
 #include       "subrou.h"
 #include       "unipar.h"
 #include       "uniprc.h"
+#include       "modrec.h"
 
+//port listening format is "IP:PORT NUMBER:num iteration"
+#define PORTREC "192.168.0.1:25:2"
 /*
 \f
 */
 /*      Program main task                               */
 /*                                                     */
 /********************************************************/
-static void task()
+static void task(u_int iteration)
 
 {
-(void) rou_alert(0,"JMPDBG starting task");
-(void) prc_settitle("JMPDBG bingo!");
-(void) sleep(10);
-(void) rou_alert(0,"JMPDBG task completed");
+pid_t *pidlst;
+int phase;
+_Bool proceed;
+
+pidlst=(pid_t *)calloc(iteration,sizeof(pid_t));
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //looping forever email receiving processes
+      for (int i=0;i<iteration;i++) {
+        (void) random();
+        if (pidlst[i]!=(pid_t)0) {
+          if (prc_checkprocess(pidlst[i])==true) 
+            continue;
+          }
+        switch (pidlst[i]=fork()) {
+          case -1       :       //trouble trouble?
+            pidlst[i]=(pid_t)0;
+            break;
+          case  0       :
+            (void) rec_getemail();
+            (void) exit(0);     //email receiving task terminated
+            break;
+          default       :       
+            break;
+          }
+        }
+      phase--;                  //let try again
+      (void) sleep(2);          //next check in 5 second
+      (void) prc_nozombie();
+      break;
+    case 1      :       //Terminating all remaining process
+      if (pidlst!=(pid_t *)0) { //always
+        u_int stillrun;
+  
+        stillrun=0;
+        for (int i=0;i<iteration;i++) {
+          if (pidlst[i]==(pid_t)0)
+            continue;
+          if (prc_checkprocess(pidlst[i])==true) {
+            (void) kill(pidlst[i],SIGQUIT);
+            stillrun++;
+            }
+          else
+            pidlst[i]=(pid_t)0;
+          }
+        if (stillrun>0) {
+          (void) sleep(2);
+          phase--;       //let send signal again
+          }
+        }
+      break;
+    default     :       //SAFE Guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+(void) free(pidlst);
 }
 /*
 \f
@@ -68,12 +128,14 @@ while (proceed==true) {
       (void) rou_modesubrou(true);
       (void) par_modeunipar(true);
       (void) prc_modeuniprc(true);
+      (void) rec_modemodrec(true);
       break;
     case 3      :       //doing main tash
-      (void) task();
+      (void) task(5);
       break;
     case 4      :       //doing main tash
       (void) prc_cleantitle();
+      (void) rec_modemodrec(false);
       (void) prc_modeuniprc(false);
       (void) par_modeunipar(false);
       (void) rou_modesubrou(false);
index d0a2cec202acf49270c84d13ba72fed3a093d42d..b31bbf9dd9b48c9b6d42c778ef9d827b33fac74e 100644 (file)
@@ -16,6 +16,7 @@ clean :
 #Equivalences
 #--------------------------------------------------------------------
 OBJS=                                          \
+         modrec.o                              \
          unipar.o uniprc.o                     \
          subrou.o 
 
@@ -24,6 +25,9 @@ objs  : $(OBJS)
 
 #--------------------------------------------------------------------
 #Dependances
+modrec.o:                                      \
+          subrou.h                             \
+          modrec.h modrec.c
 
 uniprc.o:                                      \
           subrou.h                             \
diff --git a/lib/modrec.c b/lib/modrec.c
new file mode 100644 (file)
index 0000000..bd06d68
--- /dev/null
@@ -0,0 +1,76 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Module to handle all email incoming             */
+/*                                                     */
+/********************************************************/
+#include        <stdio.h>
+#include        <stdlib.h>
+#include        <unistd.h>
+
+#include        "subrou.h"
+#include        "uniprc.h"
+#include        "modrec.h"
+
+static  _Bool modopen;          //boolean module open/close
+
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*                                                     */
+/********************************************************/
+//procedure to receive email form outside
+void rec_getemail()
+
+{
+int attend;
+
+attend=(random()%7)+2;
+(void) rou_alert(0,"JMPDBG getemail with pid='%06d' (wait for '%0d')",
+                    getpid(),attend);
+(void) prc_settitle("JMPDBG getemail with pid='%06d' (wait for '%0d')",
+                    getpid(),attend);
+(void) sleep(attend);
+(void) rou_alert(0,"JMPDBG exiting from  pid='%06d'",getpid());
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
+/*     Procedure to "open/close" module and do         */
+/*      homework purpose                                */
+/*      return zero if everything right                 */
+/*                                                      */
+/********************************************************/
+int rec_modemodrec(_Bool mode)
+
+{
+#define OPEP    "mode.c:rec_modemoderec"
+
+int status;
+
+status=0;
+if (mode!=modopen) {
+  switch ((int)mode) {
+    case true     :
+      (void) rou_modesubrou(mode);
+      (void) prc_modeuniprc(mode);
+      break;
+    case false    :
+      (void) prc_modeuniprc(mode);
+      (void) rou_modesubrou(mode);
+      break;
+    default       :
+      (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):",
+                            OPEP,(int)mode);
+      status=-1;
+      break;
+    }
+  modopen=mode;
+  }
+return status;
+#undef  OPEP
+}
diff --git a/lib/modrec.h b/lib/modrec.h
new file mode 100644 (file)
index 0000000..f806343
--- /dev/null
@@ -0,0 +1,18 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/************************************************/
+/*                                             */
+/*     Module to handle all incoming email     */
+/*                                             */
+/************************************************/
+#ifndef        MODREC
+#define MODREC
+
+#include        <stdbool.h>
+
+//procedure to receive email form outside
+extern void rec_getemail();
+
+//homework to be done before starting/stoping module.
+extern int rec_modemodrec(_Bool mode);
+
+#endif
index ae0ddd998e27acf945baafa4fda3d0d86418d663..c47a0efa528ea6745d507201b4eba39d3fb10acf 100644 (file)
@@ -4,6 +4,7 @@
 /*     Module for low level subroutine                 */
 /*                                                     */
 /********************************************************/
+#include        <math.h>
 #include        <stdio.h>
 #include        <stdlib.h>
 #include        <string.h>
@@ -14,7 +15,7 @@
 
 //version definition 
 #define VERSION "0.1"
-#define RELEASE "10"
+#define RELEASE "11"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level
@@ -181,6 +182,7 @@ if (mode!=modopen) {
       debug=0;
       off64_time=(time_t)0;
       off_date=(time_t)0;
+      (void) srand((int)(M_PI*100000000));
       if (appname!=(char *)0)
         (void) free(appname);
       appname=strdup(APPNAME);
index 4ff3b290f430c4c95e7a56068bd9be9d6a3045f8..d56d47b098388e3ccf890a3c3f4ea90b85743a12 100644 (file)
@@ -7,6 +7,7 @@
 #include       <sys/prctl.h>
 #include       <sys/resource.h>
 #include       <sys/stat.h>
+#include       <sys/wait.h>
 #include       <dirent.h>
 #include       <errno.h>
 #include       <fcntl.h>
@@ -69,6 +70,21 @@ if (setrlimit(RLIMIT_CORE,&limites)<0) {
 */
 /********************************************************/
 /*                                                      */
+/*                                                     */
+/*     Procedure to allow exited child process         */
+/*     to leave the zombie stat.                       */
+/*                                                     */
+/********************************************************/
+void prc_nozombie()
+
+{
+while (waitpid(-1,(int *)0,WNOHANG)>0);
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
 /*     procedure to check if a process is              */
 /*     still up and running.                           */
 /*                                                      */
index d188ae27c71168d2f8e608e43039f07aa701c5c5..e197a602cde26ad0ca98a3771cac5cf01d914d34 100644 (file)
@@ -35,6 +35,10 @@ extern void prc_allow_core_dump();
 //with an explication message
 extern void prc_core_dump(const char *fmt,...);
 
+//routine to make sure all child process are out of
+//zombie state
+extern void prc_nozombie();
+
 //routine to check if a proces is still up and running
 extern _Bool prc_checkprocess(pid_t pidnumber);