chiark / gitweb /
log: pass SCM_CREDENTIALS when logging to syslog
[elogind.git] / src / log.c
index 4f9f2da2592c5a99ad96eec34ba5d7b927afbbcb..729b9ea4f6b2696f834459d20622e0caf082c85b 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1,4 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8 -*-*/
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
 
 /***
   This file is part of systemd.
@@ -73,7 +73,7 @@ static int log_open_console(void) {
                         return console_fd;
                 }
 
-                log_info("Succesfully opened /dev/console for logging.");
+                log_debug("Succesfully opened /dev/console for logging.");
         } else
                 console_fd = STDERR_FILENO;
 
@@ -99,7 +99,7 @@ static int log_open_kmsg(void) {
                 return -errno;
         }
 
-        log_info("Succesfully opened /dev/kmsg for logging.");
+        log_debug("Succesfully opened /dev/kmsg for logging.");
 
         return 0;
 }
@@ -146,7 +146,7 @@ static int log_open_syslog(void) {
                 goto fail;
         }
 
-        log_info("Succesfully opened syslog for logging.");
+        log_debug("Succesfully opened syslog for logging.");
 
         return 0;
 
@@ -227,10 +227,10 @@ static int write_to_console(
         if (show_location)
                 IOVEC_SET_STRING(iovec[n++], location);
         if (highlight)
-                IOVEC_SET_STRING(iovec[n++], "\x1B[1;31m");
+                IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
         IOVEC_SET_STRING(iovec[n++], buffer);
         if (highlight)
-                IOVEC_SET_STRING(iovec[n++], "\x1B[0m");
+                IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
         IOVEC_SET_STRING(iovec[n++], "\n");
 
         if (writev(console_fd, iovec, n) < 0)
@@ -249,6 +249,11 @@ static int write_to_syslog(
         char header_priority[16], header_time[64], header_pid[16];
         struct iovec iovec[5];
         struct msghdr msghdr;
+        union {
+                struct cmsghdr cmsghdr;
+                uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
+        } control;
+        struct ucred *ucred;
         time_t t;
         struct tm *tm;
 
@@ -265,7 +270,7 @@ static int write_to_syslog(
         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
                 return -EINVAL;
 
-        snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
+        snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
         char_array_0(header_pid);
 
         zero(iovec);
@@ -275,11 +280,23 @@ static int write_to_syslog(
         IOVEC_SET_STRING(iovec[3], header_pid);
         IOVEC_SET_STRING(iovec[4], buffer);
 
+        zero(control);
+        control.cmsghdr.cmsg_level = SOL_SOCKET;
+        control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
+        control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
+
+        ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
+        ucred->pid = getpid();
+        ucred->uid = getuid();
+        ucred->gid = getgid();
+
         zero(msghdr);
         msghdr.msg_iov = iovec;
         msghdr.msg_iovlen = ELEMENTSOF(iovec);
+        msghdr.msg_control = &control;
+        msghdr.msg_controllen = control.cmsghdr.cmsg_len;
 
-        if (sendmsg(syslog_fd, &msghdr, 0) < 0)
+        if (sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
                 return -errno;
 
         return 1;
@@ -301,7 +318,7 @@ static int write_to_kmsg(
         snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
         char_array_0(header_priority);
 
-        snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
+        snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
         char_array_0(header_pid);
 
         zero(iovec);
@@ -331,7 +348,7 @@ static int log_dispatch(
 
         do {
                 char *e;
-                int k;
+                int k = 0;
 
                 buffer += strspn(buffer, NEWLINE);
 
@@ -344,24 +361,26 @@ static int log_dispatch(
                 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
                     log_target == LOG_TARGET_SYSLOG) {
 
-                        if ((r = write_to_syslog(level, file, line, func, buffer)) < 0) {
+                        if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
                                 log_close_syslog();
                                 log_open_kmsg();
-                        } else if (r > 0)
+                        } else if (k > 0)
                                 r++;
                 }
 
-                if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
-                    log_target == LOG_TARGET_KMSG) {
+                if (k <= 0 &&
+                    (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
+                     log_target == LOG_TARGET_KMSG)) {
 
-                        if ((r = write_to_kmsg(level, file, line, func, buffer)) < 0) {
+                        if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
                                 log_close_kmsg();
                                 log_open_console();
-                        } else if (r > 0)
+                        } else if (k > 0)
                                 r++;
                 }
 
-                if ((k = write_to_console(level, file, line, func, buffer)) < 0)
+                if (k <= 0 &&
+                    (k = write_to_console(level, file, line, func, buffer)) < 0)
                         return k;
 
                 buffer = e;
@@ -474,11 +493,11 @@ void log_parse_environment(void) {
                 if (log_set_max_level_from_string(e) < 0)
                         log_warning("Failed to parse log level %s. Ignoring.", e);
 
-        if ((e = getenv("SYSTEMD_SHOW_COLOR")))
+        if ((e = getenv("SYSTEMD_LOG_COLOR")))
                 if (log_show_color_from_string(e) < 0)
                         log_warning("Failed to parse bool %s. Ignoring.", e);
 
-        if ((e = getenv("SYSTEMD_SHOW_LOCATION"))) {
+        if ((e = getenv("SYSTEMD_LOG_LOCATION"))) {
                 if (log_show_location_from_string(e) < 0)
                         log_warning("Failed to parse bool %s. Ignoring.", e);
         }