syslog() is a blocking call on eglibc. as procd provides the actual syslog, weneed...
[project/procd.git] / syslog.c
index 416740b..dbb7a5a 100644 (file)
--- a/syslog.c
+++ b/syslog.c
@@ -63,6 +63,12 @@ void log_add(char *buf, int size, int source)
        int priority = 0;
        int ret;
 
+       /* bounce out if we don't have init'ed yet (regmatch etc will blow) */
+       if (!log) {
+               fprintf(stderr, buf);
+               return;
+       }
+
        /* strip trailing newline */
        if (buf[size - 2] == '\n') {
                buf[size - 2] = '\0';
@@ -121,6 +127,34 @@ void log_add(char *buf, int size, int source)
        newest = next;
 }
 
+void log_printf(char *fmt, ...)
+{
+       static int buffer_len = 128;
+       static char *buffer;
+       va_list ap;
+       int n = 0;
+
+       do {
+               if (n)
+                       buffer_len = n + 1;
+               if (!buffer)
+                       buffer = malloc(buffer_len);
+               if (!buffer)
+                       return;
+               va_start(ap, fmt);
+               n = vsnprintf(buffer, sizeof(buffer), fmt, ap);
+               va_end(ap);
+               if (n < 1)
+                       return;
+               if (n >= buffer_len) {
+                       free(buffer);
+                       buffer = NULL;
+               }
+       } while (n >= buffer_len);
+
+       log_add(buffer, n, SOURCE_INTERNAL);
+}
+
 static void slog_cb(struct ustream *s, int bytes)
 {
        struct ustream_buf *buf = s->r.head;
@@ -274,5 +308,13 @@ void log_init(void)
 
        syslog_open();
        klog_open();
-       openlog("procd", LOG_PID, LOG_DAEMON);
+       openlog("sysinit", LOG_CONS, LOG_DAEMON);
+}
+
+void log_shutdown(void)
+{
+       ustream_free(&slog.stream);
+       ustream_free(&klog.stream);
+       close(slog.fd.fd);
+       close(klog.fd.fd);
 }