Skip to content

Commit

Permalink
add s6-rc activation
Browse files Browse the repository at this point in the history
  • Loading branch information
st3r4g committed Mar 11, 2021
1 parent bdaa87d commit ad327f0
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 2 deletions.
121 changes: 121 additions & 0 deletions activation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <ini.h>
#include <tllist.h>

/*int service_add(Service *service) {
Launcher *launcher = service->launcher;
_c_cleanup_(c_freep) char *object_path = NULL;
int r;
if (service->running)
return 0;
r = asprintf(&object_path, "/org/bus1/DBus/Name/%s", service->id);
if (r < 0)
return error_origin(-ENOMEM);
r = sd_bus_call_method(launcher->bus_controller,
NULL,
"/org/bus1/DBus/Broker",
"org.bus1.DBus.Broker",
"AddName",
NULL,
NULL,
"osu",
object_path,
service->name,
service->uid);
if (r < 0)
return error_origin(r);
service->running = true;
return 0;
}*/

struct service {
char* name;
char* systemd_service;
char* user;

int id;
};

tll(struct service) service_list = tll_init();

#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0

static int handler(void* user, const char* section, const char* name, const char* value) {
struct service* service = user;
if (MATCH("D-BUS Service", "Name")) {
service->name = strdup(value);
} else if (MATCH("D-BUS Service", "SystemdService")) {
service->systemd_service = strdup(value);
} else if (MATCH("D-BUS Service", "User")) {
service->user = strdup(value);
} else return 0;
return 1;
}

void add_dir(const char* path) {
struct dirent **namelist;
int n;

n = scandir(path, &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

chdir(path);
while (n--) {
if (namelist[n]->d_name[0] != '.') {
struct service service = {0};
ini_parse(namelist[n]->d_name, handler, &service);
if (service.name && service.systemd_service && service.user && strcmp(service.name, "org.bluez")) {
printf("%s %s %s\n", service.name, service.systemd_service, service.user);
tll_push_back(service_list, service);
}
}

free(namelist[n]);
}
free(namelist);
}

#include "sd-bus.h"

void add_service(sd_bus* bus_controller) {
int i=0;
tll_foreach(service_list, it) {
char path[128];
sprintf(path, "/org/bus1/DBus/Name/%d", i);
sd_bus_call_method(bus_controller, NULL, "/org/bus1/DBus/Broker", "org.bus1.DBus.Broker", "AddName", NULL, NULL, "osu", path, it->item.name, 0);
it->item.id = i;
i++;
}
}

static struct service *find_service_by_id(int id) {
tll_foreach(service_list, it)
if (it->item.id == id)
return &it->item;
return NULL;
}

int start_service(int id) {
int r = -1;
struct service* service = find_service_by_id(id);
if (service) {
//printf("%s\n", service->systemd_service);
char cmd[256];
sprintf(cmd, "s6-rc change %s", service->systemd_service);
fprintf(stderr, "%s\n", cmd);
r = system(cmd);
}
return r;
}
64 changes: 62 additions & 2 deletions controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "sd-bus.h"
#include "util.h"

#include <libgen.h>

static int launcher_add_listener(sd_bus* bus_controller, int fd_listen) {
sd_bus_message *m = NULL;
int r;
Expand All @@ -28,6 +30,44 @@ static int launcher_add_listener(sd_bus* bus_controller, int fd_listen) {
return 0;
}

static int on_message(sd_bus_message *m, void *userdata, sd_bus_error *error) {
sd_bus* bus_controller = userdata;
/*
* TODO: properly check for signal type
*/
const char* object_path = sd_bus_message_get_path(m);
//printf("signal received: %s\n", object_path);
uint64_t serial;
int r = sd_bus_message_read(m, "t", &serial);
#ifdef HAVE_S6
char* path = strdup(object_path);
r = start_service(atoi(basename(path)));
free(path);
#endif
if (r != 0) {
r = sd_bus_call_method(bus_controller, NULL, object_path, "org.bus1.DBus.Name", "Reset", NULL, NULL, "t", serial);
}
return 0;
/* Launcher *launcher = userdata;
const char *path, *suffix;
int r = 0;
path = sd_bus_message_get_path(m);
if (!path)
return 0;
suffix = string_prefix(path, "/org/bus1/DBus/Name/");
if (suffix) {
if (sd_bus_message_is_signal(m, "org.bus1.DBus.Name", "Activate"))
r = launcher_on_name_activate(launcher, m, suffix);
} else if (strcmp(path, "/org/bus1/DBus/Broker") == 0) {
if (sd_bus_message_is_signal(m, "org.bus1.DBus.Broker", "SetActivationEnvironment"))
r = launcher_on_set_activation_environment(launcher, m);
}
return error_trace(r);*/
}

static int bus_method_reload_config(sd_bus_message *message, void *userdata, sd_bus_error *error) {
/*
* Do nothing...
Expand All @@ -41,18 +81,38 @@ static const sd_bus_vtable launcher_vtable[] = {
SD_BUS_VTABLE_END
};

static sd_bus* bus_controller;

void controller_setup(int fd, const char* dbus_socket_path) {
sd_bus* bus_controller;
int r;
r = sd_bus_new(&bus_controller);
if (r < 0) handle_error("sd_bus_new");
r = sd_bus_set_fd(bus_controller, fd, fd);
if (r < 0) handle_error("sd_bus_set_fd");
r = sd_bus_add_object_vtable(bus_controller, NULL, "/org/bus1/DBus/Controller", "org.bus1.DBus.Controller", launcher_vtable, NULL);
if (r < 0) handle_error("sd_bus_add_object_vtable");
//sd_bus_add_filter(bus_controller, NULL, launcher_on_message, NULL);
sd_bus_add_filter(bus_controller, NULL, on_message, bus_controller);
if (r < 0) handle_error("sd_bus_add_filter");
r = sd_bus_start(bus_controller);
if (r < 0) handle_error("sd_bus_start");
if (launcher_add_listener(bus_controller, dbus_create_socket(dbus_socket_path)))
printf("AddListener failed\n");

#ifdef HAVE_S6
add_service(bus_controller);
#endif
}

void controller_run() {
for (;;) {
/* Process requests */
int r = sd_bus_process(bus_controller, NULL);
if (r < 0) handle_error("Failed to process bus");
if (r > 0) /* we processed a request, try to process another one, right-away */
continue;

/* Wait for the next request to process */
r = sd_bus_wait(bus_controller, (uint64_t) -1);
if (r < 0) handle_error("Failed to wait on bus");
}
}
1 change: 1 addition & 0 deletions controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define controller_h_INCLUDED

void controller_setup(int fd, const char* dbus_socket_path);
void controller_run();

#endif // controller_h_INCLUDED

5 changes: 5 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ int main(int argc, char* argv[]) {
close(controller[1]);
if (logfd >= 0) close(logfd);

#ifdef HAVE_S6
add_dir("/usr/share/dbus-1/system-services");
#endif

controller_setup(controller[0], dbus_socket_path);

if (notif) {
write(3, "\n", 1); //dbus ready to accept connections
close(3);
}

controller_run();
int wstatus;
waitpid(cpid, &wstatus, 0);
}
Expand Down
7 changes: 7 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ endif
add_project_arguments('-DHAVE_' + sdbus.name().to_upper() + '=1', language: 'c')

executable('dbus-controller-dummy', ['controller.c', 'dbus.c', 'main.c', 'policy.c', 'syslog.c'], dependencies: [sdbus])

if get_option('s6-rc') == true
inih = dependency('inih')
tllist = dependency('tllist')

executable('dbus-controller-s6', ['activation.c', 'controller.c', 'dbus.c', 'main.c', 'policy.c', 'syslog.c'], dependencies: [inih, sdbus, tllist], c_args : '-DHAVE_S6=1')
endif
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
option('sd-bus-provider', type: 'combo', choices: ['auto', 'libsystemd', 'libelogind', 'basu'], value: 'auto', description: 'Provider of the sd-bus library')
option('s6-rc', type : 'boolean', value : false)

0 comments on commit ad327f0

Please sign in to comment.