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