135 lines
3.5 KiB
C

#define _POSIX_C_SOURCE 200809L
#include "service.h"
#include "plugin.h"
#include <dlfcn.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static char directory[4096];
static void *active_handle;
static const Plugin *active;
static unsigned generation;
static uint64_t samples;
static int64_t total;
static int64_t last;
int service_init(const char *executable)
{
ssize_t length;
char *slash;
(void)executable;
active_handle = NULL;
active = NULL;
generation = 0;
samples = 0;
total = 0;
last = 0;
length = readlink("/proc/self/exe", directory, sizeof(directory)-1);
if (length < 0 || (size_t)length >= sizeof(directory)-1) {
return 0;
}
directory[length] = '\0';
slash = strrchr(directory, '/');
if (slash == NULL) {
return 0;
}
*slash = '\0';
return 1;
}
void service_close(void)
{
if (active_handle != NULL) {
dlclose(active_handle);
}
active_handle = NULL;
active = NULL;
}
static int make_path(char *path, size_t capacity, unsigned identity)
{
int length = snprintf(path, capacity, "%s/./plugins/libmode%u.so", directory, identity);
return length >= 0 && (size_t)length < capacity;
}
static int select_plugin(unsigned identity)
{
char path[4096];
void *pending_handle;
void *old_handle;
PluginEntry entry;
const Plugin *pending;
if (!make_path(path, sizeof(path), identity)) {
return -1;
}
pending_handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (pending_handle == NULL) {
return -1;
}
*(void **)(&entry) = dlsym(pending_handle, "plugin_entry");
pending = entry != NULL ? entry() : NULL;
if (pending == NULL || pending->abi != PLUGIN_ABI || pending->identity != identity || pending->convert == NULL) {
dlclose(pending_handle);
return -1;
}
old_handle = active_handle;
active_handle = pending_handle;
active = pending;
generation++;
if (old_handle != NULL) {
dlclose(old_handle);
}
return 0;
}
Reply service_execute(const Command *command)
{
Reply reply = {-2, 0, 0};
if (command->name == 'R' && wire_arguments(command, 0, 0)) {
service_close();
generation = 0;
samples = 0;
total = 0;
last = 0;
reply.result = 0;
} else if (command->name == 'L' && wire_arguments(command, 1, 1)) {
int64_t identity = wire_number(command, 0);
if (identity < 1 || identity > 4) {
return reply;
}
reply.result = select_plugin((unsigned)identity);
reply.value = active != NULL ? active->identity : 0;
reply.detail = generation;
} else if (command->name == 'V' && wire_arguments(command, 1, 1)) {
int64_t value = wire_number(command, 0);
if (value < -1000000 || value > 1000000) {
return reply;
}
if (active == NULL) {
reply.result = -1;
return reply;
}
last = active->convert(value);
total += last;
samples++;
reply.result = 0;
reply.value = last;
reply.detail = samples;
} else if (command->name == 'C' && wire_arguments(command, 0, 0)) {
service_close();
reply.result = 0;
}
return reply;
}
void service_observe(Observation *observation)
{
observe_value(observation, active != NULL ? active->identity : 0);
observe_value(observation, generation);
observe_value(observation, samples);
observe_value(observation, total);
observe_value(observation, last);
}