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