1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include "alloc-util.h"
13 #include "bus-error.h"
14 #include "bus-unit-util.h"
16 #include "bus-wait-for-jobs.h"
17 #include "calendarspec.h"
20 #include "format-util.h"
21 #include "main-func.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "pretty-print.h"
25 #include "process-util.h"
27 #include "signal-util.h"
28 #include "spawn-polkit-agent.h"
30 #include "terminal-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
35 static bool arg_ask_password
= true;
36 static bool arg_scope
= false;
37 static bool arg_remain_after_exit
= false;
38 static bool arg_no_block
= false;
39 static bool arg_wait
= false;
40 static const char *arg_unit
= NULL
;
41 static const char *arg_description
= NULL
;
42 static const char *arg_slice
= NULL
;
43 static bool arg_send_sighup
= false;
44 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
45 static const char *arg_host
= NULL
;
46 static bool arg_user
= false;
47 static const char *arg_service_type
= NULL
;
48 static const char *arg_exec_user
= NULL
;
49 static const char *arg_exec_group
= NULL
;
50 static int arg_nice
= 0;
51 static bool arg_nice_set
= false;
52 static char **arg_environment
= NULL
;
53 static char **arg_property
= NULL
;
55 ARG_STDIO_NONE
, /* The default, as it is for normal services, stdin connected to /dev/null, and stdout+stderr to the journal */
56 ARG_STDIO_PTY
, /* Interactive behaviour, requested by --pty: we allocate a pty and connect it to the TTY we are invoked from */
57 ARG_STDIO_DIRECT
, /* Directly pass our stdin/stdout/stderr to the activated service, useful for usage in shell pipelines, requested by --pipe */
58 ARG_STDIO_AUTO
, /* If --pipe and --pty are used together we use --pty when invoked on a TTY, and --pipe otherwise */
59 } arg_stdio
= ARG_STDIO_NONE
;
60 static char **arg_path_property
= NULL
;
61 static char **arg_socket_property
= NULL
;
62 static char **arg_timer_property
= NULL
;
63 static bool arg_with_timer
= false;
64 static bool arg_quiet
= false;
65 static bool arg_aggressive_gc
= false;
66 static char *arg_working_directory
= NULL
;
67 static bool arg_shell
= false;
68 static char **arg_cmdline
= NULL
;
70 STATIC_DESTRUCTOR_REGISTER(arg_environment
, strv_freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_path_property
, strv_freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_socket_property
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_timer_property
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_working_directory
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_cmdline
, strv_freep
);
78 static int help(void) {
79 _cleanup_free_
char *link
= NULL
;
82 r
= terminal_urlify_man("systemd-run", "1", &link
);
86 printf("%s [OPTIONS...] {COMMAND} [ARGS...]\n\n"
87 "Run the specified command in a transient scope or service.\n\n"
88 " -h --help Show this help\n"
89 " --version Show package version\n"
90 " --no-ask-password Do not prompt for password\n"
91 " --user Run as user unit\n"
92 " -H --host=[USER@]HOST Operate on remote host\n"
93 " -M --machine=CONTAINER Operate on local container\n"
94 " --scope Run this as scope rather than service\n"
95 " --unit=UNIT Run under the specified unit name\n"
96 " -p --property=NAME=VALUE Set service or scope unit property\n"
97 " --description=TEXT Description for unit\n"
98 " --slice=SLICE Run in the specified slice\n"
99 " --no-block Do not wait until operation finished\n"
100 " -r --remain-after-exit Leave service around until explicitly stopped\n"
101 " --wait Wait until service stopped again\n"
102 " --send-sighup Send SIGHUP when terminating\n"
103 " --service-type=TYPE Service type\n"
104 " --uid=USER Run as system user\n"
105 " --gid=GROUP Run as system group\n"
106 " --nice=NICE Nice level\n"
107 " --working-directory=PATH Set working directory\n"
108 " -d --same-dir Inherit working directory from caller\n"
109 " -E --setenv=NAME=VALUE Set environment\n"
110 " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n"
112 " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
113 " -q --quiet Suppress information messages during runtime\n"
114 " -G --collect Unload unit after it ran, even when failed\n"
115 " -S --shell Invoke a $SHELL interactively\n\n"
117 " --path-property=NAME=VALUE Set path unit property\n\n"
119 " --socket-property=NAME=VALUE Set socket unit property\n\n"
121 " --on-active=SECONDS Run after SECONDS delay\n"
122 " --on-boot=SECONDS Run SECONDS after machine was booted up\n"
123 " --on-startup=SECONDS Run SECONDS after systemd activation\n"
124 " --on-unit-active=SECONDS Run SECONDS after the last activation\n"
125 " --on-unit-inactive=SECONDS Run SECONDS after the last deactivation\n"
126 " --on-calendar=SPEC Realtime timer\n"
127 " --on-timezone-change Run when the timezone changes\n"
128 " --on-clock-change Run when the realtime clock jumps\n"
129 " --timer-property=NAME=VALUE Set timer unit property\n"
130 "\nSee the %s for details.\n"
131 , program_invocation_short_name
138 static int add_timer_property(const char *name
, const char *val
) {
144 p
= strjoin(name
, "=", val
);
148 if (strv_consume(&arg_timer_property
, p
) < 0)
154 static int parse_argv(int argc
, char *argv
[]) {
173 ARG_ON_UNIT_INACTIVE
,
175 ARG_ON_TIMEZONE_CHANGE
,
183 ARG_WORKING_DIRECTORY
,
187 static const struct option options
[] = {
188 { "help", no_argument
, NULL
, 'h' },
189 { "version", no_argument
, NULL
, ARG_VERSION
},
190 { "user", no_argument
, NULL
, ARG_USER
},
191 { "system", no_argument
, NULL
, ARG_SYSTEM
},
192 { "scope", no_argument
, NULL
, ARG_SCOPE
},
193 { "unit", required_argument
, NULL
, ARG_UNIT
},
194 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
195 { "slice", required_argument
, NULL
, ARG_SLICE
},
196 { "remain-after-exit", no_argument
, NULL
, 'r' },
197 { "send-sighup", no_argument
, NULL
, ARG_SEND_SIGHUP
},
198 { "host", required_argument
, NULL
, 'H' },
199 { "machine", required_argument
, NULL
, 'M' },
200 { "service-type", required_argument
, NULL
, ARG_SERVICE_TYPE
},
201 { "wait", no_argument
, NULL
, ARG_WAIT
},
202 { "uid", required_argument
, NULL
, ARG_EXEC_USER
},
203 { "gid", required_argument
, NULL
, ARG_EXEC_GROUP
},
204 { "nice", required_argument
, NULL
, ARG_NICE
},
205 { "setenv", required_argument
, NULL
, 'E' },
206 { "property", required_argument
, NULL
, 'p' },
207 { "tty", no_argument
, NULL
, 't' }, /* deprecated alias */
208 { "pty", no_argument
, NULL
, 't' },
209 { "pipe", no_argument
, NULL
, 'P' },
210 { "quiet", no_argument
, NULL
, 'q' },
211 { "on-active", required_argument
, NULL
, ARG_ON_ACTIVE
},
212 { "on-boot", required_argument
, NULL
, ARG_ON_BOOT
},
213 { "on-startup", required_argument
, NULL
, ARG_ON_STARTUP
},
214 { "on-unit-active", required_argument
, NULL
, ARG_ON_UNIT_ACTIVE
},
215 { "on-unit-inactive", required_argument
, NULL
, ARG_ON_UNIT_INACTIVE
},
216 { "on-calendar", required_argument
, NULL
, ARG_ON_CALENDAR
},
217 { "on-timezone-change",no_argument
, NULL
, ARG_ON_TIMEZONE_CHANGE
},
218 { "on-clock-change", no_argument
, NULL
, ARG_ON_CLOCK_CHANGE
},
219 { "timer-property", required_argument
, NULL
, ARG_TIMER_PROPERTY
},
220 { "path-property", required_argument
, NULL
, ARG_PATH_PROPERTY
},
221 { "socket-property", required_argument
, NULL
, ARG_SOCKET_PROPERTY
},
222 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
223 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
224 { "collect", no_argument
, NULL
, 'G' },
225 { "working-directory", required_argument
, NULL
, ARG_WORKING_DIRECTORY
},
226 { "same-dir", no_argument
, NULL
, 'd' },
227 { "shell", no_argument
, NULL
, 'S' },
231 bool with_trigger
= false;
237 while ((c
= getopt_long(argc
, argv
, "+hrH:M:E:p:tPqGdS", options
, NULL
)) >= 0)
247 case ARG_NO_ASK_PASSWORD
:
248 arg_ask_password
= false;
267 case ARG_DESCRIPTION
:
268 arg_description
= optarg
;
275 case ARG_SEND_SIGHUP
:
276 arg_send_sighup
= true;
280 arg_remain_after_exit
= true;
284 arg_transport
= BUS_TRANSPORT_REMOTE
;
289 arg_transport
= BUS_TRANSPORT_MACHINE
;
293 case ARG_SERVICE_TYPE
:
294 arg_service_type
= optarg
;
298 arg_exec_user
= optarg
;
302 arg_exec_group
= optarg
;
306 r
= parse_nice(optarg
, &arg_nice
);
308 return log_error_errno(r
, "Failed to parse nice value: %s", optarg
);
314 if (strv_extend(&arg_environment
, optarg
) < 0)
320 if (strv_extend(&arg_property
, optarg
) < 0)
325 case 't': /* --pty */
326 if (IN_SET(arg_stdio
, ARG_STDIO_DIRECT
, ARG_STDIO_AUTO
)) /* if --pipe is already used, upgrade to auto mode */
327 arg_stdio
= ARG_STDIO_AUTO
;
329 arg_stdio
= ARG_STDIO_PTY
;
332 case 'P': /* --pipe */
333 if (IN_SET(arg_stdio
, ARG_STDIO_PTY
, ARG_STDIO_AUTO
)) /* If --pty is already used, upgrade to auto mode */
334 arg_stdio
= ARG_STDIO_AUTO
;
336 arg_stdio
= ARG_STDIO_DIRECT
;
344 r
= add_timer_property("OnActiveSec", optarg
);
348 arg_with_timer
= true;
352 r
= add_timer_property("OnBootSec", optarg
);
356 arg_with_timer
= true;
360 r
= add_timer_property("OnStartupSec", optarg
);
364 arg_with_timer
= true;
367 case ARG_ON_UNIT_ACTIVE
:
368 r
= add_timer_property("OnUnitActiveSec", optarg
);
372 arg_with_timer
= true;
375 case ARG_ON_UNIT_INACTIVE
:
376 r
= add_timer_property("OnUnitInactiveSec", optarg
);
380 arg_with_timer
= true;
383 case ARG_ON_CALENDAR
: {
384 _cleanup_(calendar_spec_freep
) CalendarSpec
*cs
= NULL
;
387 /* Let's make sure the given calendar event is not in the past */
388 curr
= now(CLOCK_REALTIME
);
389 r
= calendar_spec_from_string(optarg
, &cs
);
391 return log_error_errno(r
, "Failed to parse calendar event specification");
392 r
= calendar_spec_next_usec(cs
, curr
, &next
);
394 /* The calendar event is in the past - in such case
395 * don't add an OnCalendar property and execute
396 * the command immediately instead */
397 log_warning("Specified calendar event is in the past, executing immediately");
401 r
= add_timer_property("OnCalendar", optarg
);
405 arg_with_timer
= true;
409 case ARG_ON_TIMEZONE_CHANGE
:
410 r
= add_timer_property("OnTimezoneChange", "yes");
414 arg_with_timer
= true;
417 case ARG_ON_CLOCK_CHANGE
:
418 r
= add_timer_property("OnClockChange", "yes");
422 arg_with_timer
= true;
425 case ARG_TIMER_PROPERTY
:
427 if (strv_extend(&arg_timer_property
, optarg
) < 0)
430 arg_with_timer
= arg_with_timer
||
431 STARTSWITH_SET(optarg
,
436 "OnUnitInactiveSec=",
440 case ARG_PATH_PROPERTY
:
442 if (strv_extend(&arg_path_property
, optarg
) < 0)
447 case ARG_SOCKET_PROPERTY
:
449 if (strv_extend(&arg_socket_property
, optarg
) < 0)
462 case ARG_WORKING_DIRECTORY
:
463 r
= parse_path_argument_and_warn(optarg
, true, &arg_working_directory
);
470 _cleanup_free_
char *p
= NULL
;
474 return log_error_errno(r
, "Failed to get current working directory: %m");
476 if (empty_or_root(p
))
477 arg_working_directory
= mfree(arg_working_directory
);
479 free_and_replace(arg_working_directory
, p
);
484 arg_aggressive_gc
= true;
495 assert_not_reached("Unhandled option");
498 with_trigger
= !!arg_path_property
|| !!arg_socket_property
|| arg_with_timer
;
500 /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */
501 if ((int) !!arg_path_property
+ (int) !!arg_socket_property
+ (int) arg_with_timer
> 1)
502 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
503 "Only single trigger (path, socket, timer) unit can be created.");
506 /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise
510 if (arg_stdio
== ARG_STDIO_NONE
)
511 arg_stdio
= ARG_STDIO_AUTO
;
513 if (!arg_working_directory
) {
514 r
= safe_getcwd(&arg_working_directory
);
516 return log_error_errno(r
, "Failed to get current working directory: %m");
519 if (!arg_service_type
) {
520 arg_service_type
= strdup("exec");
521 if (!arg_service_type
)
528 arg_aggressive_gc
= true;
531 if (arg_stdio
== ARG_STDIO_AUTO
) {
532 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
533 * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
534 * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
535 arg_stdio
= isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
) && isatty(STDERR_FILENO
) ?
544 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "If --shell is used, no command line is expected.");
546 l
= strv_copy(argv
+ optind
);
550 strv_free_and_replace(arg_cmdline
, l
);
552 } else if (arg_shell
) {
553 _cleanup_free_
char *s
= NULL
;
558 return log_error_errno(r
, "Failed to determine shell: %m");
564 strv_free_and_replace(arg_cmdline
, l
);
566 } else if (!arg_unit
|| !with_trigger
)
567 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Command line to execute required.");
569 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
570 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
571 "Execution in user context is not supported on non-local systems.");
573 if (arg_scope
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
574 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
575 "Scope execution is not supported on non-local systems.");
577 if (arg_scope
&& (arg_remain_after_exit
|| arg_service_type
))
578 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
579 "--remain-after-exit and --service-type= are not supported in --scope mode.");
581 if (arg_stdio
!= ARG_STDIO_NONE
&& (with_trigger
|| arg_scope
))
582 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
583 "--pty/--pipe is not compatible in timer or --scope mode.");
585 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_transport
== BUS_TRANSPORT_REMOTE
)
586 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
587 "--pty/--pipe is only supported when connecting to the local system or containers.");
589 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_no_block
)
590 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
591 "--pty/--pipe is not compatible with --no-block.");
593 if (arg_scope
&& with_trigger
)
594 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
595 "Path, socket or timer options are not supported in --scope mode.");
597 if (arg_timer_property
&& !arg_with_timer
)
598 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
599 "--timer-property= has no effect without any other timer options.");
603 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
604 "--wait may not be combined with --no-block.");
607 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
608 "--wait may not be combined with path, socket or timer operations.");
611 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
612 "--wait may not be combined with --scope.");
618 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
621 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
623 return bus_log_create_error(r
);
625 if (arg_aggressive_gc
) {
626 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
628 return bus_log_create_error(r
);
631 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
638 static int transient_cgroup_set_properties(sd_bus_message
*m
) {
642 if (!isempty(arg_slice
)) {
643 _cleanup_free_
char *slice
= NULL
;
645 r
= unit_name_mangle_with_suffix(arg_slice
, arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
, ".slice", &slice
);
647 return log_error_errno(r
, "Failed to mangle name '%s': %m", arg_slice
);
649 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
651 return bus_log_create_error(r
);
657 static int transient_kill_set_properties(sd_bus_message
*m
) {
662 if (arg_send_sighup
) {
663 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", arg_send_sighup
);
665 return bus_log_create_error(r
);
671 static int transient_service_set_properties(sd_bus_message
*m
, const char *pty_path
) {
672 bool send_term
= false;
677 r
= transient_unit_set_properties(m
, UNIT_SERVICE
, arg_property
);
681 r
= transient_kill_set_properties(m
);
685 r
= transient_cgroup_set_properties(m
);
689 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
690 r
= sd_bus_message_append(m
, "(sv)", "AddRef", "b", 1);
692 return bus_log_create_error(r
);
695 if (arg_remain_after_exit
) {
696 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit
);
698 return bus_log_create_error(r
);
701 if (arg_service_type
) {
702 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_service_type
);
704 return bus_log_create_error(r
);
708 r
= sd_bus_message_append(m
, "(sv)", "User", "s", arg_exec_user
);
710 return bus_log_create_error(r
);
713 if (arg_exec_group
) {
714 r
= sd_bus_message_append(m
, "(sv)", "Group", "s", arg_exec_group
);
716 return bus_log_create_error(r
);
720 r
= sd_bus_message_append(m
, "(sv)", "Nice", "i", arg_nice
);
722 return bus_log_create_error(r
);
725 if (arg_working_directory
) {
726 r
= sd_bus_message_append(m
, "(sv)", "WorkingDirectory", "s", arg_working_directory
);
728 return bus_log_create_error(r
);
732 r
= sd_bus_message_append(m
,
734 "StandardInput", "s", "tty",
735 "StandardOutput", "s", "tty",
736 "StandardError", "s", "tty",
737 "TTYPath", "s", pty_path
);
739 return bus_log_create_error(r
);
743 } else if (arg_stdio
== ARG_STDIO_DIRECT
) {
744 r
= sd_bus_message_append(m
,
746 "StandardInputFileDescriptor", "h", STDIN_FILENO
,
747 "StandardOutputFileDescriptor", "h", STDOUT_FILENO
,
748 "StandardErrorFileDescriptor", "h", STDERR_FILENO
);
750 return bus_log_create_error(r
);
752 send_term
= isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
) || isatty(STDERR_FILENO
);
762 n
= strjoina("TERM=", e
);
763 r
= sd_bus_message_append(m
,
765 "Environment", "as", 1, n
);
767 return bus_log_create_error(r
);
771 if (!strv_isempty(arg_environment
)) {
772 r
= sd_bus_message_open_container(m
, 'r', "sv");
774 return bus_log_create_error(r
);
776 r
= sd_bus_message_append(m
, "s", "Environment");
778 return bus_log_create_error(r
);
780 r
= sd_bus_message_open_container(m
, 'v', "as");
782 return bus_log_create_error(r
);
784 r
= sd_bus_message_append_strv(m
, arg_environment
);
786 return bus_log_create_error(r
);
788 r
= sd_bus_message_close_container(m
);
790 return bus_log_create_error(r
);
792 r
= sd_bus_message_close_container(m
);
794 return bus_log_create_error(r
);
798 if (!strv_isempty(arg_cmdline
)) {
799 r
= sd_bus_message_open_container(m
, 'r', "sv");
801 return bus_log_create_error(r
);
803 r
= sd_bus_message_append(m
, "s", "ExecStart");
805 return bus_log_create_error(r
);
807 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
809 return bus_log_create_error(r
);
811 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
813 return bus_log_create_error(r
);
815 r
= sd_bus_message_open_container(m
, 'r', "sasb");
817 return bus_log_create_error(r
);
819 r
= sd_bus_message_append(m
, "s", arg_cmdline
[0]);
821 return bus_log_create_error(r
);
823 r
= sd_bus_message_append_strv(m
, arg_cmdline
);
825 return bus_log_create_error(r
);
827 r
= sd_bus_message_append(m
, "b", false);
829 return bus_log_create_error(r
);
831 r
= sd_bus_message_close_container(m
);
833 return bus_log_create_error(r
);
835 r
= sd_bus_message_close_container(m
);
837 return bus_log_create_error(r
);
839 r
= sd_bus_message_close_container(m
);
841 return bus_log_create_error(r
);
843 r
= sd_bus_message_close_container(m
);
845 return bus_log_create_error(r
);
851 static int transient_scope_set_properties(sd_bus_message
*m
) {
856 r
= transient_unit_set_properties(m
, UNIT_SCOPE
, arg_property
);
860 r
= transient_kill_set_properties(m
);
864 r
= transient_cgroup_set_properties(m
);
868 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
870 return bus_log_create_error(r
);
875 static int transient_timer_set_properties(sd_bus_message
*m
) {
880 r
= transient_unit_set_properties(m
, UNIT_TIMER
, arg_timer_property
);
884 /* Automatically clean up our transient timers */
885 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterElapse", "b", false);
887 return bus_log_create_error(r
);
892 static int make_unit_name(sd_bus
*bus
, UnitType t
, char **ret
) {
893 const char *unique
, *id
;
899 assert(t
< _UNIT_TYPE_MAX
);
901 r
= sd_bus_get_unique_name(bus
, &unique
);
905 /* We couldn't get the unique name, which is a pretty
906 * common case if we are connected to systemd
907 * directly. In that case, just pick a random uuid as
910 r
= sd_id128_randomize(&rnd
);
912 return log_error_errno(r
, "Failed to generate random run unit name: %m");
914 if (asprintf(ret
, "run-r" SD_ID128_FORMAT_STR
".%s", SD_ID128_FORMAT_VAL(rnd
), unit_type_to_string(t
)) < 0)
920 /* We managed to get the unique name, then let's use that to
921 * name our transient units. */
923 id
= startswith(unique
, ":1.");
925 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
926 "Unique name %s has unexpected format.",
929 p
= strjoin("run-u", id
, ".", unit_type_to_string(t
));
937 typedef struct RunContext
{
943 /* The exit data of the unit */
945 uint64_t inactive_exit_usec
;
946 uint64_t inactive_enter_usec
;
948 uint64_t cpu_usage_nsec
;
949 uint64_t ip_ingress_bytes
;
950 uint64_t ip_egress_bytes
;
951 uint64_t io_read_bytes
;
952 uint64_t io_write_bytes
;
954 uint32_t exit_status
;
957 static void run_context_free(RunContext
*c
) {
960 c
->forward
= pty_forward_free(c
->forward
);
961 c
->match
= sd_bus_slot_unref(c
->match
);
962 c
->bus
= sd_bus_unref(c
->bus
);
963 c
->event
= sd_event_unref(c
->event
);
965 free(c
->active_state
);
969 static void run_context_check_done(RunContext
*c
) {
975 done
= STRPTR_IN_SET(c
->active_state
, "inactive", "failed");
979 if (c
->forward
&& done
) /* If the service is gone, it's time to drain the output */
980 done
= pty_forward_drain(c
->forward
);
983 sd_event_exit(c
->event
, EXIT_SUCCESS
);
986 static int run_context_update(RunContext
*c
, const char *path
) {
988 static const struct bus_properties_map map
[] = {
989 { "ActiveState", "s", NULL
, offsetof(RunContext
, active_state
) },
990 { "InactiveExitTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_exit_usec
) },
991 { "InactiveEnterTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_enter_usec
) },
992 { "Result", "s", NULL
, offsetof(RunContext
, result
) },
993 { "ExecMainCode", "i", NULL
, offsetof(RunContext
, exit_code
) },
994 { "ExecMainStatus", "i", NULL
, offsetof(RunContext
, exit_status
) },
995 { "CPUUsageNSec", "t", NULL
, offsetof(RunContext
, cpu_usage_nsec
) },
996 { "IPIngressBytes", "t", NULL
, offsetof(RunContext
, ip_ingress_bytes
) },
997 { "IPEgressBytes", "t", NULL
, offsetof(RunContext
, ip_egress_bytes
) },
998 { "IOReadBytes", "t", NULL
, offsetof(RunContext
, io_read_bytes
) },
999 { "IOWriteBytes", "t", NULL
, offsetof(RunContext
, io_write_bytes
) },
1003 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1006 r
= bus_map_all_properties(c
->bus
,
1007 "org.freedesktop.systemd1",
1015 sd_event_exit(c
->event
, EXIT_FAILURE
);
1016 return log_error_errno(r
, "Failed to query unit state: %s", bus_error_message(&error
, r
));
1019 run_context_check_done(c
);
1023 static int on_properties_changed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1024 RunContext
*c
= userdata
;
1029 return run_context_update(c
, sd_bus_message_get_path(m
));
1032 static int pty_forward_handler(PTYForward
*f
, int rcode
, void *userdata
) {
1033 RunContext
*c
= userdata
;
1038 sd_event_exit(c
->event
, EXIT_FAILURE
);
1039 return log_error_errno(rcode
, "Error on PTY forwarding logic: %m");
1042 run_context_check_done(c
);
1046 static int start_transient_service(
1050 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1051 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1052 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1053 _cleanup_free_
char *service
= NULL
, *pty_path
= NULL
;
1054 _cleanup_close_
int master
= -1;
1060 if (arg_stdio
== ARG_STDIO_PTY
) {
1062 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
1063 master
= posix_openpt(O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
1065 return log_error_errno(errno
, "Failed to acquire pseudo tty: %m");
1067 r
= ptsname_malloc(master
, &pty_path
);
1069 return log_error_errno(r
, "Failed to determine tty name: %m");
1071 if (unlockpt(master
) < 0)
1072 return log_error_errno(errno
, "Failed to unlock tty: %m");
1074 } else if (arg_transport
== BUS_TRANSPORT_MACHINE
) {
1075 _cleanup_(sd_bus_unrefp
) sd_bus
*system_bus
= NULL
;
1076 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pty_reply
= NULL
;
1079 r
= sd_bus_default_system(&system_bus
);
1081 return log_error_errno(r
, "Failed to connect to system bus: %m");
1083 r
= sd_bus_call_method(system_bus
,
1084 "org.freedesktop.machine1",
1085 "/org/freedesktop/machine1",
1086 "org.freedesktop.machine1.Manager",
1092 return log_error_errno(r
, "Failed to get machine PTY: %s", bus_error_message(&error
, -r
));
1094 r
= sd_bus_message_read(pty_reply
, "hs", &master
, &s
);
1096 return bus_log_parse_error(r
);
1098 master
= fcntl(master
, F_DUPFD_CLOEXEC
, 3);
1100 return log_error_errno(errno
, "Failed to duplicate master fd: %m");
1102 pty_path
= strdup(s
);
1106 assert_not_reached("Can't allocate tty via ssh");
1109 if (!arg_no_block
) {
1110 r
= bus_wait_for_jobs_new(bus
, &w
);
1112 return log_error_errno(r
, "Could not watch jobs: %m");
1116 r
= unit_name_mangle_with_suffix(arg_unit
, arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
, ".service", &service
);
1118 return log_error_errno(r
, "Failed to mangle unit name: %m");
1120 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1125 r
= sd_bus_message_new_method_call(
1128 "org.freedesktop.systemd1",
1129 "/org/freedesktop/systemd1",
1130 "org.freedesktop.systemd1.Manager",
1131 "StartTransientUnit");
1133 return bus_log_create_error(r
);
1135 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1137 return bus_log_create_error(r
);
1140 r
= sd_bus_message_append(m
, "ss", service
, "fail");
1142 return bus_log_create_error(r
);
1145 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1147 return bus_log_create_error(r
);
1149 r
= transient_service_set_properties(m
, pty_path
);
1153 r
= sd_bus_message_close_container(m
);
1155 return bus_log_create_error(r
);
1157 /* Auxiliary units */
1158 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1160 return bus_log_create_error(r
);
1162 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1164 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1166 return log_error_errno(r
, "Failed to start transient service unit: %s", bus_error_message(&error
, r
));
1171 r
= sd_bus_message_read(reply
, "o", &object
);
1173 return bus_log_parse_error(r
);
1175 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1181 log_info("Running as unit: %s", service
);
1183 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
1184 _cleanup_(run_context_free
) RunContext c
= {
1185 .cpu_usage_nsec
= NSEC_INFINITY
,
1186 .ip_ingress_bytes
= UINT64_MAX
,
1187 .ip_egress_bytes
= UINT64_MAX
,
1188 .io_read_bytes
= UINT64_MAX
,
1189 .io_write_bytes
= UINT64_MAX
,
1190 .inactive_exit_usec
= USEC_INFINITY
,
1191 .inactive_enter_usec
= USEC_INFINITY
,
1193 _cleanup_free_
char *path
= NULL
;
1195 c
.bus
= sd_bus_ref(bus
);
1197 r
= sd_event_default(&c
.event
);
1199 return log_error_errno(r
, "Failed to get event loop: %m");
1202 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1203 (void) sd_event_add_signal(c
.event
, NULL
, SIGINT
, NULL
, NULL
);
1204 (void) sd_event_add_signal(c
.event
, NULL
, SIGTERM
, NULL
, NULL
);
1207 log_info("Press ^] three times within 1s to disconnect TTY.");
1209 r
= pty_forward_new(c
.event
, master
, PTY_FORWARD_IGNORE_INITIAL_VHANGUP
, &c
.forward
);
1211 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1213 pty_forward_set_handler(c
.forward
, pty_forward_handler
, &c
);
1215 /* Make sure to process any TTY events before we process bus events */
1216 (void) pty_forward_set_priority(c
.forward
, SD_EVENT_PRIORITY_IMPORTANT
);
1219 path
= unit_dbus_path_from_name(service
);
1223 r
= sd_bus_match_signal_async(
1226 "org.freedesktop.systemd1",
1228 "org.freedesktop.DBus.Properties",
1229 "PropertiesChanged",
1230 on_properties_changed
, NULL
, &c
);
1232 return log_error_errno(r
, "Failed to request properties changed signal match: %m");
1234 r
= sd_bus_attach_event(bus
, c
.event
, SD_EVENT_PRIORITY_NORMAL
);
1236 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1238 r
= run_context_update(&c
, path
);
1242 r
= sd_event_loop(c
.event
);
1244 return log_error_errno(r
, "Failed to run event loop: %m");
1249 r
= pty_forward_get_last_char(c
.forward
, &last_char
);
1250 if (r
>= 0 && !arg_quiet
&& last_char
!= '\n')
1251 fputc('\n', stdout
);
1254 if (arg_wait
&& !arg_quiet
) {
1256 /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, in its
1257 * original settings (i.e. proper line breaks), so that we can show the summary in a pretty
1259 c
.forward
= pty_forward_free(c
.forward
);
1261 if (!isempty(c
.result
))
1262 log_info("Finished with result: %s", strna(c
.result
));
1264 if (c
.exit_code
== CLD_EXITED
)
1265 log_info("Main processes terminated with: code=%s/status=%i", sigchld_code_to_string(c
.exit_code
), c
.exit_status
);
1266 else if (c
.exit_code
> 0)
1267 log_info("Main processes terminated with: code=%s/status=%s", sigchld_code_to_string(c
.exit_code
), signal_to_string(c
.exit_status
));
1269 if (c
.inactive_enter_usec
> 0 && c
.inactive_enter_usec
!= USEC_INFINITY
&&
1270 c
.inactive_exit_usec
> 0 && c
.inactive_exit_usec
!= USEC_INFINITY
&&
1271 c
.inactive_enter_usec
> c
.inactive_exit_usec
) {
1272 char ts
[FORMAT_TIMESPAN_MAX
];
1273 log_info("Service runtime: %s", format_timespan(ts
, sizeof(ts
), c
.inactive_enter_usec
- c
.inactive_exit_usec
, USEC_PER_MSEC
));
1276 if (c
.cpu_usage_nsec
!= NSEC_INFINITY
) {
1277 char ts
[FORMAT_TIMESPAN_MAX
];
1278 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
));
1281 if (c
.ip_ingress_bytes
!= UINT64_MAX
) {
1282 char bytes
[FORMAT_BYTES_MAX
];
1283 log_info("IP traffic received: %s", format_bytes(bytes
, sizeof(bytes
), c
.ip_ingress_bytes
));
1285 if (c
.ip_egress_bytes
!= UINT64_MAX
) {
1286 char bytes
[FORMAT_BYTES_MAX
];
1287 log_info("IP traffic sent: %s", format_bytes(bytes
, sizeof(bytes
), c
.ip_egress_bytes
));
1289 if (c
.io_read_bytes
!= UINT64_MAX
) {
1290 char bytes
[FORMAT_BYTES_MAX
];
1291 log_info("IO bytes read: %s", format_bytes(bytes
, sizeof(bytes
), c
.io_read_bytes
));
1293 if (c
.io_write_bytes
!= UINT64_MAX
) {
1294 char bytes
[FORMAT_BYTES_MAX
];
1295 log_info("IO bytes written: %s", format_bytes(bytes
, sizeof(bytes
), c
.io_write_bytes
));
1299 /* Try to propagate the service's return value */
1300 if (c
.result
&& STR_IN_SET(c
.result
, "success", "exit-code") && c
.exit_code
== CLD_EXITED
)
1301 *retval
= c
.exit_status
;
1303 *retval
= EXIT_FAILURE
;
1309 static int acquire_invocation_id(sd_bus
*bus
, sd_id128_t
*ret
) {
1310 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1311 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1319 r
= sd_bus_get_property(bus
,
1320 "org.freedesktop.systemd1",
1321 "/org/freedesktop/systemd1/unit/self",
1322 "org.freedesktop.systemd1.Unit",
1328 return log_error_errno(r
, "Failed to request invocation ID for scope: %s", bus_error_message(&error
, r
));
1330 r
= sd_bus_message_read_array(reply
, 'y', &p
, &l
);
1332 return bus_log_parse_error(r
);
1334 if (l
!= sizeof(sd_id128_t
))
1335 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid UUID size, %zu != %zu.", l
, sizeof(sd_id128_t
));
1341 static int start_transient_scope(sd_bus
*bus
) {
1342 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1343 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1344 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1345 _cleanup_strv_free_
char **env
= NULL
, **user_env
= NULL
;
1346 _cleanup_free_
char *scope
= NULL
;
1347 const char *object
= NULL
;
1348 sd_id128_t invocation_id
;
1352 assert(!strv_isempty(arg_cmdline
));
1354 r
= bus_wait_for_jobs_new(bus
, &w
);
1359 r
= unit_name_mangle_with_suffix(arg_unit
, arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
, ".scope", &scope
);
1361 return log_error_errno(r
, "Failed to mangle scope name: %m");
1363 r
= make_unit_name(bus
, UNIT_SCOPE
, &scope
);
1368 r
= sd_bus_message_new_method_call(
1371 "org.freedesktop.systemd1",
1372 "/org/freedesktop/systemd1",
1373 "org.freedesktop.systemd1.Manager",
1374 "StartTransientUnit");
1376 return bus_log_create_error(r
);
1378 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1380 return bus_log_create_error(r
);
1383 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
1385 return bus_log_create_error(r
);
1388 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1390 return bus_log_create_error(r
);
1392 r
= transient_scope_set_properties(m
);
1396 r
= sd_bus_message_close_container(m
);
1398 return bus_log_create_error(r
);
1400 /* Auxiliary units */
1401 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1403 return bus_log_create_error(r
);
1405 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1407 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1409 return log_error_errno(r
, "Failed to start transient scope unit: %s", bus_error_message(&error
, -r
));
1411 r
= sd_bus_message_read(reply
, "o", &object
);
1413 return bus_log_parse_error(r
);
1415 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1419 r
= acquire_invocation_id(bus
, &invocation_id
);
1423 r
= strv_extendf(&user_env
, "INVOCATION_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(invocation_id
));
1428 if (setpriority(PRIO_PROCESS
, 0, arg_nice
) < 0)
1429 return log_error_errno(errno
, "Failed to set nice level: %m");
1432 if (arg_exec_group
) {
1435 r
= get_group_creds(&arg_exec_group
, &gid
, 0);
1437 return log_error_errno(r
, "Failed to resolve group %s: %m", arg_exec_group
);
1439 if (setresgid(gid
, gid
, gid
) < 0)
1440 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1443 if (arg_exec_user
) {
1444 const char *home
, *shell
;
1448 r
= get_user_creds(&arg_exec_user
, &uid
, &gid
, &home
, &shell
, USER_CREDS_CLEAN
|USER_CREDS_PREFER_NSS
);
1450 return log_error_errno(r
, "Failed to resolve user %s: %m", arg_exec_user
);
1453 r
= strv_extendf(&user_env
, "HOME=%s", home
);
1459 r
= strv_extendf(&user_env
, "SHELL=%s", shell
);
1464 r
= strv_extendf(&user_env
, "USER=%s", arg_exec_user
);
1468 r
= strv_extendf(&user_env
, "LOGNAME=%s", arg_exec_user
);
1472 if (!arg_exec_group
) {
1473 if (setresgid(gid
, gid
, gid
) < 0)
1474 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1477 if (setresuid(uid
, uid
, uid
) < 0)
1478 return log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", uid
);
1481 env
= strv_env_merge(3, environ
, user_env
, arg_environment
);
1486 log_info("Running scope as unit: %s", scope
);
1488 execvpe(arg_cmdline
[0], arg_cmdline
, env
);
1490 return log_error_errno(errno
, "Failed to execute: %m");
1493 static int start_transient_trigger(
1495 const char *suffix
) {
1497 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1498 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1499 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1500 _cleanup_free_
char *trigger
= NULL
, *service
= NULL
;
1501 const char *object
= NULL
;
1506 r
= bus_wait_for_jobs_new(bus
, &w
);
1511 switch (unit_name_to_type(arg_unit
)) {
1514 service
= strdup(arg_unit
);
1518 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1520 return log_error_errno(r
, "Failed to change unit suffix: %m");
1524 trigger
= strdup(arg_unit
);
1528 r
= unit_name_change_suffix(trigger
, ".service", &service
);
1530 return log_error_errno(r
, "Failed to change unit suffix: %m");
1534 r
= unit_name_mangle_with_suffix(arg_unit
, arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
, ".service", &service
);
1536 return log_error_errno(r
, "Failed to mangle unit name: %m");
1538 r
= unit_name_mangle_with_suffix(arg_unit
, arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
, suffix
, &trigger
);
1540 return log_error_errno(r
, "Failed to mangle unit name: %m");
1545 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1549 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1551 return log_error_errno(r
, "Failed to change unit suffix: %m");
1554 r
= sd_bus_message_new_method_call(
1557 "org.freedesktop.systemd1",
1558 "/org/freedesktop/systemd1",
1559 "org.freedesktop.systemd1.Manager",
1560 "StartTransientUnit");
1562 return bus_log_create_error(r
);
1564 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1566 return bus_log_create_error(r
);
1569 r
= sd_bus_message_append(m
, "ss", trigger
, "fail");
1571 return bus_log_create_error(r
);
1574 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1576 return bus_log_create_error(r
);
1578 if (streq(suffix
, ".path"))
1579 r
= transient_unit_set_properties(m
, UNIT_PATH
, arg_path_property
);
1580 else if (streq(suffix
, ".socket"))
1581 r
= transient_unit_set_properties(m
, UNIT_SOCKET
, arg_socket_property
);
1582 else if (streq(suffix
, ".timer"))
1583 r
= transient_timer_set_properties(m
);
1585 assert_not_reached("Invalid suffix");
1589 r
= sd_bus_message_close_container(m
);
1591 return bus_log_create_error(r
);
1593 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
1595 return bus_log_create_error(r
);
1597 if (!strv_isempty(arg_cmdline
)) {
1598 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
1600 return bus_log_create_error(r
);
1602 r
= sd_bus_message_append(m
, "s", service
);
1604 return bus_log_create_error(r
);
1606 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1608 return bus_log_create_error(r
);
1610 r
= transient_service_set_properties(m
, NULL
);
1614 r
= sd_bus_message_close_container(m
);
1616 return bus_log_create_error(r
);
1618 r
= sd_bus_message_close_container(m
);
1620 return bus_log_create_error(r
);
1623 r
= sd_bus_message_close_container(m
);
1625 return bus_log_create_error(r
);
1627 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1629 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1631 return log_error_errno(r
, "Failed to start transient %s unit: %s", suffix
+ 1, bus_error_message(&error
, -r
));
1633 r
= sd_bus_message_read(reply
, "o", &object
);
1635 return bus_log_parse_error(r
);
1637 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1642 log_info("Running %s as unit: %s", suffix
+ 1, trigger
);
1643 if (!strv_isempty(arg_cmdline
))
1644 log_info("Will run service as unit: %s", service
);
1650 static int run(int argc
, char* argv
[]) {
1651 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1652 _cleanup_free_
char *description
= NULL
;
1653 int r
, retval
= EXIT_SUCCESS
;
1655 log_parse_environment();
1658 r
= parse_argv(argc
, argv
);
1662 if (!strv_isempty(arg_cmdline
) && arg_transport
== BUS_TRANSPORT_LOCAL
) {
1663 _cleanup_free_
char *command
= NULL
;
1665 /* Patch in an absolute path */
1667 r
= find_binary(arg_cmdline
[0], &command
);
1669 return log_error_errno(r
, "Failed to find executable %s: %m", arg_cmdline
[0]);
1671 free_and_replace(arg_cmdline
[0], command
);
1674 if (!arg_description
) {
1675 description
= strv_join(arg_cmdline
, " ");
1679 if (arg_unit
&& isempty(description
)) {
1680 r
= free_and_strdup(&description
, arg_unit
);
1685 arg_description
= description
;
1688 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1690 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
)
1691 r
= bus_connect_transport(arg_transport
, arg_host
, arg_user
, &bus
);
1693 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1695 return log_error_errno(r
, "Failed to create bus connection: %m");
1698 r
= start_transient_scope(bus
);
1699 else if (arg_path_property
)
1700 r
= start_transient_trigger(bus
, ".path");
1701 else if (arg_socket_property
)
1702 r
= start_transient_trigger(bus
, ".socket");
1703 else if (arg_with_timer
)
1704 r
= start_transient_trigger(bus
, ".timer");
1706 r
= start_transient_service(bus
, &retval
);
1713 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);