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