]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/run/run.c
Rename formats-util.h to format-util.h
[thirdparty/systemd.git] / src / run / run.c
CommitLineData
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 45static bool arg_ask_password = true;
6c12b52e 46static bool arg_scope = false;
6577c7ce 47static bool arg_remain_after_exit = false;
3d161f99 48static bool arg_no_block = false;
2a453c2e 49static bool arg_wait = false;
6c12b52e 50static const char *arg_unit = NULL;
9f2e86af 51static const char *arg_description = NULL;
c221420b 52static const char *arg_slice = NULL;
a6c0353b 53static bool arg_send_sighup = false;
d21ed1ea 54static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
1ac67edb 55static const char *arg_host = NULL;
d21ed1ea 56static bool arg_user = false;
c7040b5d
LP
57static const char *arg_service_type = NULL;
58static const char *arg_exec_user = NULL;
59static const char *arg_exec_group = NULL;
60static int arg_nice = 0;
61static bool arg_nice_set = false;
62static char **arg_environment = NULL;
df31a6c0 63static char **arg_property = NULL;
9b15b784 64static bool arg_pty = false;
4c213d6c
WC
65static usec_t arg_on_active = 0;
66static usec_t arg_on_boot = 0;
67static usec_t arg_on_startup = 0;
68static usec_t arg_on_unit_active = 0;
69static usec_t arg_on_unit_inactive = 0;
024a8ec1 70static const char *arg_on_calendar = NULL;
4c213d6c 71static char **arg_timer_property = NULL;
095dc596 72static bool arg_quiet = false;
c2756a68 73
8c7db2fb
EV
74static 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 86static 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
122static 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
126static 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
4c213d6c
WC
406 if (arg_scope && with_timer()) {
407 log_error("Timer options are not supported in --scope mode.");
408 return -EINVAL;
409 }
410
411 if (arg_timer_property && !with_timer()) {
412 log_error("--timer-property= has no effect without any other timer options.");
413 return -EINVAL;
414 }
415
2a453c2e
LP
416 if (arg_wait) {
417 if (arg_no_block) {
418 log_error("--wait may not be combined with --no-block.");
419 return -EINVAL;
420 }
421
422 if (with_timer()) {
423 log_error("--wait may not be combined with timer operations.");
424 return -EINVAL;
425 }
426
427 if (arg_scope) {
428 log_error("--wait may not be combined with --scope.");
429 return -EINVAL;
430 }
431 }
432
6c12b52e
LP
433 return 1;
434}
435
9b15b784 436static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
c2756a68
LP
437 int r;
438
9b15b784
LP
439 r = sd_bus_message_append(m, "(sv)", "Description", "s", arg_description);
440 if (r < 0)
441 return r;
442
8673cf13
LP
443 r = bus_append_unit_property_assignment_many(m, properties);
444 if (r < 0)
445 return r;
df31a6c0 446
9b15b784
LP
447 return 0;
448}
449
450static int transient_cgroup_set_properties(sd_bus_message *m) {
451 int r;
452 assert(m);
c2756a68 453
c221420b
LP
454 if (!isempty(arg_slice)) {
455 _cleanup_free_ char *slice;
456
7410616c
LP
457 r = unit_name_mangle_with_suffix(arg_slice, UNIT_NAME_NOGLOB, ".slice", &slice);
458 if (r < 0)
459 return r;
c221420b
LP
460
461 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
462 if (r < 0)
463 return r;
464 }
465
9b15b784
LP
466 return 0;
467}
468
469static int transient_kill_set_properties(sd_bus_message *m) {
9b15b784
LP
470 assert(m);
471
aa1aad74
ZJS
472 if (arg_send_sighup)
473 return sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
474 else
475 return 0;
6c12b52e
LP
476}
477
9b15b784 478static int transient_service_set_properties(sd_bus_message *m, char **argv, const char *pty_path) {
6c12b52e
LP
479 int r;
480
8159d91a
LP
481 assert(m);
482
9b15b784
LP
483 r = transient_unit_set_properties(m, arg_property);
484 if (r < 0)
485 return r;
486
487 r = transient_kill_set_properties(m);
488 if (r < 0)
489 return r;
490
491 r = transient_cgroup_set_properties(m);
86b8d289
LP
492 if (r < 0)
493 return r;
494
2a453c2e
LP
495 if (arg_wait) {
496 r = sd_bus_message_append(m, "(sv)", "AddRef", "b", 1);
497 if (r < 0)
498 return r;
499 }
500
df31a6c0
LP
501 if (arg_remain_after_exit) {
502 r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
503 if (r < 0)
4c213d6c 504 return r;
df31a6c0 505 }
6577c7ce 506
c7040b5d
LP
507 if (arg_service_type) {
508 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
509 if (r < 0)
4c213d6c 510 return r;
c7040b5d
LP
511 }
512
513 if (arg_exec_user) {
514 r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
515 if (r < 0)
4c213d6c 516 return r;
c7040b5d
LP
517 }
518
519 if (arg_exec_group) {
520 r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
521 if (r < 0)
4c213d6c 522 return r;
c7040b5d
LP
523 }
524
525 if (arg_nice_set) {
526 r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
527 if (r < 0)
4c213d6c 528 return r;
c7040b5d
LP
529 }
530
9b15b784
LP
531 if (pty_path) {
532 const char *e;
533
534 r = sd_bus_message_append(m,
535 "(sv)(sv)(sv)(sv)",
536 "StandardInput", "s", "tty",
537 "StandardOutput", "s", "tty",
538 "StandardError", "s", "tty",
539 "TTYPath", "s", pty_path);
540 if (r < 0)
541 return r;
542
543 e = getenv("TERM");
544 if (e) {
545 char *n;
546
63c372cb 547 n = strjoina("TERM=", e);
9b15b784
LP
548 r = sd_bus_message_append(m,
549 "(sv)",
550 "Environment", "as", 1, n);
551 if (r < 0)
552 return r;
553 }
554 }
555
c7040b5d
LP
556 if (!strv_isempty(arg_environment)) {
557 r = sd_bus_message_open_container(m, 'r', "sv");
558 if (r < 0)
4c213d6c 559 return r;
c7040b5d
LP
560
561 r = sd_bus_message_append(m, "s", "Environment");
562 if (r < 0)
4c213d6c 563 return r;
c7040b5d
LP
564
565 r = sd_bus_message_open_container(m, 'v', "as");
566 if (r < 0)
4c213d6c 567 return r;
c7040b5d
LP
568
569 r = sd_bus_message_append_strv(m, arg_environment);
570 if (r < 0)
4c213d6c 571 return r;
c7040b5d
LP
572
573 r = sd_bus_message_close_container(m);
574 if (r < 0)
4c213d6c 575 return r;
c7040b5d
LP
576
577 r = sd_bus_message_close_container(m);
578 if (r < 0)
4c213d6c
WC
579 return r;
580 }
581
582 /* Exec container */
583 {
584 r = sd_bus_message_open_container(m, 'r', "sv");
585 if (r < 0)
586 return r;
587
588 r = sd_bus_message_append(m, "s", "ExecStart");
589 if (r < 0)
590 return r;
591
592 r = sd_bus_message_open_container(m, 'v', "a(sasb)");
593 if (r < 0)
594 return r;
595
596 r = sd_bus_message_open_container(m, 'a', "(sasb)");
597 if (r < 0)
598 return r;
599
600 r = sd_bus_message_open_container(m, 'r', "sasb");
601 if (r < 0)
602 return r;
603
604 r = sd_bus_message_append(m, "s", argv[0]);
605 if (r < 0)
606 return r;
607
608 r = sd_bus_message_append_strv(m, argv);
609 if (r < 0)
610 return r;
611
612 r = sd_bus_message_append(m, "b", false);
613 if (r < 0)
614 return r;
615
616 r = sd_bus_message_close_container(m);
617 if (r < 0)
618 return r;
619
620 r = sd_bus_message_close_container(m);
621 if (r < 0)
622 return r;
623
624 r = sd_bus_message_close_container(m);
625 if (r < 0)
626 return r;
627
628 r = sd_bus_message_close_container(m);
629 if (r < 0)
630 return r;
631 }
632
633 return 0;
634}
635
9b15b784
LP
636static int transient_scope_set_properties(sd_bus_message *m) {
637 int r;
638
639 assert(m);
640
641 r = transient_unit_set_properties(m, arg_property);
642 if (r < 0)
643 return r;
644
645 r = transient_kill_set_properties(m);
646 if (r < 0)
647 return r;
648
37e605f9
LP
649 r = transient_cgroup_set_properties(m);
650 if (r < 0)
651 return r;
652
9b15b784
LP
653 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
654 if (r < 0)
655 return r;
656
657 return 0;
658}
659
4c213d6c
WC
660static int transient_timer_set_properties(sd_bus_message *m) {
661 int r;
662
663 assert(m);
664
9b15b784 665 r = transient_unit_set_properties(m, arg_timer_property);
4c213d6c
WC
666 if (r < 0)
667 return r;
668
6348d701
LP
669 /* Automatically clean up our transient timers */
670 r = sd_bus_message_append(m, "(sv)", "RemainAfterElapse", "b", false);
671 if (r < 0)
672 return r;
673
4c213d6c
WC
674 if (arg_on_active) {
675 r = sd_bus_message_append(m, "(sv)", "OnActiveSec", "t", arg_on_active);
676 if (r < 0)
677 return r;
c7040b5d
LP
678 }
679
4c213d6c
WC
680 if (arg_on_boot) {
681 r = sd_bus_message_append(m, "(sv)", "OnBootSec", "t", arg_on_boot);
682 if (r < 0)
683 return r;
684 }
685
686 if (arg_on_startup) {
687 r = sd_bus_message_append(m, "(sv)", "OnStartupSec", "t", arg_on_startup);
688 if (r < 0)
689 return r;
690 }
691
692 if (arg_on_unit_active) {
693 r = sd_bus_message_append(m, "(sv)", "OnUnitActiveSec", "t", arg_on_unit_active);
694 if (r < 0)
695 return r;
696 }
697
698 if (arg_on_unit_inactive) {
699 r = sd_bus_message_append(m, "(sv)", "OnUnitInactiveSec", "t", arg_on_unit_inactive);
700 if (r < 0)
701 return r;
702 }
703
704 if (arg_on_calendar) {
705 r = sd_bus_message_append(m, "(sv)", "OnCalendar", "s", arg_on_calendar);
706 if (r < 0)
707 return r;
708 }
709
710 return 0;
711}
712
9c8d1e1a
LP
713static int make_unit_name(sd_bus *bus, UnitType t, char **ret) {
714 const char *unique, *id;
715 char *p;
716 int r;
717
718 assert(bus);
719 assert(t >= 0);
720 assert(t < _UNIT_TYPE_MAX);
721
722 r = sd_bus_get_unique_name(bus, &unique);
723 if (r < 0) {
724 sd_id128_t rnd;
725
726 /* We couldn't get the unique name, which is a pretty
727 * common case if we are connected to systemd
728 * directly. In that case, just pick a random uuid as
729 * name */
730
731 r = sd_id128_randomize(&rnd);
732 if (r < 0)
733 return log_error_errno(r, "Failed to generate random run unit name: %m");
734
735 if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0)
736 return log_oom();
737
738 return 0;
739 }
740
741 /* We managed to get the unique name, then let's use that to
742 * name our transient units. */
743
744 id = startswith(unique, ":1.");
745 if (!id) {
746 log_error("Unique name %s has unexpected format.", unique);
747 return -EINVAL;
748 }
749
605405c6 750 p = strjoin("run-u", id, ".", unit_type_to_string(t));
9c8d1e1a
LP
751 if (!p)
752 return log_oom();
753
754 *ret = p;
755 return 0;
756}
757
2a453c2e
LP
758typedef struct RunContext {
759 sd_bus *bus;
760 sd_event *event;
761 PTYForward *forward;
762 sd_bus_slot *match;
763
764 /* The exit data of the unit */
765 char *active_state;
766 uint64_t inactive_exit_usec;
767 uint64_t inactive_enter_usec;
768 char *result;
769 uint64_t cpu_usage_nsec;
770 uint32_t exit_code;
771 uint32_t exit_status;
772} RunContext;
773
774static void run_context_free(RunContext *c) {
775 assert(c);
776
777 c->forward = pty_forward_free(c->forward);
778 c->match = sd_bus_slot_unref(c->match);
779 c->bus = sd_bus_unref(c->bus);
780 c->event = sd_event_unref(c->event);
781
782 free(c->active_state);
783 free(c->result);
784}
785
786static void run_context_check_done(RunContext *c) {
787 bool done = true;
788
789 assert(c);
790
791 if (c->match)
792 done = done && (c->active_state && STR_IN_SET(c->active_state, "inactive", "failed"));
793
794 if (c->forward)
795 done = done && pty_forward_is_done(c->forward);
796
797 if (done)
798 sd_event_exit(c->event, EXIT_SUCCESS);
799}
800
801static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
802
803 static const struct bus_properties_map map[] = {
804 { "ActiveState", "s", NULL, offsetof(RunContext, active_state) },
805 { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_exit_usec) },
806 { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_enter_usec) },
807 { "Result", "s", NULL, offsetof(RunContext, result) },
808 { "ExecMainCode", "i", NULL, offsetof(RunContext, exit_code) },
809 { "ExecMainStatus", "i", NULL, offsetof(RunContext, exit_status) },
810 { "CPUUsageNSec", "t", NULL, offsetof(RunContext, cpu_usage_nsec) },
811 {}
812 };
813
814 RunContext *c = userdata;
815 int r;
816
817 r = bus_map_all_properties(c->bus,
818 "org.freedesktop.systemd1",
819 sd_bus_message_get_path(m),
820 map,
821 c);
822 if (r < 0) {
823 sd_event_exit(c->event, EXIT_FAILURE);
824 return log_error_errno(r, "Failed to query unit state: %m");
825 }
826
827 run_context_check_done(c);
828 return 0;
829}
830
831static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
832 RunContext *c = userdata;
833
834 assert(f);
835
836 if (rcode < 0) {
837 sd_event_exit(c->event, EXIT_FAILURE);
838 return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
839 }
840
841 run_context_check_done(c);
842 return 0;
843}
844
4c213d6c
WC
845static int start_transient_service(
846 sd_bus *bus,
2a453c2e
LP
847 char **argv,
848 int *retval) {
4c213d6c 849
4afd3348
LP
850 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
851 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
3d161f99 852 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
ee451d76 853 _cleanup_free_ char *service = NULL, *pty_path = NULL;
9b15b784 854 _cleanup_close_ int master = -1;
4c213d6c
WC
855 int r;
856
857 assert(bus);
858 assert(argv);
2a453c2e 859 assert(retval);
4c213d6c 860
9b15b784 861 if (arg_pty) {
9b15b784 862
ee451d76
LP
863 if (arg_transport == BUS_TRANSPORT_LOCAL) {
864 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
865 if (master < 0)
866 return log_error_errno(errno, "Failed to acquire pseudo tty: %m");
867
868 r = ptsname_malloc(master, &pty_path);
869 if (r < 0)
870 return log_error_errno(r, "Failed to determine tty name: %m");
871
395745ba
LP
872 if (unlockpt(master) < 0)
873 return log_error_errno(errno, "Failed to unlock tty: %m");
874
de33fc62 875 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
4afd3348 876 _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
20fc5811 877 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
ee451d76
LP
878 const char *s;
879
024a8ec1 880 r = sd_bus_default_system(&system_bus);
ee451d76 881 if (r < 0)
024a8ec1 882 return log_error_errno(r, "Failed to connect to system bus: %m");
ee451d76
LP
883
884 r = sd_bus_call_method(system_bus,
885 "org.freedesktop.machine1",
886 "/org/freedesktop/machine1",
887 "org.freedesktop.machine1.Manager",
888 "OpenMachinePTY",
889 &error,
20fc5811 890 &pty_reply,
ee451d76
LP
891 "s", arg_host);
892 if (r < 0) {
893 log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r));
894 return r;
895 }
896
20fc5811 897 r = sd_bus_message_read(pty_reply, "hs", &master, &s);
ee451d76
LP
898 if (r < 0)
899 return bus_log_parse_error(r);
900
901 master = fcntl(master, F_DUPFD_CLOEXEC, 3);
902 if (master < 0)
903 return log_error_errno(errno, "Failed to duplicate master fd: %m");
904
905 pty_path = strdup(s);
906 if (!pty_path)
907 return log_oom();
908 } else
909 assert_not_reached("Can't allocate tty via ssh");
9b15b784
LP
910 }
911
3d161f99
LP
912 if (!arg_no_block) {
913 r = bus_wait_for_jobs_new(bus, &w);
914 if (r < 0)
915 return log_error_errno(r, "Could not watch jobs: %m");
916 }
917
4c213d6c 918 if (arg_unit) {
7410616c
LP
919 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
920 if (r < 0)
921 return log_error_errno(r, "Failed to mangle unit name: %m");
9c8d1e1a
LP
922 } else {
923 r = make_unit_name(bus, UNIT_SERVICE, &service);
924 if (r < 0)
925 return r;
926 }
4c213d6c
WC
927
928 r = sd_bus_message_new_method_call(
929 bus,
930 &m,
931 "org.freedesktop.systemd1",
932 "/org/freedesktop/systemd1",
933 "org.freedesktop.systemd1.Manager",
934 "StartTransientUnit");
9f2e86af 935 if (r < 0)
7040b626 936 return bus_log_create_error(r);
9f2e86af 937
8c7db2fb
EV
938 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
939 if (r < 0)
940 return bus_log_create_error(r);
941
9b15b784 942 /* Name and mode */
4c213d6c 943 r = sd_bus_message_append(m, "ss", service, "fail");
c2756a68 944 if (r < 0)
7040b626 945 return bus_log_create_error(r);
c2756a68 946
9b15b784 947 /* Properties */
4c213d6c 948 r = sd_bus_message_open_container(m, 'a', "(sv)");
c2756a68 949 if (r < 0)
7040b626 950 return bus_log_create_error(r);
c2756a68 951
9b15b784 952 r = transient_service_set_properties(m, argv, pty_path);
c2756a68 953 if (r < 0)
7040b626 954 return bus_log_create_error(r);
c2756a68 955
4c213d6c 956 r = sd_bus_message_close_container(m);
c2756a68 957 if (r < 0)
7040b626 958 return bus_log_create_error(r);
c2756a68 959
9b15b784 960 /* Auxiliary units */
4c213d6c 961 r = sd_bus_message_append(m, "a(sa(sv))", 0);
c2756a68 962 if (r < 0)
7040b626 963 return bus_log_create_error(r);
c2756a68 964
8c7db2fb
EV
965 polkit_agent_open_if_enabled();
966
3d161f99 967 r = sd_bus_call(bus, m, 0, &error, &reply);
024a8ec1
LP
968 if (r < 0)
969 return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
c2756a68 970
3d161f99
LP
971 if (w) {
972 const char *object;
973
974 r = sd_bus_message_read(reply, "o", &object);
975 if (r < 0)
976 return bus_log_parse_error(r);
977
978 r = bus_wait_for_jobs_one(w, object, arg_quiet);
979 if (r < 0)
980 return r;
981 }
982
2a453c2e
LP
983 if (!arg_quiet)
984 log_info("Running as unit: %s", service);
985
986 if (arg_wait || master >= 0) {
987 _cleanup_(run_context_free) RunContext c = {};
4c213d6c 988
2a453c2e
LP
989 c.bus = sd_bus_ref(bus);
990
991 r = sd_event_default(&c.event);
4c213d6c 992 if (r < 0)
9b15b784 993 return log_error_errno(r, "Failed to get event loop: %m");
4c213d6c 994
2a453c2e
LP
995 if (master >= 0) {
996 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
997 (void) sd_event_add_signal(c.event, NULL, SIGINT, NULL, NULL);
998 (void) sd_event_add_signal(c.event, NULL, SIGTERM, NULL, NULL);
999
1000 if (!arg_quiet)
1001 log_info("Press ^] three times within 1s to disconnect TTY.");
4c213d6c 1002
2a453c2e
LP
1003 r = pty_forward_new(c.event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &c.forward);
1004 if (r < 0)
1005 return log_error_errno(r, "Failed to create PTY forwarder: %m");
1006
1007 pty_forward_set_handler(c.forward, pty_forward_handler, &c);
1008 }
4c213d6c 1009
2a453c2e
LP
1010 if (arg_wait) {
1011 _cleanup_free_ char *path = NULL;
1012 const char *mt;
4c213d6c 1013
2a453c2e
LP
1014 path = unit_dbus_path_from_name(service);
1015 if (!path)
1016 return log_oom();
1017
1018 mt = strjoina("type='signal',"
1019 "sender='org.freedesktop.systemd1',"
1020 "path='", path, "',"
1021 "interface='org.freedesktop.DBus.Properties',"
1022 "member='PropertiesChanged'");
1023 r = sd_bus_add_match(bus, &c.match, mt, on_properties_changed, &c);
1024 if (r < 0)
1025 return log_error_errno(r, "Failed to add properties changed signal.");
4c213d6c 1026
2a453c2e
LP
1027 r = sd_bus_attach_event(bus, c.event, 0);
1028 if (r < 0)
1029 return log_error_errno(r, "Failed to attach bus to event loop.");
1030 }
1031
1032 r = sd_event_loop(c.event);
4c213d6c 1033 if (r < 0)
9b15b784 1034 return log_error_errno(r, "Failed to run event loop: %m");
4c213d6c 1035
2a453c2e
LP
1036 if (c.forward) {
1037 char last_char = 0;
4c213d6c 1038
2a453c2e
LP
1039 r = pty_forward_get_last_char(c.forward, &last_char);
1040 if (r >= 0 && !arg_quiet && last_char != '\n')
1041 fputc('\n', stdout);
1042 }
4c213d6c 1043
2a453c2e
LP
1044 if (!arg_quiet) {
1045 if (!isempty(c.result))
1046 log_info("Finished with result: %s", strna(c.result));
9f2e86af 1047
2a453c2e
LP
1048 if (c.exit_code == CLD_EXITED)
1049 log_info("Main processes terminated with: code=%s/status=%i", sigchld_code_to_string(c.exit_code), c.exit_status);
1050 else if (c.exit_code > 0)
1051 log_info("Main processes terminated with: code=%s/status=%s", sigchld_code_to_string(c.exit_code), signal_to_string(c.exit_status));
1052
1053 if (c.inactive_enter_usec > 0 && c.inactive_enter_usec != USEC_INFINITY &&
1054 c.inactive_exit_usec > 0 && c.inactive_exit_usec != USEC_INFINITY &&
1055 c.inactive_enter_usec > c.inactive_exit_usec) {
1056 char ts[FORMAT_TIMESPAN_MAX];
1057 log_info("Service runtime: %s", format_timespan(ts, sizeof(ts), c.inactive_enter_usec - c.inactive_exit_usec, USEC_PER_MSEC));
1058 }
1059
1060 if (c.cpu_usage_nsec > 0 && c.cpu_usage_nsec != NSEC_INFINITY) {
1061 char ts[FORMAT_TIMESPAN_MAX];
1062 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));
1063 }
1064 }
1065
1066 /* Try to propagate the service's return value */
1067 if (c.result && STR_IN_SET(c.result, "success", "exit-code") && c.exit_code == CLD_EXITED)
1068 *retval = c.exit_status;
1069 else
1070 *retval = EXIT_FAILURE;
1071 }
7040b626
LP
1072
1073 return 0;
6c12b52e
LP
1074}
1075
1076static int start_transient_scope(
1077 sd_bus *bus,
ee451d76 1078 char **argv) {
6c12b52e 1079
4afd3348
LP
1080 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1081 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
de158ed2 1082 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
9b15b784 1083 _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
4c213d6c 1084 _cleanup_free_ char *scope = NULL;
de158ed2 1085 const char *object = NULL;
6c12b52e
LP
1086 int r;
1087
8159d91a 1088 assert(bus);
4c213d6c 1089 assert(argv);
8159d91a 1090
de158ed2
LP
1091 r = bus_wait_for_jobs_new(bus, &w);
1092 if (r < 0)
1093 return log_oom();
1094
7de80bfe 1095 if (arg_unit) {
7410616c
LP
1096 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".scope", &scope);
1097 if (r < 0)
1098 return log_error_errno(r, "Failed to mangle scope name: %m");
9c8d1e1a
LP
1099 } else {
1100 r = make_unit_name(bus, UNIT_SCOPE, &scope);
1101 if (r < 0)
1102 return r;
1103 }
6c12b52e 1104
4c213d6c 1105 r = sd_bus_message_new_method_call(
9b15b784
LP
1106 bus,
1107 &m,
1108 "org.freedesktop.systemd1",
1109 "/org/freedesktop/systemd1",
1110 "org.freedesktop.systemd1.Manager",
1111 "StartTransientUnit");
c2756a68 1112 if (r < 0)
7040b626 1113 return bus_log_create_error(r);
c2756a68 1114
8c7db2fb
EV
1115 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1116 if (r < 0)
1117 return bus_log_create_error(r);
1118
9b15b784 1119 /* Name and Mode */
4c213d6c
WC
1120 r = sd_bus_message_append(m, "ss", scope, "fail");
1121 if (r < 0)
1122 return bus_log_create_error(r);
1123
9b15b784 1124 /* Properties */
4c213d6c
WC
1125 r = sd_bus_message_open_container(m, 'a', "(sv)");
1126 if (r < 0)
1127 return bus_log_create_error(r);
1128
1129 r = transient_scope_set_properties(m);
1130 if (r < 0)
1131 return bus_log_create_error(r);
1132
1133 r = sd_bus_message_close_container(m);
1134 if (r < 0)
1135 return bus_log_create_error(r);
1136
ee451d76 1137 /* Auxiliary units */
4c213d6c 1138 r = sd_bus_message_append(m, "a(sa(sv))", 0);
6c12b52e 1139 if (r < 0)
7040b626 1140 return bus_log_create_error(r);
6c12b52e 1141
8c7db2fb
EV
1142 polkit_agent_open_if_enabled();
1143
de158ed2 1144 r = sd_bus_call(bus, m, 0, &error, &reply);
ee451d76
LP
1145 if (r < 0) {
1146 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
1147 return r;
1148 }
c2756a68 1149
4de33e7f 1150 if (arg_nice_set) {
4a62c710
MS
1151 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
1152 return log_error_errno(errno, "Failed to set nice level: %m");
4de33e7f
LP
1153 }
1154
1155 if (arg_exec_group) {
1156 gid_t gid;
1157
1158 r = get_group_creds(&arg_exec_group, &gid);
f647962d
MS
1159 if (r < 0)
1160 return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
4de33e7f 1161
4a62c710
MS
1162 if (setresgid(gid, gid, gid) < 0)
1163 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
4de33e7f
LP
1164 }
1165
1166 if (arg_exec_user) {
1167 const char *home, *shell;
1168 uid_t uid;
1169 gid_t gid;
1170
be39ccf3 1171 r = get_user_creds_clean(&arg_exec_user, &uid, &gid, &home, &shell);
f647962d
MS
1172 if (r < 0)
1173 return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
4de33e7f 1174
be39ccf3
LP
1175 if (home) {
1176 r = strv_extendf(&user_env, "HOME=%s", home);
1177 if (r < 0)
1178 return log_oom();
1179 }
4de33e7f 1180
be39ccf3
LP
1181 if (shell) {
1182 r = strv_extendf(&user_env, "SHELL=%s", shell);
1183 if (r < 0)
1184 return log_oom();
1185 }
4de33e7f
LP
1186
1187 r = strv_extendf(&user_env, "USER=%s", arg_exec_user);
1188 if (r < 0)
1189 return log_oom();
1190
1191 r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user);
1192 if (r < 0)
1193 return log_oom();
1194
1195 if (!arg_exec_group) {
4a62c710
MS
1196 if (setresgid(gid, gid, gid) < 0)
1197 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
4de33e7f
LP
1198 }
1199
4a62c710
MS
1200 if (setresuid(uid, uid, uid) < 0)
1201 return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid);
4de33e7f
LP
1202 }
1203
1204 env = strv_env_merge(3, environ, user_env, arg_environment);
1205 if (!env)
1206 return log_oom();
1207
de158ed2
LP
1208 r = sd_bus_message_read(reply, "o", &object);
1209 if (r < 0)
1210 return bus_log_parse_error(r);
1211
1212 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1213 if (r < 0)
1214 return r;
1215
095dc596 1216 if (!arg_quiet)
f3ea87af 1217 log_info("Running scope as unit: %s", scope);
7040b626 1218
4de33e7f 1219 execvpe(argv[0], argv, env);
9b15b784
LP
1220
1221 return log_error_errno(errno, "Failed to execute: %m");
1222}
1223
1224static int start_transient_timer(
1225 sd_bus *bus,
ee451d76 1226 char **argv) {
9b15b784 1227
4afd3348
LP
1228 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1229 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
de158ed2 1230 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
9b15b784 1231 _cleanup_free_ char *timer = NULL, *service = NULL;
de158ed2 1232 const char *object = NULL;
9b15b784
LP
1233 int r;
1234
1235 assert(bus);
1236 assert(argv);
1237
de158ed2
LP
1238 r = bus_wait_for_jobs_new(bus, &w);
1239 if (r < 0)
1240 return log_oom();
1241
9b15b784 1242 if (arg_unit) {
7410616c 1243 switch (unit_name_to_type(arg_unit)) {
9b15b784
LP
1244
1245 case UNIT_SERVICE:
1246 service = strdup(arg_unit);
1247 if (!service)
1248 return log_oom();
1249
7410616c
LP
1250 r = unit_name_change_suffix(service, ".timer", &timer);
1251 if (r < 0)
1252 return log_error_errno(r, "Failed to change unit suffix: %m");
9b15b784
LP
1253 break;
1254
1255 case UNIT_TIMER:
1256 timer = strdup(arg_unit);
1257 if (!timer)
1258 return log_oom();
1259
7410616c
LP
1260 r = unit_name_change_suffix(timer, ".service", &service);
1261 if (r < 0)
1262 return log_error_errno(r, "Failed to change unit suffix: %m");
9b15b784
LP
1263 break;
1264
1265 default:
7410616c
LP
1266 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
1267 if (r < 0)
1268 return log_error_errno(r, "Failed to mangle unit name: %m");
9b15b784 1269
7410616c
LP
1270 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".timer", &timer);
1271 if (r < 0)
1272 return log_error_errno(r, "Failed to mangle unit name: %m");
9b15b784
LP
1273
1274 break;
1275 }
9c8d1e1a
LP
1276 } else {
1277 r = make_unit_name(bus, UNIT_SERVICE, &service);
1278 if (r < 0)
1279 return r;
1280
1281 r = unit_name_change_suffix(service, ".timer", &timer);
1282 if (r < 0)
1283 return log_error_errno(r, "Failed to change unit suffix: %m");
1284 }
9b15b784
LP
1285
1286 r = sd_bus_message_new_method_call(
1287 bus,
1288 &m,
1289 "org.freedesktop.systemd1",
1290 "/org/freedesktop/systemd1",
1291 "org.freedesktop.systemd1.Manager",
1292 "StartTransientUnit");
1293 if (r < 0)
1294 return bus_log_create_error(r);
1295
8c7db2fb
EV
1296 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1297 if (r < 0)
1298 return bus_log_create_error(r);
1299
9b15b784
LP
1300 /* Name and Mode */
1301 r = sd_bus_message_append(m, "ss", timer, "fail");
1302 if (r < 0)
1303 return bus_log_create_error(r);
1304
1305 /* Properties */
1306 r = sd_bus_message_open_container(m, 'a', "(sv)");
1307 if (r < 0)
1308 return bus_log_create_error(r);
1309
1310 r = transient_timer_set_properties(m);
1311 if (r < 0)
1312 return bus_log_create_error(r);
1313
1314 r = sd_bus_message_close_container(m);
1315 if (r < 0)
1316 return bus_log_create_error(r);
1317
1318 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
1319 if (r < 0)
1320 return bus_log_create_error(r);
1321
ad2c1701 1322 if (!strv_isempty(argv)) {
9b15b784
LP
1323 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
1324 if (r < 0)
1325 return bus_log_create_error(r);
1326
1327 r = sd_bus_message_append(m, "s", service);
1328 if (r < 0)
1329 return bus_log_create_error(r);
1330
1331 r = sd_bus_message_open_container(m, 'a', "(sv)");
1332 if (r < 0)
1333 return bus_log_create_error(r);
1334
1335 r = transient_service_set_properties(m, argv, NULL);
1336 if (r < 0)
1337 return bus_log_create_error(r);
1338
1339 r = sd_bus_message_close_container(m);
1340 if (r < 0)
1341 return bus_log_create_error(r);
1342
1343 r = sd_bus_message_close_container(m);
1344 if (r < 0)
1345 return bus_log_create_error(r);
1346 }
1347
1348 r = sd_bus_message_close_container(m);
1349 if (r < 0)
1350 return bus_log_create_error(r);
1351
8c7db2fb
EV
1352 polkit_agent_open_if_enabled();
1353
de158ed2 1354 r = sd_bus_call(bus, m, 0, &error, &reply);
ee451d76
LP
1355 if (r < 0) {
1356 log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
1357 return r;
1358 }
9b15b784 1359
de158ed2
LP
1360 r = sd_bus_message_read(reply, "o", &object);
1361 if (r < 0)
1362 return bus_log_parse_error(r);
1363
1364 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1365 if (r < 0)
1366 return r;
1367
3dea75de
NO
1368 if (!arg_quiet) {
1369 log_info("Running timer as unit: %s", timer);
1370 if (argv[0])
1371 log_info("Will run service as unit: %s", service);
1372 }
9b15b784
LP
1373
1374 return 0;
c2756a68
LP
1375}
1376
1377int main(int argc, char* argv[]) {
4afd3348 1378 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
c9d954b2 1379 _cleanup_free_ char *description = NULL, *command = NULL;
2a453c2e 1380 int r, retval = EXIT_SUCCESS;
c2756a68
LP
1381
1382 log_parse_environment();
1383 log_open();
1384
6c12b52e
LP
1385 r = parse_argv(argc, argv);
1386 if (r <= 0)
66b1e746 1387 goto finish;
c2756a68 1388
85eca92e
LP
1389 if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) {
1390 /* Patch in an absolute path */
1391
1392 r = find_binary(argv[optind], &command);
4c213d6c 1393 if (r < 0) {
85eca92e
LP
1394 log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
1395 goto finish;
1396 }
85eca92e 1397
4c213d6c 1398 argv[optind] = command;
c9d954b2 1399 }
c9d954b2 1400
9f2e86af
LP
1401 if (!arg_description) {
1402 description = strv_join(argv + optind, " ");
1403 if (!description) {
1404 r = log_oom();
66b1e746 1405 goto finish;
9f2e86af
LP
1406 }
1407
4c213d6c 1408 if (arg_unit && isempty(description)) {
2fc09a9c
DM
1409 r = free_and_strdup(&description, arg_unit);
1410 if (r < 0)
4c213d6c 1411 goto finish;
4c213d6c
WC
1412 }
1413
9f2e86af
LP
1414 arg_description = description;
1415 }
1416
2a453c2e
LP
1417 /* If --wait is used connect via the bus, unconditionally, as ref/unref is not supported via the limited direct
1418 * connection */
1419 if (arg_wait)
1420 r = bus_connect_transport(arg_transport, arg_host, arg_user, &bus);
1421 else
1422 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
c2756a68 1423 if (r < 0) {
da927ba9 1424 log_error_errno(r, "Failed to create bus connection: %m");
66b1e746 1425 goto finish;
c2756a68
LP
1426 }
1427
6c12b52e 1428 if (arg_scope)
ee451d76 1429 r = start_transient_scope(bus, argv + optind);
4c213d6c 1430 else if (with_timer())
ee451d76 1431 r = start_transient_timer(bus, argv + optind);
6c12b52e 1432 else
2a453c2e 1433 r = start_transient_service(bus, argv + optind, &retval);
c2756a68 1434
66b1e746 1435finish:
df31a6c0
LP
1436 strv_free(arg_environment);
1437 strv_free(arg_property);
4c213d6c 1438 strv_free(arg_timer_property);
df31a6c0 1439
2a453c2e 1440 return r < 0 ? EXIT_FAILURE : retval;
c2756a68 1441}