summaryrefslogtreecommitdiff
path: root/src/run/run.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/run/run.c')
-rw-r--r--src/run/run.c135
1 files changed, 94 insertions, 41 deletions
diff --git a/src/run/run.c b/src/run/run.c
index 0661b3bee9..5b9f31c4aa 100644
--- a/src/run/run.c
+++ b/src/run/run.c
@@ -34,9 +34,12 @@
#include "bus-error.h"
#include "calendarspec.h"
#include "ptyfwd.h"
+#include "formats-util.h"
+#include "signal-util.h"
static bool arg_scope = false;
static bool arg_remain_after_exit = false;
+static bool arg_no_block = false;
static const char *arg_unit = NULL;
static const char *arg_description = NULL;
static const char *arg_slice = NULL;
@@ -76,6 +79,7 @@ static void help(void) {
" -p --property=NAME=VALUE Set unit property\n"
" --description=TEXT Description for unit\n"
" --slice=SLICE Run in the specified slice\n"
+ " --no-block Do not wait until operation finished\n"
" -r --remain-after-exit Leave service around until explicitly stopped\n"
" --send-sighup Send SIGHUP when terminating\n"
" --service-type=TYPE Service type\n"
@@ -123,7 +127,8 @@ static int parse_argv(int argc, char *argv[]) {
ARG_ON_UNIT_ACTIVE,
ARG_ON_UNIT_INACTIVE,
ARG_ON_CALENDAR,
- ARG_TIMER_PROPERTY
+ ARG_TIMER_PROPERTY,
+ ARG_NO_BLOCK,
};
static const struct option options[] = {
@@ -154,6 +159,7 @@ static int parse_argv(int argc, char *argv[]) {
{ "on-unit-inactive", required_argument, NULL, ARG_ON_UNIT_INACTIVE },
{ "on-calendar", required_argument, NULL, ARG_ON_CALENDAR },
{ "timer-property", required_argument, NULL, ARG_TIMER_PROPERTY },
+ { "no-block", no_argument, NULL, ARG_NO_BLOCK },
{},
};
@@ -241,14 +247,12 @@ static int parse_argv(int argc, char *argv[]) {
break;
case ARG_SETENV:
-
if (strv_extend(&arg_environment, optarg) < 0)
return log_oom();
break;
case 'p':
-
if (strv_extend(&arg_property, optarg) < 0)
return log_oom();
@@ -330,6 +334,10 @@ static int parse_argv(int argc, char *argv[]) {
break;
+ case ARG_NO_BLOCK:
+ arg_no_block = true;
+ break;
+
case '?':
return -EINVAL;
@@ -389,11 +397,8 @@ static int transient_unit_set_properties(sd_bus_message *m, char **properties) {
return r;
r = bus_append_unit_property_assignment(m, *i);
- if (r < 0) {
- r = sd_bus_message_append(m, "sv", 0);
- if (r < 0)
- return r;
- }
+ if (r < 0)
+ return r;
r = sd_bus_message_close_container(m);
if (r < 0)
@@ -410,9 +415,9 @@ static int transient_cgroup_set_properties(sd_bus_message *m) {
if (!isempty(arg_slice)) {
_cleanup_free_ char *slice;
- slice = unit_name_mangle_with_suffix(arg_slice, MANGLE_NOGLOB, ".slice");
- if (!slice)
- return -ENOMEM;
+ r = unit_name_mangle_with_suffix(arg_slice, UNIT_NAME_NOGLOB, ".slice", &slice);
+ if (r < 0)
+ return r;
r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
if (r < 0)
@@ -655,8 +660,9 @@ static int start_transient_service(
sd_bus *bus,
char **argv) {
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
_cleanup_free_ char *service = NULL, *pty_path = NULL;
_cleanup_close_ int master = -1;
int r;
@@ -677,7 +683,6 @@ static int start_transient_service(
} else if (arg_transport == BUS_TRANSPORT_MACHINE) {
_cleanup_bus_unref_ sd_bus *system_bus = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
const char *s;
r = sd_bus_open_system(&system_bus);
@@ -701,6 +706,8 @@ static int start_transient_service(
if (r < 0)
return bus_log_parse_error(r);
+ reply = sd_bus_message_unref(reply);
+
master = fcntl(master, F_DUPFD_CLOEXEC, 3);
if (master < 0)
return log_error_errno(errno, "Failed to duplicate master fd: %m");
@@ -715,10 +722,16 @@ static int start_transient_service(
return log_error_errno(errno, "Failed to unlock tty: %m");
}
+ if (!arg_no_block) {
+ r = bus_wait_for_jobs_new(bus, &w);
+ if (r < 0)
+ return log_error_errno(r, "Could not watch jobs: %m");
+ }
+
if (arg_unit) {
- service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
- if (!service)
- return log_oom();
+ r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
+ if (r < 0)
+ return log_error_errno(r, "Failed to mangle unit name: %m");
} else if (asprintf(&service, "run-"PID_FMT".service", getpid()) < 0)
return log_oom();
@@ -755,12 +768,24 @@ static int start_transient_service(
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_call(bus, m, 0, &error, NULL);
+ r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
log_error("Failed to start transient service unit: %s", bus_error_message(&error, -r));
return r;
}
+ if (w) {
+ const char *object;
+
+ r = sd_bus_message_read(reply, "o", &object);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = bus_wait_for_jobs_one(w, object, arg_quiet);
+ if (r < 0)
+ return r;
+ }
+
if (master >= 0) {
_cleanup_(pty_forward_freep) PTYForward *forward = NULL;
_cleanup_event_unref_ sd_event *event = NULL;
@@ -781,7 +806,7 @@ static int start_transient_service(
if (!arg_quiet)
log_info("Running as unit %s.\nPress ^] three times within 1s to disconnect TTY.", service);
- r = pty_forward_new(event, master, false, &forward);
+ r = pty_forward_new(event, master, false, false, &forward);
if (r < 0)
return log_error_errno(r, "Failed to create PTY forwarder: %m");
@@ -807,18 +832,24 @@ static int start_transient_scope(
char **argv) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
_cleanup_strv_free_ char **env = NULL, **user_env = NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
_cleanup_free_ char *scope = NULL;
+ const char *object = NULL;
int r;
assert(bus);
assert(argv);
+ r = bus_wait_for_jobs_new(bus, &w);
+ if (r < 0)
+ return log_oom();
+
if (arg_unit) {
- scope = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
- if (!scope)
- return log_oom();
+ r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".scope", &scope);
+ if (r < 0)
+ return log_error_errno(r, "Failed to mangle scope name: %m");
} else if (asprintf(&scope, "run-"PID_FMT".scope", getpid()) < 0)
return log_oom();
@@ -855,7 +886,7 @@ static int start_transient_scope(
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_call(bus, m, 0, &error, NULL);
+ r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
log_error("Failed to start transient scope unit: %s", bus_error_message(&error, -r));
return r;
@@ -915,8 +946,16 @@ static int start_transient_scope(
if (!env)
return log_oom();
+ r = sd_bus_message_read(reply, "o", &object);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = bus_wait_for_jobs_one(w, object, arg_quiet);
+ if (r < 0)
+ return r;
+
if (!arg_quiet)
- log_info("Running as unit %s.", scope);
+ log_info("Running scope as unit %s.", scope);
execvpe(argv[0], argv, env);
@@ -928,24 +967,30 @@ static int start_transient_timer(
char **argv) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
+ _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
_cleanup_free_ char *timer = NULL, *service = NULL;
+ const char *object = NULL;
int r;
assert(bus);
assert(argv);
+ r = bus_wait_for_jobs_new(bus, &w);
+ if (r < 0)
+ return log_oom();
+
if (arg_unit) {
- switch(unit_name_to_type(arg_unit)) {
+ switch (unit_name_to_type(arg_unit)) {
case UNIT_SERVICE:
service = strdup(arg_unit);
if (!service)
return log_oom();
- timer = unit_name_change_suffix(service, ".timer");
- if (!timer)
- return log_oom();
+ r = unit_name_change_suffix(service, ".timer", &timer);
+ if (r < 0)
+ return log_error_errno(r, "Failed to change unit suffix: %m");
break;
case UNIT_TIMER:
@@ -953,19 +998,19 @@ static int start_transient_timer(
if (!timer)
return log_oom();
- service = unit_name_change_suffix(timer, ".service");
- if (!service)
- return log_oom();
+ r = unit_name_change_suffix(timer, ".service", &service);
+ if (r < 0)
+ return log_error_errno(r, "Failed to change unit suffix: %m");
break;
default:
- service = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
- if (!service)
- return log_oom();
+ r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".service", &service);
+ if (r < 0)
+ return log_error_errno(r, "Failed to mangle unit name: %m");
- timer = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".timer");
- if (!timer)
- return log_oom();
+ r = unit_name_mangle_with_suffix(arg_unit, UNIT_NAME_NOGLOB, ".timer", &timer);
+ if (r < 0)
+ return log_error_errno(r, "Failed to mangle unit name: %m");
break;
}
@@ -1035,15 +1080,23 @@ static int start_transient_timer(
if (r < 0)
return bus_log_create_error(r);
- r = sd_bus_call(bus, m, 0, &error, NULL);
+ r = sd_bus_call(bus, m, 0, &error, &reply);
if (r < 0) {
log_error("Failed to start transient timer unit: %s", bus_error_message(&error, -r));
return r;
}
- log_info("Running as unit %s.", timer);
+ r = sd_bus_message_read(reply, "o", &object);
+ if (r < 0)
+ return bus_log_parse_error(r);
+
+ r = bus_wait_for_jobs_one(w, object, arg_quiet);
+ if (r < 0)
+ return r;
+
+ log_info("Running timer as unit %s.", timer);
if (argv[0])
- log_info("Will run as unit %s.", service);
+ log_info("Will run service as unit %s.", service);
return 0;
}