diff options
-rw-r--r-- | src/diag.c | 152 | ||||
-rw-r--r-- | src/pies.c | 7 | ||||
-rw-r--r-- | src/pies.h | 11 | ||||
-rw-r--r-- | src/sysvinit.c | 10 |
4 files changed, 136 insertions, 44 deletions
@@ -13,77 +13,154 @@ You should have received a copy of the GNU General Public License along with GNU Pies. If not, see <http://www.gnu.org/licenses/>. */ #include "pies.h" -unsigned debug_level; -int source_info_option; -int diag_output = DIAG_TO_STDERR; - -void -diag_setup (int flags) +typedef struct { - if (flags) - diag_output = flags; - if (diag_output & DIAG_TO_SYSLOG) - openlog (log_tag, LOG_PID, log_facility); -} + int open; + FILE *file; +} LOGSTREAM; -void -syslog_printer (int prio, const char *fmt, va_list ap) + +static int +syslog_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap) { #if HAVE_VSYSLOG vsyslog (prio, fmt, ap); #else char buf[128]; vsnprintf (buf, sizeof buf, fmt, ap); syslog (prio, "%s", buf); #endif + return 0; } -static FILE * -stderr_open () +static int +syslog_open (int logf, LOGSTREAM *str) { - if (!init_process) - return stderr; + if (logf & DIAG_REOPEN_LOG) + { + openlog (log_tag, LOG_PID, log_facility); + str->open = 1; + } else + str->open = 0; + return 0; +} + +static void +syslog_close (LOGSTREAM *str) +{ + if (str->open) + closelog (); +} + +static int +stderr_printer (LOGSTREAM *str, int prio, const char *fmt, va_list ap) +{ + FILE *fp = str->file; + va_list aq; + + fprintf (fp, "%s: ", program_name); + va_copy (aq, ap); + vfprintf (fp, fmt, aq); + va_end (aq); + fprintf (fp, "\n"); + return 0; +} + +static int +stderr_open (int logf, LOGSTREAM *str) +{ + if (logf & DIAG_REOPEN_LOG) { int fd = console_open (O_WRONLY|O_NOCTTY|O_NDELAY); if (fd == -1) - return NULL; - return fdopen (fd, "w"); + return -1; + str->file = fdopen (fd, "w"); + if (!str->file) + { + close (fd); + return -1; + } + str->open = 1; } + else + { + str->file = stderr; + str->open = 0; + } + return 0; } static void -stderr_close (FILE *fp) +stderr_close (LOGSTREAM *str) { - if (init_process) - fclose (fp); + if (str->open) + fclose (str->file); } + +struct logger +{ + int mask; + int (*printer) (LOGSTREAM *, int prio, const char *fmt, va_list ap); + int (*open) (int flags, LOGSTREAM *); + void (*close) (LOGSTREAM *); +}; + +struct logger logger[] = { + { DIAG_TO_STDERR, stderr_printer, stderr_open, stderr_close }, + { DIAG_TO_SYSLOG, syslog_printer, syslog_open, syslog_close } +}; void -vlogmsg (int prio, const char *fmt, va_list ap) +vdiagmsg (int logf, int prio, const char *fmt, va_list ap) { - if (DIAG_OUTPUT (DIAG_TO_STDERR)) + int i; + for (i = 0; i < ARRAY_SIZE (logger); i++) { - va_list aq; - FILE *fp = stderr_open (); - if (!fp) - return; - fprintf (fp, "%s: ", program_name); - va_copy (aq, ap); - vfprintf (fp, fmt, aq); - va_end (aq); - fprintf (fp, "\n"); - stderr_close (fp); + if (logger[i].mask & (logf & DIAG_TO_MASK)) + { + LOGSTREAM stream; + if (logger[i].open (logf, &stream) == 0) + { + logger[i].printer (&stream, prio, fmt, ap); + logger[i].close (&stream); + } + } } - - if (DIAG_OUTPUT (DIAG_TO_SYSLOG)) - syslog_printer (prio, fmt, ap); +} + +void +diagmsg (int logf, int prio, const char *fmt, ...) +{ + va_list ap; + va_start (ap, fmt); + vdiagmsg (logf, prio, fmt, ap); + va_end (ap); +} + +unsigned debug_level; +int source_info_option; +int diag_output = DIAG_TO_STDERR; + +void +diag_setup (int flags) +{ + if (flags) + diag_output = flags; + if (diag_output & DIAG_TO_SYSLOG) + openlog (log_tag, LOG_PID, log_facility); +} + +void +vlogmsg (int prio, const char *fmt, va_list ap) +{ + vdiagmsg (diag_output, prio, fmt, ap); } void logmsg (int prio, const char *fmt, ...) { va_list ap; @@ -99,13 +176,12 @@ debug_msg (const char *fmt, ...) va_start (ap, fmt); vlogmsg (LOG_DEBUG, fmt, ap); va_end (ap); } - void logmsg_vprintf (int prio, const char *fmt, va_list ap) { char *p, *t, *str; static char *buf = NULL; static size_t len = 0; @@ -1965,13 +1965,13 @@ main (int argc, char **argv) } } #endif /* Set default logging */ if (init_process) - diag_flags = DIAG_TO_STDERR; + diag_flags = DIAG_TO_STDERR | DIAG_REOPEN_LOG; else diag_flags = DIAG_TO_SYSLOG | (stderr_closed_p () ? 0 : DIAG_TO_STDERR); diag_setup (diag_flags); config_init (); @@ -2134,14 +2134,15 @@ main (int argc, char **argv) case pies_status_running: logmsg (LOG_ERR, _("another pies instance already running (pid %lu)"), (unsigned long) pid); exit (EX_USAGE); } - - logmsg (LOG_INFO, _("%s %s starting"), proginfo.package, proginfo.version); + + logmsg (LOG_INFO, + _("%s %s starting"), proginfo.package, proginfo.version); if (!foreground) { check_pidfile (pidfile); if (daemon (0, 0) == -1) { @@ -443,14 +443,18 @@ void meta1_parser_set_debug (void); int meta1lex (void); int meta1error (char const *s); int meta1parse (void); /* diag.c */ -#define DIAG_TO_SYSLOG 0x1 -#define DIAG_TO_STDERR 0x2 +#define DIAG_TO_SYSLOG 0x01 +#define DIAG_TO_STDERR 0x02 +#define DIAG_TO_MASK 0x0f +#define DIAG_REOPEN_LOG 0x10 + +#define DIAG_ALL (DIAG_REOPEN_LOG|DIAG_TO_STDERR|DIAG_TO_SYSLOG) extern int diag_output; #define DIAG_OUTPUT(x) (diag_output & (x)) void diag_setup (int flags); @@ -460,12 +464,15 @@ void diag_setup (int flags); #endif #ifndef PIES_PRINTFLIKE # define PIES_PRINTFLIKE(fmt,narg) __attribute__ ((__format__ (__printf__, fmt, narg))) #endif +void diagmsg (int logf, int prio, const char *fmt, ...) + PIES_PRINTFLIKE(3,4); + void logmsg (int prio, const char *fmt, ...) PIES_PRINTFLIKE(2,3); void logmsg_printf (int prio, const char *fmt, ...) PIES_PRINTFLIKE(2,3); void logmsg_vprintf (int prio, const char *fmt, va_list ap); extern unsigned debug_level; extern int source_info_option; diff --git a/src/sysvinit.c b/src/sysvinit.c index 7fad00b..87a52d6 100644 --- a/src/sysvinit.c +++ b/src/sysvinit.c @@ -58,12 +58,14 @@ enum boot_state boot_state; int runlevel = 0; int prevlevel = 'N'; int initdefault; /* Default runlevel */ int dfl_level; int emergency_shell; + +#define DIAG_CON (DIAG_TO_STDERR|DIAG_REOPEN_LOG) int console_open (int mode) { int i, fd; @@ -427,23 +429,26 @@ sysvinit_set_runlevel (int newlevel) { switch (newlevel) { case 'A': case 'B': case 'C': + diagmsg (DIAG_ALL, LOG_INFO, + "Activating on-demand level '%c'", newlevel); sysvinit_demand (newlevel); break; default: if (newlevel == runlevel) { } - if (runlevel_index (newlevel) == -1) + else if (runlevel_index (newlevel) == -1) return -1; else { + diagmsg (DIAG_ALL, LOG_INFO, "Switching to runlevel: %c", newlevel); dfl_level = newlevel; raise (SIGALRM); } } return 0; } @@ -824,12 +829,13 @@ inittrans () newlevel = dfl_level ? dfl_level : getinitdefault (); if (trans) { /* boot -> normal */ create_fifo (); ctl_open (); + diag_setup (DIAG_TO_SYSLOG | DIAG_REOPEN_LOG); } } if (newlevel && newlevel != runlevel) { prevlevel = runlevel ? runlevel : 'N'; debug (1, ("RL TRANS: %c -> %c", prevlevel, newlevel)); @@ -846,16 +852,18 @@ inittrans () envsetup (); sysvinit_runlevel_setup (PIES_COMP_DEFAULT); /* Stop disabled programs */ sig = SIGTERM; + diagmsg (DIAG_CON, LOG_INFO, "Sending processes the TERM signal"); progman_foreach (terminate_disabled, &sig); if (progman_wait_until (no_disabled_running, NULL)) { sig = SIGKILL; + diagmsg (DIAG_CON, LOG_INFO, "Sending processes the KILL signal"); progman_foreach (terminate_disabled, &sig); progman_wait_until (no_disabled_running, NULL); } pies_schedule_children (PIES_CHLD_WAKEUP); } |