run.c revision c33b329709ebe2755181980a050d02ec7c81ed87
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen This file is part of systemd.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Copyright 2013 Lennart Poettering
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is free software; you can redistribute it and/or modify it
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen under the terms of the GNU Lesser General Public License as published by
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen the Free Software Foundation; either version 2.1 of the License, or
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen (at your option) any later version.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen systemd is distributed in the hope that it will be useful, but
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen WITHOUT ANY WARRANTY; without even the implied warranty of
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen Lesser General Public License for more details.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen You should have received a copy of the GNU Lesser General Public License
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool arg_scope = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool arg_remain_after_exit = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_unit = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_description = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_slice = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool arg_send_sighup = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_host = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool arg_user = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_service_type = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_exec_user = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic const char *arg_exec_group = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic bool arg_nice_set = false;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic char **arg_environment = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic char **arg_property = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic void help(void) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen printf("%s [OPTIONS...] COMMAND [ARGS...]\n\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "Run the specified command in a transient scope or service unit.\n\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " -h --help Show this help\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --version Show package version\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --user Run as user unit\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " -H --host=[USER@]HOST Operate on remote host\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " -M --machine=CONTAINER Operate on local container\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --scope Run this as scope rather than service\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --unit=UNIT Run under the specified unit name\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " -p --property=NAME=VALUE Set unit property\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --description=TEXT Description for unit\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --slice=SLICE Run in the specified slice\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " -r --remain-after-exit Leave service around until explicitly stopped\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --send-sighup Send SIGHUP when terminating\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --service-type=TYPE Service type\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --uid=USER Run as system user\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --gid=GROUP Run as system group\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --nice=NICE Nice level\n"
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen " --setenv=NAME=VALUE Set environment\n",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen program_invocation_short_name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int parse_argv(int argc, char *argv[]) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen static const struct option options[] = {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "help", no_argument, NULL, 'h' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "version", no_argument, NULL, ARG_VERSION },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "user", no_argument, NULL, ARG_USER },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "system", no_argument, NULL, ARG_SYSTEM },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "scope", no_argument, NULL, ARG_SCOPE },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "unit", required_argument, NULL, ARG_UNIT },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "description", required_argument, NULL, ARG_DESCRIPTION },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "slice", required_argument, NULL, ARG_SLICE },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "remain-after-exit", no_argument, NULL, 'r' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "host", required_argument, NULL, 'H' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "machine", required_argument, NULL, 'M' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "uid", required_argument, NULL, ARG_EXEC_USER },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "gid", required_argument, NULL, ARG_EXEC_GROUP },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "nice", required_argument, NULL, ARG_NICE },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "setenv", required_argument, NULL, ARG_SETENV },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen { "property", required_argument, NULL, 'p' },
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen while ((c = getopt_long(argc, argv, "+hrH:M:p:", options, NULL)) >= 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen arg_remain_after_exit = true;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen arg_transport = BUS_TRANSPORT_REMOTE;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen arg_transport = BUS_TRANSPORT_CONTAINER;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = safe_atoi(optarg, &arg_nice);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (r < 0 || arg_nice < PRIO_MIN || arg_nice >= PRIO_MAX) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to parse nice value");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (strv_extend(&arg_environment, optarg) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (strv_extend(&arg_property, optarg) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen assert_not_reached("Unhandled option");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Command line to execute required.");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Execution in user context is not supported on non-local systems.");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (arg_scope && arg_transport != BUS_TRANSPORT_LOCAL) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Scope execution is not supported on non-local systems.");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (arg_scope && (arg_remain_after_exit || arg_service_type)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("--remain-after-exit and --service-type= are not supported in --scope mode.");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bus_message **ret) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_new_method_call(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "org.freedesktop.systemd1.Manager",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen "StartTransientUnit");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "ss", name, "fail");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'a', "(sv)");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen STRV_FOREACH(i, arg_property) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'r', "sv");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = bus_append_unit_property_assignment(m, *i);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen slice = unit_name_mangle_with_suffix(arg_slice, MANGLE_NOGLOB, ".slice");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int message_start_transient_unit_send(sd_bus *bus, sd_bus_message *m, sd_bus_error *error, sd_bus_message **reply) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "a(sa(sv))", 0);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return sd_bus_call(bus, m, 0, error, reply);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int start_transient_service(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *name = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (asprintf(&name, "run-"PID_FMT".service", getpid()) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = message_start_transient_unit_new(bus, name, &m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (!strv_isempty(arg_environment)) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'r', "sv");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "s", "Environment");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'v', "as");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append_strv(m, arg_environment);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'r', "sv");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "s", "ExecStart");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'v', "a(sasb)");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'a', "(sasb)");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_open_container(m, 'r', "sasb");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "s", argv[0]);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append_strv(m, argv);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "b", false);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_close_container(m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = message_start_transient_unit_send(bus, m, error, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_info("Running as unit %s.", name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenstatic int start_transient_scope(
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *name = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen } else if (asprintf(&name, "run-"PID_FMT".scope", getpid()) < 0)
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = message_start_transient_unit_new(bus, name, &m);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = message_start_transient_unit_send(bus, m, error, NULL);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen return bus_log_create_error(r);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to set nice level: %m");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = get_group_creds(&arg_exec_group, &gid);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (setresgid(gid, gid, gid) < 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to change GID to " GID_FMT ": %m", gid);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = get_user_creds(&arg_exec_user, &uid, &gid, &home, &shell);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = strv_extendf(&user_env, "HOME=%s", home);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = strv_extendf(&user_env, "SHELL=%s", shell);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = strv_extendf(&user_env, "USER=%s", arg_exec_user);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (setresgid(gid, gid, gid) < 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to change GID to " GID_FMT ": %m", gid);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen if (setresuid(uid, uid, uid) < 0) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to change UID to " UID_FMT ": %m", uid);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen env = strv_env_merge(3, environ, user_env, arg_environment);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_info("Running as unit %s.", name);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error("Failed to execute: %m");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersenint main(int argc, char* argv[]) {
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_bus_close_unref_ sd_bus *bus = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen _cleanup_free_ char *description = NULL, *command = NULL;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = find_binary(argv[optind], arg_transport == BUS_TRANSPORT_LOCAL, &command);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error_errno(r, "Failed to find executable %s%s: %m",
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen arg_transport == BUS_TRANSPORT_LOCAL ? "" : " on local system");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen description = strv_join(argv + optind, " ");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen arg_description = description;
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = bus_open_transport_systemd(arg_transport, arg_host, arg_user, &bus);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen log_error_errno(r, "Failed to create bus connection: %m");
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = start_transient_scope(bus, argv + optind, &error);
95ed3294c632f5606327149f10cef1eb34422862Thomas Hindoe Paaboel Andersen r = start_transient_service(bus, argv + optind, &error);