1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #include "unit-name.h"
30 #include "path-util.h"
32 static bool arg_scope = false;
33 static bool arg_remain_after_exit = false;
34 static const char *arg_unit = NULL;
35 static const char *arg_description = NULL;
36 static const char *arg_slice = NULL;
37 static bool arg_send_sighup = false;
38 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
39 static char *arg_host = NULL;
40 static bool arg_user = false;
42 static int help(void) {
44 printf("%s [OPTIONS...] COMMAND [ARGS...]\n\n"
45 "Run the specified command in a transient scope or service unit.\n\n"
46 " -h --help Show this help\n"
47 " --version Show package version\n"
48 " --user Run as user unit\n"
49 " -H --host=[USER@]HOST Operate on remote host\n"
50 " -M --machine=CONTAINER Operate on local container\n"
51 " --scope Run this as scope rather than service\n"
52 " --unit=UNIT Run under the specified unit name\n"
53 " --description=TEXT Description for unit\n"
54 " --slice=SLICE Run in the specified slice\n"
55 " -r --remain-after-exit Leave service around until explicitly stopped\n"
56 " --send-sighup Send SIGHUP when terminating\n",
57 program_invocation_short_name);
62 static int parse_argv(int argc, char *argv[]) {
74 static const struct option options[] = {
75 { "help", no_argument, NULL, 'h' },
76 { "version", no_argument, NULL, ARG_VERSION },
77 { "user", no_argument, NULL, ARG_USER },
78 { "scope", no_argument, NULL, ARG_SCOPE },
79 { "unit", required_argument, NULL, ARG_UNIT },
80 { "description", required_argument, NULL, ARG_DESCRIPTION },
81 { "slice", required_argument, NULL, ARG_SLICE },
82 { "remain-after-exit", no_argument, NULL, 'r' },
83 { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
84 { "host", required_argument, NULL, 'H' },
85 { "machine", required_argument, NULL, 'M' },
94 while ((c = getopt_long(argc, argv, "+hrH:M:", options, NULL)) >= 0) {
103 puts(PACKAGE_STRING);
104 puts(SYSTEMD_FEATURES);
119 case ARG_DESCRIPTION:
120 arg_description = optarg;
127 case ARG_SEND_SIGHUP:
128 arg_send_sighup = true;
132 arg_remain_after_exit = true;
136 arg_transport = BUS_TRANSPORT_REMOTE;
141 arg_transport = BUS_TRANSPORT_CONTAINER;
149 log_error("Unknown option code %c", c);
154 if (optind >= argc) {
155 log_error("Command line to execute required.");
159 if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) {
160 log_error("Execution in user context is not supported on non-local systems.");
164 if (arg_scope && arg_transport != BUS_TRANSPORT_LOCAL) {
165 log_error("Scope execution is not supported on non-local systems.");
172 static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bus_message **ret) {
173 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
176 log_info("Running as unit %s.", name);
178 r = sd_bus_message_new_method_call(
180 "org.freedesktop.systemd1",
181 "/org/freedesktop/systemd1",
182 "org.freedesktop.systemd1.Manager",
183 "StartTransientUnit", &m);
187 r = sd_bus_message_append(m, "ss", name, "fail");
191 r = sd_bus_message_open_container(m, 'a', "(sv)");
195 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
199 if (!isempty(arg_slice)) {
200 _cleanup_free_ char *slice;
202 slice = unit_name_mangle_with_suffix(arg_slice, ".slice");
206 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
211 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
221 static int message_start_transient_unit_send(sd_bus *bus, sd_bus_message *m, sd_bus_error *error, sd_bus_message **reply) {
224 r = sd_bus_message_close_container(m);
228 return sd_bus_send_with_reply_and_block(bus, m, 0, error, reply);
231 static int start_transient_service(
234 sd_bus_error *error) {
236 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
237 _cleanup_free_ char *name = NULL;
242 name = unit_name_mangle_with_suffix(arg_unit, ".service");
244 asprintf(&name, "run-%lu.service", (unsigned long) getpid());
248 r = message_start_transient_unit_new(bus, name, &m);
252 r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
256 r = sd_bus_message_open_container(m, 'r', "sv");
260 r = sd_bus_message_append(m, "s", "ExecStart");
264 r = sd_bus_message_open_container(m, 'v', "a(sasb)");
268 r = sd_bus_message_open_container(m, 'a', "(sasb)");
272 r = sd_bus_message_open_container(m, 'r', "sasb");
276 r = sd_bus_message_append(m, "s", argv[0]);
280 r = sd_bus_message_open_container(m, 'a', "s");
284 STRV_FOREACH(i, argv) {
285 r = sd_bus_message_append(m, "s", *i);
290 r = sd_bus_message_close_container(m);
294 r = sd_bus_message_append(m, "b", false);
298 r = sd_bus_message_close_container(m);
302 r = sd_bus_message_close_container(m);
306 r = sd_bus_message_close_container(m);
310 r = sd_bus_message_close_container(m);
314 return message_start_transient_unit_send(bus, m, error, &reply);
317 static int start_transient_scope(
320 sd_bus_error *error) {
322 _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
323 _cleanup_free_ char *name = NULL;
327 name = unit_name_mangle_with_suffix(arg_unit, ".scope");
329 asprintf(&name, "run-%lu.scope", (unsigned long) getpid());
333 r = message_start_transient_unit_new(bus, name, &m);
337 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
341 r = message_start_transient_unit_send(bus, m, error, &reply);
345 execvp(argv[0], argv);
346 log_error("Failed to execute: %m");
350 int main(int argc, char* argv[]) {
351 sd_bus_error error = SD_BUS_ERROR_NULL;
352 _cleanup_bus_unref_ sd_bus *bus = NULL;
353 _cleanup_free_ char *description = NULL, *command = NULL;
356 log_parse_environment();
359 r = parse_argv(argc, argv);
363 r = find_binary(argv[optind], &command);
365 log_error("Failed to find executable %s: %s", argv[optind], strerror(-r));
368 argv[optind] = command;
370 if (!arg_description) {
371 description = strv_join(argv + optind, " ");
377 arg_description = description;
380 r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
382 log_error("Failed to create bus connection: %s", strerror(-r));
387 r = start_transient_scope(bus, argv + optind, &error);
389 r = start_transient_service(bus, argv + optind, &error);
391 log_error("Failed start transient unit: %s", error.message ? error.message : strerror(-r));
392 sd_bus_error_free(&error);
397 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;