diff --git a/src/run/run.c b/src/run/run.c index 6f110117ab6..58974370ef2 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1224,6 +1224,50 @@ static int bus_call_with_hint( return r; } +static int acquire_invocation_id(sd_bus *bus, const char *unit, sd_id128_t *ret) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_free_ char *object = NULL; + const void *p; + size_t l; + int r; + + assert(bus); + assert(ret); + + if (unit) { + object = unit_dbus_path_from_name(unit); + if (!object) + return log_oom(); + } + + r = sd_bus_get_property(bus, + "org.freedesktop.systemd1", + object ?: "/org/freedesktop/systemd1/unit/self", + "org.freedesktop.systemd1.Unit", + "InvocationID", + &error, + &reply, + "ay"); + if (r < 0) + return log_error_errno(r, "Failed to request invocation ID for unit: %s", bus_error_message(&error, r)); + + r = sd_bus_message_read_array(reply, 'y', &p, &l); + if (r < 0) + return bus_log_parse_error(r); + + if (l == 0) { + *ret = SD_ID128_NULL; + return 0; /* no uuid set */ + } + + if (l != sizeof(sd_id128_t)) + return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t)); + + memcpy(ret, p, l); + return !sd_id128_is_null(*ret); +} + static int start_transient_service(sd_bus *bus) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; @@ -1325,8 +1369,17 @@ static int start_transient_service(sd_bus *bus) { return r; } - if (!arg_quiet) - log_info("Running as unit: %s", service); + if (!arg_quiet) { + sd_id128_t invocation_id; + + r = acquire_invocation_id(bus, service, &invocation_id); + if (r < 0) + return r; + if (r == 0) /* No invocation UUID set */ + log_info("Running as unit: %s", service); + else + log_info("Running as unit: %s; invocation ID: " SD_ID128_FORMAT_STR, service, SD_ID128_FORMAT_VAL(invocation_id)); + } if (arg_wait || arg_stdio != ARG_STDIO_NONE) { _cleanup_(run_context_free) RunContext c = { @@ -1453,38 +1506,6 @@ static int start_transient_service(sd_bus *bus) { return EXIT_SUCCESS; } -static int acquire_invocation_id(sd_bus *bus, sd_id128_t *ret) { - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - const void *p; - size_t l; - int r; - - assert(bus); - assert(ret); - - r = sd_bus_get_property(bus, - "org.freedesktop.systemd1", - "/org/freedesktop/systemd1/unit/self", - "org.freedesktop.systemd1.Unit", - "InvocationID", - &error, - &reply, - "ay"); - if (r < 0) - return log_error_errno(r, "Failed to request invocation ID for scope: %s", bus_error_message(&error, r)); - - r = sd_bus_message_read_array(reply, 'y', &p, &l); - if (r < 0) - return bus_log_parse_error(r); - - if (l != sizeof(sd_id128_t)) - return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t)); - - memcpy(ret, p, l); - return 0; -} - static int start_transient_scope(sd_bus *bus) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; @@ -1573,13 +1594,15 @@ static int start_transient_scope(sd_bus *bus) { if (r < 0) return r; - r = acquire_invocation_id(bus, &invocation_id); + r = acquire_invocation_id(bus, NULL, &invocation_id); if (r < 0) return r; - - r = strv_extendf(&user_env, "INVOCATION_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id)); - if (r < 0) - return log_oom(); + if (r == 0) + log_debug("No invocation ID set."); + else { + if (strv_extendf(&user_env, "INVOCATION_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id)) < 0) + return log_oom(); + } if (arg_nice_set) { if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) @@ -1642,8 +1665,12 @@ static int start_transient_scope(sd_bus *bus) { if (!env) return log_oom(); - if (!arg_quiet) - log_info("Running scope as unit: %s", scope); + if (!arg_quiet) { + if (sd_id128_is_null(invocation_id)) + log_info("Running as unit: %s", scope); + else + log_info("Running as unit: %s; invocation ID: " SD_ID128_FORMAT_STR, scope, SD_ID128_FORMAT_VAL(invocation_id)); + } if (arg_expand_environment > 0) { _cleanup_strv_free_ char **expanded_cmdline = NULL, **unset_variables = NULL, **bad_variables = NULL; diff --git a/test/units/testsuite-65.sh b/test/units/testsuite-65.sh index 66db42d7593..6202b386606 100755 --- a/test/units/testsuite-65.sh +++ b/test/units/testsuite-65.sh @@ -827,13 +827,13 @@ check deny yes /run/systemd/system/deny-list.service check deny no deny-list.service output=$(systemd-run -p "SystemCallFilter=@system-service" -p "SystemCallFilter=~@resources:ENOANO @privileged" -p "SystemCallFilter=@clock" sleep 60 2>&1) -name=$(echo "$output" | awk '{ print $4 }') +name=$(echo "$output" | awk '{ print $4 }' | cut -d';' -f1) check allow yes /run/systemd/transient/"$name" check allow no "$name" output=$(systemd-run -p "SystemCallFilter=~@known" -p "SystemCallFilter=@system-service" -p "SystemCallFilter=~@resources:ENOANO @privileged" -p "SystemCallFilter=@clock" sleep 60 2>&1) -name=$(echo "$output" | awk '{ print $4 }') +name=$(echo "$output" | awk '{ print $4 }' | cut -d';' -f1) check deny yes /run/systemd/transient/"$name" check deny no "$name"