]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: consider shut down namespaced sd-journald instance synced
authorFrantisek Sumsal <frantisek@sumsal.cz>
Thu, 18 Jan 2024 16:20:52 +0000 (17:20 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Fri, 19 Jan 2024 11:41:34 +0000 (11:41 +0000)
If the namespaced systemd-journald instance was shut down due to
inactivity, we can consider it synchronized, so avoid throwing an error
in such case.

This should help with the random TEST-44-LOG-NAMESPACE fails where we
might try to sync the namespace just after it was shut down:

[    7.682941] H testsuite-44.sh[381]: + systemd-run --wait -p LogNamespace=foobaz echo 'hello world'
[    7.693916] H systemd-journald[389]: Failed to open /dev/kmsg, ignoring: Operation not permitted
[    7.693983] H systemd-journald[389]: Collecting audit messages is disabled.
[    7.725511] H systemd[1]: Started systemd-journald@foobar.service.
[    7.726496] H systemd[1]: Listening on systemd-journald-varlink@foobaz.socket.
[    7.726808] H systemd[1]: Listening on systemd-journald@foobaz.socket.
[    7.750774] H systemd[1]: Started run-u3.service.
[    7.795122] H systemd[1]: run-u3.service: Deactivated successfully.
[    7.842042] H testsuite-44.sh[390]: Running as unit: run-u3.service; invocation ID: 56380adeb36940a8a170d9ffd2e1e433
[    7.842561] H systemd[1]: systemd-journald-varlink@foobaz.socket: Deactivated successfully.
[    7.842762] H systemd[1]: Closed systemd-journald-varlink@foobaz.socket.
[    7.846394] H systemd[1]: systemd-journald@foobaz.socket: Deactivated successfully.
[    7.846566] H systemd[1]: Closed systemd-journald@foobaz.socket.
[    7.852983] H testsuite-44.sh[390]: Finished with result: success
[    7.852983] H testsuite-44.sh[390]: Main processes terminated with: code=exited/status=0
[    7.852983] H testsuite-44.sh[390]: Service runtime: 44ms
[    7.852983] H testsuite-44.sh[390]: CPU time consumed: 8ms
[    7.852983] H testsuite-44.sh[390]: Memory peak: 880.0K
[    7.852983] H testsuite-44.sh[390]: Memory swap peak: 0B
[    7.853785] H testsuite-44.sh[381]: + journalctl --namespace=foobar --sync
[    7.860095] H systemd-journald[389]: Received client request to sync journal.
[    7.862119] H testsuite-44.sh[381]: + journalctl --namespace=foobaz --sync
[    7.868381] H journalctl[396]: Failed to connect to /run/systemd/journal.foobaz/io.systemd.journal: Connection refused
[    7.871498] H systemd[1]: testsuite-44.service: Main process exited, code=exited, status=1/FAILURE
[    7.871642] H systemd[1]: testsuite-44.service: Failed with result 'exit-code'.
[    7.930772] H systemd[1]: Failed to start testsuite-44.service.

src/journal/journalctl.c

index 58c7993d0b6f978c6c99ea15dc6cf0797d4b8074..d3ec22e224ef369c1295fbe605fc2550807f7ee5 100644 (file)
@@ -1915,47 +1915,93 @@ static int verify(sd_journal *j, bool verbose) {
         return r;
 }
 
-static int simple_varlink_call(const char *option, const char *method) {
-        _cleanup_(varlink_flush_close_unrefp) Varlink *link = NULL;
-        const char *fn;
+static int varlink_connect_journal(Varlink **ret_link) {
+        const char *address;
         int r;
 
-        if (arg_machine)
-                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "%s is not supported in conjunction with --machine=.", option);
-
-        fn = arg_namespace ?
-                strjoina("/run/systemd/journal.", arg_namespace, "/io.systemd.journal") :
-                "/run/systemd/journal/io.systemd.journal";
+        address = arg_namespace ?
+                  strjoina("/run/systemd/journal.", arg_namespace, "/io.systemd.journal") :
+                  "/run/systemd/journal/io.systemd.journal";
 
-        r = varlink_connect_address(&link, fn);
+        r = varlink_connect_address(ret_link, address);
         if (r < 0)
-                return log_error_errno(r, "Failed to connect to %s: %m", fn);
+                return r;
 
-        (void) varlink_set_description(link, "journal");
-        (void) varlink_set_relative_timeout(link, USEC_INFINITY);
+        (void) varlink_set_description(*ret_link, "journal");
+        (void) varlink_set_relative_timeout(*ret_link, USEC_INFINITY);
 
-        return varlink_call_and_log(link, method, /* parameters= */ NULL, /* ret_parameters= */ NULL);
+        return 0;
 }
 
 static int flush_to_var(void) {
+        _cleanup_(varlink_flush_close_unrefp) Varlink *link = NULL;
+        int r;
+
+        if (arg_machine || arg_namespace)
+                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                       "--flush is not supported in conjunction with %s.",
+                                       arg_machine ? "--machine=" : "--namespace=");
+
         if (access("/run/systemd/journal/flushed", F_OK) >= 0)
                 return 0; /* Already flushed, no need to contact journald */
         if (errno != ENOENT)
                 return log_error_errno(errno, "Unable to check for existence of /run/systemd/journal/flushed: %m");
 
-        return simple_varlink_call("--flush", "io.systemd.Journal.FlushToVar");
+        r = varlink_connect_journal(&link);
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to Varlink socket: %m");
+
+        return varlink_call_and_log(link, "io.systemd.Journal.FlushToVar", /* parameters= */ NULL, /* ret_parameters= */ NULL);
 }
 
 static int relinquish_var(void) {
-        return simple_varlink_call("--relinquish-var/--smart-relinquish-var", "io.systemd.Journal.RelinquishVar");
+        _cleanup_(varlink_flush_close_unrefp) Varlink *link = NULL;
+        int r;
+
+        if (arg_machine || arg_namespace)
+                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                       "--(smart-)relinquish-var is not supported in conjunction with %s.",
+                                       arg_machine ? "--machine=" : "--namespace=");
+
+        r = varlink_connect_journal(&link);
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to Varlink socket: %m");
+
+        return varlink_call_and_log(link, "io.systemd.Journal.RelinquishVar", /* parameters= */ NULL, /* ret_parameters= */ NULL);
 }
 
 static int rotate(void) {
-        return simple_varlink_call("--rotate", "io.systemd.Journal.Rotate");
+        _cleanup_(varlink_flush_close_unrefp) Varlink *link = NULL;
+        int r;
+
+        if (arg_machine)
+                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                       "--rotate is not supported in conjunction with --machine=.");
+
+        r = varlink_connect_journal(&link);
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to Varlink socket: %m");
+
+        return varlink_call_and_log(link, "io.systemd.Journal.Rotate", /* parameters= */ NULL, /* ret_parameters= */ NULL);
 }
 
 static int sync_journal(void) {
-        return simple_varlink_call("--sync", "io.systemd.Journal.Synchronize");
+        _cleanup_(varlink_flush_close_unrefp) Varlink *link = NULL;
+        int r;
+
+        if (arg_machine)
+                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
+                                       "--sync is not supported in conjunction with --machine=.");
+
+        r = varlink_connect_journal(&link);
+        if (ERRNO_IS_NEG_DISCONNECT(r) && arg_namespace)
+                /* If the namespaced sd-journald instance was shut down due to inactivity, it should already
+                 * be synchronized */
+                return 0;
+        if (r < 0)
+                return log_error_errno(r, "Failed to connect to Varlink socket: %m");
+
+        return varlink_call_and_log(link, "io.systemd.Journal.Synchronize", /* parameters= */ NULL, /* ret_parameters= */ NULL);
 }
 
 static int action_list_fields(sd_journal *j) {