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