]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/run/run.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / run / run.c
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
20 #include <getopt.h>
21 #include <stdio.h>
22
23 #include "sd-bus.h"
24 #include "sd-event.h"
25
26 #include "alloc-util.h"
27 #include "bus-error.h"
28 #include "bus-util.h"
29 #include "calendarspec.h"
30 #include "env-util.h"
31 #include "fd-util.h"
32 #include "formats-util.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "ptyfwd.h"
36 #include "signal-util.h"
37 #include "spawn-polkit-agent.h"
38 #include "strv.h"
39 #include "terminal-util.h"
40 #include "unit-name.h"
41 #include "user-util.h"
42
43 static bool arg_ask_password = true;
44 static bool arg_scope = false;
45 static bool arg_remain_after_exit = false;
46 static bool arg_no_block = false;
47 static const char *arg_unit = NULL;
48 static const char *arg_description = NULL;
49 static const char *arg_slice = NULL;
50 static bool arg_send_sighup = false;
51 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
52 static const char *arg_host = NULL;
53 static bool arg_user = false;
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;
60 static char **arg_property = NULL;
61 static bool arg_pty = false;
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;
67 static const char *arg_on_calendar = NULL;
68 static char **arg_timer_property = NULL;
69 static bool arg_quiet = false;
70
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
83 static void help(void) {
84 printf("%s [OPTIONS...] {COMMAND} [ARGS...]\n\n"
85 "Run the specified command in a transient scope or service or timer\n"
86 "unit. If timer option is specified and unit is exist which is\n"
87 "specified with --unit option then command can be omitted.\n\n"
88 " -h --help Show this help\n"
89 " --version Show package version\n"
90 " --no-ask-password Do not prompt for password\n"
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"
99 " --no-block Do not wait until operation finished\n"
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"
106 " --setenv=NAME=VALUE Set environment\n"
107 " -t --pty Run service on pseudo tty\n"
108 " -q --quiet Suppress information messages during runtime\n\n"
109 "Timer options:\n\n"
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"
115 " --on-calendar=SPEC Realtime timer\n"
116 " --timer-property=NAME=VALUE Set timer unit property\n",
117 program_invocation_short_name);
118 }
119
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
124 static int parse_argv(int argc, char *argv[]) {
125
126 enum {
127 ARG_VERSION = 0x100,
128 ARG_NO_ASK_PASSWORD,
129 ARG_USER,
130 ARG_SYSTEM,
131 ARG_SCOPE,
132 ARG_UNIT,
133 ARG_DESCRIPTION,
134 ARG_SLICE,
135 ARG_SEND_SIGHUP,
136 ARG_EXEC_USER,
137 ARG_EXEC_GROUP,
138 ARG_SERVICE_TYPE,
139 ARG_NICE,
140 ARG_SETENV,
141 ARG_TTY,
142 ARG_ON_ACTIVE,
143 ARG_ON_BOOT,
144 ARG_ON_STARTUP,
145 ARG_ON_UNIT_ACTIVE,
146 ARG_ON_UNIT_INACTIVE,
147 ARG_ON_CALENDAR,
148 ARG_TIMER_PROPERTY,
149 ARG_NO_BLOCK,
150 };
151
152 static const struct option options[] = {
153 { "help", no_argument, NULL, 'h' },
154 { "version", no_argument, NULL, ARG_VERSION },
155 { "user", no_argument, NULL, ARG_USER },
156 { "system", no_argument, NULL, ARG_SYSTEM },
157 { "scope", no_argument, NULL, ARG_SCOPE },
158 { "unit", required_argument, NULL, ARG_UNIT },
159 { "description", required_argument, NULL, ARG_DESCRIPTION },
160 { "slice", required_argument, NULL, ARG_SLICE },
161 { "remain-after-exit", no_argument, NULL, 'r' },
162 { "send-sighup", no_argument, NULL, ARG_SEND_SIGHUP },
163 { "host", required_argument, NULL, 'H' },
164 { "machine", required_argument, NULL, 'M' },
165 { "service-type", required_argument, NULL, ARG_SERVICE_TYPE },
166 { "uid", required_argument, NULL, ARG_EXEC_USER },
167 { "gid", required_argument, NULL, ARG_EXEC_GROUP },
168 { "nice", required_argument, NULL, ARG_NICE },
169 { "setenv", required_argument, NULL, ARG_SETENV },
170 { "property", required_argument, NULL, 'p' },
171 { "tty", no_argument, NULL, 't' },
172 { "quiet", no_argument, NULL, 'q' },
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 },
180 { "no-block", no_argument, NULL, ARG_NO_BLOCK },
181 { "no-ask-password", no_argument, NULL, ARG_NO_ASK_PASSWORD },
182 {},
183 };
184
185 int r, c;
186
187 assert(argc >= 0);
188 assert(argv);
189
190 while ((c = getopt_long(argc, argv, "+hrH:M:p:tq", options, NULL)) >= 0)
191
192 switch (c) {
193
194 case 'h':
195 help();
196 return 0;
197
198 case ARG_NO_ASK_PASSWORD:
199 arg_ask_password = false;
200 break;
201
202 case ARG_VERSION:
203 return version();
204
205 case ARG_USER:
206 arg_user = true;
207 break;
208
209 case ARG_SYSTEM:
210 arg_user = false;
211 break;
212
213 case ARG_SCOPE:
214 arg_scope = true;
215 break;
216
217 case ARG_UNIT:
218 arg_unit = optarg;
219 break;
220
221 case ARG_DESCRIPTION:
222 arg_description = optarg;
223 break;
224
225 case ARG_SLICE:
226 arg_slice = optarg;
227 break;
228
229 case ARG_SEND_SIGHUP:
230 arg_send_sighup = true;
231 break;
232
233 case 'r':
234 arg_remain_after_exit = true;
235 break;
236
237 case 'H':
238 arg_transport = BUS_TRANSPORT_REMOTE;
239 arg_host = optarg;
240 break;
241
242 case 'M':
243 arg_transport = BUS_TRANSPORT_MACHINE;
244 arg_host = optarg;
245 break;
246
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);
261 if (r < 0 || arg_nice < PRIO_MIN || arg_nice >= PRIO_MAX) {
262 log_error("Failed to parse nice value");
263 return -EINVAL;
264 }
265
266 arg_nice_set = true;
267 break;
268
269 case ARG_SETENV:
270 if (strv_extend(&arg_environment, optarg) < 0)
271 return log_oom();
272
273 break;
274
275 case 'p':
276 if (strv_extend(&arg_property, optarg) < 0)
277 return log_oom();
278
279 break;
280
281 case 't':
282 arg_pty = true;
283 break;
284
285 case 'q':
286 arg_quiet = true;
287 break;
288
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
339 case ARG_ON_CALENDAR: {
340 CalendarSpec *spec = NULL;
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 }
347
348 calendar_spec_free(spec);
349 arg_on_calendar = optarg;
350 break;
351 }
352
353 case ARG_TIMER_PROPERTY:
354
355 if (strv_extend(&arg_timer_property, optarg) < 0)
356 return log_oom();
357
358 break;
359
360 case ARG_NO_BLOCK:
361 arg_no_block = true;
362 break;
363
364 case '?':
365 return -EINVAL;
366
367 default:
368 assert_not_reached("Unhandled option");
369 }
370
371 if ((optind >= argc) && (!arg_unit || !with_timer())) {
372 log_error("Command line to execute required.");
373 return -EINVAL;
374 }
375
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
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.");
388 return -EINVAL;
389 }
390
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
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
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
411 return 1;
412 }
413
414 static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
415 char **i;
416 int r;
417
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) {
423 r = bus_append_unit_property_assignment(m, *i);
424 if (r < 0)
425 return r;
426 }
427
428 return 0;
429 }
430
431 static int transient_cgroup_set_properties(sd_bus_message *m) {
432 int r;
433 assert(m);
434
435 if (!isempty(arg_slice)) {
436 _cleanup_free_ char *slice;
437
438 r = unit_name_mangle_with_suffix(arg_slice, UNIT_NAME_NOGLOB, ".slice", &slice);
439 if (r < 0)
440 return r;
441
442 r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
443 if (r < 0)
444 return r;
445 }
446
447 return 0;
448 }
449
450 static int transient_kill_set_properties(sd_bus_message *m) {
451 assert(m);
452
453 if (arg_send_sighup)
454 return sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", arg_send_sighup);
455 else
456 return 0;
457 }
458
459 static int transient_service_set_properties(sd_bus_message *m, char **argv, const char *pty_path) {
460 int r;
461
462 assert(m);
463
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);
473 if (r < 0)
474 return r;
475
476 if (arg_remain_after_exit) {
477 r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit);
478 if (r < 0)
479 return r;
480 }
481
482 if (arg_service_type) {
483 r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type);
484 if (r < 0)
485 return r;
486 }
487
488 if (arg_exec_user) {
489 r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user);
490 if (r < 0)
491 return r;
492 }
493
494 if (arg_exec_group) {
495 r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group);
496 if (r < 0)
497 return r;
498 }
499
500 if (arg_nice_set) {
501 r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice);
502 if (r < 0)
503 return r;
504 }
505
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
522 n = strjoina("TERM=", e);
523 r = sd_bus_message_append(m,
524 "(sv)",
525 "Environment", "as", 1, n);
526 if (r < 0)
527 return r;
528 }
529 }
530
531 if (!strv_isempty(arg_environment)) {
532 r = sd_bus_message_open_container(m, 'r', "sv");
533 if (r < 0)
534 return r;
535
536 r = sd_bus_message_append(m, "s", "Environment");
537 if (r < 0)
538 return r;
539
540 r = sd_bus_message_open_container(m, 'v', "as");
541 if (r < 0)
542 return r;
543
544 r = sd_bus_message_append_strv(m, arg_environment);
545 if (r < 0)
546 return r;
547
548 r = sd_bus_message_close_container(m);
549 if (r < 0)
550 return r;
551
552 r = sd_bus_message_close_container(m);
553 if (r < 0)
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
611 static 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
624 r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid());
625 if (r < 0)
626 return r;
627
628 return 0;
629 }
630
631 static int transient_timer_set_properties(sd_bus_message *m) {
632 int r;
633
634 assert(m);
635
636 r = transient_unit_set_properties(m, arg_timer_property);
637 if (r < 0)
638 return r;
639
640 /* Automatically clean up our transient timers */
641 r = sd_bus_message_append(m, "(sv)", "RemainAfterElapse", "b", false);
642 if (r < 0)
643 return r;
644
645 if (arg_on_active) {
646 r = sd_bus_message_append(m, "(sv)", "OnActiveSec", "t", arg_on_active);
647 if (r < 0)
648 return r;
649 }
650
651 if (arg_on_boot) {
652 r = sd_bus_message_append(m, "(sv)", "OnBootSec", "t", arg_on_boot);
653 if (r < 0)
654 return r;
655 }
656
657 if (arg_on_startup) {
658 r = sd_bus_message_append(m, "(sv)", "OnStartupSec", "t", arg_on_startup);
659 if (r < 0)
660 return r;
661 }
662
663 if (arg_on_unit_active) {
664 r = sd_bus_message_append(m, "(sv)", "OnUnitActiveSec", "t", arg_on_unit_active);
665 if (r < 0)
666 return r;
667 }
668
669 if (arg_on_unit_inactive) {
670 r = sd_bus_message_append(m, "(sv)", "OnUnitInactiveSec", "t", arg_on_unit_inactive);
671 if (r < 0)
672 return r;
673 }
674
675 if (arg_on_calendar) {
676 r = sd_bus_message_append(m, "(sv)", "OnCalendar", "s", arg_on_calendar);
677 if (r < 0)
678 return r;
679 }
680
681 return 0;
682 }
683
684 static int make_unit_name(sd_bus *bus, UnitType t, char **ret) {
685 const char *unique, *id;
686 char *p;
687 int r;
688
689 assert(bus);
690 assert(t >= 0);
691 assert(t < _UNIT_TYPE_MAX);
692
693 r = sd_bus_get_unique_name(bus, &unique);
694 if (r < 0) {
695 sd_id128_t rnd;
696
697 /* We couldn't get the unique name, which is a pretty
698 * common case if we are connected to systemd
699 * directly. In that case, just pick a random uuid as
700 * name */
701
702 r = sd_id128_randomize(&rnd);
703 if (r < 0)
704 return log_error_errno(r, "Failed to generate random run unit name: %m");
705
706 if (asprintf(ret, "run-r" SD_ID128_FORMAT_STR ".%s", SD_ID128_FORMAT_VAL(rnd), unit_type_to_string(t)) < 0)
707 return log_oom();
708
709 return 0;
710 }
711
712 /* We managed to get the unique name, then let's use that to
713 * name our transient units. */
714
715 id = startswith(unique, ":1.");
716 if (!id) {
717 log_error("Unique name %s has unexpected format.", unique);
718 return -EINVAL;
719 }
720
721 p = strjoin("run-u", id, ".", unit_type_to_string(t), NULL);
722 if (!p)
723 return log_oom();
724
725 *ret = p;
726 return 0;
727 }
728
729 static int start_transient_service(
730 sd_bus *bus,
731 char **argv) {
732
733 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
734 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
735 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
736 _cleanup_free_ char *service = NULL, *pty_path = NULL;
737 _cleanup_close_ int master = -1;
738 int r;
739
740 assert(bus);
741 assert(argv);
742
743 if (arg_pty) {
744
745 if (arg_transport == BUS_TRANSPORT_LOCAL) {
746 master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
747 if (master < 0)
748 return log_error_errno(errno, "Failed to acquire pseudo tty: %m");
749
750 r = ptsname_malloc(master, &pty_path);
751 if (r < 0)
752 return log_error_errno(r, "Failed to determine tty name: %m");
753
754 if (unlockpt(master) < 0)
755 return log_error_errno(errno, "Failed to unlock tty: %m");
756
757 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
758 _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
759 const char *s;
760
761 r = sd_bus_default_system(&system_bus);
762 if (r < 0)
763 return log_error_errno(r, "Failed to connect to system bus: %m");
764
765 r = sd_bus_call_method(system_bus,
766 "org.freedesktop.machine1",
767 "/org/freedesktop/machine1",
768 "org.freedesktop.machine1.Manager",
769 "OpenMachinePTY",
770 &error,
771 &reply,
772 "s", arg_host);
773 if (r < 0) {
774 log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r));
775 return r;
776 }
777
778 r = sd_bus_message_read(reply, "hs", &master, &s);
779 if (r < 0)
780 return bus_log_parse_error(r);
781
782 reply = sd_bus_message_unref(reply);
783
784 master = fcntl(master, F_DUPFD_CLOEXEC, 3);
785 if (master < 0)
786 return log_error_errno(errno, "Failed to duplicate master fd: %m");
787
788 pty_path = strdup(s);
789 if (!pty_path)
790 return log_oom();
791 } else
792 assert_not_reached("Can't allocate tty via ssh");
793 }
794
795 if (!arg_no_block) {
796 r = bus_wait_for_jobs_new(bus, &w);
797 if (r < 0)
798 return log_error_errno(r, "Could not watch jobs: %m");
799 }
800
801 if (arg_unit) {
802 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
803 if (r < 0)
804 return log_error_errno(r, "Failed to mangle unit name: %m");
805 } else {
806 r = make_unit_name(bus, UNIT_SERVICE, &service);
807 if (r < 0)
808 return r;
809 }
810
811 r = sd_bus_message_new_method_call(
812 bus,
813 &m,
814 "org.freedesktop.systemd1",
815 "/org/freedesktop/systemd1",
816 "org.freedesktop.systemd1.Manager",
817 "StartTransientUnit");
818 if (r < 0)
819 return bus_log_create_error(r);
820
821 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
822 if (r < 0)
823 return bus_log_create_error(r);
824
825 /* Name and mode */
826 r = sd_bus_message_append(m, "ss", service, "fail");
827 if (r < 0)
828 return bus_log_create_error(r);
829
830 /* Properties */
831 r = sd_bus_message_open_container(m, 'a', "(sv)");
832 if (r < 0)
833 return bus_log_create_error(r);
834
835 r = transient_service_set_properties(m, argv, pty_path);
836 if (r < 0)
837 return bus_log_create_error(r);
838
839 r = sd_bus_message_close_container(m);
840 if (r < 0)
841 return bus_log_create_error(r);
842
843 /* Auxiliary units */
844 r = sd_bus_message_append(m, "a(sa(sv))", 0);
845 if (r < 0)
846 return bus_log_create_error(r);
847
848 polkit_agent_open_if_enabled();
849
850 r = sd_bus_call(bus, m, 0, &error, &reply);
851 if (r < 0)
852 return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
853
854 if (w) {
855 const char *object;
856
857 r = sd_bus_message_read(reply, "o", &object);
858 if (r < 0)
859 return bus_log_parse_error(r);
860
861 r = bus_wait_for_jobs_one(w, object, arg_quiet);
862 if (r < 0)
863 return r;
864 }
865
866 if (master >= 0) {
867 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
868 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
869 char last_char = 0;
870
871 r = sd_event_default(&event);
872 if (r < 0)
873 return log_error_errno(r, "Failed to get event loop: %m");
874
875 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
876
877 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
878 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
879
880 if (!arg_quiet)
881 log_info("Running as unit %s.\nPress ^] three times within 1s to disconnect TTY.", service);
882
883 r = pty_forward_new(event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &forward);
884 if (r < 0)
885 return log_error_errno(r, "Failed to create PTY forwarder: %m");
886
887 r = sd_event_loop(event);
888 if (r < 0)
889 return log_error_errno(r, "Failed to run event loop: %m");
890
891 pty_forward_get_last_char(forward, &last_char);
892
893 forward = pty_forward_free(forward);
894
895 if (!arg_quiet && last_char != '\n')
896 fputc('\n', stdout);
897
898 } else if (!arg_quiet)
899 log_info("Running as unit %s.", service);
900
901 return 0;
902 }
903
904 static int start_transient_scope(
905 sd_bus *bus,
906 char **argv) {
907
908 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
909 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
910 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
911 _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
912 _cleanup_free_ char *scope = NULL;
913 const char *object = NULL;
914 int r;
915
916 assert(bus);
917 assert(argv);
918
919 r = bus_wait_for_jobs_new(bus, &w);
920 if (r < 0)
921 return log_oom();
922
923 if (arg_unit) {
924 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".scope", &scope);
925 if (r < 0)
926 return log_error_errno(r, "Failed to mangle scope name: %m");
927 } else {
928 r = make_unit_name(bus, UNIT_SCOPE, &scope);
929 if (r < 0)
930 return r;
931 }
932
933 r = sd_bus_message_new_method_call(
934 bus,
935 &m,
936 "org.freedesktop.systemd1",
937 "/org/freedesktop/systemd1",
938 "org.freedesktop.systemd1.Manager",
939 "StartTransientUnit");
940 if (r < 0)
941 return bus_log_create_error(r);
942
943 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
944 if (r < 0)
945 return bus_log_create_error(r);
946
947 /* Name and Mode */
948 r = sd_bus_message_append(m, "ss", scope, "fail");
949 if (r < 0)
950 return bus_log_create_error(r);
951
952 /* Properties */
953 r = sd_bus_message_open_container(m, 'a', "(sv)");
954 if (r < 0)
955 return bus_log_create_error(r);
956
957 r = transient_scope_set_properties(m);
958 if (r < 0)
959 return bus_log_create_error(r);
960
961 r = sd_bus_message_close_container(m);
962 if (r < 0)
963 return bus_log_create_error(r);
964
965 /* Auxiliary units */
966 r = sd_bus_message_append(m, "a(sa(sv))", 0);
967 if (r < 0)
968 return bus_log_create_error(r);
969
970 polkit_agent_open_if_enabled();
971
972 r = sd_bus_call(bus, m, 0, &error, &reply);
973 if (r < 0) {
974 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
975 return r;
976 }
977
978 if (arg_nice_set) {
979 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
980 return log_error_errno(errno, "Failed to set nice level: %m");
981 }
982
983 if (arg_exec_group) {
984 gid_t gid;
985
986 r = get_group_creds(&arg_exec_group, &gid);
987 if (r < 0)
988 return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
989
990 if (setresgid(gid, gid, gid) < 0)
991 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
992 }
993
994 if (arg_exec_user) {
995 const char *home, *shell;
996 uid_t uid;
997 gid_t gid;
998
999 r = get_user_creds(&arg_exec_user, &uid, &gid, &home, &shell);
1000 if (r < 0)
1001 return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
1002
1003 r = strv_extendf(&user_env, "HOME=%s", home);
1004 if (r < 0)
1005 return log_oom();
1006
1007 r = strv_extendf(&user_env, "SHELL=%s", shell);
1008 if (r < 0)
1009 return log_oom();
1010
1011 r = strv_extendf(&user_env, "USER=%s", arg_exec_user);
1012 if (r < 0)
1013 return log_oom();
1014
1015 r = strv_extendf(&user_env, "LOGNAME=%s", arg_exec_user);
1016 if (r < 0)
1017 return log_oom();
1018
1019 if (!arg_exec_group) {
1020 if (setresgid(gid, gid, gid) < 0)
1021 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
1022 }
1023
1024 if (setresuid(uid, uid, uid) < 0)
1025 return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid);
1026 }
1027
1028 env = strv_env_merge(3, environ, user_env, arg_environment);
1029 if (!env)
1030 return log_oom();
1031
1032 r = sd_bus_message_read(reply, "o", &object);
1033 if (r < 0)
1034 return bus_log_parse_error(r);
1035
1036 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1037 if (r < 0)
1038 return r;
1039
1040 if (!arg_quiet)
1041 log_info("Running scope as unit %s.", scope);
1042
1043 execvpe(argv[0], argv, env);
1044
1045 return log_error_errno(errno, "Failed to execute: %m");
1046 }
1047
1048 static int start_transient_timer(
1049 sd_bus *bus,
1050 char **argv) {
1051
1052 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1053 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
1054 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1055 _cleanup_free_ char *timer = NULL, *service = NULL;
1056 const char *object = NULL;
1057 int r;
1058
1059 assert(bus);
1060 assert(argv);
1061
1062 r = bus_wait_for_jobs_new(bus, &w);
1063 if (r < 0)
1064 return log_oom();
1065
1066 if (arg_unit) {
1067 switch (unit_name_to_type(arg_unit)) {
1068
1069 case UNIT_SERVICE:
1070 service = strdup(arg_unit);
1071 if (!service)
1072 return log_oom();
1073
1074 r = unit_name_change_suffix(service, ".timer", &timer);
1075 if (r < 0)
1076 return log_error_errno(r, "Failed to change unit suffix: %m");
1077 break;
1078
1079 case UNIT_TIMER:
1080 timer = strdup(arg_unit);
1081 if (!timer)
1082 return log_oom();
1083
1084 r = unit_name_change_suffix(timer, ".service", &service);
1085 if (r < 0)
1086 return log_error_errno(r, "Failed to change unit suffix: %m");
1087 break;
1088
1089 default:
1090 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
1091 if (r < 0)
1092 return log_error_errno(r, "Failed to mangle unit name: %m");
1093
1094 r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".timer", &timer);
1095 if (r < 0)
1096 return log_error_errno(r, "Failed to mangle unit name: %m");
1097
1098 break;
1099 }
1100 } else {
1101 r = make_unit_name(bus, UNIT_SERVICE, &service);
1102 if (r < 0)
1103 return r;
1104
1105 r = unit_name_change_suffix(service, ".timer", &timer);
1106 if (r < 0)
1107 return log_error_errno(r, "Failed to change unit suffix: %m");
1108 }
1109
1110 r = sd_bus_message_new_method_call(
1111 bus,
1112 &m,
1113 "org.freedesktop.systemd1",
1114 "/org/freedesktop/systemd1",
1115 "org.freedesktop.systemd1.Manager",
1116 "StartTransientUnit");
1117 if (r < 0)
1118 return bus_log_create_error(r);
1119
1120 r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password);
1121 if (r < 0)
1122 return bus_log_create_error(r);
1123
1124 /* Name and Mode */
1125 r = sd_bus_message_append(m, "ss", timer, "fail");
1126 if (r < 0)
1127 return bus_log_create_error(r);
1128
1129 /* Properties */
1130 r = sd_bus_message_open_container(m, 'a', "(sv)");
1131 if (r < 0)
1132 return bus_log_create_error(r);
1133
1134 r = transient_timer_set_properties(m);
1135 if (r < 0)
1136 return bus_log_create_error(r);
1137
1138 r = sd_bus_message_close_container(m);
1139 if (r < 0)
1140 return bus_log_create_error(r);
1141
1142 r = sd_bus_message_open_container(m, 'a', "(sa(sv))");
1143 if (r < 0)
1144 return bus_log_create_error(r);
1145
1146 if (argv[0]) {
1147 r = sd_bus_message_open_container(m, 'r', "sa(sv)");
1148 if (r < 0)
1149 return bus_log_create_error(r);
1150
1151 r = sd_bus_message_append(m, "s", service);
1152 if (r < 0)
1153 return bus_log_create_error(r);
1154
1155 r = sd_bus_message_open_container(m, 'a', "(sv)");
1156 if (r < 0)
1157 return bus_log_create_error(r);
1158
1159 r = transient_service_set_properties(m, argv, NULL);
1160 if (r < 0)
1161 return bus_log_create_error(r);
1162
1163 r = sd_bus_message_close_container(m);
1164 if (r < 0)
1165 return bus_log_create_error(r);
1166
1167 r = sd_bus_message_close_container(m);
1168 if (r < 0)
1169 return bus_log_create_error(r);
1170 }
1171
1172 r = sd_bus_message_close_container(m);
1173 if (r < 0)
1174 return bus_log_create_error(r);
1175
1176 polkit_agent_open_if_enabled();
1177
1178 r = sd_bus_call(bus, m, 0, &error, &reply);
1179 if (r < 0) {
1180 log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
1181 return r;
1182 }
1183
1184 r = sd_bus_message_read(reply, "o", &object);
1185 if (r < 0)
1186 return bus_log_parse_error(r);
1187
1188 r = bus_wait_for_jobs_one(w, object, arg_quiet);
1189 if (r < 0)
1190 return r;
1191
1192 log_info("Running timer as unit %s.", timer);
1193 if (argv[0])
1194 log_info("Will run service as unit %s.", service);
1195
1196 return 0;
1197 }
1198
1199 int main(int argc, char* argv[]) {
1200 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1201 _cleanup_free_ char *description = NULL, *command = NULL;
1202 int r;
1203
1204 log_parse_environment();
1205 log_open();
1206
1207 r = parse_argv(argc, argv);
1208 if (r <= 0)
1209 goto finish;
1210
1211 if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) {
1212 /* Patch in an absolute path */
1213
1214 r = find_binary(argv[optind], &command);
1215 if (r < 0) {
1216 log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
1217 goto finish;
1218 }
1219
1220 argv[optind] = command;
1221 }
1222
1223 if (!arg_description) {
1224 description = strv_join(argv + optind, " ");
1225 if (!description) {
1226 r = log_oom();
1227 goto finish;
1228 }
1229
1230 if (arg_unit && isempty(description)) {
1231 r = free_and_strdup(&description, arg_unit);
1232 if (r < 0)
1233 goto finish;
1234 }
1235
1236 arg_description = description;
1237 }
1238
1239 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1240 if (r < 0) {
1241 log_error_errno(r, "Failed to create bus connection: %m");
1242 goto finish;
1243 }
1244
1245 if (arg_scope)
1246 r = start_transient_scope(bus, argv + optind);
1247 else if (with_timer())
1248 r = start_transient_timer(bus, argv + optind);
1249 else
1250 r = start_transient_service(bus, argv + optind);
1251
1252 finish:
1253 strv_free(arg_environment);
1254 strv_free(arg_property);
1255 strv_free(arg_timer_property);
1256
1257 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1258 }