commit e003f87

default  ·  2024-01-03 10:01:25 +0000 UTC
parent 607335a
Moved most server state to a structure.
3 files changed,  +69, -56
M data.c
M snac.h
M data.c
+20, -20
 1@@ -150,12 +150,12 @@ void user_free(snac *snac)
 2 }
 3 
 4 
 5-int user_open(snac *snac, const char *uid)
 6+int user_open(snac *user, const char *uid)
 7 /* opens a user */
 8 {
 9     int ret = 0;
10 
11-    memset(snac, '\0', sizeof(struct _snac));
12+    *user = (snac){0};
13 
14     if (validate_uid(uid)) {
15         xs *cfg_file = NULL;
16@@ -174,52 +174,52 @@ int user_open(snac *snac, const char *uid)
17                 xs *v2 = xs_tolower_i(xs_dup(v));
18 
19                 if (strcmp(lcuid, v2) == 0) {
20-                    snac->uid = xs_dup(v);
21+                    user->uid = xs_dup(v);
22                     break;
23                 }
24             }
25         }
26         else
27-            snac->uid = xs_str_new(uid);
28+            user->uid = xs_str_new(uid);
29 
30-        if (snac->uid == NULL)
31+        if (user->uid == NULL)
32             return ret;
33 
34-        snac->basedir = xs_fmt("%s/user/%s", srv_basedir, snac->uid);
35+        user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
36 
37-        cfg_file = xs_fmt("%s/user.json", snac->basedir);
38+        cfg_file = xs_fmt("%s/user.json", user->basedir);
39 
40         if ((f = fopen(cfg_file, "r")) != NULL) {
41             /* read full config file */
42-            snac->config = xs_json_load(f);
43+            user->config = xs_json_load(f);
44             fclose(f);
45 
46-            if (snac->config != NULL) {
47-                xs *key_file = xs_fmt("%s/key.json", snac->basedir);
48+            if (user->config != NULL) {
49+                xs *key_file = xs_fmt("%s/key.json", user->basedir);
50 
51                 if ((f = fopen(key_file, "r")) != NULL) {
52-                    snac->key = xs_json_load(f);
53+                    user->key = xs_json_load(f);
54                     fclose(f);
55 
56-                    if (snac->key != NULL) {
57-                        snac->actor = xs_fmt("%s/%s", srv_baseurl, snac->uid);
58-                        snac->md5   = xs_md5_hex(snac->actor, strlen(snac->actor));
59+                    if (user->key != NULL) {
60+                        user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
61+                        user->md5   = xs_md5_hex(user->actor, strlen(user->actor));
62 
63                         /* everything is ok right now */
64                         ret = 1;
65 
66                         /* does it have a configuration override? */
67-                        xs *cfg_file_o = xs_fmt("%s/user_o.json", snac->basedir);
68+                        xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
69                         if ((f = fopen(cfg_file_o, "r")) != NULL) {
70-                            snac->config_o = xs_json_load(f);
71+                            user->config_o = xs_json_load(f);
72                             fclose(f);
73 
74-                            if (snac->config_o == NULL)
75+                            if (user->config_o == NULL)
76                                 srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
77                         }
78 
79-                        if (snac->config_o == NULL)
80-                            snac->config_o = xs_dict_new();
81+                        if (user->config_o == NULL)
82+                            user->config_o = xs_dict_new();
83                     }
84                     else
85                         srv_log(xs_fmt("error parsing '%s'", key_file));
86@@ -237,7 +237,7 @@ int user_open(snac *snac, const char *uid)
87         srv_debug(1, xs_fmt("invalid user '%s'", uid));
88 
89     if (!ret)
90-        user_free(snac);
91+        user_free(user);
92 
93     return ret;
94 }
+40, -35
  1@@ -26,11 +26,10 @@
  2 #include <poll.h>
  3 #endif
  4 
  5-int use_fcgi = 0;
  6+/** server stat **/
  7 
  8-int srv_running = 0;
  9-
 10-time_t srv_start_time = 0;
 11+srv_stat s_stat = {0};
 12+srv_stat *p_stat = NULL;
 13 
 14 /** job control **/
 15 
 16@@ -229,13 +228,9 @@ int server_get_handler(xs_dict *req, const char *q_path,
 17         *ctype = "text/plain";
 18         *body  = xs_str_new("UP\n");
 19 
 20-        xs *uptime = xs_str_time_diff(time(NULL) - srv_start_time);
 21+        xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time);
 22         srv_log(xs_fmt("status: uptime: %s", uptime));
 23-
 24-        pthread_mutex_lock(&job_mutex);
 25-        int l = xs_list_len(job_fifo);
 26-        pthread_mutex_unlock(&job_mutex);
 27-        srv_log(xs_fmt("status: job_fifo len: %d", l));
 28+        srv_log(xs_fmt("status: job_fifo len: %d", p_stat->job_fifo_size));
 29     }
 30 
 31     if (status != 0)
 32@@ -262,7 +257,7 @@ void httpd_connection(FILE *f)
 33     char *p;
 34     int fcgi_id;
 35 
 36-    if (use_fcgi)
 37+    if (p_stat->use_fcgi)
 38         req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id);
 39     else
 40         req = xs_httpd_request(f, &payload, &p_size);
 41@@ -400,7 +395,7 @@ void httpd_connection(FILE *f)
 42     headers = xs_dict_append(headers, "access-control-allow-origin", "*");
 43     headers = xs_dict_append(headers, "access-control-allow-headers", "*");
 44 
 45-    if (use_fcgi)
 46+    if (p_stat->use_fcgi)
 47         xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
 48     else
 49         xs_httpd_response(f, status, headers, body, b_size);
 50@@ -454,6 +449,8 @@ void job_post(const xs_val *job, int urgent)
 51                 job_fifo = xs_list_insert(job_fifo, 0, job);
 52             else
 53                 job_fifo = xs_list_append(job_fifo, job);
 54+
 55+            p_stat->job_fifo_size++;
 56         }
 57 
 58         /* unlock the mutex */
 59@@ -475,9 +472,12 @@ void job_wait(xs_val **job)
 60         pthread_mutex_lock(&job_mutex);
 61 
 62         /* dequeue */
 63-        if (job_fifo != NULL)
 64+        if (job_fifo != NULL) {
 65             job_fifo = xs_list_shift(job_fifo, job);
 66 
 67+            p_stat->job_fifo_size--;
 68+        }
 69+
 70         /* unlock the mutex */
 71         pthread_mutex_unlock(&job_mutex);
 72     }
 73@@ -541,7 +541,7 @@ static void *background_thread(void *arg)
 74 
 75     srv_log(xs_fmt("background thread started"));
 76 
 77-    while (srv_running) {
 78+    while (p_stat->srv_running) {
 79         time_t t;
 80         int cnt = 0;
 81 
 82@@ -605,14 +605,18 @@ void httpd(void)
 83     const char *port;
 84     int rs;
 85     pthread_t threads[MAX_THREADS] = {0};
 86-    int n_threads = 0;
 87     int n;
 88-    char sem_name[24];
 89+    xs *sem_name = NULL;
 90     sem_t anon_job_sem;
 91 
 92-    srv_start_time = time(NULL);
 93+    /* setup the server stat structure */
 94+    {
 95+        p_stat = &s_stat;
 96+    }
 97+
 98+    p_stat->srv_start_time = time(NULL);
 99 
100-    use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
101+    p_stat->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
102 
103     address = xs_dict_get(srv_config, "address");
104     port    = xs_number_str(xs_dict_get(srv_config, "port"));
105@@ -622,13 +626,13 @@ void httpd(void)
106         return;
107     }
108 
109-    srv_running = 1;
110+    p_stat->srv_running = 1;
111 
112     signal(SIGPIPE, SIG_IGN);
113     signal(SIGTERM, term_handler);
114     signal(SIGINT,  term_handler);
115 
116-    srv_log(xs_fmt("httpd%s start %s:%s %s", use_fcgi ? " (FastCGI)" : "",
117+    srv_log(xs_fmt("httpd%s start %s:%s %s", p_stat->use_fcgi ? " (FastCGI)" : "",
118                     address, port, USER_AGENT));
119 
120     /* show the number of usable file descriptors */
121@@ -639,7 +643,7 @@ void httpd(void)
122 
123     /* initialize the job control engine */
124     pthread_mutex_init(&job_mutex, NULL);
125-    snprintf(sem_name, sizeof(sem_name), "/job_%d", getpid());
126+    sem_name = xs_fmt("/job_%d", getpid());
127     job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
128 
129     if (job_sem == NULL) {
130@@ -659,29 +663,29 @@ void httpd(void)
131     pthread_mutex_init(&sleep_mutex, NULL);
132     pthread_cond_init(&sleep_cond, NULL);
133 
134-    n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
135+    p_stat->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
136 
137 #ifdef _SC_NPROCESSORS_ONLN
138-    if (n_threads == 0) {
139+    if (p_stat->n_threads == 0) {
140         /* get number of CPUs on the machine */
141-        n_threads = sysconf(_SC_NPROCESSORS_ONLN);
142+        p_stat->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
143     }
144 #endif
145 
146-    if (n_threads < 4)
147-        n_threads = 4;
148+    if (p_stat->n_threads < 4)
149+        p_stat->n_threads = 4;
150 
151-    if (n_threads > MAX_THREADS)
152-        n_threads = MAX_THREADS;
153+    if (p_stat->n_threads > MAX_THREADS)
154+        p_stat->n_threads = MAX_THREADS;
155 
156-    srv_debug(0, xs_fmt("using %d threads", n_threads));
157+    srv_debug(0, xs_fmt("using %d threads", p_stat->n_threads));
158 
159     /* thread #0 is the background thread */
160     pthread_create(&threads[0], NULL, background_thread, NULL);
161 
162     /* the rest of threads are for job processing */
163     char *ptr = (char *) 0x1;
164-    for (n = 1; n < n_threads; n++)
165+    for (n = 1; n < p_stat->n_threads; n++)
166         pthread_create(&threads[n], NULL, job_thread, ptr++);
167 
168     if (setjmp(on_break) == 0) {
169@@ -697,14 +701,14 @@ void httpd(void)
170         }
171     }
172 
173-    srv_running = 0;
174+    p_stat->srv_running = 0;
175 
176     /* send as many empty jobs as working threads */
177-    for (n = 1; n < n_threads; n++)
178+    for (n = 1; n < p_stat->n_threads; n++)
179         job_post(NULL, 0);
180 
181     /* wait for all the threads to exit */
182-    for (n = 0; n < n_threads; n++)
183+    for (n = 0; n < p_stat->n_threads; n++)
184         pthread_join(threads[n], NULL);
185 
186     pthread_mutex_lock(&job_mutex);
187@@ -714,8 +718,9 @@ void httpd(void)
188     sem_close(job_sem);
189     sem_unlink(sem_name);
190 
191-    xs *uptime = xs_str_time_diff(time(NULL) - srv_start_time);
192+    xs *uptime = xs_str_time_diff(time(NULL) - p_stat->srv_start_time);
193 
194-    srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)", use_fcgi ? " (FastCGI)" : "",
195+    srv_log(xs_fmt("httpd%s stop %s:%s (run time: %s)",
196+                p_stat->use_fcgi ? " (FastCGI)" : "",
197                 address, port, uptime));
198 }
M snac.h
+9, -1
 1@@ -31,7 +31,7 @@ void srv_log(xs_str *str);
 2 #define srv_debug(level, str) do { if (dbglevel >= (level)) \
 3     { srv_log((str)); } } while (0)
 4 
 5-typedef struct _snac {
 6+typedef struct {
 7     xs_str *uid;        /* uid */
 8     xs_str *basedir;    /* user base directory */
 9     xs_dict *config;    /* user configuration */
10@@ -41,6 +41,14 @@ typedef struct _snac {
11     xs_str *md5;        /* actor url md5 */
12 } snac;
13 
14+typedef struct {
15+    int srv_running;        /* server running on/off */
16+    int use_fcgi;           /* FastCGI use on/off */
17+    time_t srv_start_time;  /* start time */
18+    int job_fifo_size;      /* job fifo size */
19+    int n_threads;          /* number of configured threads */
20+} srv_stat;
21+
22 void snac_log(snac *user, xs_str *str);
23 #define snac_debug(user, level, str) do { if (dbglevel >= (level)) \
24     { snac_log((user), (str)); } } while (0)