maild.o : maild.c \
+ ../lib/modrec.h \
+ ../lib/uniprc.h \
../lib/unipar.h \
../lib/subrou.h \
/* 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
(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);
#Equivalences
#--------------------------------------------------------------------
OBJS= \
+ modrec.o \
unipar.o uniprc.o \
subrou.o
#--------------------------------------------------------------------
#Dependances
+modrec.o: \
+ subrou.h \
+ modrec.h modrec.c
uniprc.o: \
subrou.h \
--- /dev/null
+// 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
+}
--- /dev/null
+// 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
/* Module for low level subroutine */
/* */
/********************************************************/
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//version definition
#define VERSION "0.1"
-#define RELEASE "10"
+#define RELEASE "11"
//Public variables
PUBLIC int debug=0; //debug level
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);
#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>
*/
/********************************************************/
/* */
+/* */
+/* 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. */
/* */
//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);