static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; Operation *o = userdata; int r; assert(o); assert(si); log_debug("Operating " PID_FMT " is now complete with code=%s status=%i", o->pid, sigchld_code_to_string(si->si_code), si->si_status); o->pid = 0; if (si->si_code != CLD_EXITED) { r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally."); goto fail; } if (si->si_status == EXIT_SUCCESS) r = 0; else if (read(o->errno_fd, &r, sizeof(r)) != sizeof(r)) { /* Try to acquire error code for failed operation */ r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child failed."); goto fail; } if (o->done) { /* A completion routine is set for this operation, call it. */ r = o->done(o, r, &error); if (r < 0) { if (!sd_bus_error_is_set(&error)) sd_bus_error_set_errno(&error, r); goto fail; } } else { /* The default operation when done is to simply return an error on failure or an empty success * message on success. */ if (r < 0) { sd_bus_error_set_errno(&error, r); goto fail; } r = sd_bus_reply_method_return(o->message, NULL); if (r < 0) log_error_errno(r, "Failed to reply to message: %m"); } operation_free(o); return 0; fail: r = sd_bus_reply_method_error(o->message, &error); if (r < 0) log_error_errno(r, "Failed to reply to message: %m"); operation_free(o); return 0; }
static int busname_peek_message(BusName *n) { struct kdbus_cmd_recv cmd_recv = { .size = sizeof(cmd_recv), .flags = KDBUS_RECV_PEEK, }; struct kdbus_cmd_free cmd_free = { .size = sizeof(cmd_free), }; const char *comm = NULL; struct kdbus_item *d; struct kdbus_msg *k; size_t start, ps, sz, delta; void *p = NULL; pid_t pid = 0; int r; /* Generate a friendly debug log message about which process * caused triggering of this bus name. This simply peeks the * metadata of the first queued message and logs it. */ assert(n); /* Let's shortcut things a bit, if debug logging is turned off * anyway. */ if (log_get_max_level() < LOG_DEBUG) return 0; r = ioctl(n->starter_fd, KDBUS_CMD_RECV, &cmd_recv); if (r < 0) { if (errno == EINTR || errno == EAGAIN) return 0; log_unit_error(UNIT(n)->id, "%s: Failed to query activation message: %m", UNIT(n)->id); return -errno; } /* We map as late as possible, and unmap imemdiately after * use. On 32bit address space is scarce and we want to be * able to handle a lot of activator connections at the same * time, and hence shouldn't keep the mmap()s around for * longer than necessary. */ ps = page_size(); start = (cmd_recv.msg.offset / ps) * ps; delta = cmd_recv.msg.offset - start; sz = PAGE_ALIGN(delta + cmd_recv.msg.msg_size); p = mmap(NULL, sz, PROT_READ, MAP_SHARED, n->starter_fd, start); if (p == MAP_FAILED) { log_unit_error(UNIT(n)->id, "%s: Failed to map activation message: %m", UNIT(n)->id); r = -errno; goto finish; } k = (struct kdbus_msg *) ((uint8_t *) p + delta); KDBUS_ITEM_FOREACH(d, k, items) { switch (d->type) { case KDBUS_ITEM_PIDS: pid = d->pids.pid; break; case KDBUS_ITEM_PID_COMM: comm = d->str; break; } } if (pid > 0) log_unit_debug(UNIT(n)->id, "%s: Activation triggered by process " PID_FMT " (%s)", UNIT(n)->id, pid, strna(comm)); r = 0; finish: if (p) (void) munmap(p, sz); cmd_free.offset = cmd_recv.msg.offset; if (ioctl(n->starter_fd, KDBUS_CMD_FREE, &cmd_free) < 0) log_unit_warning(UNIT(n)->id, "Failed to free peeked message, ignoring: %m"); return r; } static int busname_dispatch_io(sd_event_source *source, int fd, uint32_t revents, void *userdata) { BusName *n = userdata; assert(n); assert(fd >= 0); if (n->state != BUSNAME_LISTENING) return 0; log_unit_debug(UNIT(n)->id, "Activation request on %s", UNIT(n)->id); if (revents != EPOLLIN) { log_unit_error(UNIT(n)->id, "%s: Got unexpected poll event (0x%x) on starter fd.", UNIT(n)->id, revents); goto fail; } busname_peek_message(n); busname_enter_running(n); return 0; fail: busname_enter_dead(n, BUSNAME_FAILURE_RESOURCES); return 0; } static void busname_sigchld_event(Unit *u, pid_t pid, int code, int status) { BusName *n = BUSNAME(u); BusNameResult f; assert(n); assert(pid >= 0); if (pid != n->control_pid) return; n->control_pid = 0; if (is_clean_exit(code, status, NULL)) f = BUSNAME_SUCCESS; else if (code == CLD_EXITED) f = BUSNAME_FAILURE_EXIT_CODE; else if (code == CLD_KILLED) f = BUSNAME_FAILURE_SIGNAL; else if (code == CLD_DUMPED) f = BUSNAME_FAILURE_CORE_DUMP; else assert_not_reached("Unknown sigchld code"); log_unit_full(u->id, f == BUSNAME_SUCCESS ? LOG_DEBUG : LOG_NOTICE, "%s control process exited, code=%s status=%i", u->id, sigchld_code_to_string(code), status); if (f != BUSNAME_SUCCESS) n->result = f; switch (n->state) { case BUSNAME_MAKING: if (f == BUSNAME_SUCCESS) busname_enter_listening(n); else busname_enter_signal(n, BUSNAME_SIGTERM, f); break; case BUSNAME_SIGTERM: case BUSNAME_SIGKILL: busname_enter_dead(n, f); break; default: assert_not_reached("Uh, control process died at wrong time."); } /* Notify clients about changed exit status */ unit_add_to_dbus_queue(u); }
static int start_transient_service( sd_bus *bus, char **argv, int *retval) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL; _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_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; assert(bus); assert(argv); assert(retval); if (arg_pty) { if (arg_transport == BUS_TRANSPORT_LOCAL) { master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY); if (master < 0) return log_error_errno(errno, "Failed to acquire pseudo tty: %m"); r = ptsname_malloc(master, &pty_path); if (r < 0) return log_error_errno(r, "Failed to determine tty name: %m"); if (unlockpt(master) < 0) return log_error_errno(errno, "Failed to unlock tty: %m"); } else if (arg_transport == BUS_TRANSPORT_MACHINE) { _cleanup_(sd_bus_unrefp) sd_bus *system_bus = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *pty_reply = NULL; const char *s; r = sd_bus_default_system(&system_bus); if (r < 0) return log_error_errno(r, "Failed to connect to system bus: %m"); r = sd_bus_call_method(system_bus, "org.freedesktop.machine1", "/org/freedesktop/machine1", "org.freedesktop.machine1.Manager", "OpenMachinePTY", &error, &pty_reply, "s", arg_host); if (r < 0) { log_error("Failed to get machine PTY: %s", bus_error_message(&error, -r)); return r; } r = sd_bus_message_read(pty_reply, "hs", &master, &s); if (r < 0) return bus_log_parse_error(r); master = fcntl(master, F_DUPFD_CLOEXEC, 3); if (master < 0) return log_error_errno(errno, "Failed to duplicate master fd: %m"); pty_path = strdup(s); if (!pty_path) return log_oom(); } else assert_not_reached("Can't allocate tty via ssh"); } 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) { 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 { r = make_unit_name(bus, UNIT_SERVICE, &service); if (r < 0) return r; } r = sd_bus_message_new_method_call( bus, &m, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartTransientUnit"); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_set_allow_interactive_authorization(m, arg_ask_password); if (r < 0) return bus_log_create_error(r); /* Name and mode */ r = sd_bus_message_append(m, "ss", service, "fail"); if (r < 0) return bus_log_create_error(r); /* Properties */ r = sd_bus_message_open_container(m, 'a', "(sv)"); if (r < 0) return bus_log_create_error(r); r = transient_service_set_properties(m, argv, pty_path); if (r < 0) return bus_log_create_error(r); r = sd_bus_message_close_container(m); if (r < 0) return bus_log_create_error(r); /* Auxiliary units */ r = sd_bus_message_append(m, "a(sa(sv))", 0); if (r < 0) return bus_log_create_error(r); polkit_agent_open_if_enabled(); r = sd_bus_call(bus, m, 0, &error, &reply); if (r < 0) return log_error_errno(r, "Failed to start transient service unit: %s", bus_error_message(&error, 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 (!arg_quiet) log_info("Running as unit: %s", service); if (arg_wait || master >= 0) { _cleanup_(run_context_free) RunContext c = {}; c.bus = sd_bus_ref(bus); r = sd_event_default(&c.event); if (r < 0) return log_error_errno(r, "Failed to get event loop: %m"); if (master >= 0) { assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGWINCH, SIGTERM, SIGINT, -1) >= 0); (void) sd_event_add_signal(c.event, NULL, SIGINT, NULL, NULL); (void) sd_event_add_signal(c.event, NULL, SIGTERM, NULL, NULL); if (!arg_quiet) log_info("Press ^] three times within 1s to disconnect TTY."); r = pty_forward_new(c.event, master, PTY_FORWARD_IGNORE_INITIAL_VHANGUP, &c.forward); if (r < 0) return log_error_errno(r, "Failed to create PTY forwarder: %m"); pty_forward_set_handler(c.forward, pty_forward_handler, &c); } if (arg_wait) { _cleanup_free_ char *path = NULL; const char *mt; path = unit_dbus_path_from_name(service); if (!path) return log_oom(); mt = strjoina("type='signal'," "sender='org.freedesktop.systemd1'," "path='", path, "'," "interface='org.freedesktop.DBus.Properties'," "member='PropertiesChanged'"); r = sd_bus_add_match(bus, &c.match, mt, on_properties_changed, &c); if (r < 0) return log_error_errno(r, "Failed to add properties changed signal."); r = sd_bus_attach_event(bus, c.event, 0); if (r < 0) return log_error_errno(r, "Failed to attach bus to event loop."); } r = sd_event_loop(c.event); if (r < 0) return log_error_errno(r, "Failed to run event loop: %m"); if (c.forward) { char last_char = 0; r = pty_forward_get_last_char(c.forward, &last_char); if (r >= 0 && !arg_quiet && last_char != '\n') fputc('\n', stdout); } if (!arg_quiet) { if (!isempty(c.result)) log_info("Finished with result: %s", strna(c.result)); if (c.exit_code == CLD_EXITED) log_info("Main processes terminated with: code=%s/status=%i", sigchld_code_to_string(c.exit_code), c.exit_status); else if (c.exit_code > 0) log_info("Main processes terminated with: code=%s/status=%s", sigchld_code_to_string(c.exit_code), signal_to_string(c.exit_status)); if (c.inactive_enter_usec > 0 && c.inactive_enter_usec != USEC_INFINITY && c.inactive_exit_usec > 0 && c.inactive_exit_usec != USEC_INFINITY && c.inactive_enter_usec > c.inactive_exit_usec) { char ts[FORMAT_TIMESPAN_MAX]; log_info("Service runtime: %s", format_timespan(ts, sizeof(ts), c.inactive_enter_usec - c.inactive_exit_usec, USEC_PER_MSEC)); } if (c.cpu_usage_nsec > 0 && c.cpu_usage_nsec != NSEC_INFINITY) { char ts[FORMAT_TIMESPAN_MAX]; log_info("CPU time consumed: %s", format_timespan(ts, sizeof(ts), (c.cpu_usage_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC, USEC_PER_MSEC)); } } /* Try to propagate the service's return value */ if (c.result && STR_IN_SET(c.result, "success", "exit-code") && c.exit_code == CLD_EXITED) *retval = c.exit_status; else *retval = EXIT_FAILURE; } return 0; }