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