Copied atexit() code from fakesmtp.c to fakepop.c so that its
authorDavid Levine <levinedl@acm.org>
Sat, 1 Dec 2012 16:52:27 +0000 (10:52 -0600)
committerDavid Levine <levinedl@acm.org>
Sat, 1 Dec 2012 16:52:27 +0000 (10:52 -0600)
pid file gets cleaned up.  Also copied the sig handler.

test/fakepop.c

index 7ff62a8..389588d 100644 (file)
@@ -36,6 +36,8 @@
                                continue; \
                        }
 
+static void killpidfile(void);
+static void handleterm(int);
 static void putpop(int, char *);
 static void putpopbulk(int, char *);
 static int getpop(int, char *, ssize_t);
@@ -183,6 +185,9 @@ main(int argc, char *argv[])
        fprintf(pid, "%ld\n", (long) getpid());
        fclose(pid);
 
+       signal(SIGTERM, handleterm);
+       atexit(killpidfile);
+
        FD_ZERO(&readfd);
        FD_SET(l, &readfd);
 
@@ -412,3 +417,27 @@ readmessage(FILE *file)
 
        return buffer;
 }
+
+/*
+ * Handle a SIGTERM
+ */
+
+static void
+handleterm(int signal)
+{
+       (void) signal;
+
+       killpidfile();
+       fflush(NULL);
+       _exit(1);
+}
+
+/*
+ * Get rid of our pid file
+ */
+
+static void
+killpidfile(void)
+{
+       unlink(PIDFILE);
+}