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"
19 #include "exit-status.h"
21 #include "format-util.h"
22 #include "main-func.h"
23 #include "parse-util.h"
24 #include "path-util.h"
25 #include "pretty-print.h"
26 #include "process-util.h"
28 #include "signal-util.h"
29 #include "spawn-polkit-agent.h"
31 #include "terminal-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
36 static bool arg_ask_password
= true;
37 static bool arg_scope
= false;
38 static bool arg_remain_after_exit
= false;
39 static bool arg_no_block
= false;
40 static bool arg_wait
= false;
41 static const char *arg_unit
= NULL
;
42 static const char *arg_description
= NULL
;
43 static const char *arg_slice
= NULL
;
44 static bool arg_send_sighup
= false;
45 static BusTransport arg_transport
= BUS_TRANSPORT_LOCAL
;
46 static const char *arg_host
= NULL
;
47 static bool arg_user
= false;
48 static const char *arg_service_type
= NULL
;
49 static const char *arg_exec_user
= NULL
;
50 static const char *arg_exec_group
= NULL
;
51 static int arg_nice
= 0;
52 static bool arg_nice_set
= false;
53 static char **arg_environment
= NULL
;
54 static char **arg_property
= NULL
;
56 ARG_STDIO_NONE
, /* The default, as it is for normal services, stdin connected to /dev/null, and stdout+stderr to the journal */
57 ARG_STDIO_PTY
, /* Interactive behaviour, requested by --pty: we allocate a pty and connect it to the TTY we are invoked from */
58 ARG_STDIO_DIRECT
, /* Directly pass our stdin/stdout/stderr to the activated service, useful for usage in shell pipelines, requested by --pipe */
59 ARG_STDIO_AUTO
, /* If --pipe and --pty are used together we use --pty when invoked on a TTY, and --pipe otherwise */
60 } arg_stdio
= ARG_STDIO_NONE
;
61 static char **arg_path_property
= NULL
;
62 static char **arg_socket_property
= NULL
;
63 static char **arg_timer_property
= NULL
;
64 static bool arg_with_timer
= false;
65 static bool arg_quiet
= false;
66 static bool arg_aggressive_gc
= false;
67 static char *arg_working_directory
= NULL
;
68 static bool arg_shell
= false;
69 static char **arg_cmdline
= NULL
;
71 STATIC_DESTRUCTOR_REGISTER(arg_environment
, strv_freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_property
, strv_freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_path_property
, strv_freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_socket_property
, strv_freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_timer_property
, strv_freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_working_directory
, freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_cmdline
, strv_freep
);
79 static int help(void) {
80 _cleanup_free_
char *link
= NULL
;
83 r
= terminal_urlify_man("systemd-run", "1", &link
);
87 printf("%s [OPTIONS...] COMMAND [ARGUMENTS...]\n"
88 "\n%sRun the specified command in a transient scope or service.%s\n\n"
89 " -h --help Show this help\n"
90 " --version Show package version\n"
91 " --no-ask-password Do not prompt for password\n"
92 " --user Run as user unit\n"
93 " -H --host=[USER@]HOST Operate on remote host\n"
94 " -M --machine=CONTAINER Operate on local container\n"
95 " --scope Run this as scope rather than service\n"
96 " -u --unit=UNIT Run under the specified unit name\n"
97 " -p --property=NAME=VALUE Set service or scope unit property\n"
98 " --description=TEXT Description for unit\n"
99 " --slice=SLICE Run in the specified slice\n"
100 " --no-block Do not wait until operation finished\n"
101 " -r --remain-after-exit Leave service around until explicitly stopped\n"
102 " --wait Wait until service stopped again\n"
103 " --send-sighup Send SIGHUP when terminating\n"
104 " --service-type=TYPE Service type\n"
105 " --uid=USER Run as system user\n"
106 " --gid=GROUP Run as system group\n"
107 " --nice=NICE Nice level\n"
108 " --working-directory=PATH Set working directory\n"
109 " -d --same-dir Inherit working directory from caller\n"
110 " -E --setenv=NAME=VALUE Set environment\n"
111 " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n"
113 " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n"
114 " -q --quiet Suppress information messages during runtime\n"
115 " -G --collect Unload unit after it ran, even when failed\n"
116 " -S --shell Invoke a $SHELL interactively\n\n"
118 " --path-property=NAME=VALUE Set path unit property\n\n"
120 " --socket-property=NAME=VALUE Set socket unit property\n\n"
122 " --on-active=SECONDS Run after SECONDS delay\n"
123 " --on-boot=SECONDS Run SECONDS after machine was booted up\n"
124 " --on-startup=SECONDS Run SECONDS after systemd activation\n"
125 " --on-unit-active=SECONDS Run SECONDS after the last activation\n"
126 " --on-unit-inactive=SECONDS Run SECONDS after the last deactivation\n"
127 " --on-calendar=SPEC Realtime timer\n"
128 " --on-timezone-change Run when the timezone changes\n"
129 " --on-clock-change Run when the realtime clock jumps\n"
130 " --timer-property=NAME=VALUE Set timer unit property\n"
131 "\nSee the %s for details.\n"
132 , program_invocation_short_name
133 , ansi_highlight(), ansi_normal()
140 static int add_timer_property(const char *name
, const char *val
) {
146 p
= strjoin(name
, "=", val
);
150 if (strv_consume(&arg_timer_property
, p
) < 0)
156 static int parse_argv(int argc
, char *argv
[]) {
174 ARG_ON_UNIT_INACTIVE
,
176 ARG_ON_TIMEZONE_CHANGE
,
184 ARG_WORKING_DIRECTORY
,
188 static const struct option options
[] = {
189 { "help", no_argument
, NULL
, 'h' },
190 { "version", no_argument
, NULL
, ARG_VERSION
},
191 { "user", no_argument
, NULL
, ARG_USER
},
192 { "system", no_argument
, NULL
, ARG_SYSTEM
},
193 { "scope", no_argument
, NULL
, ARG_SCOPE
},
194 { "unit", required_argument
, NULL
, 'u' },
195 { "description", required_argument
, NULL
, ARG_DESCRIPTION
},
196 { "slice", required_argument
, NULL
, ARG_SLICE
},
197 { "remain-after-exit", no_argument
, NULL
, 'r' },
198 { "send-sighup", no_argument
, NULL
, ARG_SEND_SIGHUP
},
199 { "host", required_argument
, NULL
, 'H' },
200 { "machine", required_argument
, NULL
, 'M' },
201 { "service-type", required_argument
, NULL
, ARG_SERVICE_TYPE
},
202 { "wait", no_argument
, NULL
, ARG_WAIT
},
203 { "uid", required_argument
, NULL
, ARG_EXEC_USER
},
204 { "gid", required_argument
, NULL
, ARG_EXEC_GROUP
},
205 { "nice", required_argument
, NULL
, ARG_NICE
},
206 { "setenv", required_argument
, NULL
, 'E' },
207 { "property", required_argument
, NULL
, 'p' },
208 { "tty", no_argument
, NULL
, 't' }, /* deprecated alias */
209 { "pty", no_argument
, NULL
, 't' },
210 { "pipe", no_argument
, NULL
, 'P' },
211 { "quiet", no_argument
, NULL
, 'q' },
212 { "on-active", required_argument
, NULL
, ARG_ON_ACTIVE
},
213 { "on-boot", required_argument
, NULL
, ARG_ON_BOOT
},
214 { "on-startup", required_argument
, NULL
, ARG_ON_STARTUP
},
215 { "on-unit-active", required_argument
, NULL
, ARG_ON_UNIT_ACTIVE
},
216 { "on-unit-inactive", required_argument
, NULL
, ARG_ON_UNIT_INACTIVE
},
217 { "on-calendar", required_argument
, NULL
, ARG_ON_CALENDAR
},
218 { "on-timezone-change",no_argument
, NULL
, ARG_ON_TIMEZONE_CHANGE
},
219 { "on-clock-change", no_argument
, NULL
, ARG_ON_CLOCK_CHANGE
},
220 { "timer-property", required_argument
, NULL
, ARG_TIMER_PROPERTY
},
221 { "path-property", required_argument
, NULL
, ARG_PATH_PROPERTY
},
222 { "socket-property", required_argument
, NULL
, ARG_SOCKET_PROPERTY
},
223 { "no-block", no_argument
, NULL
, ARG_NO_BLOCK
},
224 { "no-ask-password", no_argument
, NULL
, ARG_NO_ASK_PASSWORD
},
225 { "collect", no_argument
, NULL
, 'G' },
226 { "working-directory", required_argument
, NULL
, ARG_WORKING_DIRECTORY
},
227 { "same-dir", no_argument
, NULL
, 'd' },
228 { "shell", no_argument
, NULL
, 'S' },
232 bool with_trigger
= false;
238 while ((c
= getopt_long(argc
, argv
, "+hrH:M:E:p:tPqGdSu:", options
, NULL
)) >= 0)
248 case ARG_NO_ASK_PASSWORD
:
249 arg_ask_password
= false;
268 case ARG_DESCRIPTION
:
269 arg_description
= optarg
;
276 case ARG_SEND_SIGHUP
:
277 arg_send_sighup
= true;
281 arg_remain_after_exit
= true;
285 arg_transport
= BUS_TRANSPORT_REMOTE
;
290 arg_transport
= BUS_TRANSPORT_MACHINE
;
294 case ARG_SERVICE_TYPE
:
295 arg_service_type
= optarg
;
299 arg_exec_user
= optarg
;
303 arg_exec_group
= optarg
;
307 r
= parse_nice(optarg
, &arg_nice
);
309 return log_error_errno(r
, "Failed to parse nice value: %s", optarg
);
315 if (strv_extend(&arg_environment
, optarg
) < 0)
321 if (strv_extend(&arg_property
, optarg
) < 0)
326 case 't': /* --pty */
327 if (IN_SET(arg_stdio
, ARG_STDIO_DIRECT
, ARG_STDIO_AUTO
)) /* if --pipe is already used, upgrade to auto mode */
328 arg_stdio
= ARG_STDIO_AUTO
;
330 arg_stdio
= ARG_STDIO_PTY
;
333 case 'P': /* --pipe */
334 if (IN_SET(arg_stdio
, ARG_STDIO_PTY
, ARG_STDIO_AUTO
)) /* If --pty is already used, upgrade to auto mode */
335 arg_stdio
= ARG_STDIO_AUTO
;
337 arg_stdio
= ARG_STDIO_DIRECT
;
345 r
= add_timer_property("OnActiveSec", optarg
);
349 arg_with_timer
= true;
353 r
= add_timer_property("OnBootSec", optarg
);
357 arg_with_timer
= true;
361 r
= add_timer_property("OnStartupSec", optarg
);
365 arg_with_timer
= true;
368 case ARG_ON_UNIT_ACTIVE
:
369 r
= add_timer_property("OnUnitActiveSec", optarg
);
373 arg_with_timer
= true;
376 case ARG_ON_UNIT_INACTIVE
:
377 r
= add_timer_property("OnUnitInactiveSec", optarg
);
381 arg_with_timer
= true;
384 case ARG_ON_CALENDAR
: {
385 _cleanup_(calendar_spec_freep
) CalendarSpec
*cs
= NULL
;
387 r
= calendar_spec_from_string(optarg
, &cs
);
389 return log_error_errno(r
, "Failed to parse calendar event specification: %m");
391 /* Let's make sure the given calendar event is not in the past */
392 r
= calendar_spec_next_usec(cs
, now(CLOCK_REALTIME
), NULL
);
394 /* The calendar event is in the past — let's warn about this, but install it
395 * anyway as is. The service manager will trigger the service right away.
396 * Moreover, the server side might have a different clock or timezone than we
397 * do, hence it should decide when or whether to run something. */
398 log_warning("Specified calendar expression is in the past, proceeding anyway.");
400 return log_error_errno(r
, "Failed to calculate next time calendar expression elapses: %m");
402 r
= add_timer_property("OnCalendar", optarg
);
406 arg_with_timer
= true;
410 case ARG_ON_TIMEZONE_CHANGE
:
411 r
= add_timer_property("OnTimezoneChange", "yes");
415 arg_with_timer
= true;
418 case ARG_ON_CLOCK_CHANGE
:
419 r
= add_timer_property("OnClockChange", "yes");
423 arg_with_timer
= true;
426 case ARG_TIMER_PROPERTY
:
428 if (strv_extend(&arg_timer_property
, optarg
) < 0)
431 arg_with_timer
= arg_with_timer
||
432 STARTSWITH_SET(optarg
,
437 "OnUnitInactiveSec=",
441 case ARG_PATH_PROPERTY
:
443 if (strv_extend(&arg_path_property
, optarg
) < 0)
448 case ARG_SOCKET_PROPERTY
:
450 if (strv_extend(&arg_socket_property
, optarg
) < 0)
463 case ARG_WORKING_DIRECTORY
:
464 r
= parse_path_argument_and_warn(optarg
, true, &arg_working_directory
);
471 _cleanup_free_
char *p
= NULL
;
475 return log_error_errno(r
, "Failed to get current working directory: %m");
477 if (empty_or_root(p
))
478 arg_working_directory
= mfree(arg_working_directory
);
480 free_and_replace(arg_working_directory
, p
);
485 arg_aggressive_gc
= true;
496 assert_not_reached("Unhandled option");
499 with_trigger
= !!arg_path_property
|| !!arg_socket_property
|| arg_with_timer
;
501 /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */
502 if ((int) !!arg_path_property
+ (int) !!arg_socket_property
+ (int) arg_with_timer
> 1)
503 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
504 "Only single trigger (path, socket, timer) unit can be created.");
507 /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise
511 if (arg_stdio
== ARG_STDIO_NONE
)
512 arg_stdio
= ARG_STDIO_AUTO
;
514 if (!arg_working_directory
) {
515 r
= safe_getcwd(&arg_working_directory
);
517 return log_error_errno(r
, "Failed to get current working directory: %m");
520 if (!arg_service_type
) {
521 arg_service_type
= strdup("exec");
522 if (!arg_service_type
)
529 arg_aggressive_gc
= true;
532 if (arg_stdio
== ARG_STDIO_AUTO
) {
533 /* If we both --pty and --pipe are specified we'll automatically pick --pty if we are connected fully
534 * to a TTY and pick direct fd passing otherwise. This way, we automatically adapt to usage in a shell
535 * pipeline, but we are neatly interactive with tty-level isolation otherwise. */
536 arg_stdio
= isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
) && isatty(STDERR_FILENO
) ?
545 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "If --shell is used, no command line is expected.");
547 l
= strv_copy(argv
+ optind
);
551 strv_free_and_replace(arg_cmdline
, l
);
553 } else if (arg_shell
) {
554 _cleanup_free_
char *s
= NULL
;
559 return log_error_errno(r
, "Failed to determine shell: %m");
565 strv_free_and_replace(arg_cmdline
, l
);
567 } else if (!arg_unit
|| !with_trigger
)
568 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Command line to execute required.");
570 if (arg_user
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
571 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
572 "Execution in user context is not supported on non-local systems.");
574 if (arg_scope
&& arg_transport
!= BUS_TRANSPORT_LOCAL
)
575 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
576 "Scope execution is not supported on non-local systems.");
578 if (arg_scope
&& (arg_remain_after_exit
|| arg_service_type
))
579 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
580 "--remain-after-exit and --service-type= are not supported in --scope mode.");
582 if (arg_stdio
!= ARG_STDIO_NONE
&& (with_trigger
|| arg_scope
))
583 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
584 "--pty/--pipe is not compatible in timer or --scope mode.");
586 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_transport
== BUS_TRANSPORT_REMOTE
)
587 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
588 "--pty/--pipe is only supported when connecting to the local system or containers.");
590 if (arg_stdio
!= ARG_STDIO_NONE
&& arg_no_block
)
591 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
592 "--pty/--pipe is not compatible with --no-block.");
594 if (arg_scope
&& with_trigger
)
595 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
596 "Path, socket or timer options are not supported in --scope mode.");
598 if (arg_timer_property
&& !arg_with_timer
)
599 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
600 "--timer-property= has no effect without any other timer options.");
604 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
605 "--wait may not be combined with --no-block.");
608 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
609 "--wait may not be combined with path, socket or timer operations.");
612 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
613 "--wait may not be combined with --scope.");
619 static int transient_unit_set_properties(sd_bus_message
*m
, UnitType t
, char **properties
) {
622 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", arg_description
);
624 return bus_log_create_error(r
);
626 if (arg_aggressive_gc
) {
627 r
= sd_bus_message_append(m
, "(sv)", "CollectMode", "s", "inactive-or-failed");
629 return bus_log_create_error(r
);
632 r
= bus_append_unit_property_assignment_many(m
, t
, properties
);
639 static int transient_cgroup_set_properties(sd_bus_message
*m
) {
643 if (!isempty(arg_slice
)) {
644 _cleanup_free_
char *slice
= NULL
;
646 r
= unit_name_mangle_with_suffix(arg_slice
, "as slice",
647 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
650 return log_error_errno(r
, "Failed to mangle name '%s': %m", arg_slice
);
652 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
654 return bus_log_create_error(r
);
660 static int transient_kill_set_properties(sd_bus_message
*m
) {
665 if (arg_send_sighup
) {
666 r
= sd_bus_message_append(m
, "(sv)", "SendSIGHUP", "b", arg_send_sighup
);
668 return bus_log_create_error(r
);
674 static int transient_service_set_properties(sd_bus_message
*m
, const char *pty_path
) {
675 bool send_term
= false;
680 r
= transient_unit_set_properties(m
, UNIT_SERVICE
, arg_property
);
684 r
= transient_kill_set_properties(m
);
688 r
= transient_cgroup_set_properties(m
);
692 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
693 r
= sd_bus_message_append(m
, "(sv)", "AddRef", "b", 1);
695 return bus_log_create_error(r
);
698 if (arg_remain_after_exit
) {
699 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit
);
701 return bus_log_create_error(r
);
704 if (arg_service_type
) {
705 r
= sd_bus_message_append(m
, "(sv)", "Type", "s", arg_service_type
);
707 return bus_log_create_error(r
);
711 r
= sd_bus_message_append(m
, "(sv)", "User", "s", arg_exec_user
);
713 return bus_log_create_error(r
);
716 if (arg_exec_group
) {
717 r
= sd_bus_message_append(m
, "(sv)", "Group", "s", arg_exec_group
);
719 return bus_log_create_error(r
);
723 r
= sd_bus_message_append(m
, "(sv)", "Nice", "i", arg_nice
);
725 return bus_log_create_error(r
);
728 if (arg_working_directory
) {
729 r
= sd_bus_message_append(m
, "(sv)", "WorkingDirectory", "s", arg_working_directory
);
731 return bus_log_create_error(r
);
735 r
= sd_bus_message_append(m
,
737 "StandardInput", "s", "tty",
738 "StandardOutput", "s", "tty",
739 "StandardError", "s", "tty",
740 "TTYPath", "s", pty_path
);
742 return bus_log_create_error(r
);
746 } else if (arg_stdio
== ARG_STDIO_DIRECT
) {
747 r
= sd_bus_message_append(m
,
749 "StandardInputFileDescriptor", "h", STDIN_FILENO
,
750 "StandardOutputFileDescriptor", "h", STDOUT_FILENO
,
751 "StandardErrorFileDescriptor", "h", STDERR_FILENO
);
753 return bus_log_create_error(r
);
755 send_term
= isatty(STDIN_FILENO
) || isatty(STDOUT_FILENO
) || isatty(STDERR_FILENO
);
765 n
= strjoina("TERM=", e
);
766 r
= sd_bus_message_append(m
,
768 "Environment", "as", 1, n
);
770 return bus_log_create_error(r
);
774 if (!strv_isempty(arg_environment
)) {
775 r
= sd_bus_message_open_container(m
, 'r', "sv");
777 return bus_log_create_error(r
);
779 r
= sd_bus_message_append(m
, "s", "Environment");
781 return bus_log_create_error(r
);
783 r
= sd_bus_message_open_container(m
, 'v', "as");
785 return bus_log_create_error(r
);
787 r
= sd_bus_message_append_strv(m
, arg_environment
);
789 return bus_log_create_error(r
);
791 r
= sd_bus_message_close_container(m
);
793 return bus_log_create_error(r
);
795 r
= sd_bus_message_close_container(m
);
797 return bus_log_create_error(r
);
801 if (!strv_isempty(arg_cmdline
)) {
802 r
= sd_bus_message_open_container(m
, 'r', "sv");
804 return bus_log_create_error(r
);
806 r
= sd_bus_message_append(m
, "s", "ExecStart");
808 return bus_log_create_error(r
);
810 r
= sd_bus_message_open_container(m
, 'v', "a(sasb)");
812 return bus_log_create_error(r
);
814 r
= sd_bus_message_open_container(m
, 'a', "(sasb)");
816 return bus_log_create_error(r
);
818 r
= sd_bus_message_open_container(m
, 'r', "sasb");
820 return bus_log_create_error(r
);
822 r
= sd_bus_message_append(m
, "s", arg_cmdline
[0]);
824 return bus_log_create_error(r
);
826 r
= sd_bus_message_append_strv(m
, arg_cmdline
);
828 return bus_log_create_error(r
);
830 r
= sd_bus_message_append(m
, "b", false);
832 return bus_log_create_error(r
);
834 r
= sd_bus_message_close_container(m
);
836 return bus_log_create_error(r
);
838 r
= sd_bus_message_close_container(m
);
840 return bus_log_create_error(r
);
842 r
= sd_bus_message_close_container(m
);
844 return bus_log_create_error(r
);
846 r
= sd_bus_message_close_container(m
);
848 return bus_log_create_error(r
);
854 static int transient_scope_set_properties(sd_bus_message
*m
) {
859 r
= transient_unit_set_properties(m
, UNIT_SCOPE
, arg_property
);
863 r
= transient_kill_set_properties(m
);
867 r
= transient_cgroup_set_properties(m
);
871 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached());
873 return bus_log_create_error(r
);
878 static int transient_timer_set_properties(sd_bus_message
*m
) {
883 r
= transient_unit_set_properties(m
, UNIT_TIMER
, arg_timer_property
);
887 /* Automatically clean up our transient timers */
888 r
= sd_bus_message_append(m
, "(sv)", "RemainAfterElapse", "b", false);
890 return bus_log_create_error(r
);
895 static int make_unit_name(sd_bus
*bus
, UnitType t
, char **ret
) {
896 const char *unique
, *id
;
902 assert(t
< _UNIT_TYPE_MAX
);
904 r
= sd_bus_get_unique_name(bus
, &unique
);
908 /* We couldn't get the unique name, which is a pretty
909 * common case if we are connected to systemd
910 * directly. In that case, just pick a random uuid as
913 r
= sd_id128_randomize(&rnd
);
915 return log_error_errno(r
, "Failed to generate random run unit name: %m");
917 if (asprintf(ret
, "run-r" SD_ID128_FORMAT_STR
".%s", SD_ID128_FORMAT_VAL(rnd
), unit_type_to_string(t
)) < 0)
923 /* We managed to get the unique name, then let's use that to
924 * name our transient units. */
926 id
= startswith(unique
, ":1.");
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
929 "Unique name %s has unexpected format.",
932 p
= strjoin("run-u", id
, ".", unit_type_to_string(t
));
940 typedef struct RunContext
{
946 /* The exit data of the unit */
948 uint64_t inactive_exit_usec
;
949 uint64_t inactive_enter_usec
;
951 uint64_t cpu_usage_nsec
;
952 uint64_t ip_ingress_bytes
;
953 uint64_t ip_egress_bytes
;
954 uint64_t io_read_bytes
;
955 uint64_t io_write_bytes
;
957 uint32_t exit_status
;
960 static void run_context_free(RunContext
*c
) {
963 c
->forward
= pty_forward_free(c
->forward
);
964 c
->match
= sd_bus_slot_unref(c
->match
);
965 c
->bus
= sd_bus_unref(c
->bus
);
966 c
->event
= sd_event_unref(c
->event
);
968 free(c
->active_state
);
972 static void run_context_check_done(RunContext
*c
) {
978 done
= STRPTR_IN_SET(c
->active_state
, "inactive", "failed");
982 if (c
->forward
&& done
) /* If the service is gone, it's time to drain the output */
983 done
= pty_forward_drain(c
->forward
);
986 sd_event_exit(c
->event
, EXIT_SUCCESS
);
989 static int run_context_update(RunContext
*c
, const char *path
) {
991 static const struct bus_properties_map map
[] = {
992 { "ActiveState", "s", NULL
, offsetof(RunContext
, active_state
) },
993 { "InactiveExitTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_exit_usec
) },
994 { "InactiveEnterTimestampMonotonic", "t", NULL
, offsetof(RunContext
, inactive_enter_usec
) },
995 { "Result", "s", NULL
, offsetof(RunContext
, result
) },
996 { "ExecMainCode", "i", NULL
, offsetof(RunContext
, exit_code
) },
997 { "ExecMainStatus", "i", NULL
, offsetof(RunContext
, exit_status
) },
998 { "CPUUsageNSec", "t", NULL
, offsetof(RunContext
, cpu_usage_nsec
) },
999 { "IPIngressBytes", "t", NULL
, offsetof(RunContext
, ip_ingress_bytes
) },
1000 { "IPEgressBytes", "t", NULL
, offsetof(RunContext
, ip_egress_bytes
) },
1001 { "IOReadBytes", "t", NULL
, offsetof(RunContext
, io_read_bytes
) },
1002 { "IOWriteBytes", "t", NULL
, offsetof(RunContext
, io_write_bytes
) },
1006 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1009 r
= bus_map_all_properties(c
->bus
,
1010 "org.freedesktop.systemd1",
1018 sd_event_exit(c
->event
, EXIT_FAILURE
);
1019 return log_error_errno(r
, "Failed to query unit state: %s", bus_error_message(&error
, r
));
1022 run_context_check_done(c
);
1026 static int on_properties_changed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1027 RunContext
*c
= userdata
;
1032 return run_context_update(c
, sd_bus_message_get_path(m
));
1035 static int pty_forward_handler(PTYForward
*f
, int rcode
, void *userdata
) {
1036 RunContext
*c
= userdata
;
1041 sd_event_exit(c
->event
, EXIT_FAILURE
);
1042 return log_error_errno(rcode
, "Error on PTY forwarding logic: %m");
1045 run_context_check_done(c
);
1049 static int start_transient_service(
1053 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1054 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1055 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1056 _cleanup_free_
char *service
= NULL
, *pty_path
= NULL
;
1057 _cleanup_close_
int master
= -1;
1063 if (arg_stdio
== ARG_STDIO_PTY
) {
1065 if (arg_transport
== BUS_TRANSPORT_LOCAL
) {
1066 master
= posix_openpt(O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
1068 return log_error_errno(errno
, "Failed to acquire pseudo tty: %m");
1070 r
= ptsname_malloc(master
, &pty_path
);
1072 return log_error_errno(r
, "Failed to determine tty name: %m");
1074 if (unlockpt(master
) < 0)
1075 return log_error_errno(errno
, "Failed to unlock tty: %m");
1077 } else if (arg_transport
== BUS_TRANSPORT_MACHINE
) {
1078 _cleanup_(sd_bus_unrefp
) sd_bus
*system_bus
= NULL
;
1079 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pty_reply
= NULL
;
1082 r
= sd_bus_default_system(&system_bus
);
1084 return log_error_errno(r
, "Failed to connect to system bus: %m");
1086 r
= sd_bus_call_method(system_bus
,
1087 "org.freedesktop.machine1",
1088 "/org/freedesktop/machine1",
1089 "org.freedesktop.machine1.Manager",
1095 return log_error_errno(r
, "Failed to get machine PTY: %s", bus_error_message(&error
, -r
));
1097 r
= sd_bus_message_read(pty_reply
, "hs", &master
, &s
);
1099 return bus_log_parse_error(r
);
1101 master
= fcntl(master
, F_DUPFD_CLOEXEC
, 3);
1103 return log_error_errno(errno
, "Failed to duplicate master fd: %m");
1105 pty_path
= strdup(s
);
1109 assert_not_reached("Can't allocate tty via ssh");
1112 if (!arg_no_block
) {
1113 r
= bus_wait_for_jobs_new(bus
, &w
);
1115 return log_error_errno(r
, "Could not watch jobs: %m");
1119 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1120 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1121 ".service", &service
);
1123 return log_error_errno(r
, "Failed to mangle unit name: %m");
1125 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1130 r
= sd_bus_message_new_method_call(
1133 "org.freedesktop.systemd1",
1134 "/org/freedesktop/systemd1",
1135 "org.freedesktop.systemd1.Manager",
1136 "StartTransientUnit");
1138 return bus_log_create_error(r
);
1140 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1142 return bus_log_create_error(r
);
1145 r
= sd_bus_message_append(m
, "ss", service
, "fail");
1147 return bus_log_create_error(r
);
1150 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1152 return bus_log_create_error(r
);
1154 r
= transient_service_set_properties(m
, pty_path
);
1158 r
= sd_bus_message_close_container(m
);
1160 return bus_log_create_error(r
);
1162 /* Auxiliary units */
1163 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1165 return bus_log_create_error(r
);
1167 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1169 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1171 return log_error_errno(r
, "Failed to start transient service unit: %s", bus_error_message(&error
, r
));
1176 r
= sd_bus_message_read(reply
, "o", &object
);
1178 return bus_log_parse_error(r
);
1180 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1186 log_info("Running as unit: %s", service
);
1188 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
) {
1189 _cleanup_(run_context_free
) RunContext c
= {
1190 .cpu_usage_nsec
= NSEC_INFINITY
,
1191 .ip_ingress_bytes
= UINT64_MAX
,
1192 .ip_egress_bytes
= UINT64_MAX
,
1193 .io_read_bytes
= UINT64_MAX
,
1194 .io_write_bytes
= UINT64_MAX
,
1195 .inactive_exit_usec
= USEC_INFINITY
,
1196 .inactive_enter_usec
= USEC_INFINITY
,
1198 _cleanup_free_
char *path
= NULL
;
1200 c
.bus
= sd_bus_ref(bus
);
1202 r
= sd_event_default(&c
.event
);
1204 return log_error_errno(r
, "Failed to get event loop: %m");
1207 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGWINCH
, SIGTERM
, SIGINT
, -1) >= 0);
1208 (void) sd_event_add_signal(c
.event
, NULL
, SIGINT
, NULL
, NULL
);
1209 (void) sd_event_add_signal(c
.event
, NULL
, SIGTERM
, NULL
, NULL
);
1212 log_info("Press ^] three times within 1s to disconnect TTY.");
1214 r
= pty_forward_new(c
.event
, master
, PTY_FORWARD_IGNORE_INITIAL_VHANGUP
, &c
.forward
);
1216 return log_error_errno(r
, "Failed to create PTY forwarder: %m");
1218 pty_forward_set_handler(c
.forward
, pty_forward_handler
, &c
);
1220 /* Make sure to process any TTY events before we process bus events */
1221 (void) pty_forward_set_priority(c
.forward
, SD_EVENT_PRIORITY_IMPORTANT
);
1224 path
= unit_dbus_path_from_name(service
);
1228 r
= sd_bus_match_signal_async(
1231 "org.freedesktop.systemd1",
1233 "org.freedesktop.DBus.Properties",
1234 "PropertiesChanged",
1235 on_properties_changed
, NULL
, &c
);
1237 return log_error_errno(r
, "Failed to request properties changed signal match: %m");
1239 r
= sd_bus_attach_event(bus
, c
.event
, SD_EVENT_PRIORITY_NORMAL
);
1241 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
1243 r
= run_context_update(&c
, path
);
1247 r
= sd_event_loop(c
.event
);
1249 return log_error_errno(r
, "Failed to run event loop: %m");
1254 r
= pty_forward_get_last_char(c
.forward
, &last_char
);
1255 if (r
>= 0 && !arg_quiet
&& last_char
!= '\n')
1256 fputc('\n', stdout
);
1259 if (arg_wait
&& !arg_quiet
) {
1261 /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, with its
1262 * original settings (i.e. proper line breaks), so that we can show the summary in a pretty
1264 c
.forward
= pty_forward_free(c
.forward
);
1266 if (!isempty(c
.result
))
1267 log_info("Finished with result: %s", strna(c
.result
));
1269 if (c
.exit_code
== CLD_EXITED
)
1270 log_info("Main processes terminated with: code=%s/status=%i",
1271 sigchld_code_to_string(c
.exit_code
), c
.exit_status
);
1272 else if (c
.exit_code
> 0)
1273 log_info("Main processes terminated with: code=%s/status=%s",
1274 sigchld_code_to_string(c
.exit_code
), signal_to_string(c
.exit_status
));
1276 if (timestamp_is_set(c
.inactive_enter_usec
) &&
1277 timestamp_is_set(c
.inactive_exit_usec
) &&
1278 c
.inactive_enter_usec
> c
.inactive_exit_usec
) {
1279 char ts
[FORMAT_TIMESPAN_MAX
];
1280 log_info("Service runtime: %s",
1281 format_timespan(ts
, sizeof ts
, c
.inactive_enter_usec
- c
.inactive_exit_usec
, USEC_PER_MSEC
));
1284 if (c
.cpu_usage_nsec
!= NSEC_INFINITY
) {
1285 char ts
[FORMAT_TIMESPAN_MAX
];
1286 log_info("CPU time consumed: %s",
1287 format_timespan(ts
, sizeof ts
, (c
.cpu_usage_nsec
+ NSEC_PER_USEC
- 1) / NSEC_PER_USEC
, USEC_PER_MSEC
));
1290 if (c
.ip_ingress_bytes
!= UINT64_MAX
) {
1291 char bytes
[FORMAT_BYTES_MAX
];
1292 log_info("IP traffic received: %s", format_bytes(bytes
, sizeof bytes
, c
.ip_ingress_bytes
));
1294 if (c
.ip_egress_bytes
!= UINT64_MAX
) {
1295 char bytes
[FORMAT_BYTES_MAX
];
1296 log_info("IP traffic sent: %s", format_bytes(bytes
, sizeof bytes
, c
.ip_egress_bytes
));
1298 if (c
.io_read_bytes
!= UINT64_MAX
) {
1299 char bytes
[FORMAT_BYTES_MAX
];
1300 log_info("IO bytes read: %s", format_bytes(bytes
, sizeof bytes
, c
.io_read_bytes
));
1302 if (c
.io_write_bytes
!= UINT64_MAX
) {
1303 char bytes
[FORMAT_BYTES_MAX
];
1304 log_info("IO bytes written: %s", format_bytes(bytes
, sizeof bytes
, c
.io_write_bytes
));
1308 /* Try to propagate the service's return value. But if the service defines
1309 * e.g. SuccessExitStatus, honour this, and return 0 to mean "success". */
1310 if (streq_ptr(c
.result
, "success"))
1312 else if (streq_ptr(c
.result
, "exit-code") && c
.exit_status
> 0)
1313 *retval
= c
.exit_status
;
1314 else if (streq_ptr(c
.result
, "signal"))
1315 *retval
= EXIT_EXCEPTION
;
1317 *retval
= EXIT_FAILURE
;
1323 static int acquire_invocation_id(sd_bus
*bus
, sd_id128_t
*ret
) {
1324 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1325 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1333 r
= sd_bus_get_property(bus
,
1334 "org.freedesktop.systemd1",
1335 "/org/freedesktop/systemd1/unit/self",
1336 "org.freedesktop.systemd1.Unit",
1342 return log_error_errno(r
, "Failed to request invocation ID for scope: %s", bus_error_message(&error
, r
));
1344 r
= sd_bus_message_read_array(reply
, 'y', &p
, &l
);
1346 return bus_log_parse_error(r
);
1348 if (l
!= sizeof(sd_id128_t
))
1349 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid UUID size, %zu != %zu.", l
, sizeof(sd_id128_t
));
1355 static int start_transient_scope(sd_bus
*bus
) {
1356 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1357 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1358 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1359 _cleanup_strv_free_
char **env
= NULL
, **user_env
= NULL
;
1360 _cleanup_free_
char *scope
= NULL
;
1361 const char *object
= NULL
;
1362 sd_id128_t invocation_id
;
1366 assert(!strv_isempty(arg_cmdline
));
1368 r
= bus_wait_for_jobs_new(bus
, &w
);
1373 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1374 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1377 return log_error_errno(r
, "Failed to mangle scope name: %m");
1379 r
= make_unit_name(bus
, UNIT_SCOPE
, &scope
);
1384 r
= sd_bus_message_new_method_call(
1387 "org.freedesktop.systemd1",
1388 "/org/freedesktop/systemd1",
1389 "org.freedesktop.systemd1.Manager",
1390 "StartTransientUnit");
1392 return bus_log_create_error(r
);
1394 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1396 return bus_log_create_error(r
);
1399 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
1401 return bus_log_create_error(r
);
1404 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1406 return bus_log_create_error(r
);
1408 r
= transient_scope_set_properties(m
);
1412 r
= sd_bus_message_close_container(m
);
1414 return bus_log_create_error(r
);
1416 /* Auxiliary units */
1417 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1419 return bus_log_create_error(r
);
1421 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1423 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1425 return log_error_errno(r
, "Failed to start transient scope unit: %s", bus_error_message(&error
, -r
));
1427 r
= sd_bus_message_read(reply
, "o", &object
);
1429 return bus_log_parse_error(r
);
1431 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1435 r
= acquire_invocation_id(bus
, &invocation_id
);
1439 r
= strv_extendf(&user_env
, "INVOCATION_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(invocation_id
));
1444 if (setpriority(PRIO_PROCESS
, 0, arg_nice
) < 0)
1445 return log_error_errno(errno
, "Failed to set nice level: %m");
1448 if (arg_exec_group
) {
1451 r
= get_group_creds(&arg_exec_group
, &gid
, 0);
1453 return log_error_errno(r
, "Failed to resolve group %s: %m", arg_exec_group
);
1455 if (setresgid(gid
, gid
, gid
) < 0)
1456 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1459 if (arg_exec_user
) {
1460 const char *home
, *shell
;
1464 r
= get_user_creds(&arg_exec_user
, &uid
, &gid
, &home
, &shell
, USER_CREDS_CLEAN
|USER_CREDS_PREFER_NSS
);
1466 return log_error_errno(r
, "Failed to resolve user %s: %m", arg_exec_user
);
1469 r
= strv_extendf(&user_env
, "HOME=%s", home
);
1475 r
= strv_extendf(&user_env
, "SHELL=%s", shell
);
1480 r
= strv_extendf(&user_env
, "USER=%s", arg_exec_user
);
1484 r
= strv_extendf(&user_env
, "LOGNAME=%s", arg_exec_user
);
1488 if (!arg_exec_group
) {
1489 if (setresgid(gid
, gid
, gid
) < 0)
1490 return log_error_errno(errno
, "Failed to change GID to " GID_FMT
": %m", gid
);
1493 if (setresuid(uid
, uid
, uid
) < 0)
1494 return log_error_errno(errno
, "Failed to change UID to " UID_FMT
": %m", uid
);
1497 env
= strv_env_merge(3, environ
, user_env
, arg_environment
);
1502 log_info("Running scope as unit: %s", scope
);
1504 execvpe(arg_cmdline
[0], arg_cmdline
, env
);
1506 return log_error_errno(errno
, "Failed to execute: %m");
1509 static int start_transient_trigger(
1511 const char *suffix
) {
1513 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1514 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1515 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
1516 _cleanup_free_
char *trigger
= NULL
, *service
= NULL
;
1517 const char *object
= NULL
;
1522 r
= bus_wait_for_jobs_new(bus
, &w
);
1527 switch (unit_name_to_type(arg_unit
)) {
1530 service
= strdup(arg_unit
);
1534 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1536 return log_error_errno(r
, "Failed to change unit suffix: %m");
1540 trigger
= strdup(arg_unit
);
1544 r
= unit_name_change_suffix(trigger
, ".service", &service
);
1546 return log_error_errno(r
, "Failed to change unit suffix: %m");
1550 r
= unit_name_mangle_with_suffix(arg_unit
, "as unit",
1551 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1552 ".service", &service
);
1554 return log_error_errno(r
, "Failed to mangle unit name: %m");
1556 r
= unit_name_mangle_with_suffix(arg_unit
, "as trigger",
1557 arg_quiet
? 0 : UNIT_NAME_MANGLE_WARN
,
1560 return log_error_errno(r
, "Failed to mangle unit name: %m");
1565 r
= make_unit_name(bus
, UNIT_SERVICE
, &service
);
1569 r
= unit_name_change_suffix(service
, suffix
, &trigger
);
1571 return log_error_errno(r
, "Failed to change unit suffix: %m");
1574 r
= sd_bus_message_new_method_call(
1577 "org.freedesktop.systemd1",
1578 "/org/freedesktop/systemd1",
1579 "org.freedesktop.systemd1.Manager",
1580 "StartTransientUnit");
1582 return bus_log_create_error(r
);
1584 r
= sd_bus_message_set_allow_interactive_authorization(m
, arg_ask_password
);
1586 return bus_log_create_error(r
);
1589 r
= sd_bus_message_append(m
, "ss", trigger
, "fail");
1591 return bus_log_create_error(r
);
1594 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1596 return bus_log_create_error(r
);
1598 if (streq(suffix
, ".path"))
1599 r
= transient_unit_set_properties(m
, UNIT_PATH
, arg_path_property
);
1600 else if (streq(suffix
, ".socket"))
1601 r
= transient_unit_set_properties(m
, UNIT_SOCKET
, arg_socket_property
);
1602 else if (streq(suffix
, ".timer"))
1603 r
= transient_timer_set_properties(m
);
1605 assert_not_reached("Invalid suffix");
1609 r
= sd_bus_message_close_container(m
);
1611 return bus_log_create_error(r
);
1613 r
= sd_bus_message_open_container(m
, 'a', "(sa(sv))");
1615 return bus_log_create_error(r
);
1617 if (!strv_isempty(arg_cmdline
)) {
1618 r
= sd_bus_message_open_container(m
, 'r', "sa(sv)");
1620 return bus_log_create_error(r
);
1622 r
= sd_bus_message_append(m
, "s", service
);
1624 return bus_log_create_error(r
);
1626 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1628 return bus_log_create_error(r
);
1630 r
= transient_service_set_properties(m
, NULL
);
1634 r
= sd_bus_message_close_container(m
);
1636 return bus_log_create_error(r
);
1638 r
= sd_bus_message_close_container(m
);
1640 return bus_log_create_error(r
);
1643 r
= sd_bus_message_close_container(m
);
1645 return bus_log_create_error(r
);
1647 polkit_agent_open_if_enabled(arg_transport
, arg_ask_password
);
1649 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
1651 return log_error_errno(r
, "Failed to start transient %s unit: %s", suffix
+ 1, bus_error_message(&error
, -r
));
1653 r
= sd_bus_message_read(reply
, "o", &object
);
1655 return bus_log_parse_error(r
);
1657 r
= bus_wait_for_jobs_one(w
, object
, arg_quiet
);
1662 log_info("Running %s as unit: %s", suffix
+ 1, trigger
);
1663 if (!strv_isempty(arg_cmdline
))
1664 log_info("Will run service as unit: %s", service
);
1670 static int run(int argc
, char* argv
[]) {
1671 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
1672 _cleanup_free_
char *description
= NULL
;
1673 int r
, retval
= EXIT_SUCCESS
;
1675 log_show_color(true);
1676 log_parse_environment();
1679 r
= parse_argv(argc
, argv
);
1683 if (!strv_isempty(arg_cmdline
) && arg_transport
== BUS_TRANSPORT_LOCAL
) {
1684 _cleanup_free_
char *command
= NULL
;
1686 /* Patch in an absolute path */
1688 r
= find_binary(arg_cmdline
[0], &command
);
1690 return log_error_errno(r
, "Failed to find executable %s: %m", arg_cmdline
[0]);
1692 free_and_replace(arg_cmdline
[0], command
);
1695 if (!arg_description
) {
1696 description
= strv_join(arg_cmdline
, " ");
1700 if (arg_unit
&& isempty(description
)) {
1701 r
= free_and_strdup(&description
, arg_unit
);
1706 arg_description
= description
;
1709 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1711 if (arg_wait
|| arg_stdio
!= ARG_STDIO_NONE
)
1712 r
= bus_connect_transport(arg_transport
, arg_host
, arg_user
, &bus
);
1714 r
= bus_connect_transport_systemd(arg_transport
, arg_host
, arg_user
, &bus
);
1716 return log_error_errno(r
, "Failed to create bus connection: %m");
1719 r
= start_transient_scope(bus
);
1720 else if (arg_path_property
)
1721 r
= start_transient_trigger(bus
, ".path");
1722 else if (arg_socket_property
)
1723 r
= start_transient_trigger(bus
, ".socket");
1724 else if (arg_with_timer
)
1725 r
= start_transient_trigger(bus
, ".timer");
1727 r
= start_transient_service(bus
, &retval
);
1734 DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run
);