httpd.c 28 KB

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