httpd.c 19 KB

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