2.78
httpd.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_io.h"
6#include "xs_json.h"
7#include "xs_socket.h"
8#include "xs_unix_socket.h"
9#include "xs_httpd.h"
10#include "xs_mime.h"
11#include "xs_time.h"
12#include "xs_openssl.h"
13#include "xs_fcgi.h"
14#include "xs_html.h"
15
16#include "snac.h"
17
18#include <setjmp.h>
19#include <pthread.h>
20#include <semaphore.h>
21#include <fcntl.h>
22#include <stdint.h>
23
24#include <sys/resource.h> // for getrlimit()
25
26#include <sys/mman.h>
27
28#ifdef USE_POLL_FOR_SLEEP
29#include <poll.h>
30#endif
31
32/** server state **/
33srv_state *p_state = NULL;
34
35
36/** job control **/
37
38/* mutex to access the lists of jobs */
39static pthread_mutex_t job_mutex;
40
41/* semaphore to trigger job processing */
42static sem_t *job_sem;
43
44typedef struct job_fifo_item {
45 struct job_fifo_item *next;
46 xs_val *job;
47} job_fifo_item;
48
49static job_fifo_item *job_fifo_first = NULL;
50static job_fifo_item *job_fifo_last = NULL;
51
52
53/** other global data **/
54
55static jmp_buf on_break;
56
57
58/** code **/
59
60/* nodeinfo 2.0 template */
61const char *nodeinfo_2_0_template = ""
62 "{\"version\":\"2.0\","
63 "\"software\":{\"name\":\"snac\",\"version\":\"" VERSION "\"},"
64 "\"protocols\":[\"activitypub\"],"
65 "\"services\":{\"outbound\":[],\"inbound\":[]},"
66 "\"usage\":{\"users\":{\"total\":%d,\"activeMonth\":%d,\"activeHalfyear\":%d},"
67 "\"localPosts\":%d},"
68 "\"openRegistrations\":false,\"metadata\":{}}";
69
70xs_str *nodeinfo_2_0(void)
71/* builds a nodeinfo json object */
72{
73 int n_utotal = 0;
74 int n_umonth = 0;
75 int n_uhyear = 0;
76 int n_posts = 0;
77 xs *users = user_list();
78 xs_list *p = users;
79 const char *v;
80 double now = (double)time(NULL);
81
82 while (xs_list_iter(&p, &v)) {
83 /* build the full path name to the last usage log */
84 xs *llfn = xs_fmt("%s/user/%s/lastlog.txt", srv_basedir, v);
85 double llsecs = now - mtime(llfn);
86
87 if (llsecs < 60 * 60 * 24 * 30 * 6) {
88 n_uhyear++;
89
90 if (llsecs < 60 * 60 * 24 * 30)
91 n_umonth++;
92 }
93
94 n_utotal++;
95
96 /* build the file to each user public.idx */
97 xs *pidxfn = xs_fmt("%s/user/%s/public.idx", srv_basedir, v);
98 n_posts += index_len(pidxfn);
99 }
100
101 return xs_fmt(nodeinfo_2_0_template, n_utotal, n_umonth, n_uhyear, n_posts);
102}
103
104
105static xs_str *greeting_html(void)
106/* processes and returns greeting.html */
107{
108 /* try to open greeting.html */
109 xs *fn = xs_fmt("%s/greeting.html", srv_basedir);
110 FILE *f;
111 xs_str *s = NULL;
112
113 if ((f = fopen(fn, "r")) != NULL) {
114 s = xs_readall(f);
115 fclose(f);
116
117 /* replace %host% */
118 s = xs_replace_i(s, "%host%", xs_dict_get(srv_config, "host"));
119
120 const char *adm_email = xs_dict_get(srv_config, "admin_email");
121 if (xs_is_null(adm_email) || *adm_email == '\0')
122 adm_email = "the administrator of this instance";
123
124 /* replace %admin_email */
125 s = xs_replace_i(s, "%admin_email%", adm_email);
126
127 /* does it have a %userlist% mark? */
128 if (xs_str_in(s, "%userlist%") != -1) {
129 const char *host = xs_dict_get(srv_config, "host");
130 xs *list = user_list();
131 xs_list *p = list;
132 const xs_str *uid;
133
134 xs_html *ul = xs_html_tag("ul",
135 xs_html_attr("class", "snac-user-list"));
136
137 p = list;
138 while (xs_list_iter(&p, &uid)) {
139 snac user;
140
141 if (strcmp(uid, "relay") && user_open(&user, uid)) {
142 xs *formatted_name = format_text_with_emoji(NULL, xs_dict_get(user.config, "name"), 1, NULL);
143
144 xs_html_add(ul,
145 xs_html_tag("li",
146 xs_html_tag("a",
147 xs_html_attr("href", user.actor),
148 xs_html_text("@"),
149 xs_html_text(uid),
150 xs_html_text("@"),
151 xs_html_text(host),
152 xs_html_text(" ("),
153 xs_html_raw(formatted_name),
154 xs_html_text(")"))));
155
156 user_free(&user);
157 }
158 }
159
160 xs *s1 = xs_html_render(ul);
161 s = xs_replace_i(s, "%userlist%", s1);
162 }
163 }
164
165 return s;
166}
167
168
169const char *share_page = ""
170"<!DOCTYPE html>\n"
171"<html>\n"
172"<head>\n"
173"<title>%s - snac</title>\n"
174"<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">\n"
175"<link rel=\"stylesheet\" type=\"text/css\" href=\"%s/style.css\"/>\n"
176"<style>:root {color-scheme: light dark}</style>\n"
177"</head>\n"
178"<body><h1>%s link share</h1>\n"
179"<form method=\"get\" action=\"%s/share-bridge\">\n"
180"<textarea name=\"content\" rows=\"6\" wrap=\"virtual\" required=\"required\" style=\"width: 50em\">%s</textarea>\n"
181"<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\" required=\"required\"></p>\n"
182"<input type=\"submit\" value=\"OK\">\n"
183"</form><p>%s</p></body></html>\n"
184"";
185
186
187const char *authorize_interaction_page = ""
188"<!DOCTYPE html>\n"
189"<html>\n"
190"<head>\n"
191"<title>%s - snac</title>\n"
192"<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">\n"
193"<link rel=\"stylesheet\" type=\"text/css\" href=\"%s/style.css\"/>\n"
194"<style>:root {color-scheme: light dark}</style>\n"
195"</head>\n"
196"<body><h1>%s authorize interaction</h1>\n"
197"<form method=\"get\" action=\"%s/auth-int-bridge\">\n"
198"<select name=\"action\">\n"
199"<option value=\"Follow\">Follow</option>\n"
200"<option value=\"Boost\">Boost</option>\n"
201"<option value=\"Like\">Like</option>\n"
202"</select> %s\n"
203"<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
204"<p>Login: <input type=\"text\" name=\"login\" autocapitalize=\"off\" required=\"required\"></p>\n"
205"<input type=\"submit\" value=\"OK\">\n"
206"</form><p>%s</p></body></html>\n"
207"";
208
209
210int server_get_handler(xs_dict *req, const char *q_path,
211 char **body, int *b_size, char **ctype)
212/* basic server services */
213{
214 int status = 0;
215
216 const snac *user = NULL;
217
218 /* is it the server root? */
219 if (*q_path == '\0' || strcmp(q_path, "/") == 0) {
220 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
221 const char *t = NULL;
222
223 if (xs_type(q_vars) == XSTYPE_DICT && (t = xs_dict_get(q_vars, "t"))) {
224 /** search by tag **/
225 int skip = 0;
226 int show = xs_number_get(xs_dict_get_def(srv_config, "def_timeline_entries",
227 xs_dict_get_def(srv_config, "max_timeline_entries", "50")));
228 const char *v;
229
230 if ((v = xs_dict_get(q_vars, "skip")) != NULL)
231 skip = atoi(v);
232 if ((v = xs_dict_get(q_vars, "show")) != NULL)
233 show = atoi(v);
234
235 xs *tl = tag_search(t, skip, show + 1);
236 int more = 0;
237 if (xs_list_len(tl) >= show + 1) {
238 /* drop the last one */
239 tl = xs_list_del(tl, -1);
240 more = 1;
241 }
242
243 const char *accept = xs_dict_get(req, "accept");
244 if (!xs_is_null(accept) && strcmp(accept, "application/rss+xml") == 0) {
245 xs *link = xs_fmt("%s/?t=%s", srv_baseurl, t);
246
247 *body = rss_from_timeline(NULL, tl, link, link, link);
248 *ctype = "application/rss+xml; charset=utf-8";
249 }
250 else {
251 xs *page = xs_fmt("?t=%s", t);
252 xs *title = xs_fmt(L("Search results for tag #%s"), t);
253 *body = html_timeline(NULL, tl, 0, skip, show, more, title, page, 0, NULL);
254 }
255 }
256 else
257 if (xs_type(xs_dict_get(srv_config, "show_instance_timeline")) == XSTYPE_TRUE) {
258 /** instance timeline **/
259 xs *tl = timeline_instance_list(0, 30);
260 *body = html_timeline(NULL, tl, 0, 0, 0, 0,
261 L("Recent posts by users in this instance"), NULL, 0, NULL);
262 }
263 else
264 *body = greeting_html();
265
266 if (*body)
267 status = HTTP_STATUS_OK;
268 }
269 else
270 if (strcmp(q_path, "/susie.png") == 0 || strcmp(q_path, "/favicon.ico") == 0 ) {
271 status = HTTP_STATUS_OK;
272 *body = xs_base64_dec(default_avatar_base64(), b_size);
273 *ctype = "image/png";
274 }
275 else
276 if (strcmp(q_path, "/.well-known/nodeinfo") == 0) {
277 status = HTTP_STATUS_OK;
278 *ctype = "application/json; charset=utf-8";
279 *body = xs_fmt("{\"links\":["
280 "{\"rel\":\"http:/" "/nodeinfo.diaspora.software/ns/schema/2.0\","
281 "\"href\":\"%s/nodeinfo_2_0\"}]}",
282 srv_baseurl);
283 }
284 else
285 if (strcmp(q_path, "/.well-known/host-meta") == 0) {
286 status = HTTP_STATUS_OK;
287 *ctype = "application/xrd+xml";
288 *body = xs_fmt("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
289 "<XRD>"
290 "<Link rel=\"lrdd\" type=\"application/xrd+xml\" template=\"https://%s/.well-known/webfinger?resource={uri}\"/>"
291 "</XRD>", xs_dict_get(srv_config, "host"));
292 }
293 else
294 if (strcmp(q_path, "/nodeinfo_2_0") == 0) {
295 status = HTTP_STATUS_OK;
296 *ctype = "application/json; charset=utf-8";
297 *body = nodeinfo_2_0();
298 }
299 else
300 if (strcmp(q_path, "/robots.txt") == 0) {
301 status = HTTP_STATUS_OK;
302 *ctype = "text/plain";
303 *body = xs_str_new("User-agent: *\n"
304 "Disallow: /\n");
305 }
306 else
307 if (strcmp(q_path, "/style.css") == 0) {
308 FILE *f;
309 xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
310
311 if ((f = fopen(css_fn, "r")) != NULL) {
312 *body = xs_readall(f);
313 fclose(f);
314
315 status = HTTP_STATUS_OK;
316 *ctype = "text/css";
317 }
318 }
319 else
320 if (strcmp(q_path, "/share") == 0) {
321 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
322 const char *url = xs_dict_get(q_vars, "url");
323 const char *text = xs_dict_get(q_vars, "text");
324 xs *s = NULL;
325
326 if (xs_type(text) == XSTYPE_STRING) {
327 if (xs_type(url) == XSTYPE_STRING)
328 s = xs_fmt("%s:\n\n%s\n", text, url);
329 else
330 s = xs_fmt("%s\n", text);
331 }
332 else
333 if (xs_type(url) == XSTYPE_STRING)
334 s = xs_fmt("%s\n", url);
335 else
336 s = xs_str_new(NULL);
337
338 status = HTTP_STATUS_OK;
339 *ctype = "text/html; charset=utf-8";
340 *body = xs_fmt(share_page,
341 xs_dict_get(srv_config, "host"),
342 srv_baseurl,
343 xs_dict_get(srv_config, "host"),
344 srv_baseurl,
345 s,
346 USER_AGENT
347 );
348 }
349 else
350 if (strcmp(q_path, "/authorize_interaction") == 0) {
351 const xs_dict *q_vars = xs_dict_get(req, "q_vars");
352 const char *uri = xs_dict_get(q_vars, "uri");
353
354 if (xs_is_string(uri)) {
355 status = HTTP_STATUS_OK;
356 *ctype = "text/html; charset=utf-8";
357 *body = xs_fmt(authorize_interaction_page,
358 xs_dict_get(srv_config, "host"),
359 srv_baseurl,
360 xs_dict_get(srv_config, "host"),
361 srv_baseurl,
362 uri,
363 uri,
364 USER_AGENT
365 );
366 }
367 }
368
369 if (status != 0)
370 srv_debug(1, xs_fmt("server_get_handler serving '%s' %d", q_path, status));
371
372 return status;
373}
374
375
376void httpd_connection(FILE *f)
377/* the connection processor */
378{
379 xs *req;
380 const char *method;
381 int status = 0;
382 xs_str *body = NULL;
383 int b_size = 0;
384 char *ctype = NULL;
385 xs *headers = xs_dict_new();
386 xs *q_path = NULL;
387 xs *payload = NULL;
388 xs *etag = NULL;
389 xs *last_modified = NULL;
390 xs *link = NULL;
391 int p_size = 0;
392 const char *p;
393 int fcgi_id;
394
395 if (p_state->use_fcgi)
396 req = xs_fcgi_request(f, &payload, &p_size, &fcgi_id);
397 else
398 req = xs_httpd_request(f, &payload, &p_size);
399
400 if (req == NULL) {
401 /* probably because a timeout */
402 fclose(f);
403 return;
404 }
405
406 if (!(method = xs_dict_get(req, "method")) || !(p = xs_dict_get(req, "path"))) {
407 /* missing needed headers; discard */
408 fclose(f);
409 return;
410 }
411
412 q_path = xs_dup(p);
413
414 /* crop the q_path from leading / and the prefix */
415 if (xs_endswith(q_path, "/"))
416 q_path = xs_crop_i(q_path, 0, -1);
417
418 p = xs_dict_get(srv_config, "prefix");
419 if (xs_startswith(q_path, p))
420 q_path = xs_crop_i(q_path, strlen(p), 0);
421
422 if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0) {
423 /* cascade through */
424 if (status == 0)
425 status = server_get_handler(req, q_path, &body, &b_size, &ctype);
426
427 if (status == 0)
428 status = webfinger_get_handler(req, q_path, &body, &b_size, &ctype);
429
430 if (status == 0)
431 status = activitypub_get_handler(req, q_path, &body, &b_size, &ctype);
432
433#ifndef NO_MASTODON_API
434 if (status == 0)
435 status = oauth_get_handler(req, q_path, &body, &b_size, &ctype);
436
437 if (status == 0)
438 status = mastoapi_get_handler(req, q_path, &body, &b_size, &ctype, &link);
439#endif /* NO_MASTODON_API */
440
441 if (status == 0)
442 status = html_get_handler(req, q_path, &body, &b_size, &ctype, &etag, &last_modified);
443 }
444 else
445 if (strcmp(method, "POST") == 0) {
446
447#ifndef NO_MASTODON_API
448 if (status == 0)
449 status = oauth_post_handler(req, q_path,
450 payload, p_size, &body, &b_size, &ctype);
451
452 if (status == 0)
453 status = mastoapi_post_handler(req, q_path,
454 payload, p_size, &body, &b_size, &ctype);
455#endif
456
457 if (status == 0)
458 status = activitypub_post_handler(req, q_path,
459 payload, p_size, &body, &b_size, &ctype);
460
461 if (status == 0)
462 status = html_post_handler(req, q_path,
463 payload, p_size, &body, &b_size, &ctype);
464 }
465 else
466 if (strcmp(method, "PUT") == 0) {
467
468#ifndef NO_MASTODON_API
469 if (status == 0)
470 status = mastoapi_put_handler(req, q_path,
471 payload, p_size, &body, &b_size, &ctype);
472#endif
473
474 }
475 else
476 if (strcmp(method, "PATCH") == 0) {
477
478#ifndef NO_MASTODON_API
479 if (status == 0)
480 status = mastoapi_patch_handler(req, q_path,
481 payload, p_size, &body, &b_size, &ctype);
482#endif
483
484 }
485 else
486 if (strcmp(method, "OPTIONS") == 0) {
487 const char *methods = "OPTIONS, GET, HEAD, POST, PUT, DELETE";
488 headers = xs_dict_append(headers, "allow", methods);
489 headers = xs_dict_append(headers, "access-control-allow-methods", methods);
490 status = HTTP_STATUS_OK;
491 }
492 else
493 if (strcmp(method, "DELETE") == 0) {
494#ifndef NO_MASTODON_API
495 if (status == 0)
496 status = mastoapi_delete_handler(req, q_path,
497 payload, p_size, &body, &b_size, &ctype);
498#endif
499 }
500
501 /* unattended? it's an error */
502 if (status == 0) {
503 srv_archive_error("unattended_method", "unattended method", req, payload);
504 srv_debug(1, xs_fmt("httpd_connection unattended %s %s", method, q_path));
505 status = HTTP_STATUS_NOT_FOUND;
506 }
507
508 if (status == HTTP_STATUS_FORBIDDEN)
509 body = xs_str_new("<h1>403 Forbidden (" USER_AGENT ")</h1>");
510
511 if (status == HTTP_STATUS_NOT_FOUND)
512 body = xs_str_new("<h1>404 Not Found (" USER_AGENT ")</h1>");
513
514 if (status == HTTP_STATUS_BAD_REQUEST && body != NULL)
515 body = xs_str_new("<h1>400 Bad Request (" USER_AGENT ")</h1>");
516
517 if (status == HTTP_STATUS_SEE_OTHER)
518 headers = xs_dict_append(headers, "location", body);
519
520 if (status == HTTP_STATUS_UNAUTHORIZED && body) {
521 xs *www_auth = xs_fmt("Basic realm=\"@%s@%s snac login\"",
522 body, xs_dict_get(srv_config, "host"));
523
524 headers = xs_dict_append(headers, "WWW-Authenticate", www_auth);
525 headers = xs_dict_append(headers, "Cache-Control", "no-cache, must-revalidate, max-age=0");
526 }
527
528 if (ctype == NULL)
529 ctype = "text/html; charset=utf-8";
530
531 headers = xs_dict_append(headers, "content-type", ctype);
532 headers = xs_dict_append(headers, "x-creator", USER_AGENT);
533
534 if (!xs_is_null(etag))
535 headers = xs_dict_append(headers, "etag", etag);
536 if (!xs_is_null(last_modified))
537 headers = xs_dict_append(headers, "last-modified", last_modified);
538 if (!xs_is_null(link))
539 headers = xs_dict_append(headers, "Link", link);
540
541 /* if there are any additional headers, add them */
542 const xs_dict *more_headers = xs_dict_get(srv_config, "http_headers");
543 if (xs_type(more_headers) == XSTYPE_DICT) {
544 const char *k, *v;
545 int c = 0;
546 while (xs_dict_next(more_headers, &k, &v, &c))
547 headers = xs_dict_set(headers, k, v);
548 }
549
550 if (b_size == 0 && body != NULL)
551 b_size = strlen(body);
552
553 /* if it was a HEAD, no body will be sent */
554 if (strcmp(method, "HEAD") == 0)
555 body = xs_free(body);
556
557 headers = xs_dict_append(headers, "access-control-allow-origin", "*");
558 headers = xs_dict_append(headers, "access-control-allow-headers", "*");
559 headers = xs_dict_append(headers, "access-control-expose-headers", "Link");
560
561 /* disable any form of fucking JavaScript */
562 headers = xs_dict_append(headers, "Content-Security-Policy", "script-src ;");
563
564 if (p_state->use_fcgi)
565 xs_fcgi_response(f, status, headers, body, b_size, fcgi_id);
566 else
567 xs_httpd_response(f, status, http_status_text(status), headers, body, b_size);
568
569 fclose(f);
570
571 srv_archive("RECV", NULL, req, payload, p_size, status, headers, body, b_size);
572
573 /* JSON validation check */
574 if (!xs_is_null(body) && strcmp(ctype, "application/json") == 0) {
575 xs *j = xs_json_loads(body);
576
577 if (j == NULL) {
578 srv_log(xs_fmt("bad JSON"));
579 srv_archive_error("bad_json", "bad JSON", req, body);
580 }
581 }
582
583 xs_free(body);
584}
585
586
587void job_post(const xs_val *job, int urgent)
588/* posts a job for the threads to process it */
589{
590 if (job != NULL) {
591 /* lock the mutex */
592 pthread_mutex_lock(&job_mutex);
593
594 job_fifo_item *i = xs_realloc(NULL, sizeof(job_fifo_item));
595 *i = (job_fifo_item){ NULL, xs_dup(job) };
596
597 if (job_fifo_first == NULL)
598 job_fifo_first = job_fifo_last = i;
599 else
600 if (urgent) {
601 /* prepend */
602 i->next = job_fifo_first;
603 job_fifo_first = i;
604 }
605 else {
606 /* append */
607 job_fifo_last->next = i;
608 job_fifo_last = i;
609 }
610
611 p_state->job_fifo_size++;
612
613 if (p_state->job_fifo_size > p_state->peak_job_fifo_size)
614 p_state->peak_job_fifo_size = p_state->job_fifo_size;
615
616 /* unlock the mutex */
617 pthread_mutex_unlock(&job_mutex);
618
619 /* ask for someone to attend it */
620 sem_post(job_sem);
621 }
622}
623
624
625void job_wait(xs_val **job)
626/* waits for an available job */
627{
628 *job = NULL;
629
630 if (sem_wait(job_sem) == 0) {
631 /* lock the mutex */
632 pthread_mutex_lock(&job_mutex);
633
634 /* dequeue */
635 job_fifo_item *i = job_fifo_first;
636
637 if (i != NULL) {
638 job_fifo_first = i->next;
639
640 if (job_fifo_first == NULL)
641 job_fifo_last = NULL;
642
643 *job = i->job;
644 xs_free(i);
645
646 p_state->job_fifo_size--;
647 }
648
649 /* unlock the mutex */
650 pthread_mutex_unlock(&job_mutex);
651 }
652}
653
654
655static void *job_thread(void *arg)
656/* job thread */
657{
658 int pid = (int)(uintptr_t)arg;
659
660 srv_debug(1, xs_fmt("job thread %d started", pid));
661
662 for (;;) {
663 xs *job = NULL;
664
665 p_state->th_state[pid] = THST_WAIT;
666
667 job_wait(&job);
668
669 if (job == NULL) /* corrupted message? */
670 continue;
671
672 if (xs_type(job) == XSTYPE_FALSE) /* special message: exit */
673 break;
674 else
675 if (xs_type(job) == XSTYPE_DATA) {
676 /* it's a socket */
677 FILE *f = NULL;
678
679 p_state->th_state[pid] = THST_IN;
680
681 xs_data_get(&f, job);
682
683 if (f != NULL)
684 httpd_connection(f);
685 }
686 else {
687 /* it's a q_item */
688 p_state->th_state[pid] = THST_QUEUE;
689
690 process_queue_item(job);
691 }
692 }
693
694 p_state->th_state[pid] = THST_STOP;
695
696 srv_debug(1, xs_fmt("job thread %d stopped", pid));
697
698 return NULL;
699}
700
701/* background thread sleep control */
702static pthread_mutex_t sleep_mutex;
703static pthread_cond_t sleep_cond;
704
705static void *background_thread(void *arg)
706/* background thread (queue management and other things) */
707{
708 time_t t, purge_time, rss_time;
709
710 (void)arg;
711
712 t = time(NULL);
713
714 /* first purge time */
715 purge_time = t + 10 * 60;
716
717 /* first RSS polling time */
718 rss_time = t + 15 * 60;
719
720 srv_log(xs_fmt("background thread started"));
721
722 while (p_state->srv_running) {
723 int cnt = 0;
724
725 p_state->th_state[0] = THST_QUEUE;
726
727 {
728 xs *list = user_list();
729 const char *uid;
730
731 /* process queues for all users */
732 xs_list_foreach(list, uid) {
733 snac user;
734
735 if (user_open(&user, uid)) {
736 cnt += process_user_queue(&user);
737 user_free(&user);
738 }
739 }
740 }
741
742 /* global queue */
743 cnt += process_queue();
744
745 t = time(NULL);
746
747 /* time to purge? */
748 if (t > purge_time) {
749 /* next purge time is tomorrow */
750 purge_time = t + 24 * 60 * 60;
751
752 xs *q_item = xs_dict_new();
753 q_item = xs_dict_append(q_item, "type", "purge");
754 job_post(q_item, 0);
755 }
756
757 /* time to poll the RSS? */
758 if (t > rss_time) {
759 /* next RSS poll time */
760 int hours = xs_number_get(xs_dict_get_def(srv_config, "rss_hashtag_poll_hours", "4"));
761
762 /* don't hammer servers too much */
763 if (hours < 1)
764 hours = 1;
765
766 rss_time = t + 60 * 60 * hours;
767
768 xs *q_item = xs_dict_new();
769 q_item = xs_dict_append(q_item, "type", "rss_hashtag_poll");
770 job_post(q_item, 0);
771 }
772
773 if (cnt == 0) {
774 /* sleep 3 seconds */
775
776 p_state->th_state[0] = THST_WAIT;
777
778#ifdef USE_POLL_FOR_SLEEP
779 poll(NULL, 0, 3 * 1000);
780#else
781 struct timespec ts;
782
783 clock_gettime(CLOCK_REALTIME, &ts);
784 ts.tv_sec += 3;
785
786 pthread_mutex_lock(&sleep_mutex);
787 while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
788 pthread_mutex_unlock(&sleep_mutex);
789#endif
790 }
791 }
792
793 p_state->th_state[0] = THST_STOP;
794
795 srv_log(xs_fmt("background thread stopped"));
796
797 return NULL;
798}
799
800
801void term_handler(int s)
802{
803 (void)s;
804
805 longjmp(on_break, 1);
806}
807
808
809srv_state *srv_state_op(xs_str **fname, int op)
810/* opens or deletes the shared memory object */
811{
812 int fd;
813 srv_state *ss = NULL;
814
815 if (*fname == NULL)
816 *fname = xs_fmt("/%s_snac_state", xs_dict_get(srv_config, "host"));
817
818 switch (op) {
819 case 0: /* open for writing */
820
821#ifdef WITHOUT_SHM
822
823 errno = ENOTSUP;
824
825#else
826
827 if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) {
828 ftruncate(fd, sizeof(*ss));
829
830 if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE,
831 MAP_SHARED, fd, 0)) == MAP_FAILED)
832 ss = NULL;
833
834 close(fd);
835 }
836
837#endif
838
839 if (ss == NULL) {
840 /* shared memory error: just create a plain structure */
841 srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno)));
842 ss = malloc(sizeof(*ss));
843 }
844
845 /* init structure */
846 *ss = (srv_state){0};
847 ss->s_size = sizeof(*ss);
848
849 break;
850
851 case 1: /* open for reading */
852
853#ifdef WITHOUT_SHM
854
855 errno = ENOTSUP;
856
857#else
858
859 if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) {
860 if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
861 ss = NULL;
862
863 close(fd);
864 }
865
866#endif
867
868 if (ss == NULL) {
869 /* shared memory error */
870 srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno)));
871 }
872 else
873 if (ss->s_size != sizeof(*ss)) {
874 srv_log(xs_fmt("error: struct size mismatch (%d != %d)",
875 ss->s_size, sizeof(*ss)));
876
877 munmap(ss, sizeof(*ss));
878
879 ss = NULL;
880 }
881
882 break;
883
884 case 2: /* unlink */
885
886#ifndef WITHOUT_SHM
887
888 if (*fname)
889 shm_unlink(*fname);
890
891#endif
892
893 break;
894 }
895
896 return ss;
897}
898
899
900void httpd(void)
901/* starts the server */
902{
903 const char *address = NULL;
904 const char *port = NULL;
905 xs *full_address = NULL;
906 int rs;
907 pthread_t threads[MAX_THREADS] = {0};
908 int n;
909 xs *sem_name = NULL;
910 xs *shm_name = NULL;
911 sem_t anon_job_sem;
912 xs *pidfile = xs_fmt("%s/server.pid", srv_basedir);
913 int pidfd;
914
915 {
916 /* do some pidfile locking acrobatics */
917 if ((pidfd = open(pidfile, O_RDWR | O_CREAT, 0660)) == -1) {
918 srv_log(xs_fmt("Cannot create pidfile %s -- cannot continue", pidfile));
919 return;
920 }
921
922 if (lockf(pidfd, F_TLOCK, 1) == -1) {
923 srv_log(xs_fmt("Cannot lock pidfile %s -- server already running?", pidfile));
924 close(pidfd);
925 return;
926 }
927
928 ftruncate(pidfd, 0);
929
930 xs *s = xs_fmt("%d\n", (int)getpid());
931 write(pidfd, s, strlen(s));
932 }
933
934 address = xs_dict_get(srv_config, "address");
935
936 if (*address == '/') {
937 rs = xs_unix_socket_server(address, NULL);
938 full_address = xs_fmt("unix:%s", address);
939 }
940 else {
941 port = xs_number_str(xs_dict_get(srv_config, "port"));
942 full_address = xs_fmt("%s:%s", address, port);
943
944 rs = xs_socket_server(address, port);
945 }
946
947 if (rs == -1) {
948 srv_log(xs_fmt("cannot bind socket to %s", full_address));
949 return;
950 }
951
952 /* setup the server stat structure */
953 p_state = srv_state_op(&shm_name, 0);
954
955 p_state->srv_start_time = time(NULL);
956
957 p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
958
959 p_state->srv_running = 1;
960
961 signal(SIGPIPE, SIG_IGN);
962 signal(SIGTERM, term_handler);
963 signal(SIGINT, term_handler);
964
965 srv_log(xs_fmt("httpd%s start %s %s", p_state->use_fcgi ? " (FastCGI)" : "",
966 full_address, USER_AGENT));
967
968 /* show the number of usable file descriptors */
969 struct rlimit r;
970 getrlimit(RLIMIT_NOFILE, &r);
971 srv_debug(1, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
972 (int) r.rlim_cur, (int) r.rlim_max));
973
974 /* initialize the job control engine */
975 pthread_mutex_init(&job_mutex, NULL);
976 sem_name = xs_fmt("/job_%d", getpid());
977 job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
978
979 if (job_sem == NULL) {
980 /* error opening a named semaphore; try with an anonymous one */
981 if (sem_init(&anon_job_sem, 0, 0) != -1)
982 job_sem = &anon_job_sem;
983 }
984
985 if (job_sem == NULL) {
986 srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
987 return;
988 }
989
990 /* initialize sleep control */
991 pthread_mutex_init(&sleep_mutex, NULL);
992 pthread_cond_init(&sleep_cond, NULL);
993
994 p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
995
996#ifdef _SC_NPROCESSORS_ONLN
997 if (p_state->n_threads == 0) {
998 /* get number of CPUs on the machine */
999 p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
1000 }
1001#endif
1002
1003 if (p_state->n_threads < 4)
1004 p_state->n_threads = 4;
1005
1006 if (p_state->n_threads > MAX_THREADS)
1007 p_state->n_threads = MAX_THREADS;
1008
1009 srv_debug(0, xs_fmt("using %d threads", p_state->n_threads));
1010
1011 /* thread #0 is the background thread */
1012 pthread_create(&threads[0], NULL, background_thread, NULL);
1013
1014 /* the rest of threads are for job processing */
1015 char *ptr = (char *) 0x1;
1016 for (n = 1; n < p_state->n_threads; n++)
1017 pthread_create(&threads[n], NULL, job_thread, ptr++);
1018
1019 if (setjmp(on_break) == 0) {
1020 for (;;) {
1021 int cs = xs_socket_accept(rs);
1022
1023 if (cs != -1) {
1024 FILE *f = fdopen(cs, "r+");
1025 xs *job = xs_data_new(&f, sizeof(FILE *));
1026 job_post(job, 1);
1027 }
1028 else
1029 break;
1030 }
1031 }
1032
1033 p_state->srv_running = 0;
1034
1035 /* send as many exit jobs as working threads */
1036 for (n = 1; n < p_state->n_threads; n++)
1037 job_post(xs_stock(XSTYPE_FALSE), 0);
1038
1039 /* wait for all the threads to exit */
1040 for (n = 0; n < p_state->n_threads; n++)
1041 pthread_join(threads[n], NULL);
1042
1043 sem_close(job_sem);
1044 sem_unlink(sem_name);
1045
1046 srv_state_op(&shm_name, 2);
1047
1048 xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
1049
1050 srv_log(xs_fmt("httpd%s stop %s (run time: %s)",
1051 p_state->use_fcgi ? " (FastCGI)" : "",
1052 full_address, uptime));
1053
1054 unlink(pidfile);
1055}