OSSP CVS Repository

ossp - Difference in ossp-pkg/lmtp2nntp/lmtp2nntp.c versions 1.68 and 1.69
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [History

ossp-pkg/lmtp2nntp/lmtp2nntp.c 1.68 -> 1.69

--- lmtp2nntp.c  2001/10/08 14:30:11     1.68
+++ lmtp2nntp.c  2001/10/09 08:22:29     1.69
@@ -132,8 +132,11 @@
     char           *option_deliverymodefakedsn;
     int             option_maxmessagesize;
     int             option_waittime;
+    int             option_timeout;
     char           *option_mailfrom;
     unsigned int    option_levelmask;
+    char           *option_pidfile;
+    int             option_killflag;
     l2_stream_t    *l2;
     sa_addr_t      *saaAltio;
     sa_t           *saAltio;
@@ -187,10 +190,13 @@
             "[-d deliverymode] "
             "[-g groupmode] "
             "[-h host[:port][,host[:port], ...]] "
+            "[-k] "
             "[-l level[:logfile]] "
             "[-m mailfrom] "
             "[-n nodename] "
+            "[-p pidfile] "
             "[-s size] "
+            "[-t timeout] "
             "[-v] "
             "[-w waittime] "
             "newsgroup [newsgroup ...] "
@@ -339,6 +345,7 @@
     l2_channel_t *chBuf;
     l2_channel_t *chFile;
     pid_t         pid;
+    FILE         *fd;
 
     /* library version check (run-time) */
     if (l2_version.v_hex < L2_VERSION_HEX_REQ) {
@@ -362,8 +369,11 @@
     ctx->option_deliverymodefakedsn    = "5.7.1"; /* Delivery not authorized, message refused */
     ctx->option_maxmessagesize = 8 * 1024 * 1024;
     ctx->option_waittime = -1;
+    ctx->option_timeout = 3;
     ctx->option_mailfrom = NULL;
     ctx->option_levelmask = L2_LEVEL_NONE;
+    ctx->option_pidfile = NULL;
+    ctx->option_killflag = FALSE;
     ctx->l2 = NULL;
     ctx->saaAltio = NULL;
     ctx->saAltio = NULL;
@@ -397,7 +407,7 @@
      */
 
     /* read in the arguments */
-    while ((i = getopt(argc, argv, "a:b:d:g:h:l:m:n:s:vw:")) != -1) {
+    while ((i = getopt(argc, argv, "a:b:d:g:h:kl:m:n:p:s:t:vw:")) != -1) {
         switch (i) {
             case 'a': /*POD [B<-a> I<altio>] */
                 if ((rc = sa_addr_create(&ctx->saaAltio)) != SA_OK) {
@@ -552,6 +562,9 @@
                 }
                 free(azHosts);
                 break;
+            case 'k': /*POD [B<-k>] */
+                ctx->option_killflag = TRUE;
+                break;
             case 'l': /*POD [B<-l> I<level>[:I<logfile>]] */
                 if ((cp = strrchr(optarg, ':')) != NULL) {
                     *cp++ = NUL;
@@ -594,6 +607,9 @@
                 }
                 strcpy(ctx->uname.nodename, optarg);
                 break;
+            case 'p': /*POD [B<-p> I<pidfile>] */
+                ctx->option_pidfile = strdup(optarg);
+                break;
             case 's': /*POD [B<-s> I<size>] */
                     ctx->option_maxmessagesize = atoi(optarg);
                     if(ctx->option_maxmessagesize < 64) {
@@ -601,6 +617,13 @@
                         CU(ERR_EXECUTION);
                     }
                 break;
+            case 't': /*POD [B<-t> I<timeout>] */
+                    ctx->option_timeout = atoi(optarg);
+                    if(ctx->option_timeout < 0) {
+                        fprintf(stderr, "%s:Error: timeout %d to option -t must be a positive integer.\n", ctx->progname, ctx->option_waittime);
+                        CU(ERR_EXECUTION);
+                    }
+                break;
             case 'v': /*POD [B<-v>] (version)*/
                 fprintf(stdout, "%s\n", lmtp2nntp_version.v_gnu);
                 CU(0);
@@ -633,7 +656,7 @@
     }
 
     if (l2_stream_formatter(ctx->l2, 'P', formatter_prefix, &ctx->ctx) != L2_OK) {
-        fprintf(stderr, "%s:Error: logging failed to register formatter\n", ctx->progname);
+        fprintf(stderr, "%s:Error: logging failed to register prefix formatter\n", ctx->progname);
         CU(ERR_EXECUTION);
     }
     if (l2_stream_formatter(ctx->l2, 'D', l2_util_fmt_dump, NULL) != L2_OK) {
@@ -702,6 +725,26 @@
         CU(ERR_EXECUTION);
     }
 
+    if ((ctx->option_pidfile != NULL) && ctx->option_killflag) {
+        if ((fd = fopen(ctx->option_pidfile, "r")) == NULL)
+            log1(ctx, ERROR, "cannot open pidfile \"%s\" for reading %m", ctx->option_pidfile);
+        else {
+            if (fscanf(fd, "%d\n", &pid) != 1) {
+                fclose(fd);
+                log1(ctx, ERROR, "cannot extract pid from pidfile \"%s\"", ctx->option_pidfile);
+            }
+            else {
+                fclose(fd);
+                log1(ctx, TRACE, "going to kill pid[%d]", pid);
+                if (kill(pid, SIGHUP) == -1)
+                    log1(ctx, ERROR, "killing pid[%d] failed %m", pid);
+                if (unlink(ctx->option_pidfile) == -1)
+                    log1(ctx, ERROR, "unlinking pidfile \"%s\" failed %m", ctx->option_pidfile);
+            }
+        }
+        CU(0);
+    }
+
     catchsignal(0, ctx);
     signal(SIGCHLD, (void(*)())catchsignal);
     signal(SIGHUP,  (void(*)())catchsignal);
@@ -715,7 +758,6 @@
     signal(SIGUSR1,            SIG_IGN    );
     signal(SIGUSR2,            SIG_IGN    );
 
-        
     /* loop for LMTP protocol with support for alternate io through daemon */
     if (ctx->saAltio == NULL) {
         /* initialize LMTP context */
@@ -757,8 +799,17 @@
     } else {
         pid = getpid();
         daemonize();
+        if (ctx->option_pidfile != NULL) {
+            if ((fd = fopen(ctx->option_pidfile, "w+")) == NULL)
+                log1(ctx, ERROR, "cannot open pidfile \"%s\" for writing %m", ctx->option_pidfile);
+            else {
+                fprintf(fd, "%d\n", getpid());
+                fclose(fd);
+            }
+        }
         log1(ctx, NOTICE, "startup daemonized, previous pid[%d]", pid); l2_stream_flush(ctx->l2);
-        sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL,    9, 0);
+
+        sa_timeout(ctx->saAltio, SA_TIMEOUT_ALL, ctx->option_timeout, 0);
         sa_timeout(ctx->saAltio, SA_TIMEOUT_ACCEPT, 0, 0);
         while ((rc = sa_accept(ctx->saAltio, &ctx->saaIO, &ctx->saIO)) == SA_OK) {
             l2_stream_flush(ctx->l2); /* must flush before fork() */
@@ -818,6 +869,8 @@
     CUS:
     log0(ctx, NOTICE, "graceful shutdown shortly before exit - no more logging");
     l2_stream_destroy(ctx->l2);
+    if (ctx->option_pidfile != NULL)
+        free(ctx->option_pidfile);
     if (ctx->option_logfile != NULL)
         free(ctx->option_logfile);
     if (ctx->progname != NULL)

CVSTrac 2.0.1