Skip to content

Instantly share code, notes, and snippets.

@00mjk
Forked from protoben/gaps_channel_setup.c
Created June 10, 2022 15:22
Show Gist options
  • Save 00mjk/9b5998c2f8ac2a9041cd912f6de14746 to your computer and use it in GitHub Desktop.
Save 00mjk/9b5998c2f8ac2a9041cd912f6de14746 to your computer and use it in GitHub Desktop.
Simple setup implementation
#include <libpirate/primitives.h>
#include <linux/limits.h>
#include <stdlib.h>
#include <string.h>
#include "gaps_resources.h"
// ^ declares resource and resource_parameter structs, as well as communication
// functions gaps_request_resource() and gaps_signal_failure()
extern struct gaps_resource *__gaps_channel_gaps_resources;
// ^ defined by the linker as
// struct resource __<resource_type>_gaps_resources;
// for each declared resource type
//
// by convention, maybe terminated by a resource with all zero fields
struct gaps_channel_config {
char path[PATH_MAX];
int flags;
// more fields here, obviously
};
__attribute__((constructor))
void setup_gaps_channels_auto(void) {
char *fdstr = getenv("__GAPS_RUN_FD");
if(!fdstr)
exit(1);
int fd = atoi(fdstr);
int i;
for(i = 0; __gaps_channel_gaps_resources[i].gr_name; ++i) {
struct gaps_resource const *r = &__gaps_channel_gaps_resources[i];
struct gaps_channel_config cfg;
// check parameter list to we whether resource should be automatically
// initialized
int j, manual_init = 0;
for(j = 0; r->gr_params[j].grp_name; ++j) {
struct gaps_resource_param *rp = &r->gr_params[j];
if(!strcmp(rp->grp_name, "init") && !strcmp(rp->grp_value, "manual"))
++manual_init;
}
if(manual_init)
continue;
// request resource config
if(gaps_request_resource(r->gr_obj, &cfg, r->gr_name, r->gr_type, fd)) {
gaps_signal_failure(fd, "no configuration for channel %s", r->gr_name);
exit(1);
}
// perform configuration
if(pirate_set_pathname(*(int*)r->gr_obj, cfg.path)) {
gaps_signal_failure(fd, "unable to set path channel %s", r->gr_name);
exit(1);
}
// more config ...
if(pirate_open(fd, cfg.flags)) {
gaps_signal_failure(fd, "unable to open channel %s", r->gr_name);
exit(1);
}
}
}
struct gaps_resource_param {
char *grp_name;
char *grp_value;
};
struct gaps_resource {
char *gr_name;
char *gr_type;
void *gr_obj;
struct gaps_resource_param *gr_params;
};
int gaps_request_resource(void *obj, void *cfg, char *name, char *type, int fd);
int gaps_signal_failure(int fd, char *fmt, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment