httpd.c 26 KB

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