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