* Changes to lprintf() and start_daemon() submitted by Kevin Kilbride
authorArt Cancro <ajc@citadel.org>
Sat, 12 Feb 2005 16:58:37 +0000 (16:58 +0000)
committerArt Cancro <ajc@citadel.org>
Sat, 12 Feb 2005 16:58:37 +0000 (16:58 +0000)
  for more "true unix" approaches to each.

citadel/ChangeLog
citadel/server_main.c
citadel/sysdep.c

index a3ff78cd378a1c5b87518a1ab9fcd0386565892e..af2c3676e7a5b1c5bdfa506746a42ce8465a4353 100644 (file)
@@ -1,4 +1,8 @@
  $Log$
+ Revision 640.2  2005/02/12 16:58:36  ajc
+ * Changes to lprintf() and start_daemon() submitted by Kevin Kilbride
+   for more "true unix" approaches to each.
+
  Revision 640.1  2005/02/11 03:51:51  ajc
  * <.A>ide <U>ser-edit is now <.A>ide <U>ser <E>dit
  * Added a new <.A>ide <U>ser <D>elete command, because it is unintuitive
@@ -6371,3 +6375,4 @@ Sat Jul 11 00:20:48 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
 
 Fri Jul 10 1998 Art Cancro <ajc@uncensored.citadel.org>
        * Initial CVS import
+
index e770bc6f4e39a24813ae2f9e4d4b3832c78af9fa..6e25f3cc6fab49556ef8bfc9dffcf7433dce4dc9 100644 (file)
 #include "snprintf.h"
 #endif
 
+int running_as_daemon = 0;
+
 /*
  * Here's where it all begins.
  */
 int main(int argc, char **argv)
 {
-       char tracefile[128];            /* Name of file to log traces to */
+       char facility[32];
        int a, i;                       /* General-purpose variables */
        struct passwd *pw;
        int drop_root_perms = 1;
        size_t size;
         
-       /* specify default port name and trace file */
-       strcpy(tracefile, "");
-
        /* initialize the master context */
        InitializeMasterCC();
 
+       /* set default syslog facility */
+       syslog_facility = LOG_DAEMON;
+
        /* parse command-line arguments */
        for (a=1; a<argc; ++a) {
 
-               /* -t specifies where to log trace messages to */
-               if (!strncmp(argv[a], "-t", 2)) {
-                       safestrncpy(tracefile, argv[a], sizeof tracefile);
-                       strcpy(tracefile, &tracefile[2]);
-                       freopen(tracefile, "r", stdin);
-                       freopen(tracefile, "w", stdout);
-                       freopen(tracefile, "w", stderr);
-                       chmod(tracefile, 0600);
-               }
-
-               else if (!strncmp(argv[a], "-l", 2)) {
-                       safestrncpy(tracefile, argv[a], sizeof tracefile);
-                       strcpy(tracefile, &tracefile[2]);
-                       syslog_facility = SyslogFacility(tracefile);
-                       if (syslog_facility >= 0) {
-                               openlog("citadel", LOG_PID, syslog_facility);
-                       }
+               if (!strncmp(argv[a], "-l", 2)) {
+                       safestrncpy(facility, argv[a], sizeof(facility));
+                       syslog_facility = SyslogFacility(facility);
                }
 
                /* run in the background if -d was specified */
                else if (!strcmp(argv[a], "-d")) {
-                       start_daemon( (strlen(tracefile) > 0) ? 0 : 1 ) ;
+                       running_as_daemon = 1;
                }
 
                /* -x specifies the desired logging level */
@@ -130,7 +118,7 @@ int main(int argc, char **argv)
                /* any other parameter makes it crash and burn */
                else {
                        lprintf(CTDL_EMERG,     "citserver: usage: "
-                                       "citserver [-tTraceFile] "
+                                       "citserver "
                                        "[-lLogFacility] "
                                        "[-d] [-f]"
                                        " [-xLogLevel] [-hHomeDir]\n");
@@ -139,6 +127,14 @@ int main(int argc, char **argv)
 
        }
 
+       /* daemonize, if we were asked to */
+       if (running_as_daemon) { start_daemon(0); drop_root_perms = 1; }
+
+       /* initialize the syslog facility */
+       if (running_as_daemon) openlog("Citadel", LOG_NDELAY, syslog_facility);
+       else openlog("Citadel", LOG_PERROR|LOG_NDELAY, syslog_facility);
+       setlogmask(LOG_UPTO(verbosity));
+       
        /* Tell 'em who's in da house */
        lprintf(CTDL_NOTICE, "\n");
        lprintf(CTDL_NOTICE, "\n");
index 61f7c363b1fe8273489f077347276637ef8846ec..85854cd40812b6b0390f066c422363879341d249 100644 (file)
@@ -106,26 +106,30 @@ int syslog_facility = (-1);
  * Note: the variable "buf" below needs to be large enough to handle any
  * log data sent through this function.  BE CAREFUL!
  */
+extern int running_as_daemon;
+static int enable_syslog = 1;
 void lprintf(enum LogLevel loglevel, const char *format, ...) {   
        va_list arg_ptr;
-       char buf[SIZ];
 
+       if (enable_syslog) {
+               va_start(arg_ptr, format);
+                       vsyslog(loglevel, format, arg_ptr);
+               va_end(arg_ptr);
+       }
+
+       if (enable_syslog && LogHookTable == 0) return;
+
+       /* legacy output code; hooks get processed first */
+       char buf[SIZ];
        va_start(arg_ptr, format);   
-       vsnprintf(buf, sizeof(buf), format, arg_ptr);   
+               vsnprintf(buf, sizeof(buf), format, arg_ptr);   
        va_end(arg_ptr);   
+       PerformLogHooks(loglevel, buf);
 
-       if (syslog_facility >= 0) {
-               if (loglevel <= verbosity) {
-                       /* Hackery -IO */
-                       if (CC->cs_pid != 0) {
-                               memmove(&buf[6], buf, sizeof(buf) - 6);
-                               snprintf(buf, 6, "[%3d]", CC->cs_pid);
-                               buf[5] = ' ';
-                       }
-                       syslog(loglevel, "%s", buf);
-               }
-       }
-       else if (loglevel <= verbosity) { 
+       if (enable_syslog || running_as_daemon) return;
+
+       /* if we run in forground and syslog is disabled, log to terminal */
+       if (loglevel <= verbosity) { 
                struct timeval tv;
                struct tm tim;
                time_t unixtime;
@@ -150,8 +154,6 @@ void lprintf(enum LogLevel loglevel, const char *format, ...) {
                }
                fflush(stderr);
        }
-
-       PerformLogHooks(loglevel, buf);
 }   
 
 
@@ -696,18 +698,15 @@ void kill_session(int session_to_kill) {
 
 
 /*
- * Start running as a daemon.  Only close stdio if do_close_stdio is set.
+ * Start running as a daemon.
  */
-void start_daemon(int do_close_stdio) {
-       if (do_close_stdio) {
-               /* close(0); */
-               close(1);
-               close(2);
-       }
+void start_daemon(int unused) {
+       close(0); close(1); close(2);
+       if (fork()) exit(0);
+       setsid();
        signal(SIGHUP,SIG_IGN);
        signal(SIGINT,SIG_IGN);
        signal(SIGQUIT,SIG_IGN);
-       if (fork()!=0) exit(0);
 }
 
 
@@ -1111,7 +1110,8 @@ int SyslogFacility(char *name)
                if(!strcasecmp(name, facTbl[i].name))
                        return facTbl[i].facility;
        }
-       return -1;
+       enable_syslog = 0;
+       return LOG_DAEMON;
 }