]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: spoa-server: Load files
authorThierry FOURNIER <thierry.fournier@ozon.io>
Fri, 23 Feb 2018 14:12:55 +0000 (15:12 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 13 May 2019 15:43:47 +0000 (17:43 +0200)
Declare files to be executed at the begining and execute it. The binding
between the engine and the file is done throught the extension.

contrib/spoa_server/spoa.c
contrib/spoa_server/spoa.h

index 73b71338570cde4948453a3e6d45e16043e3dc89..e484315f658e826b4269dd732423177d749d2377 100644 (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;
index 22d6788513813d541828db2fd773d47a4d78deb6..ca8518122a34c83b117229a567bec5c1de02cd08 100644 (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 {