X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Frun%2Frun.c;h=b9be1455c42dcc8c8cd38cbcdd1c0581a05fe1a3;hb=be63641349b1350c6b33976791eb89af7427de7b;hp=c6d3aabbdbc040dbff3182b5bf092c4ca11a6b7f;hpb=4de33e7f3238a6fe616e61139ab87e221572e5e5;p=elogind.git diff --git a/src/run/run.c b/src/run/run.c index c6d3aabbd..b9be1455c 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -249,8 +249,6 @@ static int message_start_transient_unit_new(sd_bus *bus, const char *name, sd_bu assert(name); assert(ret); - log_info("Running as unit %s.", name); - r = sd_bus_message_new_method_call( bus, &m, @@ -337,122 +335,128 @@ static int start_transient_service( _cleanup_free_ char *name = NULL; int r; - if (arg_unit) + if (arg_unit) { name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service"); - else - asprintf(&name, "run-%lu.service", (unsigned long) getpid()); - if (!name) - return -ENOMEM; + if (!name) + return log_oom(); + } else if (asprintf(&name, "run-"PID_FMT".service", getpid()) < 0) + return log_oom(); r = message_start_transient_unit_new(bus, name, &m); if (r < 0) - return r; + return bus_log_create_error(r); if (arg_remain_after_exit) { r = sd_bus_message_append(m, "(sv)", "RemainAfterExit", "b", arg_remain_after_exit); if (r < 0) - return r; + return bus_log_create_error(r); } if (arg_service_type) { r = sd_bus_message_append(m, "(sv)", "Type", "s", arg_service_type); if (r < 0) - return r; + return bus_log_create_error(r); } if (arg_exec_user) { r = sd_bus_message_append(m, "(sv)", "User", "s", arg_exec_user); if (r < 0) - return r; + return bus_log_create_error(r); } if (arg_exec_group) { r = sd_bus_message_append(m, "(sv)", "Group", "s", arg_exec_group); if (r < 0) - return r; + return bus_log_create_error(r); } if (arg_nice_set) { r = sd_bus_message_append(m, "(sv)", "Nice", "i", arg_nice); if (r < 0) - return r; + return bus_log_create_error(r); } if (!strv_isempty(arg_environment)) { r = sd_bus_message_open_container(m, 'r', "sv"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append(m, "s", "Environment"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_open_container(m, 'v', "as"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append_strv(m, arg_environment); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); } r = sd_bus_message_open_container(m, 'r', "sv"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append(m, "s", "ExecStart"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_open_container(m, 'v', "a(sasb)"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_open_container(m, 'a', "(sasb)"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_open_container(m, 'r', "sasb"); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append(m, "s", argv[0]); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append_strv(m, argv); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append(m, "b", false); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) - return r; + return bus_log_create_error(r); + + r = message_start_transient_unit_send(bus, m, error, NULL); + if (r < 0) + return bus_log_create_error(r); + + log_info("Running as unit %s.", name); - return message_start_transient_unit_send(bus, m, error, NULL); + return 0; } static int start_transient_scope( @@ -467,24 +471,24 @@ static int start_transient_scope( assert(bus); - if (arg_unit) + if (arg_unit) { name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope"); - else - asprintf(&name, "run-%lu.scope", (unsigned long) getpid()); - if (!name) - return -ENOMEM; + if (!name) + return log_oom(); + } else if (asprintf(&name, "run-"PID_FMT".scope", getpid()) < 0) + return log_oom(); r = message_start_transient_unit_new(bus, name, &m); if (r < 0) - return r; + return bus_log_create_error(r); r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, (uint32_t) getpid()); if (r < 0) - return r; + return bus_log_create_error(r); r = message_start_transient_unit_send(bus, m, error, NULL); if (r < 0) - return r; + return bus_log_create_error(r); if (arg_nice_set) { if (setpriority(PRIO_PROCESS, 0, arg_nice) < 0) { @@ -552,6 +556,8 @@ static int start_transient_scope( if (!env) return log_oom(); + log_info("Running as unit %s.", name); + execvpe(argv[0], argv, env); log_error("Failed to execute: %m"); return -errno; @@ -587,7 +593,7 @@ int main(int argc, char* argv[]) { arg_description = description; } - r = bus_open_transport(arg_transport, arg_host, arg_user, &bus); + r = bus_open_transport_systemd(arg_transport, arg_host, arg_user, &bus); if (r < 0) { log_error("Failed to create bus connection: %s", strerror(-r)); goto finish; @@ -597,8 +603,6 @@ int main(int argc, char* argv[]) { r = start_transient_scope(bus, argv + optind, &error); else r = start_transient_service(bus, argv + optind, &error); - if (r < 0) - log_error("Failed start transient unit: %s", bus_error_message(&error, r)); finish: strv_free(arg_environment);