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