Files
rosetta/services/herdd/target.c
T

178 lines
3.6 KiB
C
Raw Normal View History

2026-05-30 19:47:49 +01:00
#include "target.h"
#include "log.h"
#include "service.h"
#include <errno.h>
2026-05-30 10:19:33 +01:00
#include <fx/collections/array.h>
#include <fx/collections/hashtable.h>
2026-07-19 13:32:19 +01:00
#include <fx/serial/ctx.h>
#include <fx/serial/toml.h>
2026-05-30 10:19:33 +01:00
#include <fx/string.h>
#include <fx/stringstream.h>
#include <magenta/log.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2026-05-30 19:47:49 +01:00
static struct queue targets = QUEUE_INIT;
2026-05-30 19:47:49 +01:00
struct queue *global_targets(void)
{
2026-05-30 19:47:49 +01:00
return &targets;
}
2026-05-30 19:47:49 +01:00
static int parse_target_data(fx_object *in, struct target *out)
{
2026-05-30 10:19:33 +01:00
if (!fx_object_is_type(in, FX_TYPE_HASHTABLE)) {
return -1;
}
2026-05-30 19:47:49 +01:00
const fx_value *target_v = fx_hashtable_get(in, &FX_CSTR("target"));
if (!target_v || !fx_value_is_type(target_v, FX_TYPE_HASHTABLE)) {
return -1;
}
2026-05-30 19:47:49 +01:00
fx_hashtable *in_target = NULL;
fx_value_get_object(target_v, &in_target);
2026-05-30 19:47:49 +01:00
const fx_value *unit_v = fx_hashtable_get(in, &FX_CSTR("unit"));
if (!unit_v || !fx_value_is_type(unit_v, FX_TYPE_HASHTABLE)) {
return -1;
}
2026-05-30 19:47:49 +01:00
fx_hashtable *in_unit = NULL;
fx_value_get_object(unit_v, &in_unit);
2026-05-30 10:19:33 +01:00
const fx_value *description_v
2026-05-30 19:47:49 +01:00
= fx_hashtable_get(in_unit, &FX_CSTR("description"));
2026-05-30 10:19:33 +01:00
if (!description_v
|| !fx_value_is_type(description_v, FX_TYPE_STRING)) {
return -1;
}
2026-05-30 10:19:33 +01:00
fx_string *description = NULL;
fx_value_get_object(description_v, &description);
2026-05-30 10:19:33 +01:00
const fx_value *deps_v
2026-05-30 19:47:49 +01:00
= fx_hashtable_get(in_target, &FX_CSTR("requires"));
2026-05-30 10:19:33 +01:00
if (deps_v && !fx_value_is_type(deps_v, FX_TYPE_ARRAY)) {
return -1;
}
2026-05-30 10:19:33 +01:00
fx_array *deps = NULL;
fx_value_get_object(deps_v, &deps);
int ret = 0;
2026-05-30 10:19:33 +01:00
size_t dep_count = fx_array_get_size(deps);
const fx_iterator *it = fx_iterator_begin(deps);
fx_foreach(dep, it)
{
2026-05-30 10:19:33 +01:00
if (!fx_value_is_type(dep, FX_TYPE_STRING)) {
ret = -1;
break;
}
}
fx_iterator_unref(it);
if (ret != 0) {
return ret;
}
out->rl_requires_count = dep_count;
out->rl_requires = calloc(dep_count, sizeof(char *));
it = fx_iterator_begin(deps);
size_t i = 0;
2026-05-30 10:19:33 +01:00
fx_foreach(dep, it)
{
2026-05-30 10:19:33 +01:00
fx_string *str = NULL;
fx_value_get_object(dep, &str);
out->rl_requires[i++] = fx_string_steal(str);
}
fx_iterator_unref(it);
out->rl_description = fx_string_steal(description);
return 0;
}
2026-05-30 19:47:49 +01:00
int target_load(const char *name, const char *path, struct target **out)
{
2026-05-30 19:47:49 +01:00
struct target *rl = malloc(sizeof *rl);
if (!rl) {
errno = ENOMEM;
return -1;
}
FILE *fp = fopen(path, "r");
if (!fp) {
free(rl);
return -1;
}
fx_stream *in = fx_stream_open_fp(fp);
fx_serial_ctx *ctx = fx_toml_serial_ctx_create();
2026-05-30 10:19:33 +01:00
fx_value data;
2026-04-20 22:17:28 +01:00
fx_result result = fx_serial_ctx_deserialise(ctx, in, &data, 0);
if (fx_result_is_error(result)) {
fx_throw(result);
free(rl);
errno = EFTYPE;
return -1;
}
2026-05-30 10:19:33 +01:00
if (!fx_value_is_type(&data, FX_TYPE_HASHTABLE)) {
free(rl);
errno = EFTYPE;
return -1;
}
2026-05-30 19:47:49 +01:00
if (parse_target_data(data.v_object, rl) != 0) {
free(rl);
errno = EFTYPE;
return -1;
}
2026-05-30 19:47:49 +01:00
rl->rl_name = fx_strdup(name);
fx_stream_unref(in);
2026-05-30 10:19:33 +01:00
fx_value_unset(&data);
fclose(fp);
*out = rl;
return 0;
}
2026-05-30 19:47:49 +01:00
struct target *target_find(const char *name)
{
2026-05-30 19:47:49 +01:00
struct queue_entry *cur = queue_first(&targets);
while (cur) {
2026-05-30 19:47:49 +01:00
struct target *rl
= QUEUE_CONTAINER(struct target, rl_entry, cur);
if (!strcmp(rl->rl_name, name)) {
return rl;
}
cur = queue_next(cur);
}
return NULL;
}
2026-05-30 19:47:49 +01:00
int target_activate(struct target *rl)
{
for (size_t i = 0; i < rl->rl_requires_count; i++) {
const char *svc_name = rl->rl_requires[i];
struct service *svc = service_find(svc_name);
if (!svc) {
log_fail("Cannot find service %s.", svc_name);
return -1;
}
int ret = service_start(svc);
if (ret != 0) {
return ret;
}
}
2026-05-30 19:47:49 +01:00
log_ok("Reached target %s.", rl->rl_description);
return 0;
}