1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-locator.h"
15 #include "bus-map-properties.h"
16 #include "bus-unit-util.h"
17 #include "bus-wait-for-jobs.h"
18 #include "calendarspec.h"
20 #include "exit-status.h"
22 #include "format-util.h"
23 #include "main-func.h"
24 #include "parse-argument.h"
25 #include "parse-util.h"
26 #include "path-util.h"
27 #include "pretty-print.h"
28 #include "process-util.h"
30 #include "signal-util.h"
31 #include "spawn-polkit-agent.h"
33 #include "terminal-util.h"
35 #include "unit-name.h"
36 #include "user-util.h"
38 static bool arg_ask_password
= true;
39 static bool arg_scope
= false;
40 static bool arg_remain_after_exit
= false;
41 static bool arg_no_block
= false;
42 static bool arg_wait
= false;
43 static const char *arg_unit
= NULL
;
44 static const char *arg_description
= NULL
;
45 static const char *arg_slice
= NULL
;
46 static bool arg_slice_inherit
= false;
47 static bool arg_send_sighup
= false;
48 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
49 static const char *arg_host
= NULL
;
50 static bool arg_user
= false;
51 static const char *arg_service_type
= NULL
;
52 static const char *arg_exec_user
= NULL
;
53 static const char *arg_exec_group
= NULL
;
54 static int arg_nice
= 0;
55 static bool arg_nice_set
= false;
56 static char **arg_environment
= NULL
;
57 static char **arg_property
= NULL
;
59 ARG_STDIO_NONE
, /* The default, as it is for normal services, stdin connected to /dev/null, and stdout+stderr to the journal */
60 ARG_STDIO_PTY
, /* Interactive behaviour, requested by --pty: we allocate a pty and connect it to the TTY we are invoked from */
61 ARG_STDIO_DIRECT
, /* Directly pass our stdin/stdout/stderr to the activated service, useful for usage in shell pipelines, requested by --pipe */
62 ARG_STDIO_AUTO
, /* If --pipe and --pty are used together we use --pty when invoked on a TTY, and --pipe otherwise */
63 } arg_stdio
= ARG_STDIO_NONE
;
64 static char **arg_path_property
= NULL
;
65 static char **arg_socket_property
= NULL
;
66 static char **arg_timer_property
= NULL
;
67 static bool arg_with_timer
= false;
68 static bool arg_quiet
= false;
69 static bool arg_aggressive_gc
= false;
70 static char *arg_working_directory
= NULL
;
71 static bool arg_shell
= false;
72 static char **arg_cmdline
= NULL
;
74 STATIC_DESTRUCTOR_REGISTER(arg_environment
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_path_property
, strv_freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_socket_property
, strv_freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_timer_property
, strv_freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_working_directory
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_cmdline
, strv_freep
);
82 static int help(void) {
83 _cleanup_free_
char *link
= NULL
;
86 r
= terminal_urlify_man("systemd-run", "1", &link
);
90 printf("%s [OPTIONS...] COMMAND [ARGUMENTS...]\n"
91 "\n%sRun the specified command in a transient scope or service.%s\n\n"
92 " -h --help Show this help\n"
93 " --version Show package version\n"
94 " --no-ask-password Do not prompt for password\n"
95 " --user Run as user unit\n"
96 " -H --host=[USER@]HOST Operate on remote host\n"
97 " -M --machine=CONTAINER Operate on local container\n"
98 " --scope Run this as scope rather than service\n"
99 " -u --unit=UNIT Run under the specified unit name\n"
100 " -p --property=NAME=VALUE Set service or scope unit property\n"
101 " --description=TEXT Description for unit\n"
102 " --slice=SLICE Run in the specified slice\n"
103 " --slice-inherit Inherit the slice\n"
104 " --no-block Do not wait until operation finished\n"
105 " -r --remain-after-exit Leave service around until explicitly stopped\n"
106 " --wait Wait until service stopped again\n"
107 " --send-sighup Send SIGHUP when terminating\n"
108 " --service-type=TYPE Service type\n"
109 " --uid=USER Run as system user\n"
110 " --gid=GROUP Run as system group\n"
111 " --nice=NICE Nice level\n"
112 " --working-directory=PATH Set working directory\n"
113 " -d --same-dir Inherit working directory from caller\n"
114 " -E --setenv=NAME[=VALUE] Set environment variable\n"
115 " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n"
117 " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
118 " -q --quiet Suppress information messages during runtime\n"
119 " -G --collect Unload unit after it ran, even when failed\n"
120 " -S --shell Invoke a $SHELL interactively\n\n"
122 " --path-property=NAME=VALUE Set path unit property\n\n"
124 " --socket-property=NAME=VALUE Set socket unit property\n\n"
126 " --on-active=SECONDS Run after SECONDS delay\n"
127 " --on-boot=SECONDS Run SECONDS after machine was booted up\n"
128 " --on-startup=SECONDS Run SECONDS after systemd activation\n"
129 " --on-unit-active=SECONDS Run SECONDS after the last activation\n"
130 " --on-unit-inactive=SECONDS Run SECONDS after the last deactivation\n"
131 " --on-calendar=SPEC Realtime timer\n"
132 " --on-timezone-change Run when the timezone changes\n"
133 " --on-clock-change Run when the realtime clock jumps\n"
134 " --timer-property=NAME=VALUE Set timer unit property\n"
135 "\nSee the %s for details.\n",
136 program_invocation_short_name
,
144 static int add_timer_property(const char *name
, const char *val
) {
150 p
= strjoin(name
, "=", val
);
154 if (strv_consume(&arg_timer_property
, p
) < 0)
160 static int parse_argv(int argc
, char *argv
[]) {
179 ARG_ON_UNIT_INACTIVE
,
181 ARG_ON_TIMEZONE_CHANGE
,
189 ARG_WORKING_DIRECTORY
,
193 static const struct option options
[] = {
194 { "help", no_argument
, NULL
, 'h' },
195 { "version", no_argument
, NULL
, ARG_VERSION
},
196 { "user", no_argument
, NULL
, ARG_USER
},
197 { "system", no_argument
, NULL
, ARG_SYSTEM
},
198 { "scope", no_argument
, NULL
, ARG_SCOPE
},
199 { "unit", required_argument
, NULL
, 'u' },
200 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
201 { "slice", required_argument
, NULL
, ARG_SLICE
},
202 { "slice-inherit", no_argument
, NULL
, ARG_SLICE_INHERIT
},
203 { "remain-after-exit", no_argument
, NULL
, 'r' },
204 { "send-sighup", no_argument
, NULL
, ARG_SEND_SIGHUP
},
205 { "host", required_argument
, NULL
, 'H' },
206 { "machine", required_argument
, NULL
, 'M' },
207 { "service-type", required_argument
, NULL
, ARG_SERVICE_TYPE
},
208 { "wait", no_argument
, NULL
, ARG_WAIT
},
209 { "uid", required_argument
, NULL
, ARG_EXEC_USER
},
210 { "gid", required_argument
, NULL
, ARG_EXEC_GROUP
},
211 { "nice", required_argument
, NULL
, ARG_NICE
},
212 { "setenv", required_argument
, NULL
, 'E' },
213 { "property", required_argument
, NULL
, 'p' },
214 { "tty", no_argument
, NULL
, 't' }, /* deprecated alias */
215 { "pty", no_argument
, NULL
, 't' },
216 { "pipe", no_argument
, NULL
, 'P' },
217 { "quiet", no_argument
, NULL
, 'q' },
218 { "on-active", required_argument
, NULL
, ARG_ON_ACTIVE
},
219 { "on-boot", required_argument
, NULL
, ARG_ON_BOOT
},
220 { "on-startup", required_argument
, NULL
, ARG_ON_STARTUP
},
221 { "on-unit-active", required_argument
, NULL
, ARG_ON_UNIT_ACTIVE
},
222 { "on-unit-inactive", required_argument
, NULL
, ARG_ON_UNIT_INACTIVE
},
223 { "on-calendar", required_argument
, NULL
, ARG_ON_CALENDAR
},
224 { "on-timezone-change",no_argument
, NULL
, ARG_ON_TIMEZONE_CHANGE
},
225 { "on-clock-change", no_argument
, NULL
, ARG_ON_CLOCK_CHANGE
},
226 { "timer-property", required_argument
, NULL
, ARG_TIMER_PROPERTY
},
227 { "path-property", required_argument
, NULL
, ARG_PATH_PROPERTY
},
228 { "socket-property", required_argument
, NULL
, ARG_SOCKET_PROPERTY
},
229 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
230 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
231 { "collect", no_argument
, NULL
, 'G' },
232 { "working-directory", required_argument
, NULL
, ARG_WORKING_DIRECTORY
},
233 { "same-dir", no_argument
, NULL
, 'd' },
234 { "shell", no_argument
, NULL
, 'S' },
238 bool with_trigger
= false;
244 while ((c
= getopt_long(argc
, argv
, "+hrH:M:E:p:tPqGdSu:", options
, NULL
)) >= 0)
254 case ARG_NO_ASK_PASSWORD
:
255 arg_ask_password
= false;
274 case ARG_DESCRIPTION
:
275 arg_description
= optarg
;
282 case ARG_SLICE_INHERIT
:
283 arg_slice_inherit
= true;
286 case ARG_SEND_SIGHUP
:
287 arg_send_sighup
= true;
291 arg_remain_after_exit
= true;
295 arg_transport
= BUS_TRANSPORT_REMOTE
;
300 arg_transport
= BUS_TRANSPORT_MACHINE
;
304 case ARG_SERVICE_TYPE
:
305 arg_service_type
= optarg
;
309 arg_exec_user
= optarg
;
313 arg_exec_group
= optarg
;
317 r
= parse_nice(optarg
, &arg_nice
);
319 return log_error_errno(r
, "Failed to parse nice value: %s", optarg
);
325 r
= strv_env_replace_strdup_passthrough(&arg_environment
, optarg
);
327 return log_error_errno(r
, "Cannot assign environment variable %s: %m", optarg
);
332 if (strv_extend(&arg_property
, optarg
) < 0)
337 case 't': /* --pty */
338 if (IN_SET(arg_stdio
, ARG_STDIO_DIRECT
, ARG_STDIO_AUTO
)) /* if --pipe is already used, upgrade to auto mode */
339 arg_stdio
= ARG_STDIO_AUTO
;
341 arg_stdio
= ARG_STDIO_PTY
;
344 case 'P': /* --pipe */
345 if (IN_SET(arg_stdio
, ARG_STDIO_PTY
, ARG_STDIO_AUTO
)) /* If --pty is already used, upgrade to auto mode */
346 arg_stdio
= ARG_STDIO_AUTO
;
348 arg_stdio
= ARG_STDIO_DIRECT
;
356 r
= add_timer_property("OnActiveSec", optarg
);
360 arg_with_timer
= true;
364 r
= add_timer_property("OnBootSec", optarg
);
368 arg_with_timer
= true;
372 r
= add_timer_property("OnStartupSec", optarg
);
376 arg_with_timer
= true;
379 case ARG_ON_UNIT_ACTIVE
:
380 r
= add_timer_property("OnUnitActiveSec", optarg
);
384 arg_with_timer
= true;
387 case ARG_ON_UNIT_INACTIVE
:
388 r
= add_timer_property("OnUnitInactiveSec", optarg
);
392 arg_with_timer
= true;
395 case ARG_ON_CALENDAR
: {
396 _cleanup_(calendar_spec_freep
) CalendarSpec
*cs
= NULL
;
398 r
= calendar_spec_from_string(optarg
, &cs
);
400 return log_error_errno(r
, "Failed to parse calendar event specification: %m");
402 /* Let's make sure the given calendar event is not in the past */
403 r
= calendar_spec_next_usec(cs
, now(CLOCK_REALTIME
), NULL
);
405 /* The calendar event is in the past — let's warn about this, but install it
406 * anyway as is. The service manager will trigger the service right away.
407 * Moreover, the server side might have a different clock or timezone than we
408 * do, hence it should decide when or whether to run something. */
409 log_warning("Specified calendar expression is in the past, proceeding anyway.");
411 return log_error_errno(r
, "Failed to calculate next time calendar expression elapses: %m");
413 r
= add_timer_property("OnCalendar", optarg
);
417 arg_with_timer
= true;
421 case ARG_ON_TIMEZONE_CHANGE
:
422 r
= add_timer_property("OnTimezoneChange", "yes");
426 arg_with_timer
= true;
429 case ARG_ON_CLOCK_CHANGE
:
430 r
= add_timer_property("OnClockChange", "yes");
434 arg_with_timer
= true;
437 case ARG_TIMER_PROPERTY
:
439 if (strv_extend(&arg_timer_property
, optarg
) < 0)
442 arg_with_timer
= arg_with_timer
||
443 STARTSWITH_SET(optarg
,
448 "OnUnitInactiveSec=",
452 case ARG_PATH_PROPERTY
:
454 if (strv_extend(&arg_path_property
, optarg
) < 0)
459 case ARG_SOCKET_PROPERTY
:
461 if (strv_extend(&arg_socket_property
, optarg
) < 0)
474 case ARG_WORKING_DIRECTORY
:
475 r
= parse_path_argument(optarg
, true, &arg_working_directory
);
482 _cleanup_free_
char *p
= NULL
;
486 return log_error_errno(r
, "Failed to get current working directory: %m");
488 if (empty_or_root(p
))
489 arg_working_directory
= mfree(arg_working_directory
);
491 free_and_replace(arg_working_directory
, p
);
496 arg_aggressive_gc
= true;
507 assert_not_reached();
510 /* If we are talking to the per-user instance PolicyKit isn't going to help */
512 arg_ask_password
= false;
514 with_trigger
= !!arg_path_property
|| !!arg_socket_property
|| arg_with_timer
;
516 /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */
517 if ((int) !!arg_path_property
+ (int) !!arg_socket_property
+ (int) arg_with_timer
> 1)
518 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
519 "Only single trigger (path, socket, timer) unit can be created.");
522 /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise
526 if (arg_stdio
== ARG_STDIO_NONE
)
527 arg_stdio
= ARG_STDIO_AUTO
;
529 if (!arg_working_directory
) {
530 r
= safe_getcwd(&arg_working_directory
);
532 return log_error_errno(r
, "Failed to get current working directory: %m");
535 if (!arg_service_type
) {
536 arg_service_type
= strdup("exec");
537 if (!arg_service_type
)
544 arg_aggressive_gc
= true;
547 if (arg_stdio
== ARG_STDIO_AUTO
)
548 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
549 * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
550 * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
551 arg_stdio
= isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
) && isatty(STDERR_FILENO
) ?
559 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "If --shell is used, no command line is expected.");
561 l
= strv_copy(argv
+ optind
);
565 strv_free_and_replace(arg_cmdline
, l
);
567 } else if (arg_shell
) {
568 _cleanup_free_
char *s
= NULL
;
573 return log_error_errno(r
, "Failed to determine shell: %m");
579 strv_free_and_replace(arg_cmdline
, l
);
581 } else if (!arg_unit
|| !with_trigger
)
582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Command line to execute required.");
584 if (arg_user
&& arg_transport
== BUS_TRANSPORT_REMOTE
)
585 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
586 "Execution in user context is not supported on remote systems.");
588 if (arg_scope
&& arg_transport
== BUS_TRANSPORT_REMOTE
)
589 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
590 "Scope execution is not supported on remote systems.");
592 if (arg_scope
&& (arg_remain_after_exit
|| arg_service_type
))
593 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
594 "--remain-after-exit and --service-type= are not supported in --scope mode.");
596 if (arg_stdio
!= ARG_STDIO_NONE
&& (with_trigger
|| arg_scope
))
597 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
598 "--pty/--pipe is not compatible in timer or --scope mode.");
600 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_transport
== BUS_TRANSPORT_REMOTE
)
601 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
602 "--pty/--pipe is only supported when connecting to the local system or containers.");
604 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_no_block
)
605 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
606 "--pty/--pipe is not compatible with --no-block.");
608 if (arg_scope
&& with_trigger
)
609 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
610 "Path, socket or timer options are not supported in --scope mode.");
612 if (arg_timer_property
&& !arg_with_timer
)
613 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
614 "--timer-property= has no effect without any other timer options.");
618 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
619 "--wait may not be combined with --no-block.");
622 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
623 "--wait may not be combined with path, socket or timer operations.");
626 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
627 "--wait may not be combined with --scope.");
633 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
636 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
638 return bus_log_create_error(r
);
640 if (arg_aggressive_gc
) {
641 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
643 return bus_log_create_error(r
);
646 return bus_append_unit_property_assignment_many(m
, t
, properties
);
649 static int transient_cgroup_set_properties(sd_bus_message
*m
) {
650 _cleanup_free_
char *name
= NULL
;
651 _cleanup_free_
char *slice
= NULL
;
655 if (arg_slice_inherit
) {
659 r
= cg_pid_get_user_slice(0, &name
);
661 r
= cg_pid_get_slice(0, &name
);
663 return log_error_errno(r
, "Failed to get PID slice: %m");
665 end
= endswith(name
, ".slice");
671 if (!isempty(arg_slice
) && !strextend_with_separator(&name
, "-", arg_slice
))
677 r
= unit_name_mangle_with_suffix(name
, "as slice",
678 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
681 return log_error_errno(r
, "Failed to mangle name '%s': %m", arg_slice
);
683 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
685 return bus_log_create_error(r
);
690 static int transient_kill_set_properties(sd_bus_message
*m
) {
695 if (arg_send_sighup
) {
696 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", arg_send_sighup
);
698 return bus_log_create_error(r
);
704 static int transient_service_set_properties(sd_bus_message
*m
, const char *pty_path
) {
705 bool send_term
= false;
710 r
= transient_unit_set_properties(m
, UNIT_SERVICE
, arg_property
);
714 r
= transient_kill_set_properties(m
);
718 r
= transient_cgroup_set_properties(m
);
722 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
723 r
= sd_bus_message_append(m
, "(sv)", "AddRef", "b", 1);
725 return bus_log_create_error(r
);
728 if (arg_remain_after_exit
) {
729 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit
);
731 return bus_log_create_error(r
);
734 if (arg_service_type
) {
735 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_service_type
);
737 return bus_log_create_error(r
);
741 r
= sd_bus_message_append(m
, "(sv)", "User", "s", arg_exec_user
);
743 return bus_log_create_error(r
);
746 if (arg_exec_group
) {
747 r
= sd_bus_message_append(m
, "(sv)", "Group", "s", arg_exec_group
);
749 return bus_log_create_error(r
);
753 r
= sd_bus_message_append(m
, "(sv)", "Nice", "i", arg_nice
);
755 return bus_log_create_error(r
);
758 if (arg_working_directory
) {
759 r
= sd_bus_message_append(m
, "(sv)", "WorkingDirectory", "s", arg_working_directory
);
761 return bus_log_create_error(r
);
765 r
= sd_bus_message_append(m
,
767 "StandardInput", "s", "tty",
768 "StandardOutput", "s", "tty",
769 "StandardError", "s", "tty",
770 "TTYPath", "s", pty_path
);
772 return bus_log_create_error(r
);
776 } else if (arg_stdio
== ARG_STDIO_DIRECT
) {
777 r
= sd_bus_message_append(m
,
779 "StandardInputFileDescriptor", "h", STDIN_FILENO
,
780 "StandardOutputFileDescriptor", "h", STDOUT_FILENO
,
781 "StandardErrorFileDescriptor", "h", STDERR_FILENO
);
783 return bus_log_create_error(r
);
785 send_term
= isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
) || isatty(STDERR_FILENO
);
793 _cleanup_free_
char *n
= NULL
;
795 n
= strjoin("TERM=", e
);
799 r
= sd_bus_message_append(m
,
801 "Environment", "as", 1, n
);
803 return bus_log_create_error(r
);
807 if (!strv_isempty(arg_environment
)) {
808 r
= sd_bus_message_open_container(m
, 'r', "sv");
810 return bus_log_create_error(r
);
812 r
= sd_bus_message_append(m
, "s", "Environment");
814 return bus_log_create_error(r
);
816 r
= sd_bus_message_open_container(m
, 'v', "as");
818 return bus_log_create_error(r
);
820 r
= sd_bus_message_append_strv(m
, arg_environment
);
822 return bus_log_create_error(r
);
824 r
= sd_bus_message_close_container(m
);
826 return bus_log_create_error(r
);
828 r
= sd_bus_message_close_container(m
);
830 return bus_log_create_error(r
);
834 if (!strv_isempty(arg_cmdline
)) {
835 r
= sd_bus_message_open_container(m
, 'r', "sv");
837 return bus_log_create_error(r
);
839 r
= sd_bus_message_append(m
, "s", "ExecStart");
841 return bus_log_create_error(r
);
843 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
845 return bus_log_create_error(r
);
847 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
849 return bus_log_create_error(r
);
851 r
= sd_bus_message_open_container(m
, 'r', "sasb");
853 return bus_log_create_error(r
);
855 r
= sd_bus_message_append(m
, "s", arg_cmdline
[0]);
857 return bus_log_create_error(r
);
859 r
= sd_bus_message_append_strv(m
, arg_cmdline
);
861 return bus_log_create_error(r
);
863 r
= sd_bus_message_append(m
, "b", false);
865 return bus_log_create_error(r
);
867 r
= sd_bus_message_close_container(m
);
869 return bus_log_create_error(r
);
871 r
= sd_bus_message_close_container(m
);
873 return bus_log_create_error(r
);
875 r
= sd_bus_message_close_container(m
);
877 return bus_log_create_error(r
);
879 r
= sd_bus_message_close_container(m
);
881 return bus_log_create_error(r
);
887 static int transient_scope_set_properties(sd_bus_message
*m
) {
892 r
= transient_unit_set_properties(m
, UNIT_SCOPE
, arg_property
);
896 r
= transient_kill_set_properties(m
);
900 r
= transient_cgroup_set_properties(m
);
904 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
906 return bus_log_create_error(r
);
911 static int transient_timer_set_properties(sd_bus_message
*m
) {
916 r
= transient_unit_set_properties(m
, UNIT_TIMER
, arg_timer_property
);
920 /* Automatically clean up our transient timers */
921 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterElapse", "b", false);
923 return bus_log_create_error(r
);
928 static int make_unit_name(sd_bus
*bus
, UnitType t
, char **ret
) {
929 const char *unique
, *id
;
935 assert(t
< _UNIT_TYPE_MAX
);
937 r
= sd_bus_get_unique_name(bus
, &unique
);
941 /* We couldn't get the unique name, which is a pretty
942 * common case if we are connected to systemd
943 * directly. In that case, just pick a random uuid as
946 r
= sd_id128_randomize(&rnd
);
948 return log_error_errno(r
, "Failed to generate random run unit name: %m");
950 if (asprintf(ret
, "run-r" SD_ID128_FORMAT_STR
".%s", SD_ID128_FORMAT_VAL(rnd
), unit_type_to_string(t
)) < 0)
956 /* We managed to get the unique name, then let's use that to name our transient units. */
958 id
= startswith(unique
, ":1."); /* let' strip the usual prefix */
960 id
= startswith(unique
, ":"); /* the spec only requires things to start with a colon, hence
961 * let's add a generic fallback for that. */
963 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
964 "Unique name %s has unexpected format.",
967 p
= strjoin("run-u", id
, ".", unit_type_to_string(t
));
975 typedef struct RunContext
{
981 /* Current state of the unit */
985 /* The exit data of the unit */
986 uint64_t inactive_exit_usec
;
987 uint64_t inactive_enter_usec
;
989 uint64_t cpu_usage_nsec
;
990 uint64_t ip_ingress_bytes
;
991 uint64_t ip_egress_bytes
;
992 uint64_t io_read_bytes
;
993 uint64_t io_write_bytes
;
995 uint32_t exit_status
;
998 static void run_context_free(RunContext
*c
) {
1001 c
->forward
= pty_forward_free(c
->forward
);
1002 c
->match
= sd_bus_slot_unref(c
->match
);
1003 c
->bus
= sd_bus_unref(c
->bus
);
1004 c
->event
= sd_event_unref(c
->event
);
1006 free(c
->active_state
);
1010 static void run_context_check_done(RunContext
*c
) {
1016 done
= STRPTR_IN_SET(c
->active_state
, "inactive", "failed") && !c
->has_job
;
1020 if (c
->forward
&& done
) /* If the service is gone, it's time to drain the output */
1021 done
= pty_forward_drain(c
->forward
);
1024 sd_event_exit(c
->event
, EXIT_SUCCESS
);
1027 static int map_job(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1033 r
= sd_bus_message_read(m
, "(uo)", &id
, &job
);
1037 *b
= id
!= 0 || !streq(job
, "/");
1041 static int run_context_update(RunContext
*c
, const char *path
) {
1043 static const struct bus_properties_map map
[] = {
1044 { "ActiveState", "s", NULL
, offsetof(RunContext
, active_state
) },
1045 { "InactiveExitTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_exit_usec
) },
1046 { "InactiveEnterTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_enter_usec
) },
1047 { "Result", "s", NULL
, offsetof(RunContext
, result
) },
1048 { "ExecMainCode", "i", NULL
, offsetof(RunContext
, exit_code
) },
1049 { "ExecMainStatus", "i", NULL
, offsetof(RunContext
, exit_status
) },
1050 { "CPUUsageNSec", "t", NULL
, offsetof(RunContext
, cpu_usage_nsec
) },
1051 { "IPIngressBytes", "t", NULL
, offsetof(RunContext
, ip_ingress_bytes
) },
1052 { "IPEgressBytes", "t", NULL
, offsetof(RunContext
, ip_egress_bytes
) },
1053 { "IOReadBytes", "t", NULL
, offsetof(RunContext
, io_read_bytes
) },
1054 { "IOWriteBytes", "t", NULL
, offsetof(RunContext
, io_write_bytes
) },
1055 { "Job", "(uo)", map_job
, offsetof(RunContext
, has_job
) },
1059 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1062 r
= bus_map_all_properties(c
->bus
,
1063 "org.freedesktop.systemd1",
1071 sd_event_exit(c
->event
, EXIT_FAILURE
);
1072 return log_error_errno(r
, "Failed to query unit state: %s", bus_error_message(&error
, r
));
1075 run_context_check_done(c
);
1079 static int on_properties_changed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1080 RunContext
*c
= ASSERT_PTR(userdata
);
1084 return run_context_update(c
, sd_bus_message_get_path(m
));
1087 static int pty_forward_handler(PTYForward
*f
, int rcode
, void *userdata
) {
1088 RunContext
*c
= userdata
;
1093 sd_event_exit(c
->event
, EXIT_FAILURE
);
1094 return log_error_errno(rcode
, "Error on PTY forwarding logic: %m");
1097 run_context_check_done(c
);
1101 static int start_transient_service(
1105 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1106 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1107 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1108 _cleanup_free_
char *service
= NULL
, *pty_path
= NULL
;
1109 _cleanup_close_
int master
= -1;
1115 if (arg_stdio
== ARG_STDIO_PTY
) {
1117 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
1118 master
= posix_openpt(O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
1120 return log_error_errno(errno
, "Failed to acquire pseudo tty: %m");
1122 r
= ptsname_malloc(master
, &pty_path
);
1124 return log_error_errno(r
, "Failed to determine tty name: %m");
1126 if (unlockpt(master
) < 0)
1127 return log_error_errno(errno
, "Failed to unlock tty: %m");
1129 } else if (arg_transport
== BUS_TRANSPORT_MACHINE
) {
1130 _cleanup_(sd_bus_unrefp
) sd_bus
*system_bus
= NULL
;
1131 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pty_reply
= NULL
;
1134 r
= sd_bus_default_system(&system_bus
);
1136 return log_error_errno(r
, "Failed to connect to system bus: %m");
1138 r
= sd_bus_call_method(system_bus
,
1139 "org.freedesktop.machine1",
1140 "/org/freedesktop/machine1",
1141 "org.freedesktop.machine1.Manager",
1147 return log_error_errno(r
, "Failed to get machine PTY: %s", bus_error_message(&error
, r
));
1149 r
= sd_bus_message_read(pty_reply
, "hs", &master
, &s
);
1151 return bus_log_parse_error(r
);
1153 master
= fcntl(master
, F_DUPFD_CLOEXEC
, 3);
1155 return log_error_errno(errno
, "Failed to duplicate master fd: %m");
1157 pty_path
= strdup(s
);
1161 assert_not_reached();
1164 /* Optionally, wait for the start job to complete. If we are supposed to read the service's stdin
1165 * lets skip this however, because we should start that already when the start job is running, and
1166 * there's little point in waiting for the start job to complete in that case anyway, as we'll wait
1167 * for EOF anyway, which is going to be much later. */
1168 if (!arg_no_block
&& arg_stdio
== ARG_STDIO_NONE
) {
1169 r
= bus_wait_for_jobs_new(bus
, &w
);
1171 return log_error_errno(r
, "Could not watch jobs: %m");
1175 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1176 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1177 ".service", &service
);
1179 return log_error_errno(r
, "Failed to mangle unit name: %m");
1181 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1186 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
1188 return bus_log_create_error(r
);
1190 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1192 return bus_log_create_error(r
);
1195 r
= sd_bus_message_append(m
, "ss", service
, "fail");
1197 return bus_log_create_error(r
);
1200 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1202 return bus_log_create_error(r
);
1204 r
= transient_service_set_properties(m
, pty_path
);
1208 r
= sd_bus_message_close_container(m
);
1210 return bus_log_create_error(r
);
1212 /* Auxiliary units */
1213 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1215 return bus_log_create_error(r
);
1217 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1219 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1221 return log_error_errno(r
, "Failed to start transient service unit: %s", bus_error_message(&error
, r
));
1226 r
= sd_bus_message_read(reply
, "o", &object
);
1228 return bus_log_parse_error(r
);
1230 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, arg_user
? STRV_MAKE_CONST("--user") : NULL
);
1236 log_info("Running as unit: %s", service
);
1238 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
1239 _cleanup_(run_context_free
) RunContext c
= {
1240 .cpu_usage_nsec
= NSEC_INFINITY
,
1241 .ip_ingress_bytes
= UINT64_MAX
,
1242 .ip_egress_bytes
= UINT64_MAX
,
1243 .io_read_bytes
= UINT64_MAX
,
1244 .io_write_bytes
= UINT64_MAX
,
1245 .inactive_exit_usec
= USEC_INFINITY
,
1246 .inactive_enter_usec
= USEC_INFINITY
,
1248 _cleanup_free_
char *path
= NULL
;
1250 c
.bus
= sd_bus_ref(bus
);
1252 r
= sd_event_default(&c
.event
);
1254 return log_error_errno(r
, "Failed to get event loop: %m");
1257 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1258 (void) sd_event_add_signal(c
.event
, NULL
, SIGINT
, NULL
, NULL
);
1259 (void) sd_event_add_signal(c
.event
, NULL
, SIGTERM
, NULL
, NULL
);
1262 log_info("Press ^] three times within 1s to disconnect TTY.");
1264 r
= pty_forward_new(c
.event
, master
, PTY_FORWARD_IGNORE_INITIAL_VHANGUP
, &c
.forward
);
1266 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1268 pty_forward_set_handler(c
.forward
, pty_forward_handler
, &c
);
1270 /* Make sure to process any TTY events before we process bus events */
1271 (void) pty_forward_set_priority(c
.forward
, SD_EVENT_PRIORITY_IMPORTANT
);
1274 path
= unit_dbus_path_from_name(service
);
1278 r
= sd_bus_match_signal_async(
1281 "org.freedesktop.systemd1",
1283 "org.freedesktop.DBus.Properties",
1284 "PropertiesChanged",
1285 on_properties_changed
, NULL
, &c
);
1287 return log_error_errno(r
, "Failed to request properties changed signal match: %m");
1289 r
= sd_bus_attach_event(bus
, c
.event
, SD_EVENT_PRIORITY_NORMAL
);
1291 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1293 r
= run_context_update(&c
, path
);
1297 r
= sd_event_loop(c
.event
);
1299 return log_error_errno(r
, "Failed to run event loop: %m");
1304 r
= pty_forward_get_last_char(c
.forward
, &last_char
);
1305 if (r
>= 0 && !arg_quiet
&& last_char
!= '\n')
1306 fputc('\n', stdout
);
1309 if (arg_wait
&& !arg_quiet
) {
1311 /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, with its
1312 * original settings (i.e. proper line breaks), so that we can show the summary in a pretty
1314 c
.forward
= pty_forward_free(c
.forward
);
1316 if (!isempty(c
.result
))
1317 log_info("Finished with result: %s", strna(c
.result
));
1319 if (c
.exit_code
== CLD_EXITED
)
1320 log_info("Main processes terminated with: code=%s/status=%u",
1321 sigchld_code_to_string(c
.exit_code
), c
.exit_status
);
1322 else if (c
.exit_code
> 0)
1323 log_info("Main processes terminated with: code=%s/status=%s",
1324 sigchld_code_to_string(c
.exit_code
), signal_to_string(c
.exit_status
));
1326 if (timestamp_is_set(c
.inactive_enter_usec
) &&
1327 timestamp_is_set(c
.inactive_exit_usec
) &&
1328 c
.inactive_enter_usec
> c
.inactive_exit_usec
)
1329 log_info("Service runtime: %s",
1330 FORMAT_TIMESPAN(c
.inactive_enter_usec
- c
.inactive_exit_usec
, USEC_PER_MSEC
));
1332 if (c
.cpu_usage_nsec
!= NSEC_INFINITY
)
1333 log_info("CPU time consumed: %s",
1334 FORMAT_TIMESPAN(DIV_ROUND_UP(c
.cpu_usage_nsec
, NSEC_PER_USEC
), USEC_PER_MSEC
));
1336 if (c
.ip_ingress_bytes
!= UINT64_MAX
)
1337 log_info("IP traffic received: %s", FORMAT_BYTES(c
.ip_ingress_bytes
));
1339 if (c
.ip_egress_bytes
!= UINT64_MAX
)
1340 log_info("IP traffic sent: %s", FORMAT_BYTES(c
.ip_egress_bytes
));
1342 if (c
.io_read_bytes
!= UINT64_MAX
)
1343 log_info("IO bytes read: %s", FORMAT_BYTES(c
.io_read_bytes
));
1345 if (c
.io_write_bytes
!= UINT64_MAX
)
1346 log_info("IO bytes written: %s", FORMAT_BYTES(c
.io_write_bytes
));
1349 /* Try to propagate the service's return value. But if the service defines
1350 * e.g. SuccessExitStatus, honour this, and return 0 to mean "success". */
1351 if (streq_ptr(c
.result
, "success"))
1353 else if (streq_ptr(c
.result
, "exit-code") && c
.exit_status
> 0)
1354 *retval
= c
.exit_status
;
1355 else if (streq_ptr(c
.result
, "signal"))
1356 *retval
= EXIT_EXCEPTION
;
1358 *retval
= EXIT_FAILURE
;
1364 static int acquire_invocation_id(sd_bus
*bus
, sd_id128_t
*ret
) {
1365 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1366 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1374 r
= sd_bus_get_property(bus
,
1375 "org.freedesktop.systemd1",
1376 "/org/freedesktop/systemd1/unit/self",
1377 "org.freedesktop.systemd1.Unit",
1383 return log_error_errno(r
, "Failed to request invocation ID for scope: %s", bus_error_message(&error
, r
));
1385 r
= sd_bus_message_read_array(reply
, 'y', &p
, &l
);
1387 return bus_log_parse_error(r
);
1389 if (l
!= sizeof(sd_id128_t
))
1390 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid UUID size, %zu != %zu.", l
, sizeof(sd_id128_t
));
1396 static int start_transient_scope(sd_bus
*bus
) {
1397 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1398 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1399 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1400 _cleanup_strv_free_
char **env
= NULL
, **user_env
= NULL
;
1401 _cleanup_free_
char *scope
= NULL
;
1402 const char *object
= NULL
;
1403 sd_id128_t invocation_id
;
1407 assert(!strv_isempty(arg_cmdline
));
1409 r
= bus_wait_for_jobs_new(bus
, &w
);
1414 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1415 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1418 return log_error_errno(r
, "Failed to mangle scope name: %m");
1420 r
= make_unit_name(bus
, UNIT_SCOPE
, &scope
);
1425 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
1427 return bus_log_create_error(r
);
1429 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1431 return bus_log_create_error(r
);
1434 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
1436 return bus_log_create_error(r
);
1439 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1441 return bus_log_create_error(r
);
1443 r
= transient_scope_set_properties(m
);
1447 r
= sd_bus_message_close_container(m
);
1449 return bus_log_create_error(r
);
1451 /* Auxiliary units */
1452 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1454 return bus_log_create_error(r
);
1456 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1458 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1460 return log_error_errno(r
, "Failed to start transient scope unit: %s", bus_error_message(&error
, r
));
1462 r
= sd_bus_message_read(reply
, "o", &object
);
1464 return bus_log_parse_error(r
);
1466 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, arg_user
? STRV_MAKE_CONST("--user") : NULL
);
1470 r
= acquire_invocation_id(bus
, &invocation_id
);
1474 r
= strv_extendf(&user_env
, "INVOCATION_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(invocation_id
));
1479 if (setpriority(PRIO_PROCESS
, 0, arg_nice
) < 0)
1480 return log_error_errno(errno
, "Failed to set nice level: %m");
1483 if (arg_exec_group
) {
1486 r
= get_group_creds(&arg_exec_group
, &gid
, 0);
1488 return log_error_errno(r
, "Failed to resolve group %s: %m", arg_exec_group
);
1490 if (setresgid(gid
, gid
, gid
) < 0)
1491 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1494 if (arg_exec_user
) {
1495 const char *home
, *shell
;
1499 r
= get_user_creds(&arg_exec_user
, &uid
, &gid
, &home
, &shell
, USER_CREDS_CLEAN
|USER_CREDS_PREFER_NSS
);
1501 return log_error_errno(r
, "Failed to resolve user %s: %m", arg_exec_user
);
1504 r
= strv_extendf(&user_env
, "HOME=%s", home
);
1510 r
= strv_extendf(&user_env
, "SHELL=%s", shell
);
1515 r
= strv_extendf(&user_env
, "USER=%s", arg_exec_user
);
1519 r
= strv_extendf(&user_env
, "LOGNAME=%s", arg_exec_user
);
1523 if (!arg_exec_group
) {
1524 if (setresgid(gid
, gid
, gid
) < 0)
1525 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1528 if (setresuid(uid
, uid
, uid
) < 0)
1529 return log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", uid
);
1532 env
= strv_env_merge(environ
, user_env
, arg_environment
);
1537 log_info("Running scope as unit: %s", scope
);
1539 execvpe(arg_cmdline
[0], arg_cmdline
, env
);
1541 return log_error_errno(errno
, "Failed to execute: %m");
1544 static int start_transient_trigger(
1546 const char *suffix
) {
1548 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1549 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1550 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1551 _cleanup_free_
char *trigger
= NULL
, *service
= NULL
;
1552 const char *object
= NULL
;
1557 r
= bus_wait_for_jobs_new(bus
, &w
);
1562 switch (unit_name_to_type(arg_unit
)) {
1565 service
= strdup(arg_unit
);
1569 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1571 return log_error_errno(r
, "Failed to change unit suffix: %m");
1575 trigger
= strdup(arg_unit
);
1579 r
= unit_name_change_suffix(trigger
, ".service", &service
);
1581 return log_error_errno(r
, "Failed to change unit suffix: %m");
1585 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1586 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1587 ".service", &service
);
1589 return log_error_errno(r
, "Failed to mangle unit name: %m");
1591 r
= unit_name_mangle_with_suffix(arg_unit
, "as trigger",
1592 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1595 return log_error_errno(r
, "Failed to mangle unit name: %m");
1600 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1604 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1606 return log_error_errno(r
, "Failed to change unit suffix: %m");
1609 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
1611 return bus_log_create_error(r
);
1613 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1615 return bus_log_create_error(r
);
1618 r
= sd_bus_message_append(m
, "ss", trigger
, "fail");
1620 return bus_log_create_error(r
);
1623 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1625 return bus_log_create_error(r
);
1627 if (streq(suffix
, ".path"))
1628 r
= transient_unit_set_properties(m
, UNIT_PATH
, arg_path_property
);
1629 else if (streq(suffix
, ".socket"))
1630 r
= transient_unit_set_properties(m
, UNIT_SOCKET
, arg_socket_property
);
1631 else if (streq(suffix
, ".timer"))
1632 r
= transient_timer_set_properties(m
);
1634 assert_not_reached();
1638 r
= sd_bus_message_close_container(m
);
1640 return bus_log_create_error(r
);
1642 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
1644 return bus_log_create_error(r
);
1646 if (!strv_isempty(arg_cmdline
)) {
1647 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
1649 return bus_log_create_error(r
);
1651 r
= sd_bus_message_append(m
, "s", service
);
1653 return bus_log_create_error(r
);
1655 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1657 return bus_log_create_error(r
);
1659 r
= transient_service_set_properties(m
, NULL
);
1663 r
= sd_bus_message_close_container(m
);
1665 return bus_log_create_error(r
);
1667 r
= sd_bus_message_close_container(m
);
1669 return bus_log_create_error(r
);
1672 r
= sd_bus_message_close_container(m
);
1674 return bus_log_create_error(r
);
1676 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1678 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1680 return log_error_errno(r
, "Failed to start transient %s unit: %s", suffix
+ 1, bus_error_message(&error
, r
));
1682 r
= sd_bus_message_read(reply
, "o", &object
);
1684 return bus_log_parse_error(r
);
1686 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
, arg_user
? STRV_MAKE_CONST("--user") : NULL
);
1691 log_info("Running %s as unit: %s", suffix
+ 1, trigger
);
1692 if (!strv_isempty(arg_cmdline
))
1693 log_info("Will run service as unit: %s", service
);
1699 static bool shall_make_executable_absolute(void) {
1700 if (strv_isempty(arg_cmdline
))
1702 if (arg_transport
!= BUS_TRANSPORT_LOCAL
)
1705 FOREACH_STRING(f
, "RootDirectory=", "RootImage=", "ExecSearchPath=", "MountImages=", "ExtensionImages=")
1706 if (strv_find_startswith(arg_property
, f
))
1712 static int run(int argc
, char* argv
[]) {
1713 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1714 _cleanup_free_
char *description
= NULL
;
1715 int r
, retval
= EXIT_SUCCESS
;
1717 log_show_color(true);
1718 log_parse_environment();
1721 r
= parse_argv(argc
, argv
);
1725 if (shall_make_executable_absolute()) {
1726 /* Patch in an absolute path to fail early for user convenience, but only when we can do it
1727 * (i.e. we will be running from the same file system). This also uses the user's $PATH,
1728 * while we use a fixed search path in the manager. */
1730 _cleanup_free_
char *command
= NULL
;
1731 r
= find_executable(arg_cmdline
[0], &command
);
1733 return log_error_errno(r
, "Failed to find executable %s: %m", arg_cmdline
[0]);
1735 free_and_replace(arg_cmdline
[0], command
);
1738 if (!arg_description
) {
1739 description
= strv_join(arg_cmdline
, " ");
1743 if (arg_unit
&& isempty(description
)) {
1744 r
= free_and_strdup(&description
, arg_unit
);
1749 arg_description
= description
;
1752 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1754 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
|| (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
))
1755 r
= bus_connect_transport(arg_transport
, arg_host
, arg_user
, &bus
);
1757 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1759 return bus_log_connect_error(r
, arg_transport
);
1762 r
= start_transient_scope(bus
);
1763 else if (arg_path_property
)
1764 r
= start_transient_trigger(bus
, ".path");
1765 else if (arg_socket_property
)
1766 r
= start_transient_trigger(bus
, ".socket");
1767 else if (arg_with_timer
)
1768 r
= start_transient_trigger(bus
, ".timer");
1770 r
= start_transient_service(bus
, &retval
);
1777 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);