]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c2756a68 | 2 | |
6c12b52e | 3 | #include <getopt.h> |
3f6fd1ba | 4 | #include <stdio.h> |
ca78ad1d ZJS |
5 | #include <fcntl.h> |
6 | #include <sys/stat.h> | |
7 | #include <sys/types.h> | |
c2756a68 LP |
8 | |
9 | #include "sd-bus.h" | |
9b15b784 | 10 | #include "sd-event.h" |
3f6fd1ba | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
3f6fd1ba | 13 | #include "bus-error.h" |
9b71e4ab | 14 | #include "bus-locator.h" |
807542be | 15 | #include "bus-map-properties.h" |
20b16441 | 16 | #include "bus-unit-util.h" |
e45c81b8 | 17 | #include "bus-wait-for-jobs.h" |
3f6fd1ba | 18 | #include "calendarspec.h" |
4de33e7f | 19 | #include "env-util.h" |
7f3614e5 | 20 | #include "exit-status.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
f97b34a6 | 22 | #include "format-util.h" |
ef08ad7a | 23 | #include "main-func.h" |
614b022c | 24 | #include "parse-argument.h" |
cf0fbc49 | 25 | #include "parse-util.h" |
4bcc8c3c | 26 | #include "path-util.h" |
294bf0c3 | 27 | #include "pretty-print.h" |
2a453c2e | 28 | #include "process-util.h" |
9b15b784 | 29 | #include "ptyfwd.h" |
24882e06 | 30 | #include "signal-util.h" |
8c7db2fb | 31 | #include "spawn-polkit-agent.h" |
3f6fd1ba | 32 | #include "strv.h" |
66cb2fde | 33 | #include "terminal-util.h" |
89ada3ba | 34 | #include "unit-def.h" |
3f6fd1ba | 35 | #include "unit-name.h" |
b1d4f8e1 | 36 | #include "user-util.h" |
c2756a68 | 37 | |
8c7db2fb | 38 | static bool arg_ask_password = true; |
6c12b52e | 39 | static bool arg_scope = false; |
6577c7ce | 40 | static bool arg_remain_after_exit = false; |
3d161f99 | 41 | static bool arg_no_block = false; |
2a453c2e | 42 | static bool arg_wait = false; |
6c12b52e | 43 | static const char *arg_unit = NULL; |
9f2e86af | 44 | static const char *arg_description = NULL; |
c221420b | 45 | static const char *arg_slice = NULL; |
2c7039b3 | 46 | static bool arg_slice_inherit = false; |
a6c0353b | 47 | static bool arg_send_sighup = false; |
d21ed1ea | 48 | static BusTransport arg_transport = BUS_TRANSPORT_LOCAL; |
1ac67edb | 49 | static const char *arg_host = NULL; |
d21ed1ea | 50 | static bool arg_user = false; |
c7040b5d LP |
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; | |
df31a6c0 | 57 | static char **arg_property = NULL; |
5dca7739 LP |
58 | static enum { |
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; | |
d59ef3e2 YW |
64 | static char **arg_path_property = NULL; |
65 | static char **arg_socket_property = NULL; | |
4c213d6c | 66 | static char **arg_timer_property = NULL; |
787be190 | 67 | static bool arg_with_timer = false; |
095dc596 | 68 | static bool arg_quiet = false; |
fe9d0be9 | 69 | static bool arg_aggressive_gc = false; |
2d21165a | 70 | static char *arg_working_directory = NULL; |
badd28e1 LP |
71 | static bool arg_shell = false; |
72 | static char **arg_cmdline = NULL; | |
c2756a68 | 73 | |
0565120f LP |
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); | |
81 | ||
37ec0fdd LP |
82 | static int help(void) { |
83 | _cleanup_free_ char *link = NULL; | |
84 | int r; | |
85 | ||
86 | r = terminal_urlify_man("systemd-run", "1", &link); | |
87 | if (r < 0) | |
88 | return log_oom(); | |
89 | ||
353b2baa LP |
90 | printf("%s [OPTIONS...] COMMAND [ARGUMENTS...]\n" |
91 | "\n%sRun the specified command in a transient scope or service.%s\n\n" | |
4c213d6c WC |
92 | " -h --help Show this help\n" |
93 | " --version Show package version\n" | |
8c7db2fb | 94 | " --no-ask-password Do not prompt for password\n" |
4c213d6c WC |
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" | |
7976b9f0 | 99 | " -u --unit=UNIT Run under the specified unit name\n" |
ad2c1701 | 100 | " -p --property=NAME=VALUE Set service or scope unit property\n" |
4c213d6c WC |
101 | " --description=TEXT Description for unit\n" |
102 | " --slice=SLICE Run in the specified slice\n" | |
2c7039b3 | 103 | " --slice-inherit Inherit the slice\n" |
3d161f99 | 104 | " --no-block Do not wait until operation finished\n" |
4c213d6c | 105 | " -r --remain-after-exit Leave service around until explicitly stopped\n" |
2a453c2e | 106 | " --wait Wait until service stopped again\n" |
4c213d6c WC |
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" | |
2d21165a LP |
112 | " --working-directory=PATH Set working directory\n" |
113 | " -d --same-dir Inherit working directory from caller\n" | |
0337b3d5 | 114 | " -E --setenv=NAME[=VALUE] Set environment variable\n" |
5dca7739 LP |
115 | " -t --pty Run service on pseudo TTY as STDIN/STDOUT/\n" |
116 | " STDERR\n" | |
117 | " -P --pipe Pass STDIN/STDOUT/STDERR directly to service\n" | |
fe9d0be9 | 118 | " -q --quiet Suppress information messages during runtime\n" |
badd28e1 LP |
119 | " -G --collect Unload unit after it ran, even when failed\n" |
120 | " -S --shell Invoke a $SHELL interactively\n\n" | |
d59ef3e2 YW |
121 | "Path options:\n" |
122 | " --path-property=NAME=VALUE Set path unit property\n\n" | |
123 | "Socket options:\n" | |
124 | " --socket-property=NAME=VALUE Set socket unit property\n\n" | |
ad2c1701 | 125 | "Timer options:\n" |
b57b0625 ZJS |
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" | |
4c213d6c | 131 | " --on-calendar=SPEC Realtime timer\n" |
efebb613 LP |
132 | " --on-timezone-change Run when the timezone changes\n" |
133 | " --on-clock-change Run when the realtime clock jumps\n" | |
ad2c1701 | 134 | " --timer-property=NAME=VALUE Set timer unit property\n" |
bc556335 DDM |
135 | "\nSee the %s for details.\n", |
136 | program_invocation_short_name, | |
137 | ansi_highlight(), | |
138 | ansi_normal(), | |
139 | link); | |
37ec0fdd LP |
140 | |
141 | return 0; | |
6c12b52e LP |
142 | } |
143 | ||
f2b9f2c8 | 144 | static int add_timer_property(const char *name, const char *val) { |
18939728 | 145 | char *p; |
f2b9f2c8 YW |
146 | |
147 | assert(name); | |
148 | assert(val); | |
149 | ||
150 | p = strjoin(name, "=", val); | |
151 | if (!p) | |
152 | return log_oom(); | |
153 | ||
154 | if (strv_consume(&arg_timer_property, p) < 0) | |
155 | return log_oom(); | |
156 | ||
f2b9f2c8 | 157 | return 0; |
4c213d6c WC |
158 | } |
159 | ||
6c12b52e LP |
160 | static int parse_argv(int argc, char *argv[]) { |
161 | ||
162 | enum { | |
163 | ARG_VERSION = 0x100, | |
164 | ARG_USER, | |
66b1e746 | 165 | ARG_SYSTEM, |
6c12b52e | 166 | ARG_SCOPE, |
c221420b | 167 | ARG_DESCRIPTION, |
a6c0353b | 168 | ARG_SLICE, |
2c7039b3 | 169 | ARG_SLICE_INHERIT, |
a6c0353b | 170 | ARG_SEND_SIGHUP, |
5cfde70c | 171 | ARG_SERVICE_TYPE, |
c7040b5d LP |
172 | ARG_EXEC_USER, |
173 | ARG_EXEC_GROUP, | |
c7040b5d | 174 | ARG_NICE, |
4c213d6c WC |
175 | ARG_ON_ACTIVE, |
176 | ARG_ON_BOOT, | |
177 | ARG_ON_STARTUP, | |
178 | ARG_ON_UNIT_ACTIVE, | |
179 | ARG_ON_UNIT_INACTIVE, | |
180 | ARG_ON_CALENDAR, | |
efebb613 LP |
181 | ARG_ON_TIMEZONE_CHANGE, |
182 | ARG_ON_CLOCK_CHANGE, | |
3d161f99 | 183 | ARG_TIMER_PROPERTY, |
d59ef3e2 YW |
184 | ARG_PATH_PROPERTY, |
185 | ARG_SOCKET_PROPERTY, | |
3d161f99 | 186 | ARG_NO_BLOCK, |
5cfde70c | 187 | ARG_NO_ASK_PASSWORD, |
2a453c2e | 188 | ARG_WAIT, |
2d21165a | 189 | ARG_WORKING_DIRECTORY, |
badd28e1 | 190 | ARG_SHELL, |
6c12b52e LP |
191 | }; |
192 | ||
193 | static const struct option options[] = { | |
2d21165a LP |
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 }, | |
7976b9f0 | 199 | { "unit", required_argument, NULL, 'u' }, |
2d21165a LP |
200 | { "description", required_argument, NULL, ARG_DESCRIPTION }, |
201 | { "slice", required_argument, NULL, ARG_SLICE }, | |
2c7039b3 | 202 | { "slice-inherit", no_argument, NULL, ARG_SLICE_INHERIT }, |
2d21165a LP |
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 }, | |
efebb613 LP |
224 | { "on-timezone-change",no_argument, NULL, ARG_ON_TIMEZONE_CHANGE}, |
225 | { "on-clock-change", no_argument, NULL, ARG_ON_CLOCK_CHANGE }, | |
2d21165a LP |
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' }, | |
badd28e1 | 234 | { "shell", no_argument, NULL, 'S' }, |
eb9da376 | 235 | {}, |
6c12b52e LP |
236 | }; |
237 | ||
d59ef3e2 | 238 | bool with_trigger = false; |
c7040b5d | 239 | int r, c; |
6c12b52e LP |
240 | |
241 | assert(argc >= 0); | |
242 | assert(argv); | |
243 | ||
7976b9f0 | 244 | while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdSu:", options, NULL)) >= 0) |
6c12b52e LP |
245 | |
246 | switch (c) { | |
247 | ||
248 | case 'h': | |
37ec0fdd | 249 | return help(); |
6c12b52e | 250 | |
ad2c1701 LP |
251 | case ARG_VERSION: |
252 | return version(); | |
253 | ||
8c7db2fb EV |
254 | case ARG_NO_ASK_PASSWORD: |
255 | arg_ask_password = false; | |
256 | break; | |
257 | ||
6c12b52e LP |
258 | case ARG_USER: |
259 | arg_user = true; | |
260 | break; | |
261 | ||
66b1e746 LP |
262 | case ARG_SYSTEM: |
263 | arg_user = false; | |
264 | break; | |
265 | ||
6c12b52e LP |
266 | case ARG_SCOPE: |
267 | arg_scope = true; | |
268 | break; | |
269 | ||
7976b9f0 | 270 | case 'u': |
6c12b52e LP |
271 | arg_unit = optarg; |
272 | break; | |
273 | ||
9f2e86af LP |
274 | case ARG_DESCRIPTION: |
275 | arg_description = optarg; | |
276 | break; | |
277 | ||
c221420b LP |
278 | case ARG_SLICE: |
279 | arg_slice = optarg; | |
280 | break; | |
281 | ||
2c7039b3 MAL |
282 | case ARG_SLICE_INHERIT: |
283 | arg_slice_inherit = true; | |
284 | break; | |
285 | ||
a6c0353b LP |
286 | case ARG_SEND_SIGHUP: |
287 | arg_send_sighup = true; | |
288 | break; | |
289 | ||
6577c7ce LP |
290 | case 'r': |
291 | arg_remain_after_exit = true; | |
292 | break; | |
293 | ||
d21ed1ea LP |
294 | case 'H': |
295 | arg_transport = BUS_TRANSPORT_REMOTE; | |
296 | arg_host = optarg; | |
297 | break; | |
298 | ||
299 | case 'M': | |
de33fc62 | 300 | arg_transport = BUS_TRANSPORT_MACHINE; |
d21ed1ea LP |
301 | arg_host = optarg; |
302 | break; | |
303 | ||
c7040b5d LP |
304 | case ARG_SERVICE_TYPE: |
305 | arg_service_type = optarg; | |
306 | break; | |
307 | ||
308 | case ARG_EXEC_USER: | |
309 | arg_exec_user = optarg; | |
310 | break; | |
311 | ||
312 | case ARG_EXEC_GROUP: | |
313 | arg_exec_group = optarg; | |
314 | break; | |
315 | ||
316 | case ARG_NICE: | |
41bf0590 LP |
317 | r = parse_nice(optarg, &arg_nice); |
318 | if (r < 0) | |
319 | return log_error_errno(r, "Failed to parse nice value: %s", optarg); | |
c7040b5d LP |
320 | |
321 | arg_nice_set = true; | |
322 | break; | |
323 | ||
b5911366 | 324 | case 'E': |
0337b3d5 ZJS |
325 | r = strv_env_replace_strdup_passthrough(&arg_environment, optarg); |
326 | if (r < 0) | |
327 | return log_error_errno(r, "Cannot assign environment variable %s: %m", optarg); | |
c7040b5d LP |
328 | |
329 | break; | |
330 | ||
df31a6c0 | 331 | case 'p': |
df31a6c0 LP |
332 | if (strv_extend(&arg_property, optarg) < 0) |
333 | return log_oom(); | |
334 | ||
335 | break; | |
336 | ||
5dca7739 LP |
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; | |
340 | else | |
341 | arg_stdio = ARG_STDIO_PTY; | |
342 | break; | |
343 | ||
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; | |
347 | else | |
348 | arg_stdio = ARG_STDIO_DIRECT; | |
9b15b784 LP |
349 | break; |
350 | ||
095dc596 LP |
351 | case 'q': |
352 | arg_quiet = true; | |
353 | break; | |
354 | ||
4c213d6c | 355 | case ARG_ON_ACTIVE: |
f2b9f2c8 YW |
356 | r = add_timer_property("OnActiveSec", optarg); |
357 | if (r < 0) | |
4c213d6c | 358 | return r; |
4c213d6c | 359 | |
787be190 | 360 | arg_with_timer = true; |
4c213d6c WC |
361 | break; |
362 | ||
363 | case ARG_ON_BOOT: | |
f2b9f2c8 YW |
364 | r = add_timer_property("OnBootSec", optarg); |
365 | if (r < 0) | |
4c213d6c | 366 | return r; |
4c213d6c | 367 | |
787be190 | 368 | arg_with_timer = true; |
4c213d6c WC |
369 | break; |
370 | ||
371 | case ARG_ON_STARTUP: | |
f2b9f2c8 YW |
372 | r = add_timer_property("OnStartupSec", optarg); |
373 | if (r < 0) | |
4c213d6c | 374 | return r; |
4c213d6c | 375 | |
787be190 | 376 | arg_with_timer = true; |
4c213d6c WC |
377 | break; |
378 | ||
379 | case ARG_ON_UNIT_ACTIVE: | |
f2b9f2c8 YW |
380 | r = add_timer_property("OnUnitActiveSec", optarg); |
381 | if (r < 0) | |
4c213d6c | 382 | return r; |
4c213d6c | 383 | |
787be190 | 384 | arg_with_timer = true; |
4c213d6c WC |
385 | break; |
386 | ||
387 | case ARG_ON_UNIT_INACTIVE: | |
f2b9f2c8 YW |
388 | r = add_timer_property("OnUnitInactiveSec", optarg); |
389 | if (r < 0) | |
4c213d6c | 390 | return r; |
4c213d6c | 391 | |
787be190 | 392 | arg_with_timer = true; |
4c213d6c WC |
393 | break; |
394 | ||
10434dbd FS |
395 | case ARG_ON_CALENDAR: { |
396 | _cleanup_(calendar_spec_freep) CalendarSpec *cs = NULL; | |
10434dbd | 397 | |
10434dbd FS |
398 | r = calendar_spec_from_string(optarg, &cs); |
399 | if (r < 0) | |
04220fda LP |
400 | return log_error_errno(r, "Failed to parse calendar event specification: %m"); |
401 | ||
7842c5f2 | 402 | /* Let's make sure the given calendar event is not in the past */ |
04220fda LP |
403 | r = calendar_spec_next_usec(cs, now(CLOCK_REALTIME), NULL); |
404 | if (r == -ENOENT) | |
e608bf6f ZJS |
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. */ | |
04220fda LP |
409 | log_warning("Specified calendar expression is in the past, proceeding anyway."); |
410 | else if (r < 0) | |
411 | return log_error_errno(r, "Failed to calculate next time calendar expression elapses: %m"); | |
10434dbd | 412 | |
f2b9f2c8 YW |
413 | r = add_timer_property("OnCalendar", optarg); |
414 | if (r < 0) | |
efebb613 LP |
415 | return r; |
416 | ||
417 | arg_with_timer = true; | |
418 | break; | |
10434dbd | 419 | } |
efebb613 LP |
420 | |
421 | case ARG_ON_TIMEZONE_CHANGE: | |
422 | r = add_timer_property("OnTimezoneChange", "yes"); | |
423 | if (r < 0) | |
424 | return r; | |
425 | ||
426 | arg_with_timer = true; | |
427 | break; | |
428 | ||
429 | case ARG_ON_CLOCK_CHANGE: | |
430 | r = add_timer_property("OnClockChange", "yes"); | |
431 | if (r < 0) | |
4c213d6c | 432 | return r; |
024a8ec1 | 433 | |
787be190 | 434 | arg_with_timer = true; |
4c213d6c WC |
435 | break; |
436 | ||
437 | case ARG_TIMER_PROPERTY: | |
438 | ||
439 | if (strv_extend(&arg_timer_property, optarg) < 0) | |
440 | return log_oom(); | |
441 | ||
787be190 | 442 | arg_with_timer = arg_with_timer || |
49fe5c09 LP |
443 | STARTSWITH_SET(optarg, |
444 | "OnActiveSec=", | |
445 | "OnBootSec=", | |
446 | "OnStartupSec=", | |
447 | "OnUnitActiveSec=", | |
448 | "OnUnitInactiveSec=", | |
449 | "OnCalendar="); | |
4c213d6c WC |
450 | break; |
451 | ||
d59ef3e2 YW |
452 | case ARG_PATH_PROPERTY: |
453 | ||
454 | if (strv_extend(&arg_path_property, optarg) < 0) | |
455 | return log_oom(); | |
456 | ||
457 | break; | |
458 | ||
459 | case ARG_SOCKET_PROPERTY: | |
460 | ||
461 | if (strv_extend(&arg_socket_property, optarg) < 0) | |
462 | return log_oom(); | |
463 | ||
464 | break; | |
465 | ||
3d161f99 LP |
466 | case ARG_NO_BLOCK: |
467 | arg_no_block = true; | |
468 | break; | |
469 | ||
2a453c2e LP |
470 | case ARG_WAIT: |
471 | arg_wait = true; | |
472 | break; | |
473 | ||
2d21165a | 474 | case ARG_WORKING_DIRECTORY: |
614b022c | 475 | r = parse_path_argument(optarg, true, &arg_working_directory); |
2d21165a LP |
476 | if (r < 0) |
477 | return r; | |
478 | ||
479 | break; | |
480 | ||
481 | case 'd': { | |
482 | _cleanup_free_ char *p = NULL; | |
483 | ||
484 | r = safe_getcwd(&p); | |
485 | if (r < 0) | |
486 | return log_error_errno(r, "Failed to get current working directory: %m"); | |
487 | ||
488 | if (empty_or_root(p)) | |
489 | arg_working_directory = mfree(arg_working_directory); | |
490 | else | |
491 | free_and_replace(arg_working_directory, p); | |
492 | break; | |
493 | } | |
494 | ||
fe9d0be9 LP |
495 | case 'G': |
496 | arg_aggressive_gc = true; | |
497 | break; | |
498 | ||
badd28e1 LP |
499 | case 'S': |
500 | arg_shell = true; | |
501 | break; | |
502 | ||
6c12b52e LP |
503 | case '?': |
504 | return -EINVAL; | |
505 | ||
506 | default: | |
04499a70 | 507 | assert_not_reached(); |
6c12b52e | 508 | } |
6c12b52e | 509 | |
966f3a24 LP |
510 | /* If we are talking to the per-user instance PolicyKit isn't going to help */ |
511 | if (arg_user) | |
512 | arg_ask_password = false; | |
513 | ||
787be190 | 514 | with_trigger = !!arg_path_property || !!arg_socket_property || arg_with_timer; |
d59ef3e2 YW |
515 | |
516 | /* currently, only single trigger (path, socket, timer) unit can be created simultaneously */ | |
787be190 | 517 | if ((int) !!arg_path_property + (int) !!arg_socket_property + (int) arg_with_timer > 1) |
baaa35ad ZJS |
518 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
519 | "Only single trigger (path, socket, timer) unit can be created."); | |
5dca7739 | 520 | |
badd28e1 LP |
521 | if (arg_shell) { |
522 | /* If --shell is imply --pty --pipe --same-dir --service-type=exec --wait --collect, unless otherwise | |
523 | * specified. */ | |
524 | ||
525 | if (!arg_scope) { | |
526 | if (arg_stdio == ARG_STDIO_NONE) | |
527 | arg_stdio = ARG_STDIO_AUTO; | |
528 | ||
529 | if (!arg_working_directory) { | |
530 | r = safe_getcwd(&arg_working_directory); | |
531 | if (r < 0) | |
532 | return log_error_errno(r, "Failed to get current working directory: %m"); | |
533 | } | |
534 | ||
535 | if (!arg_service_type) { | |
536 | arg_service_type = strdup("exec"); | |
537 | if (!arg_service_type) | |
538 | return log_oom(); | |
539 | } | |
540 | ||
541 | arg_wait = true; | |
542 | } | |
543 | ||
544 | arg_aggressive_gc = true; | |
545 | } | |
546 | ||
d46b79bb | 547 | if (arg_stdio == ARG_STDIO_AUTO) |
5dca7739 LP |
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) ? | |
552 | ARG_STDIO_PTY : | |
553 | ARG_STDIO_DIRECT; | |
5dca7739 | 554 | |
badd28e1 LP |
555 | if (argc > optind) { |
556 | char **l; | |
557 | ||
558 | if (arg_shell) | |
559 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "If --shell is used, no command line is expected."); | |
560 | ||
561 | l = strv_copy(argv + optind); | |
562 | if (!l) | |
563 | return log_oom(); | |
564 | ||
565 | strv_free_and_replace(arg_cmdline, l); | |
566 | ||
567 | } else if (arg_shell) { | |
568 | _cleanup_free_ char *s = NULL; | |
569 | char **l; | |
570 | ||
571 | r = get_shell(&s); | |
572 | if (r < 0) | |
573 | return log_error_errno(r, "Failed to determine shell: %m"); | |
574 | ||
575 | l = strv_new(s); | |
576 | if (!l) | |
577 | return log_oom(); | |
578 | ||
579 | strv_free_and_replace(arg_cmdline, l); | |
580 | ||
581 | } else if (!arg_unit || !with_trigger) | |
582 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Command line to execute required."); | |
6c12b52e | 583 | |
cbdc2949 | 584 | if (arg_user && arg_transport == BUS_TRANSPORT_REMOTE) |
baaa35ad | 585 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
cbdc2949 | 586 | "Execution in user context is not supported on remote systems."); |
d21ed1ea | 587 | |
cbdc2949 | 588 | if (arg_scope && arg_transport == BUS_TRANSPORT_REMOTE) |
baaa35ad | 589 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
cbdc2949 | 590 | "Scope execution is not supported on remote systems."); |
d21ed1ea | 591 | |
baaa35ad ZJS |
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."); | |
c7040b5d | 595 | |
baaa35ad ZJS |
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."); | |
9b15b784 | 599 | |
baaa35ad ZJS |
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."); | |
024a8ec1 | 603 | |
baaa35ad ZJS |
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."); | |
578c03bc | 607 | |
baaa35ad ZJS |
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."); | |
4c213d6c | 611 | |
787be190 | 612 | if (arg_timer_property && !arg_with_timer) |
baaa35ad ZJS |
613 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
614 | "--timer-property= has no effect without any other timer options."); | |
4c213d6c | 615 | |
2a453c2e | 616 | if (arg_wait) { |
baaa35ad ZJS |
617 | if (arg_no_block) |
618 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
619 | "--wait may not be combined with --no-block."); | |
2a453c2e | 620 | |
baaa35ad ZJS |
621 | if (with_trigger) |
622 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
623 | "--wait may not be combined with path, socket or timer operations."); | |
2a453c2e | 624 | |
baaa35ad ZJS |
625 | if (arg_scope) |
626 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
627 | "--wait may not be combined with --scope."); | |
2a453c2e LP |
628 | } |
629 | ||
6c12b52e LP |
630 | return 1; |
631 | } | |
632 | ||
89ada3ba | 633 | static int transient_unit_set_properties(sd_bus_message *m, UnitType t, char **properties) { |
c2756a68 LP |
634 | int r; |
635 | ||
9b15b784 LP |
636 | r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description); |
637 | if (r < 0) | |
f53bddf3 | 638 | return bus_log_create_error(r); |
9b15b784 | 639 | |
fe9d0be9 LP |
640 | if (arg_aggressive_gc) { |
641 | r = sd_bus_message_append(m, "(sv)", "CollectMode", "s", "inactive-or-failed"); | |
642 | if (r < 0) | |
f53bddf3 | 643 | return bus_log_create_error(r); |
fe9d0be9 LP |
644 | } |
645 | ||
89ada3ba | 646 | r = bus_append_unit_property_assignment_many(m, t, properties); |
8673cf13 LP |
647 | if (r < 0) |
648 | return r; | |
df31a6c0 | 649 | |
9b15b784 LP |
650 | return 0; |
651 | } | |
652 | ||
653 | static int transient_cgroup_set_properties(sd_bus_message *m) { | |
2c7039b3 MAL |
654 | _cleanup_free_ char *name = NULL; |
655 | _cleanup_free_ char *slice = NULL; | |
9b15b784 LP |
656 | int r; |
657 | assert(m); | |
c2756a68 | 658 | |
2c7039b3 MAL |
659 | if (arg_slice_inherit) { |
660 | char *end; | |
c221420b | 661 | |
2c7039b3 MAL |
662 | if (arg_user) |
663 | r = cg_pid_get_user_slice(0, &name); | |
664 | else | |
665 | r = cg_pid_get_slice(0, &name); | |
7410616c | 666 | if (r < 0) |
2c7039b3 | 667 | return log_error_errno(r, "Failed to get PID slice: %m"); |
c221420b | 668 | |
2c7039b3 MAL |
669 | end = endswith(name, ".slice"); |
670 | if (!end) | |
671 | return -ENXIO; | |
672 | *end = 0; | |
c221420b LP |
673 | } |
674 | ||
eda397c9 YW |
675 | if (!isempty(arg_slice) && !strextend_with_separator(&name, "-", arg_slice)) |
676 | return log_oom(); | |
2c7039b3 MAL |
677 | |
678 | if (!name) | |
679 | return 0; | |
680 | ||
681 | r = unit_name_mangle_with_suffix(name, "as slice", | |
682 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
683 | ".slice", &slice); | |
684 | if (r < 0) | |
685 | return log_error_errno(r, "Failed to mangle name '%s': %m", arg_slice); | |
686 | ||
687 | r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice); | |
688 | if (r < 0) | |
689 | return bus_log_create_error(r); | |
690 | ||
9b15b784 LP |
691 | return 0; |
692 | } | |
693 | ||
694 | static int transient_kill_set_properties(sd_bus_message *m) { | |
f53bddf3 LP |
695 | int r; |
696 | ||
9b15b784 LP |
697 | assert(m); |
698 | ||
f53bddf3 LP |
699 | if (arg_send_sighup) { |
700 | r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup); | |
701 | if (r < 0) | |
702 | return bus_log_create_error(r); | |
703 | } | |
704 | ||
705 | return 0; | |
6c12b52e LP |
706 | } |
707 | ||
badd28e1 | 708 | static int transient_service_set_properties(sd_bus_message *m, const char *pty_path) { |
5dca7739 | 709 | bool send_term = false; |
6c12b52e LP |
710 | int r; |
711 | ||
8159d91a LP |
712 | assert(m); |
713 | ||
89ada3ba | 714 | r = transient_unit_set_properties(m, UNIT_SERVICE, arg_property); |
9b15b784 LP |
715 | if (r < 0) |
716 | return r; | |
717 | ||
718 | r = transient_kill_set_properties(m); | |
719 | if (r < 0) | |
720 | return r; | |
721 | ||
722 | r = transient_cgroup_set_properties(m); | |
86b8d289 LP |
723 | if (r < 0) |
724 | return r; | |
725 | ||
5dca7739 | 726 | if (arg_wait || arg_stdio != ARG_STDIO_NONE) { |
2a453c2e LP |
727 | r = sd_bus_message_append(m, "(sv)", "AddRef", "b", 1); |
728 | if (r < 0) | |
f53bddf3 | 729 | return bus_log_create_error(r); |
2a453c2e LP |
730 | } |
731 | ||
df31a6c0 LP |
732 | if (arg_remain_after_exit) { |
733 | r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit); | |
734 | if (r < 0) | |
f53bddf3 | 735 | return bus_log_create_error(r); |
df31a6c0 | 736 | } |
6577c7ce | 737 | |
c7040b5d LP |
738 | if (arg_service_type) { |
739 | r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type); | |
740 | if (r < 0) | |
f53bddf3 | 741 | return bus_log_create_error(r); |
c7040b5d LP |
742 | } |
743 | ||
744 | if (arg_exec_user) { | |
745 | r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user); | |
746 | if (r < 0) | |
f53bddf3 | 747 | return bus_log_create_error(r); |
c7040b5d LP |
748 | } |
749 | ||
750 | if (arg_exec_group) { | |
751 | r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group); | |
752 | if (r < 0) | |
f53bddf3 | 753 | return bus_log_create_error(r); |
c7040b5d LP |
754 | } |
755 | ||
756 | if (arg_nice_set) { | |
757 | r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice); | |
758 | if (r < 0) | |
f53bddf3 | 759 | return bus_log_create_error(r); |
c7040b5d LP |
760 | } |
761 | ||
2d21165a LP |
762 | if (arg_working_directory) { |
763 | r = sd_bus_message_append(m, "(sv)", "WorkingDirectory", "s", arg_working_directory); | |
764 | if (r < 0) | |
765 | return bus_log_create_error(r); | |
766 | } | |
767 | ||
9b15b784 | 768 | if (pty_path) { |
9b15b784 LP |
769 | r = sd_bus_message_append(m, |
770 | "(sv)(sv)(sv)(sv)", | |
771 | "StandardInput", "s", "tty", | |
772 | "StandardOutput", "s", "tty", | |
773 | "StandardError", "s", "tty", | |
774 | "TTYPath", "s", pty_path); | |
775 | if (r < 0) | |
f53bddf3 | 776 | return bus_log_create_error(r); |
9b15b784 | 777 | |
5dca7739 LP |
778 | send_term = true; |
779 | ||
780 | } else if (arg_stdio == ARG_STDIO_DIRECT) { | |
781 | r = sd_bus_message_append(m, | |
782 | "(sv)(sv)(sv)", | |
783 | "StandardInputFileDescriptor", "h", STDIN_FILENO, | |
784 | "StandardOutputFileDescriptor", "h", STDOUT_FILENO, | |
785 | "StandardErrorFileDescriptor", "h", STDERR_FILENO); | |
786 | if (r < 0) | |
f53bddf3 | 787 | return bus_log_create_error(r); |
5dca7739 LP |
788 | |
789 | send_term = isatty(STDIN_FILENO) || isatty(STDOUT_FILENO) || isatty(STDERR_FILENO); | |
790 | } | |
791 | ||
792 | if (send_term) { | |
793 | const char *e; | |
794 | ||
9b15b784 LP |
795 | e = getenv("TERM"); |
796 | if (e) { | |
797 | char *n; | |
798 | ||
63c372cb | 799 | n = strjoina("TERM=", e); |
9b15b784 LP |
800 | r = sd_bus_message_append(m, |
801 | "(sv)", | |
802 | "Environment", "as", 1, n); | |
803 | if (r < 0) | |
f53bddf3 | 804 | return bus_log_create_error(r); |
9b15b784 LP |
805 | } |
806 | } | |
807 | ||
c7040b5d LP |
808 | if (!strv_isempty(arg_environment)) { |
809 | r = sd_bus_message_open_container(m, 'r', "sv"); | |
810 | if (r < 0) | |
f53bddf3 | 811 | return bus_log_create_error(r); |
c7040b5d LP |
812 | |
813 | r = sd_bus_message_append(m, "s", "Environment"); | |
814 | if (r < 0) | |
f53bddf3 | 815 | return bus_log_create_error(r); |
c7040b5d LP |
816 | |
817 | r = sd_bus_message_open_container(m, 'v', "as"); | |
818 | if (r < 0) | |
f53bddf3 | 819 | return bus_log_create_error(r); |
c7040b5d LP |
820 | |
821 | r = sd_bus_message_append_strv(m, arg_environment); | |
822 | if (r < 0) | |
f53bddf3 | 823 | return bus_log_create_error(r); |
c7040b5d LP |
824 | |
825 | r = sd_bus_message_close_container(m); | |
826 | if (r < 0) | |
f53bddf3 | 827 | return bus_log_create_error(r); |
c7040b5d LP |
828 | |
829 | r = sd_bus_message_close_container(m); | |
830 | if (r < 0) | |
f53bddf3 | 831 | return bus_log_create_error(r); |
4c213d6c WC |
832 | } |
833 | ||
834 | /* Exec container */ | |
badd28e1 | 835 | if (!strv_isempty(arg_cmdline)) { |
4c213d6c WC |
836 | r = sd_bus_message_open_container(m, 'r', "sv"); |
837 | if (r < 0) | |
f53bddf3 | 838 | return bus_log_create_error(r); |
4c213d6c WC |
839 | |
840 | r = sd_bus_message_append(m, "s", "ExecStart"); | |
841 | if (r < 0) | |
f53bddf3 | 842 | return bus_log_create_error(r); |
4c213d6c WC |
843 | |
844 | r = sd_bus_message_open_container(m, 'v', "a(sasb)"); | |
845 | if (r < 0) | |
f53bddf3 | 846 | return bus_log_create_error(r); |
4c213d6c WC |
847 | |
848 | r = sd_bus_message_open_container(m, 'a', "(sasb)"); | |
849 | if (r < 0) | |
f53bddf3 | 850 | return bus_log_create_error(r); |
4c213d6c WC |
851 | |
852 | r = sd_bus_message_open_container(m, 'r', "sasb"); | |
853 | if (r < 0) | |
f53bddf3 | 854 | return bus_log_create_error(r); |
4c213d6c | 855 | |
badd28e1 | 856 | r = sd_bus_message_append(m, "s", arg_cmdline[0]); |
4c213d6c | 857 | if (r < 0) |
f53bddf3 | 858 | return bus_log_create_error(r); |
4c213d6c | 859 | |
badd28e1 | 860 | r = sd_bus_message_append_strv(m, arg_cmdline); |
4c213d6c | 861 | if (r < 0) |
f53bddf3 | 862 | return bus_log_create_error(r); |
4c213d6c WC |
863 | |
864 | r = sd_bus_message_append(m, "b", false); | |
865 | if (r < 0) | |
f53bddf3 | 866 | return bus_log_create_error(r); |
4c213d6c WC |
867 | |
868 | r = sd_bus_message_close_container(m); | |
869 | if (r < 0) | |
f53bddf3 | 870 | return bus_log_create_error(r); |
4c213d6c WC |
871 | |
872 | r = sd_bus_message_close_container(m); | |
873 | if (r < 0) | |
f53bddf3 | 874 | return bus_log_create_error(r); |
4c213d6c WC |
875 | |
876 | r = sd_bus_message_close_container(m); | |
877 | if (r < 0) | |
f53bddf3 | 878 | return bus_log_create_error(r); |
4c213d6c WC |
879 | |
880 | r = sd_bus_message_close_container(m); | |
881 | if (r < 0) | |
f53bddf3 | 882 | return bus_log_create_error(r); |
4c213d6c WC |
883 | } |
884 | ||
885 | return 0; | |
886 | } | |
887 | ||
9b15b784 LP |
888 | static int transient_scope_set_properties(sd_bus_message *m) { |
889 | int r; | |
890 | ||
891 | assert(m); | |
892 | ||
89ada3ba | 893 | r = transient_unit_set_properties(m, UNIT_SCOPE, arg_property); |
9b15b784 LP |
894 | if (r < 0) |
895 | return r; | |
896 | ||
897 | r = transient_kill_set_properties(m); | |
898 | if (r < 0) | |
899 | return r; | |
900 | ||
37e605f9 LP |
901 | r = transient_cgroup_set_properties(m); |
902 | if (r < 0) | |
903 | return r; | |
904 | ||
df0ff127 | 905 | r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid_cached()); |
9b15b784 | 906 | if (r < 0) |
f53bddf3 | 907 | return bus_log_create_error(r); |
9b15b784 LP |
908 | |
909 | return 0; | |
910 | } | |
911 | ||
4c213d6c WC |
912 | static int transient_timer_set_properties(sd_bus_message *m) { |
913 | int r; | |
914 | ||
915 | assert(m); | |
916 | ||
89ada3ba | 917 | r = transient_unit_set_properties(m, UNIT_TIMER, arg_timer_property); |
4c213d6c WC |
918 | if (r < 0) |
919 | return r; | |
920 | ||
6348d701 LP |
921 | /* Automatically clean up our transient timers */ |
922 | r = sd_bus_message_append(m, "(sv)", "RemainAfterElapse", "b", false); | |
923 | if (r < 0) | |
f53bddf3 | 924 | return bus_log_create_error(r); |
6348d701 | 925 | |
4c213d6c WC |
926 | return 0; |
927 | } | |
928 | ||
9c8d1e1a LP |
929 | static int make_unit_name(sd_bus *bus, UnitType t, char **ret) { |
930 | const char *unique, *id; | |
931 | char *p; | |
932 | int r; | |
933 | ||
934 | assert(bus); | |
935 | assert(t >= 0); | |
936 | assert(t < _UNIT_TYPE_MAX); | |
937 | ||
938 | r = sd_bus_get_unique_name(bus, &unique); | |
939 | if (r < 0) { | |
940 | sd_id128_t rnd; | |
941 | ||
942 | /* We couldn't get the unique name, which is a pretty | |
943 | * common case if we are connected to systemd | |
944 | * directly. In that case, just pick a random uuid as | |
945 | * name */ | |
946 | ||
947 | r = sd_id128_randomize(&rnd); | |
948 | if (r < 0) | |
949 | return log_error_errno(r, "Failed to generate random run unit name: %m"); | |
950 | ||
951 | if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0) | |
952 | return log_oom(); | |
953 | ||
954 | return 0; | |
955 | } | |
956 | ||
e6283cbf | 957 | /* We managed to get the unique name, then let's use that to name our transient units. */ |
9c8d1e1a | 958 | |
e6283cbf LP |
959 | id = startswith(unique, ":1."); /* let' strip the usual prefix */ |
960 | if (!id) | |
961 | id = startswith(unique, ":"); /* the spec only requires things to start with a colon, hence | |
962 | * let's add a generic fallback for that. */ | |
baaa35ad ZJS |
963 | if (!id) |
964 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
965 | "Unique name %s has unexpected format.", | |
966 | unique); | |
9c8d1e1a | 967 | |
605405c6 | 968 | p = strjoin("run-u", id, ".", unit_type_to_string(t)); |
9c8d1e1a LP |
969 | if (!p) |
970 | return log_oom(); | |
971 | ||
972 | *ret = p; | |
973 | return 0; | |
974 | } | |
975 | ||
2a453c2e LP |
976 | typedef struct RunContext { |
977 | sd_bus *bus; | |
978 | sd_event *event; | |
979 | PTYForward *forward; | |
980 | sd_bus_slot *match; | |
981 | ||
2b5f7089 | 982 | /* Current state of the unit */ |
2a453c2e | 983 | char *active_state; |
2b5f7089 LP |
984 | bool has_job; |
985 | ||
986 | /* The exit data of the unit */ | |
2a453c2e LP |
987 | uint64_t inactive_exit_usec; |
988 | uint64_t inactive_enter_usec; | |
989 | char *result; | |
990 | uint64_t cpu_usage_nsec; | |
655aab20 LP |
991 | uint64_t ip_ingress_bytes; |
992 | uint64_t ip_egress_bytes; | |
826f7cb1 LP |
993 | uint64_t io_read_bytes; |
994 | uint64_t io_write_bytes; | |
2a453c2e LP |
995 | uint32_t exit_code; |
996 | uint32_t exit_status; | |
997 | } RunContext; | |
998 | ||
999 | static void run_context_free(RunContext *c) { | |
1000 | assert(c); | |
1001 | ||
1002 | c->forward = pty_forward_free(c->forward); | |
1003 | c->match = sd_bus_slot_unref(c->match); | |
1004 | c->bus = sd_bus_unref(c->bus); | |
1005 | c->event = sd_event_unref(c->event); | |
1006 | ||
1007 | free(c->active_state); | |
1008 | free(c->result); | |
1009 | } | |
1010 | ||
1011 | static void run_context_check_done(RunContext *c) { | |
95f1d6bf | 1012 | bool done; |
2a453c2e LP |
1013 | |
1014 | assert(c); | |
1015 | ||
1016 | if (c->match) | |
2b5f7089 | 1017 | done = STRPTR_IN_SET(c->active_state, "inactive", "failed") && !c->has_job; |
95f1d6bf LP |
1018 | else |
1019 | done = true; | |
2a453c2e | 1020 | |
95f1d6bf LP |
1021 | if (c->forward && done) /* If the service is gone, it's time to drain the output */ |
1022 | done = pty_forward_drain(c->forward); | |
2a453c2e LP |
1023 | |
1024 | if (done) | |
1025 | sd_event_exit(c->event, EXIT_SUCCESS); | |
1026 | } | |
1027 | ||
2b5f7089 LP |
1028 | static int map_job(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { |
1029 | bool *b = userdata; | |
1030 | const char *job; | |
1031 | uint32_t id; | |
1032 | int r; | |
1033 | ||
1034 | r = sd_bus_message_read(m, "(uo)", &id, &job); | |
1035 | if (r < 0) | |
1036 | return r; | |
1037 | ||
1038 | *b = id != 0 || !streq(job, "/"); | |
1039 | return 0; | |
1040 | } | |
1041 | ||
5b1bad8d | 1042 | static int run_context_update(RunContext *c, const char *path) { |
2a453c2e LP |
1043 | |
1044 | static const struct bus_properties_map map[] = { | |
2b5f7089 LP |
1045 | { "ActiveState", "s", NULL, offsetof(RunContext, active_state) }, |
1046 | { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_exit_usec) }, | |
1047 | { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_enter_usec) }, | |
1048 | { "Result", "s", NULL, offsetof(RunContext, result) }, | |
1049 | { "ExecMainCode", "i", NULL, offsetof(RunContext, exit_code) }, | |
1050 | { "ExecMainStatus", "i", NULL, offsetof(RunContext, exit_status) }, | |
1051 | { "CPUUsageNSec", "t", NULL, offsetof(RunContext, cpu_usage_nsec) }, | |
1052 | { "IPIngressBytes", "t", NULL, offsetof(RunContext, ip_ingress_bytes) }, | |
1053 | { "IPEgressBytes", "t", NULL, offsetof(RunContext, ip_egress_bytes) }, | |
1054 | { "IOReadBytes", "t", NULL, offsetof(RunContext, io_read_bytes) }, | |
1055 | { "IOWriteBytes", "t", NULL, offsetof(RunContext, io_write_bytes) }, | |
1056 | { "Job", "(uo)", map_job, offsetof(RunContext, has_job) }, | |
2a453c2e LP |
1057 | {} |
1058 | }; | |
1059 | ||
f9e0eefc | 1060 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2a453c2e LP |
1061 | int r; |
1062 | ||
1063 | r = bus_map_all_properties(c->bus, | |
1064 | "org.freedesktop.systemd1", | |
5b1bad8d | 1065 | path, |
2a453c2e | 1066 | map, |
a7e4861c | 1067 | BUS_MAP_STRDUP, |
f9e0eefc | 1068 | &error, |
f37f8a61 | 1069 | NULL, |
2a453c2e LP |
1070 | c); |
1071 | if (r < 0) { | |
1072 | sd_event_exit(c->event, EXIT_FAILURE); | |
f9e0eefc | 1073 | return log_error_errno(r, "Failed to query unit state: %s", bus_error_message(&error, r)); |
2a453c2e LP |
1074 | } |
1075 | ||
1076 | run_context_check_done(c); | |
1077 | return 0; | |
1078 | } | |
1079 | ||
5b1bad8d LP |
1080 | static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { |
1081 | RunContext *c = userdata; | |
1082 | ||
1083 | assert(m); | |
1084 | assert(c); | |
1085 | ||
1086 | return run_context_update(c, sd_bus_message_get_path(m)); | |
1087 | } | |
1088 | ||
2a453c2e LP |
1089 | static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) { |
1090 | RunContext *c = userdata; | |
1091 | ||
1092 | assert(f); | |
1093 | ||
1094 | if (rcode < 0) { | |
1095 | sd_event_exit(c->event, EXIT_FAILURE); | |
1096 | return log_error_errno(rcode, "Error on PTY forwarding logic: %m"); | |
1097 | } | |
1098 | ||
1099 | run_context_check_done(c); | |
1100 | return 0; | |
1101 | } | |
1102 | ||
4c213d6c WC |
1103 | static int start_transient_service( |
1104 | sd_bus *bus, | |
2a453c2e | 1105 | int *retval) { |
4c213d6c | 1106 | |
4afd3348 LP |
1107 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; |
1108 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
3d161f99 | 1109 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
ee451d76 | 1110 | _cleanup_free_ char *service = NULL, *pty_path = NULL; |
9b15b784 | 1111 | _cleanup_close_ int master = -1; |
4c213d6c WC |
1112 | int r; |
1113 | ||
1114 | assert(bus); | |
2a453c2e | 1115 | assert(retval); |
4c213d6c | 1116 | |
5dca7739 | 1117 | if (arg_stdio == ARG_STDIO_PTY) { |
9b15b784 | 1118 | |
ee451d76 | 1119 | if (arg_transport == BUS_TRANSPORT_LOCAL) { |
669fc4e5 | 1120 | master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
ee451d76 LP |
1121 | if (master < 0) |
1122 | return log_error_errno(errno, "Failed to acquire pseudo tty: %m"); | |
1123 | ||
1124 | r = ptsname_malloc(master, &pty_path); | |
1125 | if (r < 0) | |
1126 | return log_error_errno(r, "Failed to determine tty name: %m"); | |
1127 | ||
395745ba LP |
1128 | if (unlockpt(master) < 0) |
1129 | return log_error_errno(errno, "Failed to unlock tty: %m"); | |
1130 | ||
de33fc62 | 1131 | } else if (arg_transport == BUS_TRANSPORT_MACHINE) { |
4afd3348 | 1132 | _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL; |
20fc5811 | 1133 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL; |
ee451d76 LP |
1134 | const char *s; |
1135 | ||
024a8ec1 | 1136 | r = sd_bus_default_system(&system_bus); |
ee451d76 | 1137 | if (r < 0) |
024a8ec1 | 1138 | return log_error_errno(r, "Failed to connect to system bus: %m"); |
ee451d76 LP |
1139 | |
1140 | r = sd_bus_call_method(system_bus, | |
1141 | "org.freedesktop.machine1", | |
1142 | "/org/freedesktop/machine1", | |
1143 | "org.freedesktop.machine1.Manager", | |
1144 | "OpenMachinePTY", | |
1145 | &error, | |
20fc5811 | 1146 | &pty_reply, |
ee451d76 | 1147 | "s", arg_host); |
4ae25393 | 1148 | if (r < 0) |
2a03b9ed | 1149 | return log_error_errno(r, "Failed to get machine PTY: %s", bus_error_message(&error, r)); |
ee451d76 | 1150 | |
20fc5811 | 1151 | r = sd_bus_message_read(pty_reply, "hs", &master, &s); |
ee451d76 LP |
1152 | if (r < 0) |
1153 | return bus_log_parse_error(r); | |
1154 | ||
1155 | master = fcntl(master, F_DUPFD_CLOEXEC, 3); | |
1156 | if (master < 0) | |
1157 | return log_error_errno(errno, "Failed to duplicate master fd: %m"); | |
1158 | ||
1159 | pty_path = strdup(s); | |
1160 | if (!pty_path) | |
1161 | return log_oom(); | |
1162 | } else | |
04499a70 | 1163 | assert_not_reached(); |
9b15b784 LP |
1164 | } |
1165 | ||
a7c71d21 LP |
1166 | /* Optionally, wait for the start job to complete. If we are supposed to read the service's stdin |
1167 | * lets skip this however, because we should start that already when the start job is running, and | |
1168 | * there's little point in waiting for the start job to complete in that case anyway, as we'll wait | |
1169 | * for EOF anyway, which is going to be much later. */ | |
1170 | if (!arg_no_block && arg_stdio == ARG_STDIO_NONE) { | |
3d161f99 LP |
1171 | r = bus_wait_for_jobs_new(bus, &w); |
1172 | if (r < 0) | |
1173 | return log_error_errno(r, "Could not watch jobs: %m"); | |
1174 | } | |
1175 | ||
4c213d6c | 1176 | if (arg_unit) { |
df7c4eb6 ZJS |
1177 | r = unit_name_mangle_with_suffix(arg_unit, "as unit", |
1178 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
1179 | ".service", &service); | |
7410616c LP |
1180 | if (r < 0) |
1181 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
9c8d1e1a LP |
1182 | } else { |
1183 | r = make_unit_name(bus, UNIT_SERVICE, &service); | |
1184 | if (r < 0) | |
1185 | return r; | |
1186 | } | |
4c213d6c | 1187 | |
7ad61613 | 1188 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
9f2e86af | 1189 | if (r < 0) |
7040b626 | 1190 | return bus_log_create_error(r); |
9f2e86af | 1191 | |
8c7db2fb EV |
1192 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); |
1193 | if (r < 0) | |
1194 | return bus_log_create_error(r); | |
1195 | ||
9b15b784 | 1196 | /* Name and mode */ |
4c213d6c | 1197 | r = sd_bus_message_append(m, "ss", service, "fail"); |
c2756a68 | 1198 | if (r < 0) |
7040b626 | 1199 | return bus_log_create_error(r); |
c2756a68 | 1200 | |
9b15b784 | 1201 | /* Properties */ |
4c213d6c | 1202 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
c2756a68 | 1203 | if (r < 0) |
7040b626 | 1204 | return bus_log_create_error(r); |
c2756a68 | 1205 | |
badd28e1 | 1206 | r = transient_service_set_properties(m, pty_path); |
c2756a68 | 1207 | if (r < 0) |
f53bddf3 | 1208 | return r; |
c2756a68 | 1209 | |
4c213d6c | 1210 | r = sd_bus_message_close_container(m); |
c2756a68 | 1211 | if (r < 0) |
7040b626 | 1212 | return bus_log_create_error(r); |
c2756a68 | 1213 | |
9b15b784 | 1214 | /* Auxiliary units */ |
4c213d6c | 1215 | r = sd_bus_message_append(m, "a(sa(sv))", 0); |
c2756a68 | 1216 | if (r < 0) |
7040b626 | 1217 | return bus_log_create_error(r); |
c2756a68 | 1218 | |
8a4b13c5 | 1219 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
8c7db2fb | 1220 | |
3d161f99 | 1221 | r = sd_bus_call(bus, m, 0, &error, &reply); |
024a8ec1 LP |
1222 | if (r < 0) |
1223 | return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r)); | |
c2756a68 | 1224 | |
3d161f99 LP |
1225 | if (w) { |
1226 | const char *object; | |
1227 | ||
1228 | r = sd_bus_message_read(reply, "o", &object); | |
1229 | if (r < 0) | |
1230 | return bus_log_parse_error(r); | |
1231 | ||
1232 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
1233 | if (r < 0) | |
1234 | return r; | |
1235 | } | |
1236 | ||
2a453c2e LP |
1237 | if (!arg_quiet) |
1238 | log_info("Running as unit: %s", service); | |
1239 | ||
5dca7739 | 1240 | if (arg_wait || arg_stdio != ARG_STDIO_NONE) { |
655aab20 LP |
1241 | _cleanup_(run_context_free) RunContext c = { |
1242 | .cpu_usage_nsec = NSEC_INFINITY, | |
1243 | .ip_ingress_bytes = UINT64_MAX, | |
1244 | .ip_egress_bytes = UINT64_MAX, | |
826f7cb1 LP |
1245 | .io_read_bytes = UINT64_MAX, |
1246 | .io_write_bytes = UINT64_MAX, | |
655aab20 LP |
1247 | .inactive_exit_usec = USEC_INFINITY, |
1248 | .inactive_enter_usec = USEC_INFINITY, | |
1249 | }; | |
95f1d6bf | 1250 | _cleanup_free_ char *path = NULL; |
4c213d6c | 1251 | |
2a453c2e LP |
1252 | c.bus = sd_bus_ref(bus); |
1253 | ||
1254 | r = sd_event_default(&c.event); | |
4c213d6c | 1255 | if (r < 0) |
9b15b784 | 1256 | return log_error_errno(r, "Failed to get event loop: %m"); |
4c213d6c | 1257 | |
2a453c2e LP |
1258 | if (master >= 0) { |
1259 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0); | |
1260 | (void) sd_event_add_signal(c.event, NULL, SIGINT, NULL, NULL); | |
1261 | (void) sd_event_add_signal(c.event, NULL, SIGTERM, NULL, NULL); | |
1262 | ||
1263 | if (!arg_quiet) | |
1264 | log_info("Press ^] three times within 1s to disconnect TTY."); | |
4c213d6c | 1265 | |
2a453c2e LP |
1266 | r = pty_forward_new(c.event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &c.forward); |
1267 | if (r < 0) | |
1268 | return log_error_errno(r, "Failed to create PTY forwarder: %m"); | |
1269 | ||
1270 | pty_forward_set_handler(c.forward, pty_forward_handler, &c); | |
d147457c LP |
1271 | |
1272 | /* Make sure to process any TTY events before we process bus events */ | |
1273 | (void) pty_forward_set_priority(c.forward, SD_EVENT_PRIORITY_IMPORTANT); | |
2a453c2e | 1274 | } |
4c213d6c | 1275 | |
95f1d6bf LP |
1276 | path = unit_dbus_path_from_name(service); |
1277 | if (!path) | |
1278 | return log_oom(); | |
2a453c2e | 1279 | |
75152a4d LP |
1280 | r = sd_bus_match_signal_async( |
1281 | bus, | |
1282 | &c.match, | |
1283 | "org.freedesktop.systemd1", | |
1284 | path, | |
1285 | "org.freedesktop.DBus.Properties", | |
1286 | "PropertiesChanged", | |
1287 | on_properties_changed, NULL, &c); | |
95f1d6bf | 1288 | if (r < 0) |
75152a4d | 1289 | return log_error_errno(r, "Failed to request properties changed signal match: %m"); |
4c213d6c | 1290 | |
d147457c | 1291 | r = sd_bus_attach_event(bus, c.event, SD_EVENT_PRIORITY_NORMAL); |
95f1d6bf | 1292 | if (r < 0) |
75152a4d | 1293 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); |
5b1bad8d | 1294 | |
95f1d6bf LP |
1295 | r = run_context_update(&c, path); |
1296 | if (r < 0) | |
1297 | return r; | |
2a453c2e LP |
1298 | |
1299 | r = sd_event_loop(c.event); | |
4c213d6c | 1300 | if (r < 0) |
9b15b784 | 1301 | return log_error_errno(r, "Failed to run event loop: %m"); |
4c213d6c | 1302 | |
2a453c2e LP |
1303 | if (c.forward) { |
1304 | char last_char = 0; | |
4c213d6c | 1305 | |
2a453c2e LP |
1306 | r = pty_forward_get_last_char(c.forward, &last_char); |
1307 | if (r >= 0 && !arg_quiet && last_char != '\n') | |
1308 | fputc('\n', stdout); | |
1309 | } | |
4c213d6c | 1310 | |
95f1d6bf | 1311 | if (arg_wait && !arg_quiet) { |
9182fb52 | 1312 | |
f1d60962 | 1313 | /* Explicitly destroy the PTY forwarder, so that the PTY device is usable again, with its |
9182fb52 LP |
1314 | * original settings (i.e. proper line breaks), so that we can show the summary in a pretty |
1315 | * way. */ | |
1316 | c.forward = pty_forward_free(c.forward); | |
1317 | ||
2a453c2e LP |
1318 | if (!isempty(c.result)) |
1319 | log_info("Finished with result: %s", strna(c.result)); | |
9f2e86af | 1320 | |
2a453c2e | 1321 | if (c.exit_code == CLD_EXITED) |
f1d60962 ZJS |
1322 | log_info("Main processes terminated with: code=%s/status=%i", |
1323 | sigchld_code_to_string(c.exit_code), c.exit_status); | |
2a453c2e | 1324 | else if (c.exit_code > 0) |
f1d60962 ZJS |
1325 | log_info("Main processes terminated with: code=%s/status=%s", |
1326 | sigchld_code_to_string(c.exit_code), signal_to_string(c.exit_status)); | |
2a453c2e | 1327 | |
1f65fd49 ZJS |
1328 | if (timestamp_is_set(c.inactive_enter_usec) && |
1329 | timestamp_is_set(c.inactive_exit_usec) && | |
5291f26d | 1330 | c.inactive_enter_usec > c.inactive_exit_usec) |
f1d60962 | 1331 | log_info("Service runtime: %s", |
5291f26d | 1332 | FORMAT_TIMESPAN(c.inactive_enter_usec - c.inactive_exit_usec, USEC_PER_MSEC)); |
2a453c2e | 1333 | |
5291f26d | 1334 | if (c.cpu_usage_nsec != NSEC_INFINITY) |
f1d60962 | 1335 | log_info("CPU time consumed: %s", |
5291f26d | 1336 | FORMAT_TIMESPAN(DIV_ROUND_UP(c.cpu_usage_nsec, NSEC_PER_USEC), USEC_PER_MSEC)); |
655aab20 | 1337 | |
2b59bf51 ZJS |
1338 | if (c.ip_ingress_bytes != UINT64_MAX) |
1339 | log_info("IP traffic received: %s", FORMAT_BYTES(c.ip_ingress_bytes)); | |
1340 | ||
1341 | if (c.ip_egress_bytes != UINT64_MAX) | |
1342 | log_info("IP traffic sent: %s", FORMAT_BYTES(c.ip_egress_bytes)); | |
1343 | ||
1344 | if (c.io_read_bytes != UINT64_MAX) | |
1345 | log_info("IO bytes read: %s", FORMAT_BYTES(c.io_read_bytes)); | |
1346 | ||
1347 | if (c.io_write_bytes != UINT64_MAX) | |
1348 | log_info("IO bytes written: %s", FORMAT_BYTES(c.io_write_bytes)); | |
2a453c2e LP |
1349 | } |
1350 | ||
7f3614e5 ZJS |
1351 | /* Try to propagate the service's return value. But if the service defines |
1352 | * e.g. SuccessExitStatus, honour this, and return 0 to mean "success". */ | |
1353 | if (streq_ptr(c.result, "success")) | |
1354 | *retval = 0; | |
1355 | else if (streq_ptr(c.result, "exit-code") && c.exit_status > 0) | |
2a453c2e | 1356 | *retval = c.exit_status; |
7f3614e5 ZJS |
1357 | else if (streq_ptr(c.result, "signal")) |
1358 | *retval = EXIT_EXCEPTION; | |
2a453c2e LP |
1359 | else |
1360 | *retval = EXIT_FAILURE; | |
1361 | } | |
7040b626 LP |
1362 | |
1363 | return 0; | |
6c12b52e LP |
1364 | } |
1365 | ||
00f8eea8 LP |
1366 | static int acquire_invocation_id(sd_bus *bus, sd_id128_t *ret) { |
1367 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
1368 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1369 | const void *p; | |
1370 | size_t l; | |
1371 | int r; | |
1372 | ||
1373 | assert(bus); | |
1374 | assert(ret); | |
6c12b52e | 1375 | |
00f8eea8 LP |
1376 | r = sd_bus_get_property(bus, |
1377 | "org.freedesktop.systemd1", | |
1378 | "/org/freedesktop/systemd1/unit/self", | |
1379 | "org.freedesktop.systemd1.Unit", | |
1380 | "InvocationID", | |
1381 | &error, | |
1382 | &reply, | |
1383 | "ay"); | |
1384 | if (r < 0) | |
1385 | return log_error_errno(r, "Failed to request invocation ID for scope: %s", bus_error_message(&error, r)); | |
1386 | ||
1387 | r = sd_bus_message_read_array(reply, 'y', &p, &l); | |
1388 | if (r < 0) | |
1389 | return bus_log_parse_error(r); | |
1390 | ||
1391 | if (l != sizeof(sd_id128_t)) | |
1392 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid UUID size, %zu != %zu.", l, sizeof(sd_id128_t)); | |
1393 | ||
1394 | memcpy(ret, p, l); | |
1395 | return 0; | |
1396 | } | |
1397 | ||
1398 | static int start_transient_scope(sd_bus *bus) { | |
4afd3348 LP |
1399 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1400 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
de158ed2 | 1401 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
9b15b784 | 1402 | _cleanup_strv_free_ char **env = NULL, **user_env = NULL; |
4c213d6c | 1403 | _cleanup_free_ char *scope = NULL; |
de158ed2 | 1404 | const char *object = NULL; |
00f8eea8 | 1405 | sd_id128_t invocation_id; |
6c12b52e LP |
1406 | int r; |
1407 | ||
8159d91a | 1408 | assert(bus); |
badd28e1 | 1409 | assert(!strv_isempty(arg_cmdline)); |
8159d91a | 1410 | |
de158ed2 LP |
1411 | r = bus_wait_for_jobs_new(bus, &w); |
1412 | if (r < 0) | |
1413 | return log_oom(); | |
1414 | ||
7de80bfe | 1415 | if (arg_unit) { |
df7c4eb6 ZJS |
1416 | r = unit_name_mangle_with_suffix(arg_unit, "as unit", |
1417 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
1418 | ".scope", &scope); | |
7410616c LP |
1419 | if (r < 0) |
1420 | return log_error_errno(r, "Failed to mangle scope name: %m"); | |
9c8d1e1a LP |
1421 | } else { |
1422 | r = make_unit_name(bus, UNIT_SCOPE, &scope); | |
1423 | if (r < 0) | |
1424 | return r; | |
1425 | } | |
6c12b52e | 1426 | |
7ad61613 | 1427 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
c2756a68 | 1428 | if (r < 0) |
7040b626 | 1429 | return bus_log_create_error(r); |
c2756a68 | 1430 | |
8c7db2fb EV |
1431 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); |
1432 | if (r < 0) | |
1433 | return bus_log_create_error(r); | |
1434 | ||
9b15b784 | 1435 | /* Name and Mode */ |
4c213d6c WC |
1436 | r = sd_bus_message_append(m, "ss", scope, "fail"); |
1437 | if (r < 0) | |
1438 | return bus_log_create_error(r); | |
1439 | ||
9b15b784 | 1440 | /* Properties */ |
4c213d6c WC |
1441 | r = sd_bus_message_open_container(m, 'a', "(sv)"); |
1442 | if (r < 0) | |
1443 | return bus_log_create_error(r); | |
1444 | ||
1445 | r = transient_scope_set_properties(m); | |
1446 | if (r < 0) | |
f53bddf3 | 1447 | return r; |
4c213d6c WC |
1448 | |
1449 | r = sd_bus_message_close_container(m); | |
1450 | if (r < 0) | |
1451 | return bus_log_create_error(r); | |
1452 | ||
ee451d76 | 1453 | /* Auxiliary units */ |
4c213d6c | 1454 | r = sd_bus_message_append(m, "a(sa(sv))", 0); |
6c12b52e | 1455 | if (r < 0) |
7040b626 | 1456 | return bus_log_create_error(r); |
6c12b52e | 1457 | |
8a4b13c5 | 1458 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
8c7db2fb | 1459 | |
de158ed2 | 1460 | r = sd_bus_call(bus, m, 0, &error, &reply); |
4ae25393 | 1461 | if (r < 0) |
2a03b9ed | 1462 | return log_error_errno(r, "Failed to start transient scope unit: %s", bus_error_message(&error, r)); |
c2756a68 | 1463 | |
00f8eea8 LP |
1464 | r = sd_bus_message_read(reply, "o", &object); |
1465 | if (r < 0) | |
1466 | return bus_log_parse_error(r); | |
1467 | ||
1468 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
1469 | if (r < 0) | |
1470 | return r; | |
1471 | ||
1472 | r = acquire_invocation_id(bus, &invocation_id); | |
1473 | if (r < 0) | |
1474 | return r; | |
1475 | ||
1476 | r = strv_extendf(&user_env, "INVOCATION_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(invocation_id)); | |
1477 | if (r < 0) | |
1478 | return log_oom(); | |
1479 | ||
4de33e7f | 1480 | if (arg_nice_set) { |
4a62c710 MS |
1481 | if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) |
1482 | return log_error_errno(errno, "Failed to set nice level: %m"); | |
4de33e7f LP |
1483 | } |
1484 | ||
1485 | if (arg_exec_group) { | |
1486 | gid_t gid; | |
1487 | ||
fafff8f1 | 1488 | r = get_group_creds(&arg_exec_group, &gid, 0); |
f647962d MS |
1489 | if (r < 0) |
1490 | return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group); | |
4de33e7f | 1491 | |
4a62c710 MS |
1492 | if (setresgid(gid, gid, gid) < 0) |
1493 | return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid); | |
4de33e7f LP |
1494 | } |
1495 | ||
1496 | if (arg_exec_user) { | |
1497 | const char *home, *shell; | |
1498 | uid_t uid; | |
1499 | gid_t gid; | |
1500 | ||
43ad3ad7 | 1501 | r = get_user_creds(&arg_exec_user, &uid, &gid, &home, &shell, USER_CREDS_CLEAN|USER_CREDS_PREFER_NSS); |
f647962d MS |
1502 | if (r < 0) |
1503 | return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user); | |
4de33e7f | 1504 | |
be39ccf3 LP |
1505 | if (home) { |
1506 | r = strv_extendf(&user_env, "HOME=%s", home); | |
1507 | if (r < 0) | |
1508 | return log_oom(); | |
1509 | } | |
4de33e7f | 1510 | |
be39ccf3 LP |
1511 | if (shell) { |
1512 | r = strv_extendf(&user_env, "SHELL=%s", shell); | |
1513 | if (r < 0) | |
1514 | return log_oom(); | |
1515 | } | |
4de33e7f LP |
1516 | |
1517 | r = strv_extendf(&user_env, "USER=%s", arg_exec_user); | |
1518 | if (r < 0) | |
1519 | return log_oom(); | |
1520 | ||
1521 | r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user); | |
1522 | if (r < 0) | |
1523 | return log_oom(); | |
1524 | ||
1525 | if (!arg_exec_group) { | |
4a62c710 MS |
1526 | if (setresgid(gid, gid, gid) < 0) |
1527 | return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid); | |
4de33e7f LP |
1528 | } |
1529 | ||
4a62c710 MS |
1530 | if (setresuid(uid, uid, uid) < 0) |
1531 | return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid); | |
4de33e7f LP |
1532 | } |
1533 | ||
4ab3d29f | 1534 | env = strv_env_merge(environ, user_env, arg_environment); |
4de33e7f LP |
1535 | if (!env) |
1536 | return log_oom(); | |
1537 | ||
095dc596 | 1538 | if (!arg_quiet) |
f3ea87af | 1539 | log_info("Running scope as unit: %s", scope); |
7040b626 | 1540 | |
badd28e1 | 1541 | execvpe(arg_cmdline[0], arg_cmdline, env); |
9b15b784 LP |
1542 | |
1543 | return log_error_errno(errno, "Failed to execute: %m"); | |
1544 | } | |
1545 | ||
d59ef3e2 | 1546 | static int start_transient_trigger( |
9b15b784 | 1547 | sd_bus *bus, |
d59ef3e2 | 1548 | const char *suffix) { |
9b15b784 | 1549 | |
4afd3348 LP |
1550 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1551 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; | |
de158ed2 | 1552 | _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL; |
d59ef3e2 | 1553 | _cleanup_free_ char *trigger = NULL, *service = NULL; |
de158ed2 | 1554 | const char *object = NULL; |
9b15b784 LP |
1555 | int r; |
1556 | ||
1557 | assert(bus); | |
9b15b784 | 1558 | |
de158ed2 LP |
1559 | r = bus_wait_for_jobs_new(bus, &w); |
1560 | if (r < 0) | |
1561 | return log_oom(); | |
1562 | ||
9b15b784 | 1563 | if (arg_unit) { |
7410616c | 1564 | switch (unit_name_to_type(arg_unit)) { |
9b15b784 LP |
1565 | |
1566 | case UNIT_SERVICE: | |
1567 | service = strdup(arg_unit); | |
1568 | if (!service) | |
1569 | return log_oom(); | |
1570 | ||
d59ef3e2 | 1571 | r = unit_name_change_suffix(service, suffix, &trigger); |
7410616c LP |
1572 | if (r < 0) |
1573 | return log_error_errno(r, "Failed to change unit suffix: %m"); | |
9b15b784 LP |
1574 | break; |
1575 | ||
1576 | case UNIT_TIMER: | |
d59ef3e2 YW |
1577 | trigger = strdup(arg_unit); |
1578 | if (!trigger) | |
9b15b784 LP |
1579 | return log_oom(); |
1580 | ||
d59ef3e2 | 1581 | r = unit_name_change_suffix(trigger, ".service", &service); |
7410616c LP |
1582 | if (r < 0) |
1583 | return log_error_errno(r, "Failed to change unit suffix: %m"); | |
9b15b784 LP |
1584 | break; |
1585 | ||
1586 | default: | |
df7c4eb6 ZJS |
1587 | r = unit_name_mangle_with_suffix(arg_unit, "as unit", |
1588 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
1589 | ".service", &service); | |
7410616c LP |
1590 | if (r < 0) |
1591 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
9b15b784 | 1592 | |
df7c4eb6 ZJS |
1593 | r = unit_name_mangle_with_suffix(arg_unit, "as trigger", |
1594 | arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN, | |
1595 | suffix, &trigger); | |
7410616c LP |
1596 | if (r < 0) |
1597 | return log_error_errno(r, "Failed to mangle unit name: %m"); | |
9b15b784 LP |
1598 | |
1599 | break; | |
1600 | } | |
9c8d1e1a LP |
1601 | } else { |
1602 | r = make_unit_name(bus, UNIT_SERVICE, &service); | |
1603 | if (r < 0) | |
1604 | return r; | |
1605 | ||
d59ef3e2 | 1606 | r = unit_name_change_suffix(service, suffix, &trigger); |
9c8d1e1a LP |
1607 | if (r < 0) |
1608 | return log_error_errno(r, "Failed to change unit suffix: %m"); | |
1609 | } | |
9b15b784 | 1610 | |
7ad61613 | 1611 | r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit"); |
9b15b784 LP |
1612 | if (r < 0) |
1613 | return bus_log_create_error(r); | |
1614 | ||
8c7db2fb EV |
1615 | r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); |
1616 | if (r < 0) | |
1617 | return bus_log_create_error(r); | |
1618 | ||
9b15b784 | 1619 | /* Name and Mode */ |
d59ef3e2 | 1620 | r = sd_bus_message_append(m, "ss", trigger, "fail"); |
9b15b784 LP |
1621 | if (r < 0) |
1622 | return bus_log_create_error(r); | |
1623 | ||
1624 | /* Properties */ | |
1625 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
1626 | if (r < 0) | |
1627 | return bus_log_create_error(r); | |
1628 | ||
d59ef3e2 YW |
1629 | if (streq(suffix, ".path")) |
1630 | r = transient_unit_set_properties(m, UNIT_PATH, arg_path_property); | |
1631 | else if (streq(suffix, ".socket")) | |
1632 | r = transient_unit_set_properties(m, UNIT_SOCKET, arg_socket_property); | |
1633 | else if (streq(suffix, ".timer")) | |
1634 | r = transient_timer_set_properties(m); | |
1635 | else | |
04499a70 | 1636 | assert_not_reached(); |
9b15b784 | 1637 | if (r < 0) |
f53bddf3 | 1638 | return r; |
9b15b784 LP |
1639 | |
1640 | r = sd_bus_message_close_container(m); | |
1641 | if (r < 0) | |
1642 | return bus_log_create_error(r); | |
1643 | ||
1644 | r = sd_bus_message_open_container(m, 'a', "(sa(sv))"); | |
1645 | if (r < 0) | |
1646 | return bus_log_create_error(r); | |
1647 | ||
badd28e1 | 1648 | if (!strv_isempty(arg_cmdline)) { |
9b15b784 LP |
1649 | r = sd_bus_message_open_container(m, 'r', "sa(sv)"); |
1650 | if (r < 0) | |
1651 | return bus_log_create_error(r); | |
1652 | ||
1653 | r = sd_bus_message_append(m, "s", service); | |
1654 | if (r < 0) | |
1655 | return bus_log_create_error(r); | |
1656 | ||
1657 | r = sd_bus_message_open_container(m, 'a', "(sv)"); | |
1658 | if (r < 0) | |
1659 | return bus_log_create_error(r); | |
1660 | ||
badd28e1 | 1661 | r = transient_service_set_properties(m, NULL); |
9b15b784 | 1662 | if (r < 0) |
f53bddf3 | 1663 | return r; |
9b15b784 LP |
1664 | |
1665 | r = sd_bus_message_close_container(m); | |
1666 | if (r < 0) | |
1667 | return bus_log_create_error(r); | |
1668 | ||
1669 | r = sd_bus_message_close_container(m); | |
1670 | if (r < 0) | |
1671 | return bus_log_create_error(r); | |
1672 | } | |
1673 | ||
1674 | r = sd_bus_message_close_container(m); | |
1675 | if (r < 0) | |
1676 | return bus_log_create_error(r); | |
1677 | ||
8a4b13c5 | 1678 | polkit_agent_open_if_enabled(arg_transport, arg_ask_password); |
8c7db2fb | 1679 | |
de158ed2 | 1680 | r = sd_bus_call(bus, m, 0, &error, &reply); |
4ae25393 | 1681 | if (r < 0) |
2a03b9ed | 1682 | return log_error_errno(r, "Failed to start transient %s unit: %s", suffix + 1, bus_error_message(&error, r)); |
9b15b784 | 1683 | |
de158ed2 LP |
1684 | r = sd_bus_message_read(reply, "o", &object); |
1685 | if (r < 0) | |
1686 | return bus_log_parse_error(r); | |
1687 | ||
1688 | r = bus_wait_for_jobs_one(w, object, arg_quiet); | |
1689 | if (r < 0) | |
1690 | return r; | |
1691 | ||
3dea75de | 1692 | if (!arg_quiet) { |
d59ef3e2 | 1693 | log_info("Running %s as unit: %s", suffix + 1, trigger); |
badd28e1 | 1694 | if (!strv_isempty(arg_cmdline)) |
3dea75de NO |
1695 | log_info("Will run service as unit: %s", service); |
1696 | } | |
9b15b784 LP |
1697 | |
1698 | return 0; | |
c2756a68 LP |
1699 | } |
1700 | ||
69e08309 LB |
1701 | static bool shall_make_executable_absolute(void) { |
1702 | const char *f; | |
1703 | ||
1704 | if (strv_isempty(arg_cmdline)) | |
1705 | return false; | |
1706 | if (arg_transport != BUS_TRANSPORT_LOCAL) | |
1707 | return false; | |
1708 | ||
1709 | FOREACH_STRING(f, "RootDirectory=", "RootImage=", "ExecSearchPath=", "MountImages=", "ExtensionImages=") | |
1710 | if (strv_find_startswith(arg_property, f)) | |
1711 | return false; | |
1712 | ||
1713 | return true; | |
1714 | } | |
1715 | ||
ef08ad7a | 1716 | static int run(int argc, char* argv[]) { |
4afd3348 | 1717 | _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
badd28e1 | 1718 | _cleanup_free_ char *description = NULL; |
2a453c2e | 1719 | int r, retval = EXIT_SUCCESS; |
c2756a68 | 1720 | |
1a043959 | 1721 | log_show_color(true); |
c2756a68 LP |
1722 | log_parse_environment(); |
1723 | log_open(); | |
1724 | ||
6c12b52e LP |
1725 | r = parse_argv(argc, argv); |
1726 | if (r <= 0) | |
0565120f | 1727 | return r; |
c2756a68 | 1728 | |
69e08309 | 1729 | if (shall_make_executable_absolute()) { |
8038b99d ZJS |
1730 | /* Patch in an absolute path to fail early for user convenience, but only when we can do it |
1731 | * (i.e. we will be running from the same file system). This also uses the user's $PATH, | |
1732 | * while we use a fixed search path in the manager. */ | |
85eca92e | 1733 | |
8038b99d | 1734 | _cleanup_free_ char *command = NULL; |
f7bc0c32 | 1735 | r = find_executable(arg_cmdline[0], &command); |
0565120f LP |
1736 | if (r < 0) |
1737 | return log_error_errno(r, "Failed to find executable %s: %m", arg_cmdline[0]); | |
85eca92e | 1738 | |
badd28e1 | 1739 | free_and_replace(arg_cmdline[0], command); |
c9d954b2 | 1740 | } |
c9d954b2 | 1741 | |
9f2e86af | 1742 | if (!arg_description) { |
badd28e1 | 1743 | description = strv_join(arg_cmdline, " "); |
0565120f LP |
1744 | if (!description) |
1745 | return log_oom(); | |
9f2e86af | 1746 | |
4c213d6c | 1747 | if (arg_unit && isempty(description)) { |
2fc09a9c DM |
1748 | r = free_and_strdup(&description, arg_unit); |
1749 | if (r < 0) | |
0565120f | 1750 | return log_oom(); |
4c213d6c WC |
1751 | } |
1752 | ||
9f2e86af LP |
1753 | arg_description = description; |
1754 | } | |
1755 | ||
2a453c2e LP |
1756 | /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct |
1757 | * connection */ | |
cbdc2949 | 1758 | if (arg_wait || arg_stdio != ARG_STDIO_NONE || (arg_user && arg_transport != BUS_TRANSPORT_LOCAL)) |
2a453c2e LP |
1759 | r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus); |
1760 | else | |
1761 | r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus); | |
0565120f | 1762 | if (r < 0) |
10a7340a | 1763 | return bus_log_connect_error(r, arg_transport); |
c2756a68 | 1764 | |
6c12b52e | 1765 | if (arg_scope) |
badd28e1 | 1766 | r = start_transient_scope(bus); |
d59ef3e2 | 1767 | else if (arg_path_property) |
badd28e1 | 1768 | r = start_transient_trigger(bus, ".path"); |
d59ef3e2 | 1769 | else if (arg_socket_property) |
badd28e1 | 1770 | r = start_transient_trigger(bus, ".socket"); |
787be190 | 1771 | else if (arg_with_timer) |
badd28e1 | 1772 | r = start_transient_trigger(bus, ".timer"); |
6c12b52e | 1773 | else |
badd28e1 | 1774 | r = start_transient_service(bus, &retval); |
0565120f LP |
1775 | if (r < 0) |
1776 | return r; | |
c2756a68 | 1777 | |
0565120f | 1778 | return retval; |
c2756a68 | 1779 | } |
ef08ad7a LP |
1780 | |
1781 | DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |