Skip to content

Commit

Permalink
s6-rc: stop using dbus_servicedir
Browse files Browse the repository at this point in the history
Instead get the activatable services directly from s6-rc live state.
  • Loading branch information
st3r4g committed Mar 20, 2021
1 parent 11a854a commit 45348cc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
52 changes: 27 additions & 25 deletions activation.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <string.h>
#include <unistd.h>

#include <ini.h>
#include <skalibs/djbunix.h>

#include <tllist.h>

/*int service_add(Service *service) {
Expand Down Expand Up @@ -38,47 +39,49 @@
}*/

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

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;
}
#define NAMEFILE "data/dbus-activatable-name"
#define SERVICEDIRS "servicedirs"
#define SIZE(array) (sizeof(array)/sizeof(*array))

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

size_t s6rc_livedir_len = strlen(s6rc_livedir);
char path[s6rc_livedir_len+1+SIZE(SERVICEDIRS)];
memcpy(path, s6rc_livedir, s6rc_livedir_len);
path[s6rc_livedir_len] = '/';
memcpy(path+s6rc_livedir_len+1, SERVICEDIRS, SIZE(SERVICEDIRS));
n = scandir(path, &namelist, NULL, alphasort);
if (n == -1) {
perror("scandir");
exit(EXIT_FAILURE);
}

chdir(path);
chdir(path); // TODO: remove this, the directory could be deleted by s6-rc-update
while (n--) {
if (namelist[n]->d_name[0] != '.') {
struct service service = {0};
ini_parse(namelist[n]->d_name, handler, &service);
if (service.name && strcmp(service.name, "org.bluez")) {
fprintf(stderr, "%s\n", service.name);
size_t d_len = strlen(namelist[n]->d_name);
char tmp[d_len+1+SIZE(NAMEFILE)];
memcpy(tmp, namelist[n]->d_name, d_len);
tmp[d_len] = '/';
memcpy(tmp+d_len+1, NAMEFILE, SIZE(NAMEFILE));

int r = openslurpclose(&service.name, tmp);
if (r == 1 && service.name.len > 0) {
service.name.s[service.name.len-1] = '\0';
stralloc_copyb(&service.s6rc, namelist[n]->d_name, d_len+1);
tll_push_back(service_list, service);
fprintf(stderr, "Activatable name %s provided by %s\n", service.name.s, service.s6rc.s);
}
}

Expand All @@ -94,7 +97,7 @@ void add_service(sd_bus* bus_controller) {
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);
sd_bus_call_method(bus_controller, NULL, "/org/bus1/DBus/Broker", "org.bus1.DBus.Broker", "AddName", NULL, NULL, "osu", path, it->item.name.s, 0);
it->item.id = i;
i++;
}
Expand All @@ -113,9 +116,8 @@ 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 -l %s change %s", s6rc_livedir, service->name);
sprintf(cmd, "s6-rc -l %s change %s", s6rc_livedir, service->s6rc.s);
fprintf(stderr, "%s\n", cmd);
r = system(cmd);
}
Expand Down
10 changes: 3 additions & 7 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

static const char* default_dbus_socket_path = "/run/dbus/system_bus_socket";
#ifdef HAVE_S6
static const char* default_dbus_servicedir = "/usr/share/dbus-1/system-services";
static const char* default_s6rc_livedir = "/run/s6-rc";
extern const char* s6rc_livedir; // TODO: pass around properly
#endif
Expand All @@ -36,7 +35,6 @@ optional arguments:\n\
-3 notify readiness on fd 3\n"
#ifdef HAVE_S6
"\n\
-a dbus service directory (default: %s)\n\
-l s6-rc livedir (default: %s)\n"
#endif
"\n\
Expand All @@ -47,13 +45,12 @@ int main(int argc, char* argv[]) {
bool notif = false;
bool syslog = false;
#ifdef HAVE_S6
const char* dbus_servicedir = default_dbus_servicedir;
s6rc_livedir = default_s6rc_livedir;
#endif

int opt;
#ifdef HAVE_S6
while ((opt = getopt(argc, argv, "a:d:hl:s3")) != -1) {
while ((opt = getopt(argc, argv, "d:hl:s3")) != -1) {
#else
while ((opt = getopt(argc, argv, "d:hs3")) != -1) {
#endif
Expand All @@ -62,13 +59,12 @@ int main(int argc, char* argv[]) {
case 's': syslog = true; break;
case '3': check_3_open(); notif = true; break;
#ifdef HAVE_S6
case 'a': dbus_servicedir = optarg; break;
case 'l': s6rc_livedir = optarg; break;
#endif
default:
printf(usage, default_dbus_socket_path
#ifdef HAVE_S6
, default_dbus_servicedir, default_s6rc_livedir
, default_s6rc_livedir
#endif
);
return EXIT_FAILURE;
Expand Down Expand Up @@ -119,7 +115,7 @@ int main(int argc, char* argv[]) {
if (logfd >= 0) close(logfd);

#ifdef HAVE_S6
add_dir(dbus_servicedir);
add_s6rc_servicedirs(s6rc_livedir);
#endif

controller_setup(controller[0], dbus_socket_path);
Expand Down
6 changes: 4 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ 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')
cc = meson.get_compiler('c')
skalibs = cc.find_library('skarnet')

executable('dbus-controller-s6', ['activation.c', 'controller.c', 'dbus.c', 'main.c', 'policy.c', 'syslog.c'], dependencies: [sdbus, skalibs, tllist], c_args : '-DHAVE_S6=1')
endif
4 changes: 3 additions & 1 deletion tools/s6rc-generator
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ for service in "$@"; do
exec_=$(awk -F= '$1 == "Exec" {print $2}' "$service")
systemdservice_=$(awk -F= '$1 == "SystemdService" {print $2}' "$service")

mkdir $name_
mkdir "$name_"
mkdir "$name_/data"
echo "$name_" >> "$name_/data/dbus-activatable-name"
echo 'longrun' >> "$name_/type"
echo '#!/bin/execlineb -P' >> "$name_/run"
echo "$exec_" >> "$name_/run"
Expand Down

0 comments on commit 45348cc

Please sign in to comment.