]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/journal/journald-stream.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / journal / journald-stream.c
index bc5b26d1185fa595838dd692256ed6202875ec24..6745864da11a0080100ab5046d16dfd557422896 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <unistd.h>
 #include <stddef.h>
+#include <unistd.h>
 
 #ifdef HAVE_SELINUX
 #include <selinux/selinux.h>
 #endif
 
-#include "sd-event.h"
 #include "sd-daemon.h"
-#include "socket-util.h"
-#include "selinux-util.h"
-#include "mkdir.h"
+#include "sd-event.h"
+
+#include "escape.h"
+#include "fd-util.h"
 #include "fileio.h"
+#include "journald-console.h"
+#include "journald-kmsg.h"
 #include "journald-server.h"
 #include "journald-stream.h"
 #include "journald-syslog.h"
-#include "journald-kmsg.h"
-#include "journald-console.h"
 #include "journald-wall.h"
+#include "mkdir.h"
+#include "parse-util.h"
+#include "selinux-util.h"
+#include "socket-util.h"
+#include "string-util.h"
 
 #define STDOUT_STREAMS_MAX 4096
 
@@ -59,10 +64,7 @@ struct StdoutStream {
         int fd;
 
         struct ucred ucred;
-#ifdef HAVE_SELINUX
-        security_context_t security_context;
-#endif
-
+        char *label;
         char *identifier;
         char *unit_id;
         int priority;
@@ -99,12 +101,7 @@ void stdout_stream_free(StdoutStream *s) {
         }
 
         safe_close(s->fd);
-
-#ifdef HAVE_SELINUX
-        if (s->security_context)
-                freecon(s->security_context);
-#endif
-
+        free(s->label);
         free(s->identifier);
         free(s->unit_id);
         free(s->state_file);
@@ -150,7 +147,7 @@ static int stdout_stream_save(StdoutStream *s) {
 
         r = fopen_temporary(s->state_file, &f, &temp_path);
         if (r < 0)
-                goto finish;
+                goto fail;
 
         fprintf(f,
                 "# This is private data. Do not parse\n"
@@ -171,7 +168,7 @@ static int stdout_stream_save(StdoutStream *s) {
                 escaped = cescape(s->identifier);
                 if (!escaped) {
                         r = -ENOMEM;
-                        goto finish;
+                        goto fail;
                 }
 
                 fprintf(f, "IDENTIFIER=%s\n", escaped);
@@ -183,7 +180,7 @@ static int stdout_stream_save(StdoutStream *s) {
                 escaped = cescape(s->unit_id);
                 if (!escaped) {
                         r = -ENOMEM;
-                        goto finish;
+                        goto fail;
                 }
 
                 fprintf(f, "UNIT=%s\n", escaped);
@@ -191,16 +188,13 @@ static int stdout_stream_save(StdoutStream *s) {
 
         r = fflush_and_check(f);
         if (r < 0)
-                goto finish;
+                goto fail;
 
         if (rename(temp_path, s->state_file) < 0) {
                 r = -errno;
-                goto finish;
+                goto fail;
         }
 
-        free(temp_path);
-        temp_path = NULL;
-
         /* Store the connection fd in PID 1, so that we get it passed
          * in again on next start */
         if (!s->fdstore) {
@@ -208,14 +202,15 @@ static int stdout_stream_save(StdoutStream *s) {
                 s->fdstore = true;
         }
 
-finish:
-        if (temp_path)
-                unlink(temp_path);
+        return 0;
 
-        if (r < 0)
-                log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
+fail:
+        (void) unlink(s->state_file);
 
-        return r;
+        if (temp_path)
+                (void) unlink(temp_path);
+
+        return log_error_errno(r, "Failed to save stream data %s: %m", s->state_file);
 }
 
 static int stdout_stream_log(StdoutStream *s, const char *p) {
@@ -225,8 +220,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         char syslog_facility[sizeof("SYSLOG_FACILITY=")-1 + DECIMAL_STR_MAX(int) + 1];
         _cleanup_free_ char *message = NULL, *syslog_identifier = NULL;
         unsigned n = 0;
-        char *label = NULL;
-        size_t label_len = 0;
+        size_t label_len;
 
         assert(s);
         assert(p);
@@ -271,14 +265,8 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         if (message)
                 IOVEC_SET_STRING(iovec[n++], message);
 
-#ifdef HAVE_SELINUX
-        if (s->security_context) {
-                label = (char*) s->security_context;
-                label_len = strlen((char*) s->security_context);
-        }
-#endif
-
-        server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority, 0);
+        label_len = s->label ? strlen(s->label) : 0;
+        server_dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, s->label, label_len, s->unit_id, priority, 0);
         return 0;
 }
 
@@ -489,12 +477,11 @@ static int stdout_stream_install(Server *s, int fd, StdoutStream **ret) {
         if (r < 0)
                 return log_error_errno(r, "Failed to determine peer credentials: %m");
 
-#ifdef HAVE_SELINUX
         if (mac_selinux_use()) {
-                if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
-                        log_error_errno(errno, "Failed to determine peer security context: %m");
+                r = getpeersec(fd, &stream->label);
+                if (r < 0 && r != -EOPNOTSUPP)
+                        (void) log_warning_errno(r, "Failed to determine peer security context: %m");
         }
-#endif
 
         (void) shutdown(fd, SHUT_WR);
 
@@ -645,7 +632,7 @@ static int stdout_stream_restore(Server *s, const char *fname, int fd) {
         return 0;
 }
 
-static int server_restore_streams(Server *s, FDSet *fds) {
+int server_restore_streams(Server *s, FDSet *fds) {
         _cleanup_closedir_ DIR *d = NULL;
         struct dirent *de;
         int r;
@@ -699,7 +686,7 @@ fail:
         return log_error_errno(errno, "Failed to read streams directory: %m");
 }
 
-int server_open_stdout_socket(Server *s, FDSet *fds) {
+int server_open_stdout_socket(Server *s) {
         int r;
 
         assert(s);
@@ -720,7 +707,7 @@ int server_open_stdout_socket(Server *s, FDSet *fds) {
                 if (r < 0)
                         return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path);
 
-                chmod(sa.un.sun_path, 0666);
+                (void) chmod(sa.un.sun_path, 0666);
 
                 if (listen(s->stdout_fd, SOMAXCONN) < 0)
                         return log_error_errno(errno, "listen(%s) failed: %m", sa.un.sun_path);
@@ -735,8 +722,5 @@ int server_open_stdout_socket(Server *s, FDSet *fds) {
         if (r < 0)
                 return log_error_errno(r, "Failed to adjust priority of stdout server event source: %m");
 
-        /* Try to restore streams, but don't bother if this fails */
-        (void) server_restore_streams(s, fds);
-
         return 0;
 }