#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 */
/* any other parameter makes it crash and burn */
else {
lprintf(CTDL_EMERG, "citserver: usage: "
- "citserver [-tTraceFile] "
+ "citserver "
"[-lLogFacility] "
"[-d] [-f]"
" [-xLogLevel] [-hHomeDir]\n");
}
+ /* 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");
* 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;
}
fflush(stderr);
}
-
- PerformLogHooks(loglevel, buf);
}
/*
- * 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);
}
if(!strcasecmp(name, facTbl[i].name))
return facTbl[i].facility;
}
- return -1;
+ enable_syslog = 0;
+ return LOG_DAEMON;
}