MINOR: spoa-server: Load files

Declare files to be executed at the begining and execute it. The binding
between the engine and the file is done throught the extension.
This commit is contained in:
Thierry FOURNIER 2018-02-23 15:12:55 +01:00 committed by Willy Tarreau
parent 892f6647c1
commit 8b9a73bac0
2 changed files with 41 additions and 2 deletions

View File

@ -99,6 +99,23 @@ bool debug = false;
pthread_key_t worker_id;
static struct ps *ps_list = NULL;
static struct ps_message *ps_messages = NULL;
static int nfiles = 0;
static char **files = NULL;
static inline void add_file(const char *file)
{
nfiles++;
files = realloc(files, sizeof(*files) * nfiles);
if (files == NULL) {
fprintf(stderr, "Out of memory error\n");
exit(EXIT_FAILURE);
}
files[nfiles - 1] = strdup(file);
if (files[nfiles - 1] == NULL) {
fprintf(stderr, "Out of memory error\n");
exit(EXIT_FAILURE);
}
}
void ps_register(struct ps *ps)
{
@ -963,6 +980,8 @@ spoa_worker(void *data)
int *info = (int *)data;
int csock, lsock = info[0];
struct ps *ps;
int i;
int len;
signal(SIGPIPE, SIG_IGN);
pthread_setspecific(worker_id, &info[1]);
@ -971,6 +990,20 @@ spoa_worker(void *data)
for (ps = ps_list; ps != NULL; ps = ps->next)
ps->init_worker(&w);
/* Load files */
for (i = 0; i < nfiles; i++) {
len = strlen(files[i]);
for (ps = ps_list; ps != NULL; ps = ps->next)
if (strcmp(files[i] + len - strlen(ps->ext), ps->ext) == 0)
break;
if (ps == NULL) {
LOG("Can't load file \"%s\"\n", files[i]);
goto out;
}
if (!ps->load_file(&w, files[i]))
goto out;
}
while (1) {
socklen_t sz = sizeof(client);
@ -1040,11 +1073,13 @@ int process_create(pid_t *pid, void *(*ps)(void *), void *data)
static void
usage(char *prog)
{
fprintf(stderr, "Usage: %s [-h] [-d] [-p <port>] [-n <num-workers>]\n", prog);
fprintf(stderr, "Usage: %s [-h] [-d] [-p <port>] [-n <num-workers>] -f <file>\n", prog);
fprintf(stderr, " -h Print this message\n");
fprintf(stderr, " -d Enable the debug mode\n");
fprintf(stderr, " -p <port> Specify the port to listen on (default: 12345)\n");
fprintf(stderr, " -n <num-workers> Specify the number of workers (default: 5)\n");
fprintf(stderr, " -f <file> Specify the file whoch contains the processing code.\n");
fprintf(stderr, " This argument can specified more than once.\n");
}
int
@ -1060,7 +1095,7 @@ main(int argc, char **argv)
nbworkers = NUM_WORKERS;
port = DEFAULT_PORT;
while ((opt = getopt(argc, argv, "hdn:p:")) != -1) {
while ((opt = getopt(argc, argv, "hdn:p:f:")) != -1) {
switch (opt) {
case 'h':
usage(argv[0]);
@ -1074,6 +1109,9 @@ main(int argc, char **argv)
case 'p':
port = atoi(optarg);
break;
case 'f':
add_file(optarg);
break;
default:
usage(argv[0]);
return EXIT_FAILURE;

View File

@ -90,6 +90,7 @@ struct ps {
char *ext;
int (*init_worker)(struct worker *w);
int (*exec_message)(struct worker *w, void *ref, int nargs, struct spoe_kv *args);
int (*load_file)(struct worker *w, const char *file);
};
struct ps_message {