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