]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/run/run.c
run: do not try to use reply after freeing it (#3318)
[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 or timer\n"
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"
89 " -h --help Show this help\n"
90 " --version Show package version\n"
91 " --no-ask-password Do not prompt for password\n"
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"
100 " --no-block Do not wait until operation finished\n"
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"
107 " -E --setenv=NAME=VALUE Set environment\n"
108 " -t --pty Run service on pseudo tty\n"
109 " -q --quiet Suppress information messages during runtime\n\n"
110 "Timer options:\n\n"
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"
116 " --on-calendar=SPEC Realtime timer\n"
117 " --timer-property=NAME=VALUE Set timer unit property\n",
118 program_invocation_short_name);
119 }
120
121 static 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
125 static int parse_argv(int argc, char *argv[]) {
126
127 enum {
128 ARG_VERSION = 0x100,
129 ARG_USER,
130 ARG_SYSTEM,
131 ARG_SCOPE,
132 ARG_UNIT,
133 ARG_DESCRIPTION,
134 ARG_SLICE,
135 ARG_SEND_SIGHUP,
136 ARG_SERVICE_TYPE,
137 ARG_EXEC_USER,
138 ARG_EXEC_GROUP,
139 ARG_NICE,
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,
146 ARG_TIMER_PROPERTY,
147 ARG_NO_BLOCK,
148 ARG_NO_ASK_PASSWORD,
149 };
150
151 static const struct option options[] = {
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 },
168 { "setenv", required_argument, NULL, 'E' },
169 { "property", required_argument, NULL, 'p' },
170 { "tty", no_argument, NULL, 't' }, /* deprecated */
171 { "pty", 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 'E':
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 = transient_cgroup_set_properties(m);
625 if (r < 0)
626 return r;
627
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
635 static int transient_timer_set_properties(sd_bus_message *m) {
636 int r;
637
638 assert(m);
639
640 r = transient_unit_set_properties(m, arg_timer_property);
641 if (r < 0)
642 return r;
643
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
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;
653 }
654
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
688 static 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
733 static int start_transient_service(
734 sd_bus *bus,
735 char **argv) {
736
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;
739 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
740 _cleanup_free_ char *service = NULL, *pty_path = NULL;
741 _cleanup_close_ int master = -1;
742 int r;
743
744 assert(bus);
745 assert(argv);
746
747 if (arg_pty) {
748
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
758 if (unlockpt(master) < 0)
759 return log_error_errno(errno, "Failed to unlock tty: %m");
760
761 } else if (arg_transport == BUS_TRANSPORT_MACHINE) {
762 _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL;
763 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL;
764 const char *s;
765
766 r = sd_bus_default_system(&system_bus);
767 if (r < 0)
768 return log_error_errno(r, "Failed to connect to system bus: %m");
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,
776 &pty_reply,
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
783 r = sd_bus_message_read(pty_reply, "hs", &master, &s);
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");
796 }
797
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
804 if (arg_unit) {
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");
808 } else {
809 r = make_unit_name(bus, UNIT_SERVICE, &service);
810 if (r < 0)
811 return r;
812 }
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");
821 if (r < 0)
822 return bus_log_create_error(r);
823
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
828 /* Name and mode */
829 r = sd_bus_message_append(m, "ss", service, "fail");
830 if (r < 0)
831 return bus_log_create_error(r);
832
833 /* Properties */
834 r = sd_bus_message_open_container(m, 'a', "(sv)");
835 if (r < 0)
836 return bus_log_create_error(r);
837
838 r = transient_service_set_properties(m, argv, pty_path);
839 if (r < 0)
840 return bus_log_create_error(r);
841
842 r = sd_bus_message_close_container(m);
843 if (r < 0)
844 return bus_log_create_error(r);
845
846 /* Auxiliary units */
847 r = sd_bus_message_append(m, "a(sa(sv))", 0);
848 if (r < 0)
849 return bus_log_create_error(r);
850
851 polkit_agent_open_if_enabled();
852
853 r = sd_bus_call(bus, m, 0, &error, &reply);
854 if (r < 0)
855 return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, r));
856
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
869 if (master >= 0) {
870 _cleanup_(pty_forward_freep) PTYForward *forward = NULL;
871 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
872 char last_char = 0;
873
874 r = sd_event_default(&event);
875 if (r < 0)
876 return log_error_errno(r, "Failed to get event loop: %m");
877
878 assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0);
879
880 (void) sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
881 (void) sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
882
883 if (!arg_quiet)
884 log_info("Running as unit: %s\nPress ^] three times within 1s to disconnect TTY.", service);
885
886 r = pty_forward_new(event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &forward);
887 if (r < 0)
888 return log_error_errno(r, "Failed to create PTY forwarder: %m");
889
890 r = sd_event_loop(event);
891 if (r < 0)
892 return log_error_errno(r, "Failed to run event loop: %m");
893
894 pty_forward_get_last_char(forward, &last_char);
895
896 forward = pty_forward_free(forward);
897
898 if (!arg_quiet && last_char != '\n')
899 fputc('\n', stdout);
900
901 } else if (!arg_quiet)
902 log_info("Running as unit: %s", service);
903
904 return 0;
905 }
906
907 static int start_transient_scope(
908 sd_bus *bus,
909 char **argv) {
910
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;
913 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
914 _cleanup_strv_free_ char **env = NULL, **user_env = NULL;
915 _cleanup_free_ char *scope = NULL;
916 const char *object = NULL;
917 int r;
918
919 assert(bus);
920 assert(argv);
921
922 r = bus_wait_for_jobs_new(bus, &w);
923 if (r < 0)
924 return log_oom();
925
926 if (arg_unit) {
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");
930 } else {
931 r = make_unit_name(bus, UNIT_SCOPE, &scope);
932 if (r < 0)
933 return r;
934 }
935
936 r = sd_bus_message_new_method_call(
937 bus,
938 &m,
939 "org.freedesktop.systemd1",
940 "/org/freedesktop/systemd1",
941 "org.freedesktop.systemd1.Manager",
942 "StartTransientUnit");
943 if (r < 0)
944 return bus_log_create_error(r);
945
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
950 /* Name and Mode */
951 r = sd_bus_message_append(m, "ss", scope, "fail");
952 if (r < 0)
953 return bus_log_create_error(r);
954
955 /* Properties */
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
968 /* Auxiliary units */
969 r = sd_bus_message_append(m, "a(sa(sv))", 0);
970 if (r < 0)
971 return bus_log_create_error(r);
972
973 polkit_agent_open_if_enabled();
974
975 r = sd_bus_call(bus, m, 0, &error, &reply);
976 if (r < 0) {
977 log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
978 return r;
979 }
980
981 if (arg_nice_set) {
982 if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0)
983 return log_error_errno(errno, "Failed to set nice level: %m");
984 }
985
986 if (arg_exec_group) {
987 gid_t gid;
988
989 r = get_group_creds(&arg_exec_group, &gid);
990 if (r < 0)
991 return log_error_errno(r, "Failed to resolve group %s: %m", arg_exec_group);
992
993 if (setresgid(gid, gid, gid) < 0)
994 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
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);
1003 if (r < 0)
1004 return log_error_errno(r, "Failed to resolve user %s: %m", arg_exec_user);
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) {
1023 if (setresgid(gid, gid, gid) < 0)
1024 return log_error_errno(errno, "Failed to change GID to " GID_FMT ": %m", gid);
1025 }
1026
1027 if (setresuid(uid, uid, uid) < 0)
1028 return log_error_errno(errno, "Failed to change UID to " UID_FMT ": %m", uid);
1029 }
1030
1031 env = strv_env_merge(3, environ, user_env, arg_environment);
1032 if (!env)
1033 return log_oom();
1034
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
1043 if (!arg_quiet)
1044 log_info("Running scope as unit: %s", scope);
1045
1046 execvpe(argv[0], argv, env);
1047
1048 return log_error_errno(errno, "Failed to execute: %m");
1049 }
1050
1051 static int start_transient_timer(
1052 sd_bus *bus,
1053 char **argv) {
1054
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;
1057 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
1058 _cleanup_free_ char *timer = NULL, *service = NULL;
1059 const char *object = NULL;
1060 int r;
1061
1062 assert(bus);
1063 assert(argv);
1064
1065 r = bus_wait_for_jobs_new(bus, &w);
1066 if (r < 0)
1067 return log_oom();
1068
1069 if (arg_unit) {
1070 switch (unit_name_to_type(arg_unit)) {
1071
1072 case UNIT_SERVICE:
1073 service = strdup(arg_unit);
1074 if (!service)
1075 return log_oom();
1076
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");
1080 break;
1081
1082 case UNIT_TIMER:
1083 timer = strdup(arg_unit);
1084 if (!timer)
1085 return log_oom();
1086
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");
1090 break;
1091
1092 default:
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");
1096
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");
1100
1101 break;
1102 }
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 }
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
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
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
1179 polkit_agent_open_if_enabled();
1180
1181 r = sd_bus_call(bus, m, 0, &error, &reply);
1182 if (r < 0) {
1183 log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
1184 return r;
1185 }
1186
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
1195 log_info("Running timer as unit: %s", timer);
1196 if (argv[0])
1197 log_info("Will run service as unit: %s", service);
1198
1199 return 0;
1200 }
1201
1202 int main(int argc, char* argv[]) {
1203 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
1204 _cleanup_free_ char *description = NULL, *command = NULL;
1205 int r;
1206
1207 log_parse_environment();
1208 log_open();
1209
1210 r = parse_argv(argc, argv);
1211 if (r <= 0)
1212 goto finish;
1213
1214 if (argc > optind && arg_transport == BUS_TRANSPORT_LOCAL) {
1215 /* Patch in an absolute path */
1216
1217 r = find_binary(argv[optind], &command);
1218 if (r < 0) {
1219 log_error_errno(r, "Failed to find executable %s: %m", argv[optind]);
1220 goto finish;
1221 }
1222
1223 argv[optind] = command;
1224 }
1225
1226 if (!arg_description) {
1227 description = strv_join(argv + optind, " ");
1228 if (!description) {
1229 r = log_oom();
1230 goto finish;
1231 }
1232
1233 if (arg_unit && isempty(description)) {
1234 r = free_and_strdup(&description, arg_unit);
1235 if (r < 0)
1236 goto finish;
1237 }
1238
1239 arg_description = description;
1240 }
1241
1242 r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
1243 if (r < 0) {
1244 log_error_errno(r, "Failed to create bus connection: %m");
1245 goto finish;
1246 }
1247
1248 if (arg_scope)
1249 r = start_transient_scope(bus, argv + optind);
1250 else if (with_timer())
1251 r = start_transient_timer(bus, argv + optind);
1252 else
1253 r = start_transient_service(bus, argv + optind);
1254
1255 finish:
1256 strv_free(arg_environment);
1257 strv_free(arg_property);
1258 strv_free(arg_timer_property);
1259
1260 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
1261 }