]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/run/run.c
2910fcb2725ec6537f5f9645ccdaae0ad52dcbcf
[thirdparty/systemd.git] / src / run / run.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <getopt.h>
4 #include <stdio.h>
5
6 #include "sd-bus.h"
7 #include "sd-event.h"
8
9 #include "alloc-util.h"
10 #include "bus-error.h"
11 #include "bus-unit-util.h"
12 #include "bus-util.h"
13 #include "calendarspec.h"
14 #include "env-util.h"
15 #include "fd-util.h"
16 #include "format-util.h"
17 #include "parse-util.h"
18 #include "path-util.h"
19 #include "process-util.h"
20 #include "ptyfwd.h"
21 #include "signal-util.h"
22 #include "spawn-polkit-agent.h"
23 #include "strv.h"
24 #include "terminal-util.h"
25 #include "unit-def.h"
26 #include "unit-name.h"
27 #include "user-util.h"
28
29 static bool arg_ask_password = true;
30 static bool arg_scope = false;
31 static bool arg_remain_after_exit = false;
32 static bool arg_no_block = false;
33 static bool arg_wait = false;
34 static const char *arg_unit = NULL;
35 static const char *arg_description = NULL;
36 static const char *arg_slice = NULL;
37 static bool arg_send_sighup = false;
38 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
39 static const char *arg_host = NULL;
40 static bool arg_user = false;
41 static const char *arg_service_type = NULL;
42 static const char *arg_exec_user = NULL;
43 static const char *arg_exec_group = NULL;
44 static int arg_nice = 0;
45 static bool arg_nice_set = false;
46 static char **arg_environment = NULL;
47 static char **arg_property = NULL;
48 static enum {
49 ARG_STDIO_NONE, /* The default, as it is for normal services, stdin connected to /dev/null, and stdout+stderr to the journal */
50 ARG_STDIO_PTY, /* Interactive behaviour, requested by --pty: we allocate a pty and connect it to the TTY we are invoked from */
51 ARG_STDIO_DIRECT, /* Directly pass our stdin/stdout/stderr to the activated service, useful for usage in shell pipelines, requested by --pipe */
52 ARG_STDIO_AUTO, /* If --pipe and --pty are used together we use --pty when invoked on a TTY, and --pipe otherwise */
53 } arg_stdio = ARG_STDIO_NONE;
54 static char **arg_path_property = NULL;
55 static char **arg_socket_property = NULL;
56 static char **arg_timer_property = NULL;
57 static bool with_timer = false;
58 static bool arg_quiet = false;
59 static bool arg_aggressive_gc = false;
60
61 static void help(void) {
62 printf("%s [OPTIONS...] {COMMAND} [ARGS...]\n\n"
63 "Run the specified command in a transient scope or service.\n\n"
64 " -h --help Show this help\n"
65 " --version Show package version\n"
66 " --no-ask-password Do not prompt for password\n"
67 " --user Run as user unit\n"
68 " -H --host=[USER@]HOST Operate on remote host\n"
69 " -M --machine=CONTAINER Operate on local container\n"
70 " --scope Run this as scope rather than service\n"
71 " --unit=UNIT Run under the specified unit name\n"
72 " -p --property=NAME=VALUE Set service or scope unit property\n"
73 " --description=TEXT Description for unit\n"
74 " --slice=SLICE Run in the specified slice\n"
75 " --no-block Do not wait until operation finished\n"
76 " -r --remain-after-exit Leave service around until explicitly stopped\n"
77 " --wait Wait until service stopped again\n"
78 " --send-sighup Send SIGHUP when terminating\n"
79 " --service-type=TYPE Service type\n"
80 " --uid=USER Run as system user\n"
81 " --gid=GROUP Run as system group\n"
82 " --nice=NICE Nice level\n"
83 " -E --setenv=NAME=VALUE Set environment\n"
84 " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n"
85 " STDERR\n"
86 " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
87 " -q --quiet Suppress information messages during runtime\n"
88 " -G --collect Unload unit after it ran, even when failed\n\n"
89 "Path options:\n"
90 " --path-property=NAME=VALUE Set path unit property\n\n"
91 "Socket options:\n"
92 " --socket-property=NAME=VALUE Set socket unit property\n\n"
93 "Timer options:\n"
94 " --on-active=SECONDS Run after SECONDS delay\n"
95 " --on-boot=SECONDS Run SECONDS after machine was booted up\n"
96 " --on-startup=SECONDS Run SECONDS after systemd activation\n"
97 " --on-unit-active=SECONDS Run SECONDS after the last activation\n"
98 " --on-unit-inactive=SECONDS Run SECONDS after the last deactivation\n"
99 " --on-calendar=SPEC Realtime timer\n"
100 " --timer-property=NAME=VALUE Set timer unit property\n"
101 , program_invocation_short_name);
102 }
103
104 static int add_timer_property(const char *name, const char *val) {
105 char *p;
106
107 assert(name);
108 assert(val);
109
110 p = strjoin(name, "=", val);
111 if (!p)
112 return log_oom();
113
114 if (strv_consume(&arg_timer_property, p) < 0)
115 return log_oom();
116
117 return 0;
118 }
119
120 static int parse_argv(int argc, char *argv[]) {
121
122 enum {
123 ARG_VERSION = 0x100,
124 ARG_USER,
125 ARG_SYSTEM,
126 ARG_SCOPE,
127 ARG_UNIT,
128 ARG_DESCRIPTION,
129 ARG_SLICE,
130 ARG_SEND_SIGHUP,
131 ARG_SERVICE_TYPE,
132 ARG_EXEC_USER,
133 ARG_EXEC_GROUP,
134 ARG_NICE,
135 ARG_ON_ACTIVE,
136 ARG_ON_BOOT,
137 ARG_ON_STARTUP,
138 ARG_ON_UNIT_ACTIVE,
139 ARG_ON_UNIT_INACTIVE,
140 ARG_ON_CALENDAR,
141 ARG_TIMER_PROPERTY,
142 ARG_PATH_PROPERTY,
143 ARG_SOCKET_PROPERTY,
144 ARG_NO_BLOCK,
145 ARG_NO_ASK_PASSWORD,
146 ARG_WAIT,
147 };
148
149 static const struct option options[] = {
150 { "help", no_argument, NULL, 'h' },
151 { "version", no_argument, NULL, ARG_VERSION },
152 { "user", no_argument, NULL, ARG_USER },
153 { "system", no_argument, NULL, ARG_SYSTEM },
154 { "scope", no_argument, NULL, ARG_SCOPE },
155 { "unit", required_argument, NULL, ARG_UNIT },
156 { "description", required_argument, NULL, ARG_DESCRIPTION },
157 { "slice", required_argument, NULL, ARG_SLICE },
158 { "remain-after-exit", no_argument, NULL, 'r' },
159 { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
160 { "host", required_argument, NULL, 'H' },
161 { "machine", required_argument, NULL, 'M' },
162 { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
163 { "wait", no_argument, NULL, ARG_WAIT },
164 { "uid", required_argument, NULL, ARG_EXEC_USER },
165 { "gid", required_argument, NULL, ARG_EXEC_GROUP },
166 { "nice", required_argument, NULL, ARG_NICE },
167 { "setenv", required_argument, NULL, 'E' },
168 { "property", required_argument, NULL, 'p' },
169 { "tty", no_argument, NULL, 't' }, /* deprecated alias */
170 { "pty", no_argument, NULL, 't' },
171 { "pipe", no_argument, NULL, 'P' },
172 { "quiet", no_argument, NULL, 'q' },
173 { "on-active", required_argument, NULL, ARG_ON_ACTIVE },
174 { "on-boot", required_argument, NULL, ARG_ON_BOOT },
175 { "on-startup", required_argument, NULL, ARG_ON_STARTUP },
176 { "on-unit-active", required_argument, NULL, ARG_ON_UNIT_ACTIVE },
177 { "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE },
178 { "on-calendar", required_argument, NULL, ARG_ON_CALENDAR },
179 { "timer-property", required_argument, NULL, ARG_TIMER_PROPERTY },
180 { "path-property", required_argument, NULL, ARG_PATH_PROPERTY },
181 { "socket-property", required_argument, NULL, ARG_SOCKET_PROPERTY },
182 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
183 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
184 { "collect", no_argument, NULL, 'G' },
185 {},
186 };
187
188 bool with_trigger = false;
189 int r, c;
190
191 assert(argc >= 0);
192 assert(argv);
193
194 while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqG", options, NULL)) >= 0)
195
196 switch (c) {
197
198 case 'h':
199 help();
200 return 0;
201
202 case ARG_VERSION:
203 return version();
204
205 case ARG_NO_ASK_PASSWORD:
206 arg_ask_password = false;
207 break;
208
209 case ARG_USER:
210 arg_user = true;
211 break;
212
213 case ARG_SYSTEM:
214 arg_user = false;
215 break;
216
217 case ARG_SCOPE:
218 arg_scope = true;
219 break;
220
221 case ARG_UNIT:
222 arg_unit = optarg;
223 break;
224
225 case ARG_DESCRIPTION:
226 arg_description = optarg;
227 break;
228
229 case ARG_SLICE:
230 arg_slice = optarg;
231 break;
232
233 case ARG_SEND_SIGHUP:
234 arg_send_sighup = true;
235 break;
236
237 case 'r':
238 arg_remain_after_exit = true;
239 break;
240
241 case 'H':
242 arg_transport = BUS_TRANSPORT_REMOTE;
243 arg_host = optarg;
244 break;
245
246 case 'M':
247 arg_transport = BUS_TRANSPORT_MACHINE;
248 arg_host = optarg;
249 break;
250
251 case ARG_SERVICE_TYPE:
252 arg_service_type = optarg;
253 break;
254
255 case ARG_EXEC_USER:
256 arg_exec_user = optarg;
257 break;
258
259 case ARG_EXEC_GROUP:
260 arg_exec_group = optarg;
261 break;
262
263 case ARG_NICE:
264 r = parse_nice(optarg, &arg_nice);
265 if (r < 0)
266 return log_error_errno(r, "Failed to parse nice value: %s", optarg);
267
268 arg_nice_set = true;
269 break;
270
271 case 'E':
272 if (strv_extend(&arg_environment, optarg) < 0)
273 return log_oom();
274
275 break;
276
277 case 'p':
278 if (strv_extend(&arg_property, optarg) < 0)
279 return log_oom();
280
281 break;
282
283 case 't': /* --pty */
284 if (IN_SET(arg_stdio, ARG_STDIO_DIRECT, ARG_STDIO_AUTO)) /* if --pipe is already used, upgrade to auto mode */
285 arg_stdio = ARG_STDIO_AUTO;
286 else
287 arg_stdio = ARG_STDIO_PTY;
288 break;
289
290 case 'P': /* --pipe */
291 if (IN_SET(arg_stdio, ARG_STDIO_PTY, ARG_STDIO_AUTO)) /* If --pty is already used, upgrade to auto mode */
292 arg_stdio = ARG_STDIO_AUTO;
293 else
294 arg_stdio = ARG_STDIO_DIRECT;
295 break;
296
297 case 'q':
298 arg_quiet = true;
299 break;
300
301 case ARG_ON_ACTIVE:
302 r = add_timer_property("OnActiveSec", optarg);
303 if (r < 0)
304 return r;
305
306 with_timer = true;
307 break;
308
309 case ARG_ON_BOOT:
310 r = add_timer_property("OnBootSec", optarg);
311 if (r < 0)
312 return r;
313
314 with_timer = true;
315 break;
316
317 case ARG_ON_STARTUP:
318 r = add_timer_property("OnStartupSec", optarg);
319 if (r < 0)
320 return r;
321
322 with_timer = true;
323 break;
324
325 case ARG_ON_UNIT_ACTIVE:
326 r = add_timer_property("OnUnitActiveSec", optarg);
327 if (r < 0)
328 return r;
329
330 with_timer = true;
331 break;
332
333 case ARG_ON_UNIT_INACTIVE:
334 r = add_timer_property("OnUnitInactiveSec", optarg);
335 if (r < 0)
336 return r;
337
338 with_timer = true;
339 break;
340
341 case ARG_ON_CALENDAR:
342 r = add_timer_property("OnCalendar", optarg);
343 if (r < 0)
344 return r;
345
346 with_timer = true;
347 break;
348
349 case ARG_TIMER_PROPERTY:
350
351 if (strv_extend(&arg_timer_property, optarg) < 0)
352 return log_oom();
353
354 with_timer = with_timer ||
355 !!startswith(optarg, "OnActiveSec=") ||
356 !!startswith(optarg, "OnBootSec=") ||
357 !!startswith(optarg, "OnStartupSec=") ||
358 !!startswith(optarg, "OnUnitActiveSec=") ||
359 !!startswith(optarg, "OnUnitInactiveSec=") ||
360 !!startswith(optarg, "OnCalendar=");
361 break;
362
363 case ARG_PATH_PROPERTY:
364
365 if (strv_extend(&arg_path_property, optarg) < 0)
366 return log_oom();
367
368 break;
369
370 case ARG_SOCKET_PROPERTY:
371
372 if (strv_extend(&arg_socket_property, optarg) < 0)
373 return log_oom();
374
375 break;
376
377 case ARG_NO_BLOCK:
378 arg_no_block = true;
379 break;
380
381 case ARG_WAIT:
382 arg_wait = true;
383 break;
384
385 case 'G':
386 arg_aggressive_gc = true;
387 break;
388
389 case '?':
390 return -EINVAL;
391
392 default:
393 assert_not_reached("Unhandled option");
394 }
395
396 with_trigger = !!arg_path_property || !!arg_socket_property || with_timer;
397
398 /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */
399 if ((int) !!arg_path_property + (int) !!arg_socket_property + (int) with_timer > 1) {
400 log_error("Only single trigger (path, socket, timer) unit can be created.");
401 return -EINVAL;
402 }
403
404 if (arg_stdio == ARG_STDIO_AUTO) {
405 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
406 * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
407 * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
408 arg_stdio = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) ?
409 ARG_STDIO_PTY :
410 ARG_STDIO_DIRECT;
411 }
412
413 if ((optind >= argc) && (!arg_unit || !with_trigger)) {
414 log_error("Command line to execute required.");
415 return -EINVAL;
416 }
417
418 if (arg_user && arg_transport != BUS_TRANSPORT_LOCAL) {
419 log_error("Execution in user context is not supported on non-local systems.");
420 return -EINVAL;
421 }
422
423 if (arg_scope && arg_transport != BUS_TRANSPORT_LOCAL) {
424 log_error("Scope execution is not supported on non-local systems.");
425 return -EINVAL;
426 }
427
428 if (arg_scope && (arg_remain_after_exit || arg_service_type)) {
429 log_error("--remain-after-exit and --service-type= are not supported in --scope mode.");
430 return -EINVAL;
431 }
432
433 if (arg_stdio != ARG_STDIO_NONE && (with_trigger || arg_scope)) {
434 log_error("--pty/--pipe is not compatible in timer or --scope mode.");
435 return -EINVAL;
436 }
437
438 if (arg_stdio != ARG_STDIO_NONE && arg_transport == BUS_TRANSPORT_REMOTE) {
439 log_error("--pty/--pipe is only supported when connecting to the local system or containers.");
440 return -EINVAL;
441 }
442
443 if (arg_stdio != ARG_STDIO_NONE && arg_no_block) {
444 log_error("--pty/--pipe is not compatible with --no-block.");
445 return -EINVAL;
446 }
447
448 if (arg_scope && with_trigger) {
449 log_error("Path, socket or timer options are not supported in --scope mode.");
450 return -EINVAL;
451 }
452
453 if (arg_timer_property && !with_timer) {
454 log_error("--timer-property= has no effect without any other timer options.");
455 return -EINVAL;
456 }
457
458 if (arg_wait) {
459 if (arg_no_block) {
460 log_error("--wait may not be combined with --no-block.");
461 return -EINVAL;
462 }
463
464 if (with_trigger) {
465 log_error("--wait may not be combined with path, socket or timer operations.");
466 return -EINVAL;
467 }
468
469 if (arg_scope) {
470 log_error("--wait may not be combined with --scope.");
471 return -EINVAL;
472 }
473 }
474
475 return 1;
476 }
477
478 static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) {
479 int r;
480
481 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
482 if (r < 0)
483 return bus_log_create_error(r);
484
485 if (arg_aggressive_gc) {
486 r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed");
487 if (r < 0)
488 return bus_log_create_error(r);
489 }
490
491 r = bus_append_unit_property_assignment_many(m, t, properties);
492 if (r < 0)
493 return r;
494
495 return 0;
496 }
497
498 static int transient_cgroup_set_properties(sd_bus_message *m) {
499 int r;
500 assert(m);
501
502 if (!isempty(arg_slice)) {
503 _cleanup_free_ char *slice = NULL;
504
505 r = unit_name_mangle_with_suffix(arg_slice, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".slice", &slice);
506 if (r < 0)
507 return log_error_errno(r, "Failed to mangle name '%s': %m", arg_slice);
508
509 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
510 if (r < 0)
511 return bus_log_create_error(r);
512 }
513
514 return 0;
515 }
516
517 static int transient_kill_set_properties(sd_bus_message *m) {
518 int r;
519
520 assert(m);
521
522 if (arg_send_sighup) {
523 r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
524 if (r < 0)
525 return bus_log_create_error(r);
526 }
527
528 return 0;
529 }
530
531 static int transient_service_set_properties(sd_bus_message *m, char **argv, const char *pty_path) {
532 bool send_term = false;
533 int r;
534
535 assert(m);
536
537 r = transient_unit_set_properties(m, UNIT_SERVICE, arg_property);
538 if (r < 0)
539 return r;
540
541 r = transient_kill_set_properties(m);
542 if (r < 0)
543 return r;
544
545 r = transient_cgroup_set_properties(m);
546 if (r < 0)
547 return r;
548
549 if (arg_wait || arg_stdio != ARG_STDIO_NONE) {
550 r = sd_bus_message_append(m, "(sv)", "AddRef", "b", 1);
551 if (r < 0)
552 return bus_log_create_error(r);
553 }
554
555 if (arg_remain_after_exit) {
556 r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
557 if (r < 0)
558 return bus_log_create_error(r);
559 }
560
561 if (arg_service_type) {
562 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
563 if (r < 0)
564 return bus_log_create_error(r);
565 }
566
567 if (arg_exec_user) {
568 r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
569 if (r < 0)
570 return bus_log_create_error(r);
571 }
572
573 if (arg_exec_group) {
574 r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
575 if (r < 0)
576 return bus_log_create_error(r);
577 }
578
579 if (arg_nice_set) {
580 r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
581 if (r < 0)
582 return bus_log_create_error(r);
583 }
584
585 if (pty_path) {
586 r = sd_bus_message_append(m,
587 "(sv)(sv)(sv)(sv)",
588 "StandardInput", "s", "tty",
589 "StandardOutput", "s", "tty",
590 "StandardError", "s", "tty",
591 "TTYPath", "s", pty_path);
592 if (r < 0)
593 return bus_log_create_error(r);
594
595 send_term = true;
596
597 } else if (arg_stdio == ARG_STDIO_DIRECT) {
598 r = sd_bus_message_append(m,
599 "(sv)(sv)(sv)",
600 "StandardInputFileDescriptor", "h", STDIN_FILENO,
601 "StandardOutputFileDescriptor", "h", STDOUT_FILENO,
602 "StandardErrorFileDescriptor", "h", STDERR_FILENO);
603 if (r < 0)
604 return bus_log_create_error(r);
605
606 send_term = isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO);
607 }
608
609 if (send_term) {
610 const char *e;
611
612 e = getenv("TERM");
613 if (e) {
614 char *n;
615
616 n = strjoina("TERM=", e);
617 r = sd_bus_message_append(m,
618 "(sv)",
619 "Environment", "as", 1, n);
620 if (r < 0)
621 return bus_log_create_error(r);
622 }
623 }
624
625 if (!strv_isempty(arg_environment)) {
626 r = sd_bus_message_open_container(m, 'r', "sv");
627 if (r < 0)
628 return bus_log_create_error(r);
629
630 r = sd_bus_message_append(m, "s", "Environment");
631 if (r < 0)
632 return bus_log_create_error(r);
633
634 r = sd_bus_message_open_container(m, 'v', "as");
635 if (r < 0)
636 return bus_log_create_error(r);
637
638 r = sd_bus_message_append_strv(m, arg_environment);
639 if (r < 0)
640 return bus_log_create_error(r);
641
642 r = sd_bus_message_close_container(m);
643 if (r < 0)
644 return bus_log_create_error(r);
645
646 r = sd_bus_message_close_container(m);
647 if (r < 0)
648 return bus_log_create_error(r);
649 }
650
651 /* Exec container */
652 {
653 r = sd_bus_message_open_container(m, 'r', "sv");
654 if (r < 0)
655 return bus_log_create_error(r);
656
657 r = sd_bus_message_append(m, "s", "ExecStart");
658 if (r < 0)
659 return bus_log_create_error(r);
660
661 r = sd_bus_message_open_container(m, 'v', "a(sasb)");
662 if (r < 0)
663 return bus_log_create_error(r);
664
665 r = sd_bus_message_open_container(m, 'a', "(sasb)");
666 if (r < 0)
667 return bus_log_create_error(r);
668
669 r = sd_bus_message_open_container(m, 'r', "sasb");
670 if (r < 0)
671 return bus_log_create_error(r);
672
673 r = sd_bus_message_append(m, "s", argv[0]);
674 if (r < 0)
675 return bus_log_create_error(r);
676
677 r = sd_bus_message_append_strv(m, argv);
678 if (r < 0)
679 return bus_log_create_error(r);
680
681 r = sd_bus_message_append(m, "b", false);
682 if (r < 0)
683 return bus_log_create_error(r);
684
685 r = sd_bus_message_close_container(m);
686 if (r < 0)
687 return bus_log_create_error(r);
688
689 r = sd_bus_message_close_container(m);
690 if (r < 0)
691 return bus_log_create_error(r);
692
693 r = sd_bus_message_close_container(m);
694 if (r < 0)
695 return bus_log_create_error(r);
696
697 r = sd_bus_message_close_container(m);
698 if (r < 0)
699 return bus_log_create_error(r);
700 }
701
702 return 0;
703 }
704
705 static int transient_scope_set_properties(sd_bus_message *m) {
706 int r;
707
708 assert(m);
709
710 r = transient_unit_set_properties(m, UNIT_SCOPE, arg_property);
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);
719 if (r < 0)
720 return r;
721
722 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
723 if (r < 0)
724 return bus_log_create_error(r);
725
726 return 0;
727 }
728
729 static int transient_timer_set_properties(sd_bus_message *m) {
730 int r;
731
732 assert(m);
733
734 r = transient_unit_set_properties(m, UNIT_TIMER, arg_timer_property);
735 if (r < 0)
736 return r;
737
738 /* Automatically clean up our transient timers */
739 r = sd_bus_message_append(m, "(sv)", "RemainAfterElapse", "b", false);
740 if (r < 0)
741 return bus_log_create_error(r);
742
743 return 0;
744 }
745
746 static int make_unit_name(sd_bus *bus, UnitType t, char **ret) {
747 const char *unique, *id;
748 char *p;
749 int r;
750
751 assert(bus);
752 assert(t >= 0);
753 assert(t < _UNIT_TYPE_MAX);
754
755 r = sd_bus_get_unique_name(bus, &unique);
756 if (r < 0) {
757 sd_id128_t rnd;
758
759 /* We couldn't get the unique name, which is a pretty
760 * common case if we are connected to systemd
761 * directly. In that case, just pick a random uuid as
762 * name */
763
764 r = sd_id128_randomize(&rnd);
765 if (r < 0)
766 return log_error_errno(r, "Failed to generate random run unit name: %m");
767
768 if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0)
769 return log_oom();
770
771 return 0;
772 }
773
774 /* We managed to get the unique name, then let's use that to
775 * name our transient units. */
776
777 id = startswith(unique, ":1.");
778 if (!id) {
779 log_error("Unique name %s has unexpected format.", unique);
780 return -EINVAL;
781 }
782
783 p = strjoin("run-u", id, ".", unit_type_to_string(t));
784 if (!p)
785 return log_oom();
786
787 *ret = p;
788 return 0;
789 }
790
791 typedef struct RunContext {
792 sd_bus *bus;
793 sd_event *event;
794 PTYForward *forward;
795 sd_bus_slot *match;
796
797 /* The exit data of the unit */
798 char *active_state;
799 uint64_t inactive_exit_usec;
800 uint64_t inactive_enter_usec;
801 char *result;
802 uint64_t cpu_usage_nsec;
803 uint64_t ip_ingress_bytes;
804 uint64_t ip_egress_bytes;
805 uint32_t exit_code;
806 uint32_t exit_status;
807 } RunContext;
808
809 static void run_context_free(RunContext *c) {
810 assert(c);
811
812 c->forward = pty_forward_free(c->forward);
813 c->match = sd_bus_slot_unref(c->match);
814 c->bus = sd_bus_unref(c->bus);
815 c->event = sd_event_unref(c->event);
816
817 free(c->active_state);
818 free(c->result);
819 }
820
821 static void run_context_check_done(RunContext *c) {
822 bool done;
823
824 assert(c);
825
826 if (c->match)
827 done = STRPTR_IN_SET(c->active_state, "inactive", "failed");
828 else
829 done = true;
830
831 if (c->forward && done) /* If the service is gone, it's time to drain the output */
832 done = pty_forward_drain(c->forward);
833
834 if (done)
835 sd_event_exit(c->event, EXIT_SUCCESS);
836 }
837
838 static int run_context_update(RunContext *c, const char *path) {
839
840 static const struct bus_properties_map map[] = {
841 { "ActiveState", "s", NULL, offsetof(RunContext, active_state) },
842 { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_exit_usec) },
843 { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_enter_usec) },
844 { "Result", "s", NULL, offsetof(RunContext, result) },
845 { "ExecMainCode", "i", NULL, offsetof(RunContext, exit_code) },
846 { "ExecMainStatus", "i", NULL, offsetof(RunContext, exit_status) },
847 { "CPUUsageNSec", "t", NULL, offsetof(RunContext, cpu_usage_nsec) },
848 { "IPIngressBytes", "t", NULL, offsetof(RunContext, ip_ingress_bytes) },
849 { "IPEgressBytes", "t", NULL, offsetof(RunContext, ip_egress_bytes) },
850 {}
851 };
852
853 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
854 int r;
855
856 r = bus_map_all_properties(c->bus,
857 "org.freedesktop.systemd1",
858 path,
859 map,
860 BUS_MAP_STRDUP,
861 &error,
862 NULL,
863 c);
864 if (r < 0) {
865 sd_event_exit(c->event, EXIT_FAILURE);
866 return log_error_errno(r, "Failed to query unit state: %s", bus_error_message(&error, r));
867 }
868
869 run_context_check_done(c);
870 return 0;
871 }
872
873 static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
874 RunContext *c = userdata;
875
876 assert(m);
877 assert(c);
878
879 return run_context_update(c, sd_bus_message_get_path(m));
880 }
881
882 static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
883 RunContext *c = userdata;
884
885 assert(f);
886
887 if (rcode < 0) {
888 sd_event_exit(c->event, EXIT_FAILURE);
889 return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
890 }
891
892 run_context_check_done(c);
893 return 0;
894 }
895
896 static int start_transient_service(
897 sd_bus *bus,
898 char **argv,
899 int *retval) {
900
901 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
902 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
903 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
904 _cleanup_free_ char *service = NULL, *pty_path = NULL;
905 _cleanup_close_ int master = -1;
906 int r;
907
908 assert(bus);
909 assert(argv);
910 assert(retval);
911
912 if (arg_stdio == ARG_STDIO_PTY) {
913
914 if (arg_transport == BUS_TRANSPORT_LOCAL) {
915 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK);
916 if (master < 0)
917 return log_error_errno(errno, "Failed to acquire pseudo tty: %m");
918
919 r = ptsname_malloc(master, &pty_path);
920 if (r < 0)
921 return log_error_errno(r, "Failed to determine tty name: %m");
922
923 if (unlockpt(master) < 0)
924 return log_error_errno(errno, "Failed to unlock tty: %m");
925
926 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
927 _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
928 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
929 const char *s;
930
931 r = sd_bus_default_system(&system_bus);
932 if (r < 0)
933 return log_error_errno(r, "Failed to connect to system bus: %m");
934
935 r = sd_bus_call_method(system_bus,
936 "org.freedesktop.machine1",
937 "/org/freedesktop/machine1",
938 "org.freedesktop.machine1.Manager",
939 "OpenMachinePTY",
940 &error,
941 &pty_reply,
942 "s", arg_host);
943 if (r < 0) {
944 log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r));
945 return r;
946 }
947
948 r = sd_bus_message_read(pty_reply, "hs", &master, &s);
949 if (r < 0)
950 return bus_log_parse_error(r);
951
952 master = fcntl(master, F_DUPFD_CLOEXEC, 3);
953 if (master < 0)
954 return log_error_errno(errno, "Failed to duplicate master fd: %m");
955
956 pty_path = strdup(s);
957 if (!pty_path)
958 return log_oom();
959 } else
960 assert_not_reached("Can't allocate tty via ssh");
961 }
962
963 if (!arg_no_block) {
964 r = bus_wait_for_jobs_new(bus, &w);
965 if (r < 0)
966 return log_error_errno(r, "Could not watch jobs: %m");
967 }
968
969 if (arg_unit) {
970 r = unit_name_mangle_with_suffix(arg_unit, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".service", &service);
971 if (r < 0)
972 return log_error_errno(r, "Failed to mangle unit name: %m");
973 } else {
974 r = make_unit_name(bus, UNIT_SERVICE, &service);
975 if (r < 0)
976 return r;
977 }
978
979 r = sd_bus_message_new_method_call(
980 bus,
981 &m,
982 "org.freedesktop.systemd1",
983 "/org/freedesktop/systemd1",
984 "org.freedesktop.systemd1.Manager",
985 "StartTransientUnit");
986 if (r < 0)
987 return bus_log_create_error(r);
988
989 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
990 if (r < 0)
991 return bus_log_create_error(r);
992
993 /* Name and mode */
994 r = sd_bus_message_append(m, "ss", service, "fail");
995 if (r < 0)
996 return bus_log_create_error(r);
997
998 /* Properties */
999 r = sd_bus_message_open_container(m, 'a', "(sv)");
1000 if (r < 0)
1001 return bus_log_create_error(r);
1002
1003 r = transient_service_set_properties(m, argv, pty_path);
1004 if (r < 0)
1005 return r;
1006
1007 r = sd_bus_message_close_container(m);
1008 if (r < 0)
1009 return bus_log_create_error(r);
1010
1011 /* Auxiliary units */
1012 r = sd_bus_message_append(m, "a(sa(sv))", 0);
1013 if (r < 0)
1014 return bus_log_create_error(r);
1015
1016 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1017
1018 r = sd_bus_call(bus, m, 0, &error, &reply);
1019 if (r < 0)
1020 return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
1021
1022 if (w) {
1023 const char *object;
1024
1025 r = sd_bus_message_read(reply, "o", &object);
1026 if (r < 0)
1027 return bus_log_parse_error(r);
1028
1029 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1030 if (r < 0)
1031 return r;
1032 }
1033
1034 if (!arg_quiet)
1035 log_info("Running as unit: %s", service);
1036
1037 if (arg_wait || arg_stdio != ARG_STDIO_NONE) {
1038 _cleanup_(run_context_free) RunContext c = {
1039 .cpu_usage_nsec = NSEC_INFINITY,
1040 .ip_ingress_bytes = UINT64_MAX,
1041 .ip_egress_bytes = UINT64_MAX,
1042 .inactive_exit_usec = USEC_INFINITY,
1043 .inactive_enter_usec = USEC_INFINITY,
1044 };
1045 _cleanup_free_ char *path = NULL;
1046
1047 c.bus = sd_bus_ref(bus);
1048
1049 r = sd_event_default(&c.event);
1050 if (r < 0)
1051 return log_error_errno(r, "Failed to get event loop: %m");
1052
1053 if (master >= 0) {
1054 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
1055 (void) sd_event_add_signal(c.event, NULL, SIGINT, NULL, NULL);
1056 (void) sd_event_add_signal(c.event, NULL, SIGTERM, NULL, NULL);
1057
1058 if (!arg_quiet)
1059 log_info("Press ^] three times within 1s to disconnect TTY.");
1060
1061 r = pty_forward_new(c.event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &c.forward);
1062 if (r < 0)
1063 return log_error_errno(r, "Failed to create PTY forwarder: %m");
1064
1065 pty_forward_set_handler(c.forward, pty_forward_handler, &c);
1066
1067 /* Make sure to process any TTY events before we process bus events */
1068 (void) pty_forward_set_priority(c.forward, SD_EVENT_PRIORITY_IMPORTANT);
1069 }
1070
1071 path = unit_dbus_path_from_name(service);
1072 if (!path)
1073 return log_oom();
1074
1075 r = sd_bus_match_signal_async(
1076 bus,
1077 &c.match,
1078 "org.freedesktop.systemd1",
1079 path,
1080 "org.freedesktop.DBus.Properties",
1081 "PropertiesChanged",
1082 on_properties_changed, NULL, &c);
1083 if (r < 0)
1084 return log_error_errno(r, "Failed to request properties changed signal match: %m");
1085
1086 r = sd_bus_attach_event(bus, c.event, SD_EVENT_PRIORITY_NORMAL);
1087 if (r < 0)
1088 return log_error_errno(r, "Failed to attach bus to event loop: %m");
1089
1090 r = run_context_update(&c, path);
1091 if (r < 0)
1092 return r;
1093
1094 r = sd_event_loop(c.event);
1095 if (r < 0)
1096 return log_error_errno(r, "Failed to run event loop: %m");
1097
1098 if (c.forward) {
1099 char last_char = 0;
1100
1101 r = pty_forward_get_last_char(c.forward, &last_char);
1102 if (r >= 0 && !arg_quiet && last_char != '\n')
1103 fputc('\n', stdout);
1104 }
1105
1106 if (arg_wait && !arg_quiet) {
1107
1108 /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, in its
1109 * original settings (i.e. proper line breaks), so that we can show the summary in a pretty
1110 * way. */
1111 c.forward = pty_forward_free(c.forward);
1112
1113 if (!isempty(c.result))
1114 log_info("Finished with result: %s", strna(c.result));
1115
1116 if (c.exit_code == CLD_EXITED)
1117 log_info("Main processes terminated with: code=%s/status=%i", sigchld_code_to_string(c.exit_code), c.exit_status);
1118 else if (c.exit_code > 0)
1119 log_info("Main processes terminated with: code=%s/status=%s", sigchld_code_to_string(c.exit_code), signal_to_string(c.exit_status));
1120
1121 if (c.inactive_enter_usec > 0 && c.inactive_enter_usec != USEC_INFINITY &&
1122 c.inactive_exit_usec > 0 && c.inactive_exit_usec != USEC_INFINITY &&
1123 c.inactive_enter_usec > c.inactive_exit_usec) {
1124 char ts[FORMAT_TIMESPAN_MAX];
1125 log_info("Service runtime: %s", format_timespan(ts, sizeof(ts), c.inactive_enter_usec - c.inactive_exit_usec, USEC_PER_MSEC));
1126 }
1127
1128 if (c.cpu_usage_nsec != NSEC_INFINITY) {
1129 char ts[FORMAT_TIMESPAN_MAX];
1130 log_info("CPU time consumed: %s", format_timespan(ts, sizeof(ts), (c.cpu_usage_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC, USEC_PER_MSEC));
1131 }
1132
1133 if (c.ip_ingress_bytes != UINT64_MAX) {
1134 char bytes[FORMAT_BYTES_MAX];
1135 log_info("IP traffic received: %s", format_bytes(bytes, sizeof(bytes), c.ip_ingress_bytes));
1136 }
1137 if (c.ip_egress_bytes != UINT64_MAX) {
1138 char bytes[FORMAT_BYTES_MAX];
1139 log_info("IP traffic sent: %s", format_bytes(bytes, sizeof(bytes), c.ip_egress_bytes));
1140 }
1141 }
1142
1143 /* Try to propagate the service's return value */
1144 if (c.result && STR_IN_SET(c.result, "success", "exit-code") && c.exit_code == CLD_EXITED)
1145 *retval = c.exit_status;
1146 else
1147 *retval = EXIT_FAILURE;
1148 }
1149
1150 return 0;
1151 }
1152
1153 static int start_transient_scope(
1154 sd_bus *bus,
1155 char **argv) {
1156
1157 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1158 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1159 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1160 _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
1161 _cleanup_free_ char *scope = NULL;
1162 const char *object = NULL;
1163 int r;
1164
1165 assert(bus);
1166 assert(argv);
1167
1168 r = bus_wait_for_jobs_new(bus, &w);
1169 if (r < 0)
1170 return log_oom();
1171
1172 if (arg_unit) {
1173 r = unit_name_mangle_with_suffix(arg_unit, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".scope", &scope);
1174 if (r < 0)
1175 return log_error_errno(r, "Failed to mangle scope name: %m");
1176 } else {
1177 r = make_unit_name(bus, UNIT_SCOPE, &scope);
1178 if (r < 0)
1179 return r;
1180 }
1181
1182 r = sd_bus_message_new_method_call(
1183 bus,
1184 &m,
1185 "org.freedesktop.systemd1",
1186 "/org/freedesktop/systemd1",
1187 "org.freedesktop.systemd1.Manager",
1188 "StartTransientUnit");
1189 if (r < 0)
1190 return bus_log_create_error(r);
1191
1192 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1193 if (r < 0)
1194 return bus_log_create_error(r);
1195
1196 /* Name and Mode */
1197 r = sd_bus_message_append(m, "ss", scope, "fail");
1198 if (r < 0)
1199 return bus_log_create_error(r);
1200
1201 /* Properties */
1202 r = sd_bus_message_open_container(m, 'a', "(sv)");
1203 if (r < 0)
1204 return bus_log_create_error(r);
1205
1206 r = transient_scope_set_properties(m);
1207 if (r < 0)
1208 return r;
1209
1210 r = sd_bus_message_close_container(m);
1211 if (r < 0)
1212 return bus_log_create_error(r);
1213
1214 /* Auxiliary units */
1215 r = sd_bus_message_append(m, "a(sa(sv))", 0);
1216 if (r < 0)
1217 return bus_log_create_error(r);
1218
1219 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1220
1221 r = sd_bus_call(bus, m, 0, &error, &reply);
1222 if (r < 0) {
1223 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
1224 return r;
1225 }
1226
1227 if (arg_nice_set) {
1228 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
1229 return log_error_errno(errno, "Failed to set nice level: %m");
1230 }
1231
1232 if (arg_exec_group) {
1233 gid_t gid;
1234
1235 r = get_group_creds(&arg_exec_group, &gid);
1236 if (r < 0)
1237 return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
1238
1239 if (setresgid(gid, gid, gid) < 0)
1240 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
1241 }
1242
1243 if (arg_exec_user) {
1244 const char *home, *shell;
1245 uid_t uid;
1246 gid_t gid;
1247
1248 r = get_user_creds_clean(&arg_exec_user, &uid, &gid, &home, &shell);
1249 if (r < 0)
1250 return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
1251
1252 if (home) {
1253 r = strv_extendf(&user_env, "HOME=%s", home);
1254 if (r < 0)
1255 return log_oom();
1256 }
1257
1258 if (shell) {
1259 r = strv_extendf(&user_env, "SHELL=%s", shell);
1260 if (r < 0)
1261 return log_oom();
1262 }
1263
1264 r = strv_extendf(&user_env, "USER=%s", arg_exec_user);
1265 if (r < 0)
1266 return log_oom();
1267
1268 r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user);
1269 if (r < 0)
1270 return log_oom();
1271
1272 if (!arg_exec_group) {
1273 if (setresgid(gid, gid, gid) < 0)
1274 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
1275 }
1276
1277 if (setresuid(uid, uid, uid) < 0)
1278 return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid);
1279 }
1280
1281 env = strv_env_merge(3, environ, user_env, arg_environment);
1282 if (!env)
1283 return log_oom();
1284
1285 r = sd_bus_message_read(reply, "o", &object);
1286 if (r < 0)
1287 return bus_log_parse_error(r);
1288
1289 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1290 if (r < 0)
1291 return r;
1292
1293 if (!arg_quiet)
1294 log_info("Running scope as unit: %s", scope);
1295
1296 execvpe(argv[0], argv, env);
1297
1298 return log_error_errno(errno, "Failed to execute: %m");
1299 }
1300
1301 static int start_transient_trigger(
1302 sd_bus *bus,
1303 char **argv,
1304 const char *suffix) {
1305
1306 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1307 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1308 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1309 _cleanup_free_ char *trigger = NULL, *service = NULL;
1310 const char *object = NULL;
1311 int r;
1312
1313 assert(bus);
1314 assert(argv);
1315
1316 r = bus_wait_for_jobs_new(bus, &w);
1317 if (r < 0)
1318 return log_oom();
1319
1320 if (arg_unit) {
1321 switch (unit_name_to_type(arg_unit)) {
1322
1323 case UNIT_SERVICE:
1324 service = strdup(arg_unit);
1325 if (!service)
1326 return log_oom();
1327
1328 r = unit_name_change_suffix(service, suffix, &trigger);
1329 if (r < 0)
1330 return log_error_errno(r, "Failed to change unit suffix: %m");
1331 break;
1332
1333 case UNIT_TIMER:
1334 trigger = strdup(arg_unit);
1335 if (!trigger)
1336 return log_oom();
1337
1338 r = unit_name_change_suffix(trigger, ".service", &service);
1339 if (r < 0)
1340 return log_error_errno(r, "Failed to change unit suffix: %m");
1341 break;
1342
1343 default:
1344 r = unit_name_mangle_with_suffix(arg_unit, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, ".service", &service);
1345 if (r < 0)
1346 return log_error_errno(r, "Failed to mangle unit name: %m");
1347
1348 r = unit_name_mangle_with_suffix(arg_unit, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, suffix, &trigger);
1349 if (r < 0)
1350 return log_error_errno(r, "Failed to mangle unit name: %m");
1351
1352 break;
1353 }
1354 } else {
1355 r = make_unit_name(bus, UNIT_SERVICE, &service);
1356 if (r < 0)
1357 return r;
1358
1359 r = unit_name_change_suffix(service, suffix, &trigger);
1360 if (r < 0)
1361 return log_error_errno(r, "Failed to change unit suffix: %m");
1362 }
1363
1364 r = sd_bus_message_new_method_call(
1365 bus,
1366 &m,
1367 "org.freedesktop.systemd1",
1368 "/org/freedesktop/systemd1",
1369 "org.freedesktop.systemd1.Manager",
1370 "StartTransientUnit");
1371 if (r < 0)
1372 return bus_log_create_error(r);
1373
1374 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1375 if (r < 0)
1376 return bus_log_create_error(r);
1377
1378 /* Name and Mode */
1379 r = sd_bus_message_append(m, "ss", trigger, "fail");
1380 if (r < 0)
1381 return bus_log_create_error(r);
1382
1383 /* Properties */
1384 r = sd_bus_message_open_container(m, 'a', "(sv)");
1385 if (r < 0)
1386 return bus_log_create_error(r);
1387
1388 if (streq(suffix, ".path"))
1389 r = transient_unit_set_properties(m, UNIT_PATH, arg_path_property);
1390 else if (streq(suffix, ".socket"))
1391 r = transient_unit_set_properties(m, UNIT_SOCKET, arg_socket_property);
1392 else if (streq(suffix, ".timer"))
1393 r = transient_timer_set_properties(m);
1394 else
1395 assert_not_reached("Invalid suffix");
1396 if (r < 0)
1397 return r;
1398
1399 r = sd_bus_message_close_container(m);
1400 if (r < 0)
1401 return bus_log_create_error(r);
1402
1403 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
1404 if (r < 0)
1405 return bus_log_create_error(r);
1406
1407 if (!strv_isempty(argv)) {
1408 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
1409 if (r < 0)
1410 return bus_log_create_error(r);
1411
1412 r = sd_bus_message_append(m, "s", service);
1413 if (r < 0)
1414 return bus_log_create_error(r);
1415
1416 r = sd_bus_message_open_container(m, 'a', "(sv)");
1417 if (r < 0)
1418 return bus_log_create_error(r);
1419
1420 r = transient_service_set_properties(m, argv, NULL);
1421 if (r < 0)
1422 return r;
1423
1424 r = sd_bus_message_close_container(m);
1425 if (r < 0)
1426 return bus_log_create_error(r);
1427
1428 r = sd_bus_message_close_container(m);
1429 if (r < 0)
1430 return bus_log_create_error(r);
1431 }
1432
1433 r = sd_bus_message_close_container(m);
1434 if (r < 0)
1435 return bus_log_create_error(r);
1436
1437 polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
1438
1439 r = sd_bus_call(bus, m, 0, &error, &reply);
1440 if (r < 0) {
1441 log_error("Failed to start transient %s unit: %s", suffix + 1, bus_error_message(&error, -r));
1442 return r;
1443 }
1444
1445 r = sd_bus_message_read(reply, "o", &object);
1446 if (r < 0)
1447 return bus_log_parse_error(r);
1448
1449 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1450 if (r < 0)
1451 return r;
1452
1453 if (!arg_quiet) {
1454 log_info("Running %s as unit: %s", suffix + 1, trigger);
1455 if (argv[0])
1456 log_info("Will run service as unit: %s", service);
1457 }
1458
1459 return 0;
1460 }
1461
1462 int main(int argc, char* argv[]) {
1463 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1464 _cleanup_free_ char *description = NULL, *command = NULL;
1465 int r, retval = EXIT_SUCCESS;
1466
1467 log_parse_environment();
1468 log_open();
1469
1470 r = parse_argv(argc, argv);
1471 if (r <= 0)
1472 goto finish;
1473
1474 if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) {
1475 /* Patch in an absolute path */
1476
1477 r = find_binary(argv[optind], &command);
1478 if (r < 0) {
1479 log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
1480 goto finish;
1481 }
1482
1483 argv[optind] = command;
1484 }
1485
1486 if (!arg_description) {
1487 description = strv_join(argv + optind, " ");
1488 if (!description) {
1489 r = log_oom();
1490 goto finish;
1491 }
1492
1493 if (arg_unit && isempty(description)) {
1494 r = free_and_strdup(&description, arg_unit);
1495 if (r < 0)
1496 goto finish;
1497 }
1498
1499 arg_description = description;
1500 }
1501
1502 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1503 * connection */
1504 if (arg_wait || arg_stdio != ARG_STDIO_NONE)
1505 r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus);
1506 else
1507 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1508 if (r < 0) {
1509 log_error_errno(r, "Failed to create bus connection: %m");
1510 goto finish;
1511 }
1512
1513 if (arg_scope)
1514 r = start_transient_scope(bus, argv + optind);
1515 else if (arg_path_property)
1516 r = start_transient_trigger(bus, argv + optind, ".path");
1517 else if (arg_socket_property)
1518 r = start_transient_trigger(bus, argv + optind, ".socket");
1519 else if (with_timer)
1520 r = start_transient_trigger(bus, argv + optind, ".timer");
1521 else
1522 r = start_transient_service(bus, argv + optind, &retval);
1523
1524 finish:
1525 strv_free(arg_environment);
1526 strv_free(arg_property);
1527 strv_free(arg_path_property);
1528 strv_free(arg_socket_property);
1529 strv_free(arg_timer_property);
1530
1531 return r < 0 ? EXIT_FAILURE : retval;
1532 }