]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/run/run.c
Merge pull request #17549 from yuwata/tiny-fixes
[thirdparty/systemd.git] / src / run / run.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
c2756a68 2
6c12b52e 3#include <getopt.h>
3f6fd1ba 4#include <stdio.h>
ca78ad1d
ZJS
5#include <fcntl.h>
6#include <sys/stat.h>
7#include <sys/types.h>
c2756a68
LP
8
9#include "sd-bus.h"
9b15b784 10#include "sd-event.h"
3f6fd1ba 11
b5efdb8a 12#include "alloc-util.h"
3f6fd1ba 13#include "bus-error.h"
9b71e4ab 14#include "bus-locator.h"
807542be 15#include "bus-map-properties.h"
20b16441 16#include "bus-unit-util.h"
e45c81b8 17#include "bus-wait-for-jobs.h"
3f6fd1ba 18#include "calendarspec.h"
4de33e7f 19#include "env-util.h"
7f3614e5 20#include "exit-status.h"
3ffd4af2 21#include "fd-util.h"
f97b34a6 22#include "format-util.h"
ef08ad7a 23#include "main-func.h"
cf0fbc49 24#include "parse-util.h"
4bcc8c3c 25#include "path-util.h"
294bf0c3 26#include "pretty-print.h"
2a453c2e 27#include "process-util.h"
9b15b784 28#include "ptyfwd.h"
24882e06 29#include "signal-util.h"
8c7db2fb 30#include "spawn-polkit-agent.h"
3f6fd1ba 31#include "strv.h"
66cb2fde 32#include "terminal-util.h"
89ada3ba 33#include "unit-def.h"
3f6fd1ba 34#include "unit-name.h"
b1d4f8e1 35#include "user-util.h"
c2756a68 36
8c7db2fb 37static bool arg_ask_password = true;
6c12b52e 38static bool arg_scope = false;
6577c7ce 39static bool arg_remain_after_exit = false;
3d161f99 40static bool arg_no_block = false;
2a453c2e 41static bool arg_wait = false;
6c12b52e 42static const char *arg_unit = NULL;
9f2e86af 43static const char *arg_description = NULL;
c221420b 44static const char *arg_slice = NULL;
2c7039b3 45static bool arg_slice_inherit = false;
a6c0353b 46static bool arg_send_sighup = false;
d21ed1ea 47static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
1ac67edb 48static const char *arg_host = NULL;
d21ed1ea 49static bool arg_user = false;
c7040b5d
LP
50static const char *arg_service_type = NULL;
51static const char *arg_exec_user = NULL;
52static const char *arg_exec_group = NULL;
53static int arg_nice = 0;
54static bool arg_nice_set = false;
55static char **arg_environment = NULL;
df31a6c0 56static char **arg_property = NULL;
5dca7739
LP
57static enum {
58 ARG_STDIO_NONE, /* The default, as it is for normal services, stdin connected to /dev/null, and stdout+stderr to the journal */
59 ARG_STDIO_PTY, /* Interactive behaviour, requested by --pty: we allocate a pty and connect it to the TTY we are invoked from */
60 ARG_STDIO_DIRECT, /* Directly pass our stdin/stdout/stderr to the activated service, useful for usage in shell pipelines, requested by --pipe */
61 ARG_STDIO_AUTO, /* If --pipe and --pty are used together we use --pty when invoked on a TTY, and --pipe otherwise */
62} arg_stdio = ARG_STDIO_NONE;
d59ef3e2
YW
63static char **arg_path_property = NULL;
64static char **arg_socket_property = NULL;
4c213d6c 65static char **arg_timer_property = NULL;
787be190 66static bool arg_with_timer = false;
095dc596 67static bool arg_quiet = false;
fe9d0be9 68static bool arg_aggressive_gc = false;
2d21165a 69static char *arg_working_directory = NULL;
badd28e1
LP
70static bool arg_shell = false;
71static char **arg_cmdline = NULL;
c2756a68 72
0565120f
LP
73STATIC_DESTRUCTOR_REGISTER(arg_environment, strv_freep);
74STATIC_DESTRUCTOR_REGISTER(arg_property, strv_freep);
75STATIC_DESTRUCTOR_REGISTER(arg_path_property, strv_freep);
76STATIC_DESTRUCTOR_REGISTER(arg_socket_property, strv_freep);
77STATIC_DESTRUCTOR_REGISTER(arg_timer_property, strv_freep);
78STATIC_DESTRUCTOR_REGISTER(arg_working_directory, freep);
79STATIC_DESTRUCTOR_REGISTER(arg_cmdline, strv_freep);
80
37ec0fdd
LP
81static int help(void) {
82 _cleanup_free_ char *link = NULL;
83 int r;
84
85 r = terminal_urlify_man("systemd-run", "1", &link);
86 if (r < 0)
87 return log_oom();
88
353b2baa
LP
89 printf("%s [OPTIONS...] COMMAND [ARGUMENTS...]\n"
90 "\n%sRun the specified command in a transient scope or service.%s\n\n"
4c213d6c
WC
91 " -h --help Show this help\n"
92 " --version Show package version\n"
8c7db2fb 93 " --no-ask-password Do not prompt for password\n"
4c213d6c
WC
94 " --user Run as user unit\n"
95 " -H --host=[USER@]HOST Operate on remote host\n"
96 " -M --machine=CONTAINER Operate on local container\n"
97 " --scope Run this as scope rather than service\n"
7976b9f0 98 " -u --unit=UNIT Run under the specified unit name\n"
ad2c1701 99 " -p --property=NAME=VALUE Set service or scope unit property\n"
4c213d6c
WC
100 " --description=TEXT Description for unit\n"
101 " --slice=SLICE Run in the specified slice\n"
2c7039b3 102 " --slice-inherit Inherit the slice\n"
3d161f99 103 " --no-block Do not wait until operation finished\n"
4c213d6c 104 " -r --remain-after-exit Leave service around until explicitly stopped\n"
2a453c2e 105 " --wait Wait until service stopped again\n"
4c213d6c
WC
106 " --send-sighup Send SIGHUP when terminating\n"
107 " --service-type=TYPE Service type\n"
108 " --uid=USER Run as system user\n"
109 " --gid=GROUP Run as system group\n"
110 " --nice=NICE Nice level\n"
2d21165a
LP
111 " --working-directory=PATH Set working directory\n"
112 " -d --same-dir Inherit working directory from caller\n"
b5911366 113 " -E --setenv=NAME=VALUE Set environment\n"
5dca7739
LP
114 " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n"
115 " STDERR\n"
116 " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
fe9d0be9 117 " -q --quiet Suppress information messages during runtime\n"
badd28e1
LP
118 " -G --collect Unload unit after it ran, even when failed\n"
119 " -S --shell Invoke a $SHELL interactively\n\n"
d59ef3e2
YW
120 "Path options:\n"
121 " --path-property=NAME=VALUE Set path unit property\n\n"
122 "Socket options:\n"
123 " --socket-property=NAME=VALUE Set socket unit property\n\n"
ad2c1701 124 "Timer options:\n"
b57b0625
ZJS
125 " --on-active=SECONDS Run after SECONDS delay\n"
126 " --on-boot=SECONDS Run SECONDS after machine was booted up\n"
127 " --on-startup=SECONDS Run SECONDS after systemd activation\n"
128 " --on-unit-active=SECONDS Run SECONDS after the last activation\n"
129 " --on-unit-inactive=SECONDS Run SECONDS after the last deactivation\n"
4c213d6c 130 " --on-calendar=SPEC Realtime timer\n"
efebb613
LP
131 " --on-timezone-change Run when the timezone changes\n"
132 " --on-clock-change Run when the realtime clock jumps\n"
ad2c1701 133 " --timer-property=NAME=VALUE Set timer unit property\n"
37ec0fdd
LP
134 "\nSee the %s for details.\n"
135 , program_invocation_short_name
353b2baa 136 , ansi_highlight(), ansi_normal()
37ec0fdd
LP
137 , link
138 );
139
140 return 0;
6c12b52e
LP
141}
142
f2b9f2c8 143static int add_timer_property(const char *name, const char *val) {
18939728 144 char *p;
f2b9f2c8
YW
145
146 assert(name);
147 assert(val);
148
149 p = strjoin(name, "=", val);
150 if (!p)
151 return log_oom();
152
153 if (strv_consume(&arg_timer_property, p) < 0)
154 return log_oom();
155
f2b9f2c8 156 return 0;
4c213d6c
WC
157}
158
6c12b52e
LP
159static int parse_argv(int argc, char *argv[]) {
160
161 enum {
162 ARG_VERSION = 0x100,
163 ARG_USER,
66b1e746 164 ARG_SYSTEM,
6c12b52e 165 ARG_SCOPE,
c221420b 166 ARG_DESCRIPTION,
a6c0353b 167 ARG_SLICE,
2c7039b3 168 ARG_SLICE_INHERIT,
a6c0353b 169 ARG_SEND_SIGHUP,
5cfde70c 170 ARG_SERVICE_TYPE,
c7040b5d
LP
171 ARG_EXEC_USER,
172 ARG_EXEC_GROUP,
c7040b5d 173 ARG_NICE,
4c213d6c
WC
174 ARG_ON_ACTIVE,
175 ARG_ON_BOOT,
176 ARG_ON_STARTUP,
177 ARG_ON_UNIT_ACTIVE,
178 ARG_ON_UNIT_INACTIVE,
179 ARG_ON_CALENDAR,
efebb613
LP
180 ARG_ON_TIMEZONE_CHANGE,
181 ARG_ON_CLOCK_CHANGE,
3d161f99 182 ARG_TIMER_PROPERTY,
d59ef3e2
YW
183 ARG_PATH_PROPERTY,
184 ARG_SOCKET_PROPERTY,
3d161f99 185 ARG_NO_BLOCK,
5cfde70c 186 ARG_NO_ASK_PASSWORD,
2a453c2e 187 ARG_WAIT,
2d21165a 188 ARG_WORKING_DIRECTORY,
badd28e1 189 ARG_SHELL,
6c12b52e
LP
190 };
191
192 static const struct option options[] = {
2d21165a
LP
193 { "help", no_argument, NULL, 'h' },
194 { "version", no_argument, NULL, ARG_VERSION },
195 { "user", no_argument, NULL, ARG_USER },
196 { "system", no_argument, NULL, ARG_SYSTEM },
197 { "scope", no_argument, NULL, ARG_SCOPE },
7976b9f0 198 { "unit", required_argument, NULL, 'u' },
2d21165a
LP
199 { "description", required_argument, NULL, ARG_DESCRIPTION },
200 { "slice", required_argument, NULL, ARG_SLICE },
2c7039b3 201 { "slice-inherit", no_argument, NULL, ARG_SLICE_INHERIT },
2d21165a
LP
202 { "remain-after-exit", no_argument, NULL, 'r' },
203 { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
204 { "host", required_argument, NULL, 'H' },
205 { "machine", required_argument, NULL, 'M' },
206 { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
207 { "wait", no_argument, NULL, ARG_WAIT },
208 { "uid", required_argument, NULL, ARG_EXEC_USER },
209 { "gid", required_argument, NULL, ARG_EXEC_GROUP },
210 { "nice", required_argument, NULL, ARG_NICE },
211 { "setenv", required_argument, NULL, 'E' },
212 { "property", required_argument, NULL, 'p' },
213 { "tty", no_argument, NULL, 't' }, /* deprecated alias */
214 { "pty", no_argument, NULL, 't' },
215 { "pipe", no_argument, NULL, 'P' },
216 { "quiet", no_argument, NULL, 'q' },
217 { "on-active", required_argument, NULL, ARG_ON_ACTIVE },
218 { "on-boot", required_argument, NULL, ARG_ON_BOOT },
219 { "on-startup", required_argument, NULL, ARG_ON_STARTUP },
220 { "on-unit-active", required_argument, NULL, ARG_ON_UNIT_ACTIVE },
221 { "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE },
222 { "on-calendar", required_argument, NULL, ARG_ON_CALENDAR },
efebb613
LP
223 { "on-timezone-change",no_argument, NULL, ARG_ON_TIMEZONE_CHANGE},
224 { "on-clock-change", no_argument, NULL, ARG_ON_CLOCK_CHANGE },
2d21165a
LP
225 { "timer-property", required_argument, NULL, ARG_TIMER_PROPERTY },
226 { "path-property", required_argument, NULL, ARG_PATH_PROPERTY },
227 { "socket-property", required_argument, NULL, ARG_SOCKET_PROPERTY },
228 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
229 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
230 { "collect", no_argument, NULL, 'G' },
231 { "working-directory", required_argument, NULL, ARG_WORKING_DIRECTORY },
232 { "same-dir", no_argument, NULL, 'd' },
badd28e1 233 { "shell", no_argument, NULL, 'S' },
eb9da376 234 {},
6c12b52e
LP
235 };
236
d59ef3e2 237 bool with_trigger = false;
c7040b5d 238 int r, c;
6c12b52e
LP
239
240 assert(argc >= 0);
241 assert(argv);
242
7976b9f0 243 while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdSu:", options, NULL)) >= 0)
6c12b52e
LP
244
245 switch (c) {
246
247 case 'h':
37ec0fdd 248 return help();
6c12b52e 249
ad2c1701
LP
250 case ARG_VERSION:
251 return version();
252
8c7db2fb
EV
253 case ARG_NO_ASK_PASSWORD:
254 arg_ask_password = false;
255 break;
256
6c12b52e
LP
257 case ARG_USER:
258 arg_user = true;
259 break;
260
66b1e746
LP
261 case ARG_SYSTEM:
262 arg_user = false;
263 break;
264
6c12b52e
LP
265 case ARG_SCOPE:
266 arg_scope = true;
267 break;
268
7976b9f0 269 case 'u':
6c12b52e
LP
270 arg_unit = optarg;
271 break;
272
9f2e86af
LP
273 case ARG_DESCRIPTION:
274 arg_description = optarg;
275 break;
276
c221420b
LP
277 case ARG_SLICE:
278 arg_slice = optarg;
279 break;
280
2c7039b3
MAL
281 case ARG_SLICE_INHERIT:
282 arg_slice_inherit = true;
283 break;
284
a6c0353b
LP
285 case ARG_SEND_SIGHUP:
286 arg_send_sighup = true;
287 break;
288
6577c7ce
LP
289 case 'r':
290 arg_remain_after_exit = true;
291 break;
292
d21ed1ea
LP
293 case 'H':
294 arg_transport = BUS_TRANSPORT_REMOTE;
295 arg_host = optarg;
296 break;
297
298 case 'M':
de33fc62 299 arg_transport = BUS_TRANSPORT_MACHINE;
d21ed1ea
LP
300 arg_host = optarg;
301 break;
302
c7040b5d
LP
303 case ARG_SERVICE_TYPE:
304 arg_service_type = optarg;
305 break;
306
307 case ARG_EXEC_USER:
308 arg_exec_user = optarg;
309 break;
310
311 case ARG_EXEC_GROUP:
312 arg_exec_group = optarg;
313 break;
314
315 case ARG_NICE:
41bf0590
LP
316 r = parse_nice(optarg, &arg_nice);
317 if (r < 0)
318 return log_error_errno(r, "Failed to parse nice value: %s", optarg);
c7040b5d
LP
319
320 arg_nice_set = true;
321 break;
322
b5911366 323 case 'E':
c7040b5d
LP
324 if (strv_extend(&arg_environment, optarg) < 0)
325 return log_oom();
326
327 break;
328
df31a6c0 329 case 'p':
df31a6c0
LP
330 if (strv_extend(&arg_property, optarg) < 0)
331 return log_oom();
332
333 break;
334
5dca7739
LP
335 case 't': /* --pty */
336 if (IN_SET(arg_stdio, ARG_STDIO_DIRECT, ARG_STDIO_AUTO)) /* if --pipe is already used, upgrade to auto mode */
337 arg_stdio = ARG_STDIO_AUTO;
338 else
339 arg_stdio = ARG_STDIO_PTY;
340 break;
341
342 case 'P': /* --pipe */
343 if (IN_SET(arg_stdio, ARG_STDIO_PTY, ARG_STDIO_AUTO)) /* If --pty is already used, upgrade to auto mode */
344 arg_stdio = ARG_STDIO_AUTO;
345 else
346 arg_stdio = ARG_STDIO_DIRECT;
9b15b784
LP
347 break;
348
095dc596
LP
349 case 'q':
350 arg_quiet = true;
351 break;
352
4c213d6c 353 case ARG_ON_ACTIVE:
f2b9f2c8
YW
354 r = add_timer_property("OnActiveSec", optarg);
355 if (r < 0)
4c213d6c 356 return r;
4c213d6c 357
787be190 358 arg_with_timer = true;
4c213d6c
WC
359 break;
360
361 case ARG_ON_BOOT:
f2b9f2c8
YW
362 r = add_timer_property("OnBootSec", optarg);
363 if (r < 0)
4c213d6c 364 return r;
4c213d6c 365
787be190 366 arg_with_timer = true;
4c213d6c
WC
367 break;
368
369 case ARG_ON_STARTUP:
f2b9f2c8
YW
370 r = add_timer_property("OnStartupSec", optarg);
371 if (r < 0)
4c213d6c 372 return r;
4c213d6c 373
787be190 374 arg_with_timer = true;
4c213d6c
WC
375 break;
376
377 case ARG_ON_UNIT_ACTIVE:
f2b9f2c8
YW
378 r = add_timer_property("OnUnitActiveSec", optarg);
379 if (r < 0)
4c213d6c 380 return r;
4c213d6c 381
787be190 382 arg_with_timer = true;
4c213d6c
WC
383 break;
384
385 case ARG_ON_UNIT_INACTIVE:
f2b9f2c8
YW
386 r = add_timer_property("OnUnitInactiveSec", optarg);
387 if (r < 0)
4c213d6c 388 return r;
4c213d6c 389
787be190 390 arg_with_timer = true;
4c213d6c
WC
391 break;
392
10434dbd
FS
393 case ARG_ON_CALENDAR: {
394 _cleanup_(calendar_spec_freep) CalendarSpec *cs = NULL;
10434dbd 395
10434dbd
FS
396 r = calendar_spec_from_string(optarg, &cs);
397 if (r < 0)
04220fda
LP
398 return log_error_errno(r, "Failed to parse calendar event specification: %m");
399
7842c5f2 400 /* Let's make sure the given calendar event is not in the past */
04220fda
LP
401 r = calendar_spec_next_usec(cs, now(CLOCK_REALTIME), NULL);
402 if (r == -ENOENT)
e608bf6f
ZJS
403 /* The calendar event is in the past — let's warn about this, but install it
404 * anyway as is. The service manager will trigger the service right away.
405 * Moreover, the server side might have a different clock or timezone than we
406 * do, hence it should decide when or whether to run something. */
04220fda
LP
407 log_warning("Specified calendar expression is in the past, proceeding anyway.");
408 else if (r < 0)
409 return log_error_errno(r, "Failed to calculate next time calendar expression elapses: %m");
10434dbd 410
f2b9f2c8
YW
411 r = add_timer_property("OnCalendar", optarg);
412 if (r < 0)
efebb613
LP
413 return r;
414
415 arg_with_timer = true;
416 break;
10434dbd 417 }
efebb613
LP
418
419 case ARG_ON_TIMEZONE_CHANGE:
420 r = add_timer_property("OnTimezoneChange", "yes");
421 if (r < 0)
422 return r;
423
424 arg_with_timer = true;
425 break;
426
427 case ARG_ON_CLOCK_CHANGE:
428 r = add_timer_property("OnClockChange", "yes");
429 if (r < 0)
4c213d6c 430 return r;
024a8ec1 431
787be190 432 arg_with_timer = true;
4c213d6c
WC
433 break;
434
435 case ARG_TIMER_PROPERTY:
436
437 if (strv_extend(&arg_timer_property, optarg) < 0)
438 return log_oom();
439
787be190 440 arg_with_timer = arg_with_timer ||
49fe5c09
LP
441 STARTSWITH_SET(optarg,
442 "OnActiveSec=",
443 "OnBootSec=",
444 "OnStartupSec=",
445 "OnUnitActiveSec=",
446 "OnUnitInactiveSec=",
447 "OnCalendar=");
4c213d6c
WC
448 break;
449
d59ef3e2
YW
450 case ARG_PATH_PROPERTY:
451
452 if (strv_extend(&arg_path_property, optarg) < 0)
453 return log_oom();
454
455 break;
456
457 case ARG_SOCKET_PROPERTY:
458
459 if (strv_extend(&arg_socket_property, optarg) < 0)
460 return log_oom();
461
462 break;
463
3d161f99
LP
464 case ARG_NO_BLOCK:
465 arg_no_block = true;
466 break;
467
2a453c2e
LP
468 case ARG_WAIT:
469 arg_wait = true;
470 break;
471
2d21165a
LP
472 case ARG_WORKING_DIRECTORY:
473 r = parse_path_argument_and_warn(optarg, true, &arg_working_directory);
474 if (r < 0)
475 return r;
476
477 break;
478
479 case 'd': {
480 _cleanup_free_ char *p = NULL;
481
482 r = safe_getcwd(&p);
483 if (r < 0)
484 return log_error_errno(r, "Failed to get current working directory: %m");
485
486 if (empty_or_root(p))
487 arg_working_directory = mfree(arg_working_directory);
488 else
489 free_and_replace(arg_working_directory, p);
490 break;
491 }
492
fe9d0be9
LP
493 case 'G':
494 arg_aggressive_gc = true;
495 break;
496
badd28e1
LP
497 case 'S':
498 arg_shell = true;
499 break;
500
6c12b52e
LP
501 case '?':
502 return -EINVAL;
503
504 default:
eb9da376 505 assert_not_reached("Unhandled option");
6c12b52e 506 }
6c12b52e 507
787be190 508 with_trigger = !!arg_path_property || !!arg_socket_property || arg_with_timer;
d59ef3e2
YW
509
510 /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */
787be190 511 if ((int) !!arg_path_property + (int) !!arg_socket_property + (int) arg_with_timer > 1)
baaa35ad
ZJS
512 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
513 "Only single trigger (path, socket, timer) unit can be created.");
5dca7739 514
badd28e1
LP
515 if (arg_shell) {
516 /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise
517 * specified. */
518
519 if (!arg_scope) {
520 if (arg_stdio == ARG_STDIO_NONE)
521 arg_stdio = ARG_STDIO_AUTO;
522
523 if (!arg_working_directory) {
524 r = safe_getcwd(&arg_working_directory);
525 if (r < 0)
526 return log_error_errno(r, "Failed to get current working directory: %m");
527 }
528
529 if (!arg_service_type) {
530 arg_service_type = strdup("exec");
531 if (!arg_service_type)
532 return log_oom();
533 }
534
535 arg_wait = true;
536 }
537
538 arg_aggressive_gc = true;
539 }
540
d46b79bb 541 if (arg_stdio == ARG_STDIO_AUTO)
5dca7739
LP
542 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
543 * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
544 * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
545 arg_stdio = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ?
546 ARG_STDIO_PTY :
547 ARG_STDIO_DIRECT;
5dca7739 548
badd28e1
LP
549 if (argc > optind) {
550 char **l;
551
552 if (arg_shell)
553 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --shell is used, no command line is expected.");
554
555 l = strv_copy(argv + optind);
556 if (!l)
557 return log_oom();
558
559 strv_free_and_replace(arg_cmdline, l);
560
561 } else if (arg_shell) {
562 _cleanup_free_ char *s = NULL;
563 char **l;
564
565 r = get_shell(&s);
566 if (r < 0)
567 return log_error_errno(r, "Failed to determine shell: %m");
568
569 l = strv_new(s);
570 if (!l)
571 return log_oom();
572
573 strv_free_and_replace(arg_cmdline, l);
574
575 } else if (!arg_unit || !with_trigger)
576 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Command line to execute required.");
6c12b52e 577
baaa35ad
ZJS
578 if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL)
579 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
580 "Execution in user context is not supported on non-local systems.");
d21ed1ea 581
baaa35ad
ZJS
582 if (arg_scope && arg_transport != BUS_TRANSPORT_LOCAL)
583 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
584 "Scope execution is not supported on non-local systems.");
d21ed1ea 585
baaa35ad
ZJS
586 if (arg_scope && (arg_remain_after_exit || arg_service_type))
587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
588 "--remain-after-exit and --service-type= are not supported in --scope mode.");
c7040b5d 589
baaa35ad
ZJS
590 if (arg_stdio != ARG_STDIO_NONE && (with_trigger || arg_scope))
591 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
592 "--pty/--pipe is not compatible in timer or --scope mode.");
9b15b784 593
baaa35ad
ZJS
594 if (arg_stdio != ARG_STDIO_NONE && arg_transport == BUS_TRANSPORT_REMOTE)
595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
596 "--pty/--pipe is only supported when connecting to the local system or containers.");
024a8ec1 597
baaa35ad
ZJS
598 if (arg_stdio != ARG_STDIO_NONE && arg_no_block)
599 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
600 "--pty/--pipe is not compatible with --no-block.");
578c03bc 601
baaa35ad
ZJS
602 if (arg_scope && with_trigger)
603 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
604 "Path, socket or timer options are not supported in --scope mode.");
4c213d6c 605
787be190 606 if (arg_timer_property && !arg_with_timer)
baaa35ad
ZJS
607 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
608 "--timer-property= has no effect without any other timer options.");
4c213d6c 609
2a453c2e 610 if (arg_wait) {
baaa35ad
ZJS
611 if (arg_no_block)
612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
613 "--wait may not be combined with --no-block.");
2a453c2e 614
baaa35ad
ZJS
615 if (with_trigger)
616 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
617 "--wait may not be combined with path, socket or timer operations.");
2a453c2e 618
baaa35ad
ZJS
619 if (arg_scope)
620 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
621 "--wait may not be combined with --scope.");
2a453c2e
LP
622 }
623
6c12b52e
LP
624 return 1;
625}
626
89ada3ba 627static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
c2756a68
LP
628 int r;
629
9b15b784
LP
630 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
631 if (r < 0)
f53bddf3 632 return bus_log_create_error(r);
9b15b784 633
fe9d0be9
LP
634 if (arg_aggressive_gc) {
635 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
636 if (r < 0)
f53bddf3 637 return bus_log_create_error(r);
fe9d0be9
LP
638 }
639
89ada3ba 640 r = bus_append_unit_property_assignment_many(m, t, properties);
8673cf13
LP
641 if (r < 0)
642 return r;
df31a6c0 643
9b15b784
LP
644 return 0;
645}
646
647static int transient_cgroup_set_properties(sd_bus_message *m) {
2c7039b3
MAL
648 _cleanup_free_ char *name = NULL;
649 _cleanup_free_ char *slice = NULL;
9b15b784
LP
650 int r;
651 assert(m);
c2756a68 652
2c7039b3
MAL
653 if (arg_slice_inherit) {
654 char *end;
c221420b 655
2c7039b3
MAL
656 if (arg_user)
657 r = cg_pid_get_user_slice(0, &name);
658 else
659 r = cg_pid_get_slice(0, &name);
7410616c 660 if (r < 0)
2c7039b3 661 return log_error_errno(r, "Failed to get PID slice: %m");
c221420b 662
2c7039b3
MAL
663 end = endswith(name, ".slice");
664 if (!end)
665 return -ENXIO;
666 *end = 0;
c221420b
LP
667 }
668
2c7039b3
MAL
669 if (!isempty(arg_slice)) {
670 if (name) {
671 char *j = strjoin(name, "-", arg_slice);
672 free_and_replace(name, j);
673 } else
674 name = strdup(arg_slice);
675 if (!name)
676 return log_oom();
677 }
678
679 if (!name)
680 return 0;
681
682 r = unit_name_mangle_with_suffix(name, "as slice",
683 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
684 ".slice", &slice);
685 if (r < 0)
686 return log_error_errno(r, "Failed to mangle name '%s': %m", arg_slice);
687
688 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
689 if (r < 0)
690 return bus_log_create_error(r);
691
9b15b784
LP
692 return 0;
693}
694
695static int transient_kill_set_properties(sd_bus_message *m) {
f53bddf3
LP
696 int r;
697
9b15b784
LP
698 assert(m);
699
f53bddf3
LP
700 if (arg_send_sighup) {
701 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
702 if (r < 0)
703 return bus_log_create_error(r);
704 }
705
706 return 0;
6c12b52e
LP
707}
708
badd28e1 709static int transient_service_set_properties(sd_bus_message *m, const char *pty_path) {
5dca7739 710 bool send_term = false;
6c12b52e
LP
711 int r;
712
8159d91a
LP
713 assert(m);
714
89ada3ba 715 r = transient_unit_set_properties(m, UNIT_SERVICE, arg_property);
9b15b784
LP
716 if (r < 0)
717 return r;
718
719 r = transient_kill_set_properties(m);
720 if (r < 0)
721 return r;
722
723 r = transient_cgroup_set_properties(m);
86b8d289
LP
724 if (r < 0)
725 return r;
726
5dca7739 727 if (arg_wait || arg_stdio != ARG_STDIO_NONE) {
2a453c2e
LP
728 r = sd_bus_message_append(m, "(sv)", "AddRef", "b", 1);
729 if (r < 0)
f53bddf3 730 return bus_log_create_error(r);
2a453c2e
LP
731 }
732
df31a6c0
LP
733 if (arg_remain_after_exit) {
734 r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
735 if (r < 0)
f53bddf3 736 return bus_log_create_error(r);
df31a6c0 737 }
6577c7ce 738
c7040b5d
LP
739 if (arg_service_type) {
740 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
741 if (r < 0)
f53bddf3 742 return bus_log_create_error(r);
c7040b5d
LP
743 }
744
745 if (arg_exec_user) {
746 r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
747 if (r < 0)
f53bddf3 748 return bus_log_create_error(r);
c7040b5d
LP
749 }
750
751 if (arg_exec_group) {
752 r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
753 if (r < 0)
f53bddf3 754 return bus_log_create_error(r);
c7040b5d
LP
755 }
756
757 if (arg_nice_set) {
758 r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
759 if (r < 0)
f53bddf3 760 return bus_log_create_error(r);
c7040b5d
LP
761 }
762
2d21165a
LP
763 if (arg_working_directory) {
764 r = sd_bus_message_append(m, "(sv)", "WorkingDirectory", "s", arg_working_directory);
765 if (r < 0)
766 return bus_log_create_error(r);
767 }
768
9b15b784 769 if (pty_path) {
9b15b784
LP
770 r = sd_bus_message_append(m,
771 "(sv)(sv)(sv)(sv)",
772 "StandardInput", "s", "tty",
773 "StandardOutput", "s", "tty",
774 "StandardError", "s", "tty",
775 "TTYPath", "s", pty_path);
776 if (r < 0)
f53bddf3 777 return bus_log_create_error(r);
9b15b784 778
5dca7739
LP
779 send_term = true;
780
781 } else if (arg_stdio == ARG_STDIO_DIRECT) {
782 r = sd_bus_message_append(m,
783 "(sv)(sv)(sv)",
784 "StandardInputFileDescriptor", "h", STDIN_FILENO,
785 "StandardOutputFileDescriptor", "h", STDOUT_FILENO,
786 "StandardErrorFileDescriptor", "h", STDERR_FILENO);
787 if (r < 0)
f53bddf3 788 return bus_log_create_error(r);
5dca7739
LP
789
790 send_term = isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO);
791 }
792
793 if (send_term) {
794 const char *e;
795
9b15b784
LP
796 e = getenv("TERM");
797 if (e) {
798 char *n;
799
63c372cb 800 n = strjoina("TERM=", e);
9b15b784
LP
801 r = sd_bus_message_append(m,
802 "(sv)",
803 "Environment", "as", 1, n);
804 if (r < 0)
f53bddf3 805 return bus_log_create_error(r);
9b15b784
LP
806 }
807 }
808
c7040b5d
LP
809 if (!strv_isempty(arg_environment)) {
810 r = sd_bus_message_open_container(m, 'r', "sv");
811 if (r < 0)
f53bddf3 812 return bus_log_create_error(r);
c7040b5d
LP
813
814 r = sd_bus_message_append(m, "s", "Environment");
815 if (r < 0)
f53bddf3 816 return bus_log_create_error(r);
c7040b5d
LP
817
818 r = sd_bus_message_open_container(m, 'v', "as");
819 if (r < 0)
f53bddf3 820 return bus_log_create_error(r);
c7040b5d
LP
821
822 r = sd_bus_message_append_strv(m, arg_environment);
823 if (r < 0)
f53bddf3 824 return bus_log_create_error(r);
c7040b5d
LP
825
826 r = sd_bus_message_close_container(m);
827 if (r < 0)
f53bddf3 828 return bus_log_create_error(r);
c7040b5d
LP
829
830 r = sd_bus_message_close_container(m);
831 if (r < 0)
f53bddf3 832 return bus_log_create_error(r);
4c213d6c
WC
833 }
834
835 /* Exec container */
badd28e1 836 if (!strv_isempty(arg_cmdline)) {
4c213d6c
WC
837 r = sd_bus_message_open_container(m, 'r', "sv");
838 if (r < 0)
f53bddf3 839 return bus_log_create_error(r);
4c213d6c
WC
840
841 r = sd_bus_message_append(m, "s", "ExecStart");
842 if (r < 0)
f53bddf3 843 return bus_log_create_error(r);
4c213d6c
WC
844
845 r = sd_bus_message_open_container(m, 'v', "a(sasb)");
846 if (r < 0)
f53bddf3 847 return bus_log_create_error(r);
4c213d6c
WC
848
849 r = sd_bus_message_open_container(m, 'a', "(sasb)");
850 if (r < 0)
f53bddf3 851 return bus_log_create_error(r);
4c213d6c
WC
852
853 r = sd_bus_message_open_container(m, 'r', "sasb");
854 if (r < 0)
f53bddf3 855 return bus_log_create_error(r);
4c213d6c 856
badd28e1 857 r = sd_bus_message_append(m, "s", arg_cmdline[0]);
4c213d6c 858 if (r < 0)
f53bddf3 859 return bus_log_create_error(r);
4c213d6c 860
badd28e1 861 r = sd_bus_message_append_strv(m, arg_cmdline);
4c213d6c 862 if (r < 0)
f53bddf3 863 return bus_log_create_error(r);
4c213d6c
WC
864
865 r = sd_bus_message_append(m, "b", false);
866 if (r < 0)
f53bddf3 867 return bus_log_create_error(r);
4c213d6c
WC
868
869 r = sd_bus_message_close_container(m);
870 if (r < 0)
f53bddf3 871 return bus_log_create_error(r);
4c213d6c
WC
872
873 r = sd_bus_message_close_container(m);
874 if (r < 0)
f53bddf3 875 return bus_log_create_error(r);
4c213d6c
WC
876
877 r = sd_bus_message_close_container(m);
878 if (r < 0)
f53bddf3 879 return bus_log_create_error(r);
4c213d6c
WC
880
881 r = sd_bus_message_close_container(m);
882 if (r < 0)
f53bddf3 883 return bus_log_create_error(r);
4c213d6c
WC
884 }
885
886 return 0;
887}
888
9b15b784
LP
889static int transient_scope_set_properties(sd_bus_message *m) {
890 int r;
891
892 assert(m);
893
89ada3ba 894 r = transient_unit_set_properties(m, UNIT_SCOPE, arg_property);
9b15b784
LP
895 if (r < 0)
896 return r;
897
898 r = transient_kill_set_properties(m);
899 if (r < 0)
900 return r;
901
37e605f9
LP
902 r = transient_cgroup_set_properties(m);
903 if (r < 0)
904 return r;
905
df0ff127 906 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
9b15b784 907 if (r < 0)
f53bddf3 908 return bus_log_create_error(r);
9b15b784
LP
909
910 return 0;
911}
912
4c213d6c
WC
913static int transient_timer_set_properties(sd_bus_message *m) {
914 int r;
915
916 assert(m);
917
89ada3ba 918 r = transient_unit_set_properties(m, UNIT_TIMER, arg_timer_property);
4c213d6c
WC
919 if (r < 0)
920 return r;
921
6348d701
LP
922 /* Automatically clean up our transient timers */
923 r = sd_bus_message_append(m, "(sv)", "RemainAfterElapse", "b", false);
924 if (r < 0)
f53bddf3 925 return bus_log_create_error(r);
6348d701 926
4c213d6c
WC
927 return 0;
928}
929
9c8d1e1a
LP
930static int make_unit_name(sd_bus *bus, UnitType t, char **ret) {
931 const char *unique, *id;
932 char *p;
933 int r;
934
935 assert(bus);
936 assert(t >= 0);
937 assert(t < _UNIT_TYPE_MAX);
938
939 r = sd_bus_get_unique_name(bus, &unique);
940 if (r < 0) {
941 sd_id128_t rnd;
942
943 /* We couldn't get the unique name, which is a pretty
944 * common case if we are connected to systemd
945 * directly. In that case, just pick a random uuid as
946 * name */
947
948 r = sd_id128_randomize(&rnd);
949 if (r < 0)
950 return log_error_errno(r, "Failed to generate random run unit name: %m");
951
952 if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0)
953 return log_oom();
954
955 return 0;
956 }
957
958 /* We managed to get the unique name, then let's use that to
959 * name our transient units. */
960
961 id = startswith(unique, ":1.");
baaa35ad
ZJS
962 if (!id)
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
964 "Unique name %s has unexpected format.",
965 unique);
9c8d1e1a 966
605405c6 967 p = strjoin("run-u", id, ".", unit_type_to_string(t));
9c8d1e1a
LP
968 if (!p)
969 return log_oom();
970
971 *ret = p;
972 return 0;
973}
974
2a453c2e
LP
975typedef struct RunContext {
976 sd_bus *bus;
977 sd_event *event;
978 PTYForward *forward;
979 sd_bus_slot *match;
980
2b5f7089 981 /* Current state of the unit */
2a453c2e 982 char *active_state;
2b5f7089
LP
983 bool has_job;
984
985 /* The exit data of the unit */
2a453c2e
LP
986 uint64_t inactive_exit_usec;
987 uint64_t inactive_enter_usec;
988 char *result;
989 uint64_t cpu_usage_nsec;
655aab20
LP
990 uint64_t ip_ingress_bytes;
991 uint64_t ip_egress_bytes;
826f7cb1
LP
992 uint64_t io_read_bytes;
993 uint64_t io_write_bytes;
2a453c2e
LP
994 uint32_t exit_code;
995 uint32_t exit_status;
996} RunContext;
997
998static void run_context_free(RunContext *c) {
999 assert(c);
1000
1001 c->forward = pty_forward_free(c->forward);
1002 c->match = sd_bus_slot_unref(c->match);
1003 c->bus = sd_bus_unref(c->bus);
1004 c->event = sd_event_unref(c->event);
1005
1006 free(c->active_state);
1007 free(c->result);
1008}
1009
1010static void run_context_check_done(RunContext *c) {
95f1d6bf 1011 bool done;
2a453c2e
LP
1012
1013 assert(c);
1014
1015 if (c->match)
2b5f7089 1016 done = STRPTR_IN_SET(c->active_state, "inactive", "failed") && !c->has_job;
95f1d6bf
LP
1017 else
1018 done = true;
2a453c2e 1019
95f1d6bf
LP
1020 if (c->forward && done) /* If the service is gone, it's time to drain the output */
1021 done = pty_forward_drain(c->forward);
2a453c2e
LP
1022
1023 if (done)
1024 sd_event_exit(c->event, EXIT_SUCCESS);
1025}
1026
2b5f7089
LP
1027static int map_job(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1028 bool *b = userdata;
1029 const char *job;
1030 uint32_t id;
1031 int r;
1032
1033 r = sd_bus_message_read(m, "(uo)", &id, &job);
1034 if (r < 0)
1035 return r;
1036
1037 *b = id != 0 || !streq(job, "/");
1038 return 0;
1039}
1040
5b1bad8d 1041static int run_context_update(RunContext *c, const char *path) {
2a453c2e
LP
1042
1043 static const struct bus_properties_map map[] = {
2b5f7089
LP
1044 { "ActiveState", "s", NULL, offsetof(RunContext, active_state) },
1045 { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_exit_usec) },
1046 { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_enter_usec) },
1047 { "Result", "s", NULL, offsetof(RunContext, result) },
1048 { "ExecMainCode", "i", NULL, offsetof(RunContext, exit_code) },
1049 { "ExecMainStatus", "i", NULL, offsetof(RunContext, exit_status) },
1050 { "CPUUsageNSec", "t", NULL, offsetof(RunContext, cpu_usage_nsec) },
1051 { "IPIngressBytes", "t", NULL, offsetof(RunContext, ip_ingress_bytes) },
1052 { "IPEgressBytes", "t", NULL, offsetof(RunContext, ip_egress_bytes) },
1053 { "IOReadBytes", "t", NULL, offsetof(RunContext, io_read_bytes) },
1054 { "IOWriteBytes", "t", NULL, offsetof(RunContext, io_write_bytes) },
1055 { "Job", "(uo)", map_job, offsetof(RunContext, has_job) },
2a453c2e
LP
1056 {}
1057 };
1058
f9e0eefc 1059 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
2a453c2e
LP
1060 int r;
1061
1062 r = bus_map_all_properties(c->bus,
1063 "org.freedesktop.systemd1",
5b1bad8d 1064 path,
2a453c2e 1065 map,
a7e4861c 1066 BUS_MAP_STRDUP,
f9e0eefc 1067 &error,
f37f8a61 1068 NULL,
2a453c2e
LP
1069 c);
1070 if (r < 0) {
1071 sd_event_exit(c->event, EXIT_FAILURE);
f9e0eefc 1072 return log_error_errno(r, "Failed to query unit state: %s", bus_error_message(&error, r));
2a453c2e
LP
1073 }
1074
1075 run_context_check_done(c);
1076 return 0;
1077}
1078
5b1bad8d
LP
1079static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
1080 RunContext *c = userdata;
1081
1082 assert(m);
1083 assert(c);
1084
1085 return run_context_update(c, sd_bus_message_get_path(m));
1086}
1087
2a453c2e
LP
1088static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
1089 RunContext *c = userdata;
1090
1091 assert(f);
1092
1093 if (rcode < 0) {
1094 sd_event_exit(c->event, EXIT_FAILURE);
1095 return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
1096 }
1097
1098 run_context_check_done(c);
1099 return 0;
1100}
1101
4c213d6c
WC
1102static int start_transient_service(
1103 sd_bus *bus,
2a453c2e 1104 int *retval) {
4c213d6c 1105
4afd3348
LP
1106 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1107 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d161f99 1108 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
ee451d76 1109 _cleanup_free_ char *service = NULL, *pty_path = NULL;
9b15b784 1110 _cleanup_close_ int master = -1;
4c213d6c
WC
1111 int r;
1112
1113 assert(bus);
2a453c2e 1114 assert(retval);
4c213d6c 1115
5dca7739 1116 if (arg_stdio == ARG_STDIO_PTY) {
9b15b784 1117
ee451d76 1118 if (arg_transport == BUS_TRANSPORT_LOCAL) {
669fc4e5 1119 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
ee451d76
LP
1120 if (master < 0)
1121 return log_error_errno(errno, "Failed to acquire pseudo tty: %m");
1122
1123 r = ptsname_malloc(master, &pty_path);
1124 if (r < 0)
1125 return log_error_errno(r, "Failed to determine tty name: %m");
1126
395745ba
LP
1127 if (unlockpt(master) < 0)
1128 return log_error_errno(errno, "Failed to unlock tty: %m");
1129
de33fc62 1130 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
4afd3348 1131 _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
20fc5811 1132 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
ee451d76
LP
1133 const char *s;
1134
024a8ec1 1135 r = sd_bus_default_system(&system_bus);
ee451d76 1136 if (r < 0)
024a8ec1 1137 return log_error_errno(r, "Failed to connect to system bus: %m");
ee451d76
LP
1138
1139 r = sd_bus_call_method(system_bus,
1140 "org.freedesktop.machine1",
1141 "/org/freedesktop/machine1",
1142 "org.freedesktop.machine1.Manager",
1143 "OpenMachinePTY",
1144 &error,
20fc5811 1145 &pty_reply,
ee451d76 1146 "s", arg_host);
4ae25393 1147 if (r < 0)
2a03b9ed 1148 return log_error_errno(r, "Failed to get machine PTY: %s", bus_error_message(&error, r));
ee451d76 1149
20fc5811 1150 r = sd_bus_message_read(pty_reply, "hs", &master, &s);
ee451d76
LP
1151 if (r < 0)
1152 return bus_log_parse_error(r);
1153
1154 master = fcntl(master, F_DUPFD_CLOEXEC, 3);
1155 if (master < 0)
1156 return log_error_errno(errno, "Failed to duplicate master fd: %m");
1157
1158 pty_path = strdup(s);
1159 if (!pty_path)
1160 return log_oom();
1161 } else
1162 assert_not_reached("Can't allocate tty via ssh");
9b15b784
LP
1163 }
1164
a7c71d21
LP
1165 /* Optionally, wait for the start job to complete. If we are supposed to read the service's stdin
1166 * lets skip this however, because we should start that already when the start job is running, and
1167 * there's little point in waiting for the start job to complete in that case anyway, as we'll wait
1168 * for EOF anyway, which is going to be much later. */
1169 if (!arg_no_block && arg_stdio == ARG_STDIO_NONE) {
3d161f99
LP
1170 r = bus_wait_for_jobs_new(bus, &w);
1171 if (r < 0)
1172 return log_error_errno(r, "Could not watch jobs: %m");
1173 }
1174
4c213d6c 1175 if (arg_unit) {
df7c4eb6
ZJS
1176 r = unit_name_mangle_with_suffix(arg_unit, "as unit",
1177 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
1178 ".service", &service);
7410616c
LP
1179 if (r < 0)
1180 return log_error_errno(r, "Failed to mangle unit name: %m");
9c8d1e1a
LP
1181 } else {
1182 r = make_unit_name(bus, UNIT_SERVICE, &service);
1183 if (r < 0)
1184 return r;
1185 }
4c213d6c 1186
7ad61613 1187 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
9f2e86af 1188 if (r < 0)
7040b626 1189 return bus_log_create_error(r);
9f2e86af 1190
8c7db2fb
EV
1191 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1192 if (r < 0)
1193 return bus_log_create_error(r);
1194
9b15b784 1195 /* Name and mode */
4c213d6c 1196 r = sd_bus_message_append(m, "ss", service, "fail");
c2756a68 1197 if (r < 0)
7040b626 1198 return bus_log_create_error(r);
c2756a68 1199
9b15b784 1200 /* Properties */
4c213d6c 1201 r = sd_bus_message_open_container(m, 'a', "(sv)");
c2756a68 1202 if (r < 0)
7040b626 1203 return bus_log_create_error(r);
c2756a68 1204
badd28e1 1205 r = transient_service_set_properties(m, pty_path);
c2756a68 1206 if (r < 0)
f53bddf3 1207 return r;
c2756a68 1208
4c213d6c 1209 r = sd_bus_message_close_container(m);
c2756a68 1210 if (r < 0)
7040b626 1211 return bus_log_create_error(r);
c2756a68 1212
9b15b784 1213 /* Auxiliary units */
4c213d6c 1214 r = sd_bus_message_append(m, "a(sa(sv))", 0);
c2756a68 1215 if (r < 0)
7040b626 1216 return bus_log_create_error(r);
c2756a68 1217
8a4b13c5 1218 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
8c7db2fb 1219
3d161f99 1220 r = sd_bus_call(bus, m, 0, &error, &reply);
024a8ec1
LP
1221 if (r < 0)
1222 return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
c2756a68 1223
3d161f99
LP
1224 if (w) {
1225 const char *object;
1226
1227 r = sd_bus_message_read(reply, "o", &object);
1228 if (r < 0)
1229 return bus_log_parse_error(r);
1230
1231 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1232 if (r < 0)
1233 return r;
1234 }
1235
2a453c2e
LP
1236 if (!arg_quiet)
1237 log_info("Running as unit: %s", service);
1238
5dca7739 1239 if (arg_wait || arg_stdio != ARG_STDIO_NONE) {
655aab20
LP
1240 _cleanup_(run_context_free) RunContext c = {
1241 .cpu_usage_nsec = NSEC_INFINITY,
1242 .ip_ingress_bytes = UINT64_MAX,
1243 .ip_egress_bytes = UINT64_MAX,
826f7cb1
LP
1244 .io_read_bytes = UINT64_MAX,
1245 .io_write_bytes = UINT64_MAX,
655aab20
LP
1246 .inactive_exit_usec = USEC_INFINITY,
1247 .inactive_enter_usec = USEC_INFINITY,
1248 };
95f1d6bf 1249 _cleanup_free_ char *path = NULL;
4c213d6c 1250
2a453c2e
LP
1251 c.bus = sd_bus_ref(bus);
1252
1253 r = sd_event_default(&c.event);
4c213d6c 1254 if (r < 0)
9b15b784 1255 return log_error_errno(r, "Failed to get event loop: %m");
4c213d6c 1256
2a453c2e
LP
1257 if (master >= 0) {
1258 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
1259 (void) sd_event_add_signal(c.event, NULL, SIGINT, NULL, NULL);
1260 (void) sd_event_add_signal(c.event, NULL, SIGTERM, NULL, NULL);
1261
1262 if (!arg_quiet)
1263 log_info("Press ^] three times within 1s to disconnect TTY.");
4c213d6c 1264
2a453c2e
LP
1265 r = pty_forward_new(c.event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &c.forward);
1266 if (r < 0)
1267 return log_error_errno(r, "Failed to create PTY forwarder: %m");
1268
1269 pty_forward_set_handler(c.forward, pty_forward_handler, &c);
d147457c
LP
1270
1271 /* Make sure to process any TTY events before we process bus events */
1272 (void) pty_forward_set_priority(c.forward, SD_EVENT_PRIORITY_IMPORTANT);
2a453c2e 1273 }
4c213d6c 1274
95f1d6bf
LP
1275 path = unit_dbus_path_from_name(service);
1276 if (!path)
1277 return log_oom();
2a453c2e 1278
75152a4d
LP
1279 r = sd_bus_match_signal_async(
1280 bus,
1281 &c.match,
1282 "org.freedesktop.systemd1",
1283 path,
1284 "org.freedesktop.DBus.Properties",
1285 "PropertiesChanged",
1286 on_properties_changed, NULL, &c);
95f1d6bf 1287 if (r < 0)
75152a4d 1288 return log_error_errno(r, "Failed to request properties changed signal match: %m");
4c213d6c 1289
d147457c 1290 r = sd_bus_attach_event(bus, c.event, SD_EVENT_PRIORITY_NORMAL);
95f1d6bf 1291 if (r < 0)
75152a4d 1292 return log_error_errno(r, "Failed to attach bus to event loop: %m");
5b1bad8d 1293
95f1d6bf
LP
1294 r = run_context_update(&c, path);
1295 if (r < 0)
1296 return r;
2a453c2e
LP
1297
1298 r = sd_event_loop(c.event);
4c213d6c 1299 if (r < 0)
9b15b784 1300 return log_error_errno(r, "Failed to run event loop: %m");
4c213d6c 1301
2a453c2e
LP
1302 if (c.forward) {
1303 char last_char = 0;
4c213d6c 1304
2a453c2e
LP
1305 r = pty_forward_get_last_char(c.forward, &last_char);
1306 if (r >= 0 && !arg_quiet && last_char != '\n')
1307 fputc('\n', stdout);
1308 }
4c213d6c 1309
95f1d6bf 1310 if (arg_wait && !arg_quiet) {
9182fb52 1311
f1d60962 1312 /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, with its
9182fb52
LP
1313 * original settings (i.e. proper line breaks), so that we can show the summary in a pretty
1314 * way. */
1315 c.forward = pty_forward_free(c.forward);
1316
2a453c2e
LP
1317 if (!isempty(c.result))
1318 log_info("Finished with result: %s", strna(c.result));
9f2e86af 1319
2a453c2e 1320 if (c.exit_code == CLD_EXITED)
f1d60962
ZJS
1321 log_info("Main processes terminated with: code=%s/status=%i",
1322 sigchld_code_to_string(c.exit_code), c.exit_status);
2a453c2e 1323 else if (c.exit_code > 0)
f1d60962
ZJS
1324 log_info("Main processes terminated with: code=%s/status=%s",
1325 sigchld_code_to_string(c.exit_code), signal_to_string(c.exit_status));
2a453c2e 1326
1f65fd49
ZJS
1327 if (timestamp_is_set(c.inactive_enter_usec) &&
1328 timestamp_is_set(c.inactive_exit_usec) &&
2a453c2e
LP
1329 c.inactive_enter_usec > c.inactive_exit_usec) {
1330 char ts[FORMAT_TIMESPAN_MAX];
f1d60962
ZJS
1331 log_info("Service runtime: %s",
1332 format_timespan(ts, sizeof ts, c.inactive_enter_usec - c.inactive_exit_usec, USEC_PER_MSEC));
2a453c2e
LP
1333 }
1334
655aab20 1335 if (c.cpu_usage_nsec != NSEC_INFINITY) {
2a453c2e 1336 char ts[FORMAT_TIMESPAN_MAX];
f1d60962 1337 log_info("CPU time consumed: %s",
d7a0f1f4 1338 format_timespan(ts, sizeof ts, DIV_ROUND_UP(c.cpu_usage_nsec, NSEC_PER_USEC), USEC_PER_MSEC));
2a453c2e 1339 }
655aab20
LP
1340
1341 if (c.ip_ingress_bytes != UINT64_MAX) {
1342 char bytes[FORMAT_BYTES_MAX];
f1d60962 1343 log_info("IP traffic received: %s", format_bytes(bytes, sizeof bytes, c.ip_ingress_bytes));
655aab20
LP
1344 }
1345 if (c.ip_egress_bytes != UINT64_MAX) {
1346 char bytes[FORMAT_BYTES_MAX];
f1d60962 1347 log_info("IP traffic sent: %s", format_bytes(bytes, sizeof bytes, c.ip_egress_bytes));
655aab20 1348 }
826f7cb1
LP
1349 if (c.io_read_bytes != UINT64_MAX) {
1350 char bytes[FORMAT_BYTES_MAX];
f1d60962 1351 log_info("IO bytes read: %s", format_bytes(bytes, sizeof bytes, c.io_read_bytes));
826f7cb1
LP
1352 }
1353 if (c.io_write_bytes != UINT64_MAX) {
1354 char bytes[FORMAT_BYTES_MAX];
f1d60962 1355 log_info("IO bytes written: %s", format_bytes(bytes, sizeof bytes, c.io_write_bytes));
826f7cb1 1356 }
2a453c2e
LP
1357 }
1358
7f3614e5
ZJS
1359 /* Try to propagate the service's return value. But if the service defines
1360 * e.g. SuccessExitStatus, honour this, and return 0 to mean "success". */
1361 if (streq_ptr(c.result, "success"))
1362 *retval = 0;
1363 else if (streq_ptr(c.result, "exit-code") && c.exit_status > 0)
2a453c2e 1364 *retval = c.exit_status;
7f3614e5
ZJS
1365 else if (streq_ptr(c.result, "signal"))
1366 *retval = EXIT_EXCEPTION;
2a453c2e
LP
1367 else
1368 *retval = EXIT_FAILURE;
1369 }
7040b626
LP
1370
1371 return 0;
6c12b52e
LP
1372}
1373
00f8eea8
LP
1374static int acquire_invocation_id(sd_bus *bus, sd_id128_t *ret) {
1375 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1376 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1377 const void *p;
1378 size_t l;
1379 int r;
1380
1381 assert(bus);
1382 assert(ret);
6c12b52e 1383
00f8eea8
LP
1384 r = sd_bus_get_property(bus,
1385 "org.freedesktop.systemd1",
1386 "/org/freedesktop/systemd1/unit/self",
1387 "org.freedesktop.systemd1.Unit",
1388 "InvocationID",
1389 &error,
1390 &reply,
1391 "ay");
1392 if (r < 0)
1393 return log_error_errno(r, "Failed to request invocation ID for scope: %s", bus_error_message(&error, r));
1394
1395 r = sd_bus_message_read_array(reply, 'y', &p, &l);
1396 if (r < 0)
1397 return bus_log_parse_error(r);
1398
1399 if (l != sizeof(sd_id128_t))
1400 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t));
1401
1402 memcpy(ret, p, l);
1403 return 0;
1404}
1405
1406static int start_transient_scope(sd_bus *bus) {
4afd3348
LP
1407 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1408 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
de158ed2 1409 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
9b15b784 1410 _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
4c213d6c 1411 _cleanup_free_ char *scope = NULL;
de158ed2 1412 const char *object = NULL;
00f8eea8 1413 sd_id128_t invocation_id;
6c12b52e
LP
1414 int r;
1415
8159d91a 1416 assert(bus);
badd28e1 1417 assert(!strv_isempty(arg_cmdline));
8159d91a 1418
de158ed2
LP
1419 r = bus_wait_for_jobs_new(bus, &w);
1420 if (r < 0)
1421 return log_oom();
1422
7de80bfe 1423 if (arg_unit) {
df7c4eb6
ZJS
1424 r = unit_name_mangle_with_suffix(arg_unit, "as unit",
1425 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
1426 ".scope", &scope);
7410616c
LP
1427 if (r < 0)
1428 return log_error_errno(r, "Failed to mangle scope name: %m");
9c8d1e1a
LP
1429 } else {
1430 r = make_unit_name(bus, UNIT_SCOPE, &scope);
1431 if (r < 0)
1432 return r;
1433 }
6c12b52e 1434
7ad61613 1435 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
c2756a68 1436 if (r < 0)
7040b626 1437 return bus_log_create_error(r);
c2756a68 1438
8c7db2fb
EV
1439 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1440 if (r < 0)
1441 return bus_log_create_error(r);
1442
9b15b784 1443 /* Name and Mode */
4c213d6c
WC
1444 r = sd_bus_message_append(m, "ss", scope, "fail");
1445 if (r < 0)
1446 return bus_log_create_error(r);
1447
9b15b784 1448 /* Properties */
4c213d6c
WC
1449 r = sd_bus_message_open_container(m, 'a', "(sv)");
1450 if (r < 0)
1451 return bus_log_create_error(r);
1452
1453 r = transient_scope_set_properties(m);
1454 if (r < 0)
f53bddf3 1455 return r;
4c213d6c
WC
1456
1457 r = sd_bus_message_close_container(m);
1458 if (r < 0)
1459 return bus_log_create_error(r);
1460
ee451d76 1461 /* Auxiliary units */
4c213d6c 1462 r = sd_bus_message_append(m, "a(sa(sv))", 0);
6c12b52e 1463 if (r < 0)
7040b626 1464 return bus_log_create_error(r);
6c12b52e 1465
8a4b13c5 1466 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
8c7db2fb 1467
de158ed2 1468 r = sd_bus_call(bus, m, 0, &error, &reply);
4ae25393 1469 if (r < 0)
2a03b9ed 1470 return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r));
c2756a68 1471
00f8eea8
LP
1472 r = sd_bus_message_read(reply, "o", &object);
1473 if (r < 0)
1474 return bus_log_parse_error(r);
1475
1476 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1477 if (r < 0)
1478 return r;
1479
1480 r = acquire_invocation_id(bus, &invocation_id);
1481 if (r < 0)
1482 return r;
1483
1484 r = strv_extendf(&user_env, "INVOCATION_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id));
1485 if (r < 0)
1486 return log_oom();
1487
4de33e7f 1488 if (arg_nice_set) {
4a62c710
MS
1489 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
1490 return log_error_errno(errno, "Failed to set nice level: %m");
4de33e7f
LP
1491 }
1492
1493 if (arg_exec_group) {
1494 gid_t gid;
1495
fafff8f1 1496 r = get_group_creds(&arg_exec_group, &gid, 0);
f647962d
MS
1497 if (r < 0)
1498 return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
4de33e7f 1499
4a62c710
MS
1500 if (setresgid(gid, gid, gid) < 0)
1501 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
4de33e7f
LP
1502 }
1503
1504 if (arg_exec_user) {
1505 const char *home, *shell;
1506 uid_t uid;
1507 gid_t gid;
1508
43ad3ad7 1509 r = get_user_creds(&arg_exec_user, &uid, &gid, &home, &shell, USER_CREDS_CLEAN|USER_CREDS_PREFER_NSS);
f647962d
MS
1510 if (r < 0)
1511 return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
4de33e7f 1512
be39ccf3
LP
1513 if (home) {
1514 r = strv_extendf(&user_env, "HOME=%s", home);
1515 if (r < 0)
1516 return log_oom();
1517 }
4de33e7f 1518
be39ccf3
LP
1519 if (shell) {
1520 r = strv_extendf(&user_env, "SHELL=%s", shell);
1521 if (r < 0)
1522 return log_oom();
1523 }
4de33e7f
LP
1524
1525 r = strv_extendf(&user_env, "USER=%s", arg_exec_user);
1526 if (r < 0)
1527 return log_oom();
1528
1529 r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user);
1530 if (r < 0)
1531 return log_oom();
1532
1533 if (!arg_exec_group) {
4a62c710
MS
1534 if (setresgid(gid, gid, gid) < 0)
1535 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
4de33e7f
LP
1536 }
1537
4a62c710
MS
1538 if (setresuid(uid, uid, uid) < 0)
1539 return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid);
4de33e7f
LP
1540 }
1541
1542 env = strv_env_merge(3, environ, user_env, arg_environment);
1543 if (!env)
1544 return log_oom();
1545
095dc596 1546 if (!arg_quiet)
f3ea87af 1547 log_info("Running scope as unit: %s", scope);
7040b626 1548
badd28e1 1549 execvpe(arg_cmdline[0], arg_cmdline, env);
9b15b784
LP
1550
1551 return log_error_errno(errno, "Failed to execute: %m");
1552}
1553
d59ef3e2 1554static int start_transient_trigger(
9b15b784 1555 sd_bus *bus,
d59ef3e2 1556 const char *suffix) {
9b15b784 1557
4afd3348
LP
1558 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1559 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
de158ed2 1560 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
d59ef3e2 1561 _cleanup_free_ char *trigger = NULL, *service = NULL;
de158ed2 1562 const char *object = NULL;
9b15b784
LP
1563 int r;
1564
1565 assert(bus);
9b15b784 1566
de158ed2
LP
1567 r = bus_wait_for_jobs_new(bus, &w);
1568 if (r < 0)
1569 return log_oom();
1570
9b15b784 1571 if (arg_unit) {
7410616c 1572 switch (unit_name_to_type(arg_unit)) {
9b15b784
LP
1573
1574 case UNIT_SERVICE:
1575 service = strdup(arg_unit);
1576 if (!service)
1577 return log_oom();
1578
d59ef3e2 1579 r = unit_name_change_suffix(service, suffix, &trigger);
7410616c
LP
1580 if (r < 0)
1581 return log_error_errno(r, "Failed to change unit suffix: %m");
9b15b784
LP
1582 break;
1583
1584 case UNIT_TIMER:
d59ef3e2
YW
1585 trigger = strdup(arg_unit);
1586 if (!trigger)
9b15b784
LP
1587 return log_oom();
1588
d59ef3e2 1589 r = unit_name_change_suffix(trigger, ".service", &service);
7410616c
LP
1590 if (r < 0)
1591 return log_error_errno(r, "Failed to change unit suffix: %m");
9b15b784
LP
1592 break;
1593
1594 default:
df7c4eb6
ZJS
1595 r = unit_name_mangle_with_suffix(arg_unit, "as unit",
1596 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
1597 ".service", &service);
7410616c
LP
1598 if (r < 0)
1599 return log_error_errno(r, "Failed to mangle unit name: %m");
9b15b784 1600
df7c4eb6
ZJS
1601 r = unit_name_mangle_with_suffix(arg_unit, "as trigger",
1602 arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
1603 suffix, &trigger);
7410616c
LP
1604 if (r < 0)
1605 return log_error_errno(r, "Failed to mangle unit name: %m");
9b15b784
LP
1606
1607 break;
1608 }
9c8d1e1a
LP
1609 } else {
1610 r = make_unit_name(bus, UNIT_SERVICE, &service);
1611 if (r < 0)
1612 return r;
1613
d59ef3e2 1614 r = unit_name_change_suffix(service, suffix, &trigger);
9c8d1e1a
LP
1615 if (r < 0)
1616 return log_error_errno(r, "Failed to change unit suffix: %m");
1617 }
9b15b784 1618
7ad61613 1619 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
9b15b784
LP
1620 if (r < 0)
1621 return bus_log_create_error(r);
1622
8c7db2fb
EV
1623 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1624 if (r < 0)
1625 return bus_log_create_error(r);
1626
9b15b784 1627 /* Name and Mode */
d59ef3e2 1628 r = sd_bus_message_append(m, "ss", trigger, "fail");
9b15b784
LP
1629 if (r < 0)
1630 return bus_log_create_error(r);
1631
1632 /* Properties */
1633 r = sd_bus_message_open_container(m, 'a', "(sv)");
1634 if (r < 0)
1635 return bus_log_create_error(r);
1636
d59ef3e2
YW
1637 if (streq(suffix, ".path"))
1638 r = transient_unit_set_properties(m, UNIT_PATH, arg_path_property);
1639 else if (streq(suffix, ".socket"))
1640 r = transient_unit_set_properties(m, UNIT_SOCKET, arg_socket_property);
1641 else if (streq(suffix, ".timer"))
1642 r = transient_timer_set_properties(m);
1643 else
1644 assert_not_reached("Invalid suffix");
9b15b784 1645 if (r < 0)
f53bddf3 1646 return r;
9b15b784
LP
1647
1648 r = sd_bus_message_close_container(m);
1649 if (r < 0)
1650 return bus_log_create_error(r);
1651
1652 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
1653 if (r < 0)
1654 return bus_log_create_error(r);
1655
badd28e1 1656 if (!strv_isempty(arg_cmdline)) {
9b15b784
LP
1657 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
1658 if (r < 0)
1659 return bus_log_create_error(r);
1660
1661 r = sd_bus_message_append(m, "s", service);
1662 if (r < 0)
1663 return bus_log_create_error(r);
1664
1665 r = sd_bus_message_open_container(m, 'a', "(sv)");
1666 if (r < 0)
1667 return bus_log_create_error(r);
1668
badd28e1 1669 r = transient_service_set_properties(m, NULL);
9b15b784 1670 if (r < 0)
f53bddf3 1671 return r;
9b15b784
LP
1672
1673 r = sd_bus_message_close_container(m);
1674 if (r < 0)
1675 return bus_log_create_error(r);
1676
1677 r = sd_bus_message_close_container(m);
1678 if (r < 0)
1679 return bus_log_create_error(r);
1680 }
1681
1682 r = sd_bus_message_close_container(m);
1683 if (r < 0)
1684 return bus_log_create_error(r);
1685
8a4b13c5 1686 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
8c7db2fb 1687
de158ed2 1688 r = sd_bus_call(bus, m, 0, &error, &reply);
4ae25393 1689 if (r < 0)
2a03b9ed 1690 return log_error_errno(r, "Failed to start transient %s unit: %s", suffix + 1, bus_error_message(&error, r));
9b15b784 1691
de158ed2
LP
1692 r = sd_bus_message_read(reply, "o", &object);
1693 if (r < 0)
1694 return bus_log_parse_error(r);
1695
1696 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1697 if (r < 0)
1698 return r;
1699
3dea75de 1700 if (!arg_quiet) {
d59ef3e2 1701 log_info("Running %s as unit: %s", suffix + 1, trigger);
badd28e1 1702 if (!strv_isempty(arg_cmdline))
3dea75de
NO
1703 log_info("Will run service as unit: %s", service);
1704 }
9b15b784
LP
1705
1706 return 0;
c2756a68
LP
1707}
1708
ef08ad7a 1709static int run(int argc, char* argv[]) {
4afd3348 1710 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
badd28e1 1711 _cleanup_free_ char *description = NULL;
2a453c2e 1712 int r, retval = EXIT_SUCCESS;
c2756a68 1713
1a043959 1714 log_show_color(true);
c2756a68
LP
1715 log_parse_environment();
1716 log_open();
1717
6c12b52e
LP
1718 r = parse_argv(argc, argv);
1719 if (r <= 0)
0565120f 1720 return r;
c2756a68 1721
8038b99d
ZJS
1722 if (!strv_isempty(arg_cmdline) &&
1723 arg_transport == BUS_TRANSPORT_LOCAL &&
1724 !strv_find_startswith(arg_property, "RootDirectory=") &&
1725 !strv_find_startswith(arg_property, "RootImage=")) {
1726 /* Patch in an absolute path to fail early for user convenience, but only when we can do it
1727 * (i.e. we will be running from the same file system). This also uses the user's $PATH,
1728 * while we use a fixed search path in the manager. */
85eca92e 1729
8038b99d 1730 _cleanup_free_ char *command = NULL;
f7bc0c32 1731 r = find_executable(arg_cmdline[0], &command);
0565120f
LP
1732 if (r < 0)
1733 return log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]);
85eca92e 1734
badd28e1 1735 free_and_replace(arg_cmdline[0], command);
c9d954b2 1736 }
c9d954b2 1737
9f2e86af 1738 if (!arg_description) {
badd28e1 1739 description = strv_join(arg_cmdline, " ");
0565120f
LP
1740 if (!description)
1741 return log_oom();
9f2e86af 1742
4c213d6c 1743 if (arg_unit && isempty(description)) {
2fc09a9c
DM
1744 r = free_and_strdup(&description, arg_unit);
1745 if (r < 0)
0565120f 1746 return log_oom();
4c213d6c
WC
1747 }
1748
9f2e86af
LP
1749 arg_description = description;
1750 }
1751
2a453c2e
LP
1752 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1753 * connection */
5dca7739 1754 if (arg_wait || arg_stdio != ARG_STDIO_NONE)
2a453c2e
LP
1755 r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus);
1756 else
1757 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
0565120f 1758 if (r < 0)
ddbab78f 1759 return bus_log_connect_error(r);
c2756a68 1760
6c12b52e 1761 if (arg_scope)
badd28e1 1762 r = start_transient_scope(bus);
d59ef3e2 1763 else if (arg_path_property)
badd28e1 1764 r = start_transient_trigger(bus, ".path");
d59ef3e2 1765 else if (arg_socket_property)
badd28e1 1766 r = start_transient_trigger(bus, ".socket");
787be190 1767 else if (arg_with_timer)
badd28e1 1768 r = start_transient_trigger(bus, ".timer");
6c12b52e 1769 else
badd28e1 1770 r = start_transient_service(bus, &retval);
0565120f
LP
1771 if (r < 0)
1772 return r;
c2756a68 1773
0565120f 1774 return retval;
c2756a68 1775}
ef08ad7a
LP
1776
1777DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);