httpd.c 28 KB

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