Changed msg_style and msg_delim to be file static to m_getfld.c
[mmh] / test / fakesmtp.c
index 19df2e9..fbfe044 100644 (file)
@@ -34,6 +34,7 @@ main(int argc, char *argv[])
        struct addrinfo hints, *res;
        int rc, l, conn, on, datamode;
        FILE *f, *pid;
+       pid_t child;
        fd_set readfd;
        struct stat st;
        struct timeval tv;
@@ -49,6 +50,33 @@ main(int argc, char *argv[])
                exit(1);
        }
 
+       /*
+        * If there is a pid file already around, kill the previously running
+        * fakesmtp process.  Hopefully this will reduce the race conditions
+        * that crop up when running the test suite.
+        */
+
+       if (stat(PIDFILE, &st) == 0) {
+               long oldpid;
+
+               if (!(pid = fopen(PIDFILE, "r"))) {
+                       fprintf(stderr, "Cannot open " PIDFILE
+                               " (%s), continuing ...\n", strerror(errno));
+               } else {
+                       rc = fscanf(pid, "%ld", &oldpid);
+                       fclose(pid);
+
+                       if (rc != 1) {
+                               fprintf(stderr, "Unable to parse pid in "
+                                       PIDFILE ", continuing ...\n");
+                       } else {
+                               kill((pid_t) oldpid, SIGTERM);
+                       }
+               }
+
+               unlink(PIDFILE);
+       }
+
        memset(&hints, 0, sizeof(hints));
 
        hints.ai_family = PF_INET;
@@ -92,34 +120,36 @@ main(int argc, char *argv[])
        }
 
        /*
-        * Now that our socket & files are set up, do the following things:
-        *
-        * - Check for a PID file.  If there is one, kill the old version.
-        * - Wait 30 seconds for a connection.  If there isn't one, then
-        *   exit.
+        * Now we fork() and print out the process ID of our child
+        * for scripts to use.  Once we do that, then exit.
         */
 
-       if (stat(PIDFILE, &st) == 0) {
-               long oldpid;
-
-               if (!(pid = fopen(PIDFILE, "r"))) {
-                       fprintf(stderr, "Cannot open " PIDFILE
-                               " (%s), continuing ...\n", strerror(errno));
-               } else {
-                       rc = fscanf(pid, "%ld", &oldpid);
-                       fclose(pid);
-
-                       if (rc != 1) {
-                               fprintf(stderr, "Unable to parse pid in "
-                                       PIDFILE ", continuing ...\n");
-                       } else {
-                               kill((pid_t) oldpid, SIGTERM);
-                       }
-               }
+       child = fork();
 
-               unlink(PIDFILE);
+       switch (child) {
+       case -1:
+               fprintf(stderr, "Unable to fork child: %s\n", strerror(errno));
+               exit(1);
+               break;
+       case 0:
+               /*
+                * Close stdin & stdout, otherwise people can
+                * think we're still doing stuff.  For now leave stderr
+                * open.
+                */
+               fclose(stdin);
+               fclose(stdout);
+               break;
+       default:
+               printf("%ld\n", (long) child);
+               exit(0);
        }
 
+       /*
+        * Now that our socket & files are set up, wait 30 seconds for
+        * a connection.  If there isn't one, then exit.
+        */
+
        if (!(pid = fopen(PIDFILE, "w"))) {
                fprintf(stderr, "Cannot open " PIDFILE ": %s\n",
                        strerror(errno));
@@ -199,6 +229,8 @@ main(int argc, char *argv[])
                 */
 
                if (strcmp(line, "QUIT") == 0) {
+                       fclose(f);
+                       f = NULL;
                        putsmtp(conn, "221 Later alligator!");
                        close(conn);
                        break;
@@ -210,7 +242,8 @@ main(int argc, char *argv[])
                }
        }
 
-       fclose(f);
+       if (f)
+               fclose(f);
 
        exit(0);
 }
@@ -240,7 +273,7 @@ static int
 getsmtp(int socket, char *data)
 {
        int cc;
-       static int bytesinbuf = 0;
+       static unsigned int bytesinbuf = 0;
        static char buffer[LINESIZE * 2], *p;
 
        for (;;) {
@@ -294,6 +327,8 @@ getsmtp(int socket, char *data)
 static void
 handleterm(int signal)
 {
+       (void) signal;
+
        killpidfile();
        fflush(NULL);
        _exit(1);