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