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