httpd.c 23 KB

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