2.77 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 = timeline_to_rss(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 purge_time;
 709
 710    (void)arg;
 711
 712    /* first purge time */
 713    purge_time = time(NULL) + 10 * 60;
 714
 715    srv_log(xs_fmt("background thread started"));
 716
 717    while (p_state->srv_running) {
 718        time_t t;
 719        int cnt = 0;
 720
 721        p_state->th_state[0] = THST_QUEUE;
 722
 723        {
 724            xs *list = user_list();
 725            char *p;
 726            const char *uid;
 727
 728            /* process queues for all users */
 729            p = list;
 730            while (xs_list_iter(&p, &uid)) {
 731                snac snac;
 732
 733                if (user_open(&snac, uid)) {
 734                    cnt += process_user_queue(&snac);
 735                    user_free(&snac);
 736                }
 737            }
 738        }
 739
 740        /* global queue */
 741        cnt += process_queue();
 742
 743        /* time to purge? */
 744        if ((t = time(NULL)) > purge_time) {
 745            /* next purge time is tomorrow */
 746            purge_time = t + 24 * 60 * 60;
 747
 748            xs *q_item = xs_dict_new();
 749            q_item = xs_dict_append(q_item, "type", "purge");
 750            job_post(q_item, 0);
 751        }
 752
 753        if (cnt == 0) {
 754            /* sleep 3 seconds */
 755
 756            p_state->th_state[0] = THST_WAIT;
 757
 758#ifdef USE_POLL_FOR_SLEEP
 759            poll(NULL, 0, 3 * 1000);
 760#else
 761            struct timespec ts;
 762
 763            clock_gettime(CLOCK_REALTIME, &ts);
 764            ts.tv_sec += 3;
 765
 766            pthread_mutex_lock(&sleep_mutex);
 767            while (pthread_cond_timedwait(&sleep_cond, &sleep_mutex, &ts) == 0);
 768            pthread_mutex_unlock(&sleep_mutex);
 769#endif
 770        }
 771    }
 772
 773    p_state->th_state[0] = THST_STOP;
 774
 775    srv_log(xs_fmt("background thread stopped"));
 776
 777    return NULL;
 778}
 779
 780
 781void term_handler(int s)
 782{
 783    (void)s;
 784
 785    longjmp(on_break, 1);
 786}
 787
 788
 789srv_state *srv_state_op(xs_str **fname, int op)
 790/* opens or deletes the shared memory object */
 791{
 792    int fd;
 793    srv_state *ss = NULL;
 794
 795    if (*fname == NULL)
 796        *fname = xs_fmt("/%s_snac_state", xs_dict_get(srv_config, "host"));
 797
 798    switch (op) {
 799    case 0: /* open for writing */
 800
 801#ifdef WITHOUT_SHM
 802
 803        errno = ENOTSUP;
 804
 805#else
 806
 807        if ((fd = shm_open(*fname, O_CREAT | O_RDWR, 0666)) != -1) {
 808            ftruncate(fd, sizeof(*ss));
 809
 810            if ((ss = mmap(0, sizeof(*ss), PROT_READ | PROT_WRITE,
 811                 MAP_SHARED, fd, 0)) == MAP_FAILED)
 812                ss = NULL;
 813
 814            close(fd);
 815        }
 816
 817#endif
 818
 819        if (ss == NULL) {
 820            /* shared memory error: just create a plain structure */
 821            srv_log(xs_fmt("warning: shm object error (%s)", strerror(errno)));
 822            ss = malloc(sizeof(*ss));
 823        }
 824
 825        /* init structure */
 826        *ss = (srv_state){0};
 827        ss->s_size = sizeof(*ss);
 828
 829        break;
 830
 831    case 1: /* open for reading */
 832
 833#ifdef WITHOUT_SHM
 834
 835        errno = ENOTSUP;
 836
 837#else
 838
 839        if ((fd = shm_open(*fname, O_RDONLY, 0666)) != -1) {
 840            if ((ss = mmap(0, sizeof(*ss), PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
 841                ss = NULL;
 842
 843            close(fd);
 844        }
 845
 846#endif
 847
 848        if (ss == NULL) {
 849            /* shared memory error */
 850            srv_log(xs_fmt("error: shm object error (%s) server not running?", strerror(errno)));
 851        }
 852        else
 853        if (ss->s_size != sizeof(*ss)) {
 854            srv_log(xs_fmt("error: struct size mismatch (%d != %d)",
 855                ss->s_size, sizeof(*ss)));
 856
 857            munmap(ss, sizeof(*ss));
 858
 859            ss = NULL;
 860        }
 861
 862        break;
 863
 864    case 2: /* unlink */
 865
 866#ifndef WITHOUT_SHM
 867
 868        if (*fname)
 869            shm_unlink(*fname);
 870
 871#endif
 872
 873        break;
 874    }
 875
 876    return ss;
 877}
 878
 879
 880void httpd(void)
 881/* starts the server */
 882{
 883    const char *address = NULL;
 884    const char *port = NULL;
 885    xs *full_address = NULL;
 886    int rs;
 887    pthread_t threads[MAX_THREADS] = {0};
 888    int n;
 889    xs *sem_name = NULL;
 890    xs *shm_name = NULL;
 891    sem_t anon_job_sem;
 892    xs *pidfile = xs_fmt("%s/server.pid", srv_basedir);
 893    int pidfd;
 894
 895    {
 896        /* do some pidfile locking acrobatics */
 897        if ((pidfd = open(pidfile, O_RDWR | O_CREAT, 0660)) == -1) {
 898            srv_log(xs_fmt("Cannot create pidfile %s -- cannot continue", pidfile));
 899            return;
 900        }
 901
 902        if (lockf(pidfd, F_TLOCK, 1) == -1) {
 903            srv_log(xs_fmt("Cannot lock pidfile %s -- server already running?", pidfile));
 904            close(pidfd);
 905            return;
 906        }
 907
 908        ftruncate(pidfd, 0);
 909
 910        xs *s = xs_fmt("%d\n", (int)getpid());
 911        write(pidfd, s, strlen(s));
 912    }
 913
 914    address = xs_dict_get(srv_config, "address");
 915
 916    if (*address == '/') {
 917        rs = xs_unix_socket_server(address, NULL);
 918        full_address = xs_fmt("unix:%s", address);
 919    }
 920    else {
 921        port = xs_number_str(xs_dict_get(srv_config, "port"));
 922        full_address = xs_fmt("%s:%s", address, port);
 923
 924        rs = xs_socket_server(address, port);
 925    }
 926
 927    if (rs == -1) {
 928        srv_log(xs_fmt("cannot bind socket to %s", full_address));
 929        return;
 930    }
 931
 932    /* setup the server stat structure */
 933    p_state = srv_state_op(&shm_name, 0);
 934
 935    p_state->srv_start_time = time(NULL);
 936
 937    p_state->use_fcgi = xs_type(xs_dict_get(srv_config, "fastcgi")) == XSTYPE_TRUE;
 938
 939    p_state->srv_running = 1;
 940
 941    signal(SIGPIPE, SIG_IGN);
 942    signal(SIGTERM, term_handler);
 943    signal(SIGINT,  term_handler);
 944
 945    srv_log(xs_fmt("httpd%s start %s %s", p_state->use_fcgi ? " (FastCGI)" : "",
 946                    full_address, USER_AGENT));
 947
 948    /* show the number of usable file descriptors */
 949    struct rlimit r;
 950    getrlimit(RLIMIT_NOFILE, &r);
 951    srv_debug(1, xs_fmt("available (rlimit) fds: %d (cur) / %d (max)",
 952                        (int) r.rlim_cur, (int) r.rlim_max));
 953
 954    /* initialize the job control engine */
 955    pthread_mutex_init(&job_mutex, NULL);
 956    sem_name = xs_fmt("/job_%d", getpid());
 957    job_sem = sem_open(sem_name, O_CREAT, 0644, 0);
 958
 959    if (job_sem == NULL) {
 960        /* error opening a named semaphore; try with an anonymous one */
 961        if (sem_init(&anon_job_sem, 0, 0) != -1)
 962            job_sem = &anon_job_sem;
 963    }
 964
 965    if (job_sem == NULL) {
 966        srv_log(xs_fmt("fatal error: cannot create semaphore -- cannot continue"));
 967        return;
 968    }
 969
 970    /* initialize sleep control */
 971    pthread_mutex_init(&sleep_mutex, NULL);
 972    pthread_cond_init(&sleep_cond, NULL);
 973
 974    p_state->n_threads = xs_number_get(xs_dict_get(srv_config, "num_threads"));
 975
 976#ifdef _SC_NPROCESSORS_ONLN
 977    if (p_state->n_threads == 0) {
 978        /* get number of CPUs on the machine */
 979        p_state->n_threads = sysconf(_SC_NPROCESSORS_ONLN);
 980    }
 981#endif
 982
 983    if (p_state->n_threads < 4)
 984        p_state->n_threads = 4;
 985
 986    if (p_state->n_threads > MAX_THREADS)
 987        p_state->n_threads = MAX_THREADS;
 988
 989    srv_debug(0, xs_fmt("using %d threads", p_state->n_threads));
 990
 991    /* thread #0 is the background thread */
 992    pthread_create(&threads[0], NULL, background_thread, NULL);
 993
 994    /* the rest of threads are for job processing */
 995    char *ptr = (char *) 0x1;
 996    for (n = 1; n < p_state->n_threads; n++)
 997        pthread_create(&threads[n], NULL, job_thread, ptr++);
 998
 999    if (setjmp(on_break) == 0) {
1000        for (;;) {
1001            int cs = xs_socket_accept(rs);
1002
1003            if (cs != -1) {
1004                FILE *f = fdopen(cs, "r+");
1005                xs *job = xs_data_new(&f, sizeof(FILE *));
1006                job_post(job, 1);
1007            }
1008            else
1009                break;
1010        }
1011    }
1012
1013    p_state->srv_running = 0;
1014
1015    /* send as many exit jobs as working threads */
1016    for (n = 1; n < p_state->n_threads; n++)
1017        job_post(xs_stock(XSTYPE_FALSE), 0);
1018
1019    /* wait for all the threads to exit */
1020    for (n = 0; n < p_state->n_threads; n++)
1021        pthread_join(threads[n], NULL);
1022
1023    sem_close(job_sem);
1024    sem_unlink(sem_name);
1025
1026    srv_state_op(&shm_name, 2);
1027
1028    xs *uptime = xs_str_time_diff(time(NULL) - p_state->srv_start_time);
1029
1030    srv_log(xs_fmt("httpd%s stop %s (run time: %s)",
1031                p_state->use_fcgi ? " (FastCGI)" : "",
1032                full_address, uptime));
1033
1034    unlink(pidfile);
1035}