#include "sd-bus.h"
#include "sd-event.h"
+#include "sd-json.h"
#include "alloc-util.h"
#include "build.h"
static char *arg_exec_path = NULL;
static bool arg_ignore_failure = false;
static char *arg_background = NULL;
+static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
STATIC_DESTRUCTOR_REGISTER(arg_description, freep);
STATIC_DESTRUCTOR_REGISTER(arg_environment, strv_freep);
" STDERR\n"
" -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
" -q --quiet Suppress information messages during runtime\n"
+ " --json=pretty|short|off Print unit name and invocation id as JSON\n"
" -G --collect Unload unit after it ran, even when failed\n"
" -S --shell Invoke a $SHELL interactively\n"
" --ignore-failure Ignore the exit status of the invoked process\n"
ARG_SHELL,
ARG_IGNORE_FAILURE,
ARG_BACKGROUND,
+ ARG_JSON,
};
static const struct option options[] = {
{ "shell", no_argument, NULL, 'S' },
{ "ignore-failure", no_argument, NULL, ARG_IGNORE_FAILURE },
{ "background", required_argument, NULL, ARG_BACKGROUND },
+ { "json", required_argument, NULL, ARG_JSON },
{},
};
return r;
break;
+ case ARG_JSON:
+ r = parse_json_argument(optarg, &arg_json_format_flags);
+ if (r <= 0)
+ return r;
+ break;
+
case '?':
return -EINVAL;
return chmod_and_chown(path, 0600, st.st_uid, st.st_gid);
}
+static int print_unit_invocation(const char *unit, sd_id128_t invocation_id) {
+ _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
+ int r;
+
+ assert(unit);
+
+ if (FLAGS_SET(arg_json_format_flags, SD_JSON_FORMAT_OFF)) {
+ if (sd_id128_is_null(invocation_id))
+ log_info("Running as unit: %s", unit);
+ else
+ log_info("Running as unit: %s; invocation ID: " SD_ID128_FORMAT_STR, unit, SD_ID128_FORMAT_VAL(invocation_id));
+ return 0;
+ }
+
+ r = sd_json_variant_set_field_string(&v, "unit", unit);
+ if (r < 0)
+ return r;
+
+ if (!sd_id128_is_null(invocation_id)) {
+ r = sd_json_variant_set_field_string(&v, "invocation_id", SD_ID128_TO_STRING(invocation_id));
+ if (r < 0)
+ return r;
+ }
+
+ return sd_json_variant_dump(v, arg_json_format_flags, stdout, NULL);
+}
+
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;
}
if (!arg_quiet) {
- sd_id128_t invocation_id;
+ sd_id128_t invocation_id = SD_ID128_NULL;
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));
+
+ r = print_unit_invocation(service, invocation_id);
+ if (r < 0)
+ return r;
}
if (arg_wait || arg_stdio != ARG_STDIO_NONE) {
_cleanup_strv_free_ char **env = NULL, **user_env = NULL;
_cleanup_free_ char *scope = NULL;
const char *object = NULL;
- sd_id128_t invocation_id;
+ sd_id128_t invocation_id = SD_ID128_NULL;
bool allow_pidfd = true;
int r;
return log_oom();
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));
+ r = print_unit_invocation(scope, invocation_id);
+ if (r < 0)
+ return r;
}
if (arg_expand_environment > 0) {