Files
rosetta/services/herdd/main.c
T

134 lines
2.9 KiB
C
Raw Normal View History

#include "log.h"
#include "queue.h"
#include "service.h"
#include "spawn.h"
2026-05-30 19:47:49 +01:00
#include "target.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
2026-05-30 10:19:33 +01:00
#include <fx/string.h>
#include <magenta/log.h>
#include <magenta/task.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
2026-03-24 20:25:49 +00:00
#include <sys/remote.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef TRACE
#define tracef(...) printf(__VA_ARGS__)
#else
#define tracef(...)
#endif
static void *thread_func(void *arg)
{
tracef("started thread with arg %p\n", arg);
errno = 100;
tracef("thread done");
return (void *)0xdeadbeef;
}
static int load_services(const char *dir, struct queue *out)
{
DIR *d = opendir(dir);
if (!d) {
printf("cannot open '%s': %s\n", dir, strerror(errno));
return -1;
}
2026-03-24 20:25:49 +00:00
struct dirent *dent = NULL;
while ((dent = readdir(d))) {
char filepath[4096];
snprintf(filepath, sizeof filepath, "%s/%s", dir, dent->d_name);
tracef(" - %s\n", dent->d_name);
struct service *s = NULL;
2026-05-30 19:47:49 +01:00
int err = service_load(dent->d_name, filepath, &s);
if (err != SUCCESS) {
printf("failed to load %s (%s)\n",
filepath,
strerror(errno));
} else {
queue_push_back(out, &s->s_entry);
}
}
closedir(d);
return SUCCESS;
}
2026-05-30 19:47:49 +01:00
static int load_targets(const char *dir, struct queue *out)
{
DIR *d = opendir(dir);
if (!d) {
printf("cannot open '%s': %s\n", dir, strerror(errno));
return -1;
}
struct dirent *dent = NULL;
while ((dent = readdir(d))) {
char filepath[4096];
snprintf(filepath, sizeof filepath, "%s/%s", dir, dent->d_name);
tracef(" - %s\n", dent->d_name);
2026-05-30 19:47:49 +01:00
struct target *rl = NULL;
int err = target_load(dent->d_name, filepath, &rl);
if (err != SUCCESS) {
printf("failed to load %s (%s)\n",
filepath,
strerror(errno));
continue;
2026-03-24 20:25:49 +00:00
}
2026-05-30 19:47:49 +01:00
tracef("target: %s\n", rl->rl_description);
for (size_t i = 0; i < rl->rl_requires_count; i++) {
tracef(" requires: %s\n", rl->rl_requires[i]);
}
queue_push_back(out, &rl->rl_entry);
}
closedir(d);
return SUCCESS;
}
int main(int argc, const char *argv[], const char *envp[])
{
sys_remote_set(SYS_REMOTE_NSD, 0, 0);
2026-05-30 19:47:49 +01:00
struct queue *targets = global_targets();
struct queue *services = global_services();
2026-05-30 19:47:49 +01:00
int err = load_targets("/etc/herdd/targets", targets);
err = load_services("/etc/herdd/services", services);
2026-05-30 19:47:49 +01:00
struct target *rl = target_find("single-user.target");
if (!rl) {
2026-05-30 19:47:49 +01:00
log_fail("Cannot find target single-user.");
return -1;
}
2026-05-30 19:47:49 +01:00
target_activate(rl);
#if 1
pid_t child = fork();
kern_logf(" fork returned %d", child);
if (child == 0) {
kern_log("this is the child");
2026-04-20 22:17:28 +01:00
long r = printf("hello\n");
kern_logf("printf returned %ld (%d)", r, (errno));
execlp(NULL, NULL);
2026-03-24 20:25:49 +00:00
} else {
kern_log("this is the parent");
2026-04-20 22:17:28 +01:00
long r = printf("goodbye\n");
kern_logf("printf returned %ld (%d)", r, (errno));
2026-03-24 20:25:49 +00:00
}
2026-04-20 22:17:28 +01:00
kern_logf("exiting (%s)", strerror(EFTYPE));
#endif
return 0;
}