Ensure that message are logged to syslog when appropriate, even in child handlers.
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 8 Oct 2010 02:35:45 +0000 (04:35 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 8 Oct 2010 02:35:45 +0000 (04:35 +0200)
Also, properly detach the root handler from its controlling TTY if htparser
is instructed to daemonize.

doc/htparser.doc
doc/userplex.doc
lib/log.c
src/htparser.c
src/userplex.c

index d95efc3..757f8cd 100644 (file)
@@ -53,7 +53,10 @@ OPTIONS
 
 *-S*::
 
-       Log messages to *syslog*(3) instead of standard error.
+       Log messages to *syslog*(3) instead of standard error. Also
+       sets the environment ASHD_USESYSLOG environment variable in
+       the root handler process, which indicates to the standard ashd
+       programs to do the same thing.
 
 *-f*::
 
index e6406f4..e69cb5d 100644 (file)
@@ -7,7 +7,7 @@ userplex - User directory multiplexer for ashd(7)
 
 SYNOPSIS
 --------
-*userplex* [*-hI*] [*-g* 'GROUP'] [*-m* 'MIN-UID'] [*-d* 'PUBDIR'] ['PROGRAM' ['ARGS'...]]
+*userplex* [*-hIs*] [*-g* 'GROUP'] [*-m* 'MIN-UID'] [*-d* 'PUBDIR'] ['PROGRAM' ['ARGS'...]]
 
 DESCRIPTION
 -----------
@@ -63,6 +63,15 @@ OPTIONS
        If given, ignore any `~/.ashd/handler` file in a user's home
        directory and use only the default handler.
 
+*-s*::
+
+       If given, set the ASHD_USESYSLOG environment variable in all
+       started children. If not given, *userplex* will actively clear
+       the variable from its children instead. The presence of the
+       variable indicates to the standard ashd programs to log
+       messages to *syslog*(3) instead of printing them on standard
+       error.
+
 *-g* 'GROUP'::
 
        If given, only users that are members of the named 'GROUP'
index 81f52ba..bd07d8d 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
 #include <config.h>
 #endif
 #include <utils.h>
+#include <log.h>
 
+static int inited = 0;
 static int tostderr = 1, tosyslog = 0;
 
+static void initlog(void)
+{
+    inited = 1;
+    if(getenv("ASHD_USESYSLOG") != NULL)
+       opensyslog();
+}
+
 void flog(int level, char *format, ...)
 {
     va_list args;
     
+    if(!inited)
+       initlog();
     va_start(args, format);
     if(tostderr) {
        vfprintf(stderr, format, args);
@@ -44,6 +55,8 @@ void flog(int level, char *format, ...)
 
 void opensyslog(void)
 {
+    if(!inited)
+       initlog();
     openlog("ashd", 0, LOG_DAEMON);
     tostderr = 0;
     tosyslog = 1;
index d1b0a1c..ceae319 100644 (file)
@@ -19,6 +19,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <fcntl.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <pwd.h>
@@ -39,6 +40,7 @@
 
 static int plex;
 static char *pidfile = NULL;
+static int daemonize, usesyslog;
 
 static void trimx(struct hthead *req)
 {
@@ -360,6 +362,26 @@ static void plexwatch(struct muth *muth, va_list args)
     }
 }
 
+static void initroot(void *uu)
+{
+    int fd;
+    
+    if(daemonize) {
+       setsid();
+       chdir("/");
+       if((fd = open("/dev/null", O_RDWR)) >= 0) {
+           dup2(fd, 0);
+           dup2(fd, 1);
+           dup2(fd, 2);
+           close(fd);
+       }
+    }
+    if(usesyslog)
+       putenv("ASHD_USESYSLOG=1");
+    else
+       unsetenv("ASHD_USESYSLOG");
+}
+
 static void usage(FILE *out)
 {
     fprintf(out, "usage: htparser [-hSf] [-u USER] [-r ROOT] [-p PIDFILE] PORTSPEC... -- ROOT [ARGS...]\n");
@@ -416,12 +438,11 @@ int main(int argc, char **argv)
 {
     int c;
     int i, s1;
-    int daemonize, logsys;
     char *root;
     FILE *pidout;
     struct passwd *pwent;
     
-    daemonize = logsys = 0;
+    daemonize = usesyslog = 0;
     root = NULL;
     pwent = NULL;
     while((c = getopt(argc, argv, "+hSfu:r:p:")) >= 0) {
@@ -433,7 +454,7 @@ int main(int argc, char **argv)
            daemonize = 1;
            break;
        case 'S':
-           logsys = 1;
+           usesyslog = 1;
            break;
        case 'u':
            if((pwent = getpwnam(optarg)) == NULL) {
@@ -463,7 +484,7 @@ int main(int argc, char **argv)
        usage(stderr);
        exit(1);
     }
-    if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
+    if((plex = stdmkchild(argv + ++i, initroot, NULL)) < 0) {
        flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
        return(1);
     }
@@ -475,7 +496,7 @@ int main(int argc, char **argv)
            return(1);
        }
     }
-    if(logsys)
+    if(usesyslog)
        opensyslog();
     if(root) {
        if(chroot(root)) {
index cfd313c..d6c978c 100644 (file)
@@ -46,6 +46,7 @@ static char *mgroup = NULL;
 static char *dirname = NULL;
 static char **childspec;
 static uid_t minuid = 0;
+static int usesyslog = 0;
 static struct user *users = NULL;
 
 static void login(struct passwd *pwd)
@@ -73,6 +74,10 @@ static void login(struct passwd *pwd)
        flog(LOG_ERR, "could not change to home directory for %s: %s", pwd->pw_name, strerror(errno));
        exit(1);
     }
+    if(usesyslog)
+       putenv("ASHD_USESYSLOG=1");
+    else
+       unsetenv("ASHD_USESYSLOG");
     putenv(sprintf2("HOME=%s", pwd->pw_dir));
     putenv(sprintf2("SHELL=%s", pwd->pw_shell));
     putenv(sprintf2("USER=%s", pwd->pw_name));
@@ -236,7 +241,7 @@ static void sighandler(int sig)
 
 static void usage(FILE *out)
 {
-    fprintf(out, "usage: userplex [-hI] [-g GROUP] [-m MIN-UID] [-d PUB-DIR] [PROGRAM ARGS...]\n");
+    fprintf(out, "usage: userplex [-hIs] [-g GROUP] [-m MIN-UID] [-d PUB-DIR] [PROGRAM ARGS...]\n");
 }
 
 int main(int argc, char **argv)
@@ -246,11 +251,14 @@ int main(int argc, char **argv)
     int fd;
     struct charvbuf csbuf;
     
-    while((c = getopt(argc, argv, "+hIg:m:d:")) >= 0) {
+    while((c = getopt(argc, argv, "+hIsg:m:d:")) >= 0) {
        switch(c) {
        case 'I':
            ignore = 1;
            break;
+       case 's':
+           usesyslog = 1;
+           break;
        case 'm':
            if((minuid = atoi(optarg)) < 1) {
                fprintf(stderr, "userplex: argument to -m must be greater than 0\n");