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