int bus_open_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) { int r; assert(transport >= 0); assert(transport < _BUS_TRANSPORT_MAX); assert(bus); assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -ENOTSUP); switch (transport) { case BUS_TRANSPORT_LOCAL: if (user) r = bus_open_user_systemd(bus); else r = bus_open_system_systemd(bus); break; case BUS_TRANSPORT_REMOTE: r = sd_bus_open_system_remote(bus, host); break; case BUS_TRANSPORT_CONTAINER: r = sd_bus_open_system_container(bus, host); break; default: assert_not_reached("Hmm, unknown transport type."); } return r; }
static void start_target(const char *target) { _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_bus_close_unref_ sd_bus *bus = NULL; int r; assert(target); r = bus_open_system_systemd(&bus); if (r < 0) { log_error_errno(r, "Failed to get D-Bus connection: %m"); return; } log_info("Running request %s/start/replace", target); /* Start these units only if we can replace base.target with it */ r = sd_bus_call_method(bus, "org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnitReplace", &error, NULL, "sss", "basic.target", target, "replace"); /* Don't print a warning if we aren't called during startup */ if (r < 0 && !sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_JOB)) log_error("Failed to start unit: %s", bus_error_message(&error, -r)); }
int main(int argc, char *argv[]) { _cleanup_bus_unref_ sd_bus *bus = NULL; int r; if (argc != 2) { log_error("Incorrect number of arguments."); return EXIT_FAILURE; } log_set_target(LOG_TARGET_AUTO); log_parse_environment(); log_open(); /* We send this event to the private D-Bus socket and then the * system instance will forward this to the system bus. We do * this to avoid an activation loop when we start dbus when we * are called when the dbus service is shut down. */ r = bus_open_system_systemd(&bus); if (r < 0) { /* If we couldn't connect we assume this was triggered * while systemd got restarted/transitioned from * initrd to the system, so let's ignore this */ log_debug("Failed to get D-Bus connection: %s", strerror(-r)); return EXIT_FAILURE; } r = sd_bus_emit_signal(bus, "/org/freedesktop/systemd1/agent", "org.freedesktop.systemd1.Agent", "Released", "s", argv[1]); if (r < 0) { log_debug("Failed to send signal message on private connection: %s", strerror(-r)); return EXIT_FAILURE; } sd_bus_flush(bus); return EXIT_SUCCESS; }
static int server_init(Server *s, unsigned n_sockets) { int r; unsigned i; assert(s); assert(n_sockets > 0); zero(*s); s->epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (s->epoll_fd < 0) { r = -errno; log_error("Failed to create epoll object: %m"); goto fail; } for (i = 0; i < n_sockets; i++) { struct epoll_event ev; Fifo *f; int fd; fd = SD_LISTEN_FDS_START+i; r = sd_is_fifo(fd, NULL); if (r < 0) { log_error("Failed to determine file descriptor type: %s", strerror(-r)); goto fail; } if (!r) { log_error("Wrong file descriptor type."); r = -EINVAL; goto fail; } f = new0(Fifo, 1); if (!f) { r = -ENOMEM; log_error("Failed to create fifo object: %m"); goto fail; } f->fd = -1; zero(ev); ev.events = EPOLLIN; ev.data.ptr = f; if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) { r = -errno; fifo_free(f); log_error("Failed to add fifo fd to epoll object: %m"); goto fail; } f->fd = fd; LIST_PREPEND(fifo, s->fifos, f); f->server = s; s->n_fifos ++; } r = bus_open_system_systemd(&s->bus); if (r < 0) { log_error("Failed to get D-Bus connection: %s", strerror(-r)); r = -EIO; goto fail; } return 0; fail: server_done(s); return r; }