chiark / gitweb /
systemctl: suppress duplicate newline if there's not log output in "systemctl status"
[elogind.git] / src / shared / logs-show.c
index 0e3fd3de2630c14e0dfee67eebf11d7cf204438f..f7d84fc723d233bd05fde439356ac124125b4733 100644 (file)
@@ -124,6 +124,11 @@ static bool print_multiline(FILE *f, unsigned prefix, unsigned n_columns, Output
                 }
         }
 
+        /* A special case: make sure that we print a newline when
+           the message is empty. */
+        if (message_len == 0)
+                fputs("\n", f);
+
         for (pos = message;
              pos < message + message_len;
              pos = end + 1, line++) {
@@ -922,6 +927,21 @@ int output_journal(
         return ret;
 }
 
+static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
+        assert(f);
+        assert(flags);
+
+        if (!(*flags & OUTPUT_BEGIN_NEWLINE))
+                return 0;
+
+        /* Print a beginning new line if that's request, but only once
+         * on the first line we print. */
+
+        fputc('\n', f);
+        *flags &= ~OUTPUT_BEGIN_NEWLINE;
+        return 0;
+}
+
 static int show_journal(FILE *f,
                         sd_journal *j,
                         OutputMode mode,
@@ -979,6 +999,7 @@ static int show_journal(FILE *f,
                         }
 
                         line ++;
+                        maybe_print_begin_newline(f, &flags);
 
                         r = output_journal(f, j, mode, n_columns, flags, ellipsized);
                         if (r < 0)
@@ -999,8 +1020,10 @@ static int show_journal(FILE *f,
                         if (r < 0)
                                 goto finish;
 
-                        if (r > 0 && not_before < cutoff)
+                        if (r > 0 && not_before < cutoff) {
+                                maybe_print_begin_newline(f, &flags);
                                 fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
+                        }
 
                         warn_cutoff = false;
                 }
@@ -1116,10 +1139,8 @@ int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
 }
 
 static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
-        _cleanup_free_ char *leader = NULL, *class = NULL;
-        _cleanup_close_pipe_ int sock[2] = { -1, -1 };
-        _cleanup_close_ int nsfd = -1;
-        const char *p, *ns;
+        _cleanup_close_pipe_ int pair[2] = { -1, -1 };
+        _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
         pid_t pid, child;
         siginfo_t si;
         char buf[37];
@@ -1132,26 +1153,15 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
         if (!filename_is_safe(machine))
                 return -EINVAL;
 
-        p = strappenda("/run/systemd/machines/", machine);
-
-        r = parse_env_file(p, NEWLINE, "LEADER", &leader, "CLASS", &class, NULL);
+        r = container_get_leader(machine, &pid);
         if (r < 0)
                 return r;
-        if (!leader)
-                return -ENODATA;
-        if (!streq_ptr(class, "container"))
-                return -EIO;
-        r = parse_pid(leader, &pid);
+
+        r = namespace_open(pid, &pidnsfd, &mntnsfd, &rootfd);
         if (r < 0)
                 return r;
 
-        ns = procfs_file_alloca(pid, "ns/mnt");
-
-        nsfd = open(ns, O_RDONLY|O_NOCTTY|O_CLOEXEC);
-        if (nsfd < 0)
-                return -errno;
-
-        if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sock) < 0)
+        if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
                 return -errno;
 
         child = fork();
@@ -1161,10 +1171,10 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
         if (child == 0) {
                 int fd;
 
-                close_nointr_nofail(sock[0]);
-                sock[0] = -1;
+                close_nointr_nofail(pair[0]);
+                pair[0] = -1;
 
-                r = setns(nsfd, CLONE_NEWNS);
+                r = namespace_enter(pidnsfd, mntnsfd, rootfd);
                 if (r < 0)
                         _exit(EXIT_FAILURE);
 
@@ -1177,24 +1187,24 @@ static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
                 if (k != 36)
                         _exit(EXIT_FAILURE);
 
-                k = send(sock[1], buf, 36, MSG_NOSIGNAL);
+                k = send(pair[1], buf, 36, MSG_NOSIGNAL);
                 if (k != 36)
                         _exit(EXIT_FAILURE);
 
                 _exit(EXIT_SUCCESS);
         }
 
-        close_nointr_nofail(sock[1]);
-        sock[1] = -1;
-
-        k = recv(sock[0], buf, 36, 0);
-        if (k != 36)
-                return -EIO;
+        close_nointr_nofail(pair[1]);
+        pair[1] = -1;
 
         r = wait_for_terminate(child, &si);
         if (r < 0 || si.si_code != CLD_EXITED || si.si_status != EXIT_SUCCESS)
                 return r < 0 ? r : -EIO;
 
+        k = recv(pair[0], buf, 36, 0);
+        if (k != 36)
+                return -EIO;
+
         buf[36] = 0;
         r = sd_id128_from_string(buf, boot_id);
         if (r < 0)