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