diff -r -u -N amavisd-snapshot-20011031.tmp/README.milter amavisd-snapshot-20011031/README.milter --- amavisd-snapshot-20011031.tmp/README.milter Sat May 19 09:07:52 2001 +++ amavisd-snapshot-20011031/README.milter Wed Dec 19 17:18:13 2001 @@ -29,13 +29,19 @@ To start amavisd with milter support, use this sequence: -rm -rf /var/amavis/amavis-milter.sock -nohup /usr/sbin/amavis-milter -p /var/amavis/amavis-milter.sock & +/usr/sbin/amavis-milter -D -p /var/amavis/amavis-milter.sock & /usr/sbin/amavisd -amavis-milter does not itself go into background currently, so nohup is -required. Watch /var/log/messages for messages from amavis-milter, and -/var/log/maillog for messages from amavisd. +The -D option is necessary to cause amavis-milter to put itself into the +background correctly and act as a daemon. In the future, this may become +the default behavior. + +You can watch for messages from amavisd in /var/log/maillog (or wherever +syslog is configured to send mail.* messages on your system). If you +specify the optional -d flag to amavis-milter, where 1<=n<=4, +amavis-milter will log to /var/amavis/amavis.client in addition to sending +messages to /var/log/messages (or wherever syslog is configured to send +most error messages). References: [1] http://www.sendmail.com/partner/resources/development/milter_api/installation.html Binary files amavisd-snapshot-20011031.tmp/amavis/.amavis-milter.c.swp and amavisd-snapshot-20011031/amavis/.amavis-milter.c.swp differ diff -r -u -N amavisd-snapshot-20011031.tmp/amavis/amavis-milter.c amavisd-snapshot-20011031/amavis/amavis-milter.c --- amavisd-snapshot-20011031.tmp/amavis/amavis-milter.c Wed Dec 19 17:09:11 2001 +++ amavisd-snapshot-20011031/amavis/amavis-milter.c Wed Dec 19 17:28:30 2001 @@ -61,6 +61,7 @@ #define TEMPLATE "/amavis-milter-XXXXXXXX" #define DEBUGFILE "/tmp/virdebug" +#define DEVNULL "/dev/null" typedef struct llstrct { char *str; @@ -671,7 +672,11 @@ char *argv[]; { int c; - const char *args = "p:d:"; + const char *args = "p:d:D"; + + pid_t pid; + int AM_DAEMON = 0; + int devnull; umask(0077); @@ -683,6 +688,14 @@ mydebug(DBG_FATAL, "Illegal conn: %s", optarg); exit(EX_USAGE); } + /* Unlink any existing file that might be in place of + * the socket we want to create. This might not exactly + * be safe, or friendly, but I'll deal with that later. + * Be nice and issue a warning if we have a problem, but + * other than that, ignore it. */ + if ((unlink(optarg)) < 0) { + mydebug(DBG_WARN, "unlink(): %s: %s", optarg, strerror(errno)); + } (void) smfi_setconn(optarg); break; case 'd': @@ -701,6 +714,9 @@ break; } break; + case 'D': + AM_DAEMON = 1; + break; default: break; } @@ -709,6 +725,69 @@ mydebug(DBG_FATAL, "smfi_register failed"); exit(EX_UNAVAILABLE); } + + /* See if we're supposed to become a daemonized process */ + if (AM_DAEMON == 1) { + + /* Fork ourselves into the background, and see if it worked */ + if ((pid = fork()) > 0) { + + mydebug(DBG_INFO, "amavis-milter forked into background"); + exit(0); + + } else if (pid == -1) { + + mydebug(DBG_FATAL, "fork() returned error: %s", strerror(errno)); + exit(EX_UNAVAILABLE); + + } + + /* Open /dev/null read-only */ + if ((devnull = open(DEVNULL, O_RDONLY, 0)) < 0) { + mydebug(DBG_FATAL, "Could not open %s: %s", DEVNULL, + strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* Reassign STDIN to /dev/null */ + close(STDIN_FILENO); + if ((dup2(devnull, STDIN_FILENO)) < 0) { + mydebug(DBG_FATAL, "Could not reassign STDIN to %s: %s", + DEVNULL, strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* Close and re-open /dev/null write-only */ + close(devnull); + if ((devnull = open(DEVNULL, O_WRONLY, 0)) < 0) { + mydebug(DBG_FATAL, "Could not open %s: %s", DEVNULL, + strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* Reassign STDOUT to /dev/null */ + close(STDOUT_FILENO); + if ((dup2(devnull, STDOUT_FILENO)) < 0) { + mydebug(DBG_FATAL, "Could not reassign STDIN to %s: %s", + DEVNULL, strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* Reassign STDERR to /dev/null */ + close(STDERR_FILENO); + if ((dup2(devnull, STDERR_FILENO)) < 0) { + mydebug(DBG_FATAL, "Could not reassign STDIN to %s: %s", + DEVNULL, strerror(errno)); + exit(EX_UNAVAILABLE); + } + + /* Finally, close /dev/null */ + close(devnull); + + + } + + /* hand control over to libmilter */ return smfi_main(); }