2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-libwebsockets.h"
24 #ifndef LWS_BUILD_HASH
25 #define LWS_BUILD_HASH "unknown-build-hash"
28 static const char *library_version = LWS_LIBRARY_VERSION " " LWS_BUILD_HASH;
31 * lws_get_library_version: get version and git hash library built from
33 * returns a const char * to a string like "1.1 178d78c"
34 * representing the library version followed by the git head hash it
37 LWS_VISIBLE const char *
38 lws_get_library_version(void)
40 return library_version;
43 static const char * const mount_protocols[] = {
54 lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
59 /* allocate the vh priv array only on demand */
60 if (!vhost->protocol_vh_privs) {
61 vhost->protocol_vh_privs = (void **)lws_zalloc(
62 vhost->count_protocols * sizeof(void *));
63 if (!vhost->protocol_vh_privs)
67 while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
70 if (n == vhost->count_protocols) {
72 while (n < vhost->count_protocols &&
73 strcmp(vhost->protocols[n].name, prot->name))
76 if (n == vhost->count_protocols)
80 vhost->protocol_vh_privs[n] = lws_zalloc(size);
81 return vhost->protocol_vh_privs[n];
85 lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot)
89 if (!vhost->protocol_vh_privs)
92 while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
95 if (n == vhost->count_protocols) {
97 while (n < vhost->count_protocols &&
98 strcmp(vhost->protocols[n].name, prot->name))
101 if (n == vhost->count_protocols) {
102 lwsl_err("%s: unknown protocol %p\n", __func__, prot);
107 return vhost->protocol_vh_privs[n];
110 static const struct lws_protocol_vhost_options *
111 lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
113 const struct lws_protocol_vhost_options *pvo = vh->pvo;
116 // lwsl_notice("%s: '%s' '%s'\n", __func__, pvo->name, name);
117 if (!strcmp(pvo->name, name))
126 * inform every vhost that hasn't already done it, that
127 * his protocols are initializing
130 lws_protocol_init(struct lws_context *context)
132 struct lws_vhost *vh = context->vhost_list;
133 const struct lws_protocol_vhost_options *pvo, *pvo1;
137 memset(&wsi, 0, sizeof(wsi));
138 wsi.context = context;
140 lwsl_info("%s\n", __func__);
145 /* only do the protocol init once for a given vhost */
146 if (vh->created_vhost_protocols)
149 /* initialize supported protocols on this vhost */
151 for (n = 0; n < vh->count_protocols; n++) {
152 wsi.protocol = &vh->protocols[n];
153 if (!vh->protocols[n].name)
155 pvo = lws_vhost_protocol_options(vh,
156 vh->protocols[n].name);
159 * linked list of options specific to
166 lwsl_notice(" vh %s prot %s opt %s\n",
168 vh->protocols[n].name,
171 if (!strcmp(pvo->name, "default")) {
172 lwsl_notice("Setting default "
173 "protocol for vh %s to %s\n",
175 vh->protocols[n].name);
176 vh->default_protocol_index = n;
178 if (!strcmp(pvo->name, "raw")) {
179 lwsl_notice("Setting raw "
180 "protocol for vh %s to %s\n",
182 vh->protocols[n].name);
183 vh->raw_protocol_index = n;
192 * inform all the protocols that they are doing their one-time
193 * initialization if they want to.
195 * NOTE the wsi is all zeros except for the context, vh and
196 * protocol ptrs so lws_get_context(wsi) etc can work
198 if (vh->protocols[n].callback(&wsi,
199 LWS_CALLBACK_PROTOCOL_INIT, NULL,
204 vh->created_vhost_protocols = 1;
209 if (!context->protocol_init_done)
210 lws_finalize_startup(context);
212 context->protocol_init_done = 1;
218 lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
219 void *user, void *in, size_t len)
222 struct lws_cgi_args *args;
224 #if defined(LWS_WITH_CGI) || defined(LWS_WITH_HTTP_PROXY)
231 case LWS_CALLBACK_HTTP:
232 #ifndef LWS_NO_SERVER
233 if (lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL))
236 if (lws_http_transaction_completed(wsi))
241 case LWS_CALLBACK_HTTP_WRITEABLE:
243 if (wsi->reason_bf & 1) {
244 if (lws_cgi_write_split_stdout_headers(wsi) < 0)
247 if (wsi->reason_bf & 8)
248 wsi->reason_bf &= ~8;
250 wsi->reason_bf &= ~1;
254 #if defined(LWS_WITH_HTTP_PROXY)
255 if (wsi->reason_bf & 2) {
256 char *px = buf + LWS_PRE;
257 int lenx = sizeof(buf) - LWS_PRE;
259 * our sink is writeable and our source has something
260 * to read. So read a lump of source material of
261 * suitable size to send or what's available, whichever
266 wsi->reason_bf &= ~2;
267 if (!lws_get_child(wsi))
269 if (lws_http_client_read(lws_get_child(wsi), &px, &lenx) < 0)
276 #if defined(LWS_WITH_HTTP_PROXY)
277 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
278 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
279 assert(lws_get_parent(wsi));
280 if (!lws_get_parent(wsi))
282 lws_get_parent(wsi)->reason_bf |= 2;
283 lws_callback_on_writable(lws_get_parent(wsi));
286 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
287 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", (int)len);
288 assert(lws_get_parent(wsi));
289 n = lws_write(lws_get_parent(wsi), (unsigned char *)in,
290 len, LWS_WRITE_HTTP);
295 case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
296 unsigned char *p, *end;
297 char ctype[64], ctlen = 0;
299 //lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
301 p = (unsigned char *)buf + LWS_PRE;
302 end = p + sizeof(buf) - LWS_PRE;
304 if (lws_add_http_header_status(lws_get_parent(wsi), HTTP_STATUS_OK, &p, end))
306 if (lws_add_http_header_by_token(lws_get_parent(wsi),
307 WSI_TOKEN_HTTP_SERVER,
308 (unsigned char *)"libwebsockets",
312 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
314 if (lws_add_http_header_by_token(lws_get_parent(wsi),
315 WSI_TOKEN_HTTP_CONTENT_TYPE,
316 (unsigned char *)ctype, ctlen, &p, end))
320 if (lws_add_http_header_content_length(lws_get_parent(wsi),
324 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
328 // lwsl_info("%s\n", buf + LWS_PRE);
330 n = lws_write(lws_get_parent(wsi), (unsigned char *)buf + LWS_PRE,
331 p - ((unsigned char *)buf + LWS_PRE),
332 LWS_WRITE_HTTP_HEADERS);
341 /* CGI IO events (POLLIN/OUT) appear here, our default policy is:
343 * - POST data goes on subprocess stdin
344 * - subprocess stdout goes on http via writeable callback
345 * - subprocess stderr goes to the logs
347 case LWS_CALLBACK_CGI:
348 args = (struct lws_cgi_args *)in;
349 switch (args->ch) { /* which of stdin/out/err ? */
351 /* TBD stdin rx flow control */
355 /* when writing to MASTER would not block */
356 lws_callback_on_writable(wsi);
359 n = read(lws_get_socket_fd(args->stdwsi[LWS_STDERR]),
360 buf, sizeof(buf) - 2);
362 if (buf[n - 1] != '\n')
365 lwsl_notice("CGI-stderr: %s\n", buf);
371 case LWS_CALLBACK_CGI_TERMINATED:
374 case LWS_CALLBACK_CGI_STDIN_DATA: /* POST body for stdin */
375 args = (struct lws_cgi_args *)in;
376 args->data[args->len] = '\0';
377 n = write(lws_get_socket_fd(args->stdwsi[LWS_STDIN]),
378 args->data, args->len);
380 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: "
381 "sent %d only %d went", n, args->len);
385 case LWS_CALLBACK_SSL_INFO:
387 struct lws_ssl_info *si = in;
390 lwsl_notice("LWS_CALLBACK_SSL_INFO: where: 0x%x, ret: 0x%x\n",
402 /* list of supported protocols and callbacks */
404 static const struct lws_protocols protocols_dummy[] = {
405 /* first protocol must always be HTTP handler */
408 "http-only", /* name */
409 lws_callback_http_dummy, /* callback */
410 0, /* per_session_data_size */
411 0, /* max frame size / rx buffer */
415 * the other protocols are provided by lws plugins
417 { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
420 #ifdef LWS_PLAT_OPTEE
421 #undef LWS_HAVE_GETENV
424 LWS_VISIBLE struct lws_vhost *
425 lws_create_vhost(struct lws_context *context,
426 struct lws_context_creation_info *info)
428 struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
429 **vh1 = &context->vhost_list;
430 const struct lws_http_mount *mounts;
431 const struct lws_protocol_vhost_options *pvo;
432 #ifdef LWS_WITH_PLUGINS
433 struct lws_plugin *plugin = context->plugin_list;
435 struct lws_protocols *lwsp;
436 int m, f = !info->pvo;
437 #ifdef LWS_HAVE_GETENV
445 if (!info->protocols)
446 info->protocols = &protocols_dummy[0];
448 vh->context = context;
449 if (!info->vhost_name)
450 vh->name = "default";
452 vh->name = info->vhost_name;
454 vh->iface = info->iface;
455 #if !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32) && !defined(OPTEE_TA) && !defined(WIN32)
456 vh->bind_iface = info->bind_iface;
459 for (vh->count_protocols = 0;
460 info->protocols[vh->count_protocols].callback;
461 vh->count_protocols++)
464 vh->options = info->options;
466 vh->headers = info->headers;
467 vh->ssl_info_event_mask = info->ssl_info_event_mask;
468 if (info->keepalive_timeout)
469 vh->keepalive_timeout = info->keepalive_timeout;
471 vh->keepalive_timeout = 5;
474 * give the vhost a unified list of protocols including the
475 * ones that came from plugins
477 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
478 (vh->count_protocols +
479 context->plugin_protocol_count + 1));
485 m = vh->count_protocols;
486 memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);
488 /* for compatibility, all protocols enabled on vhost if only
489 * the default vhost exists. Otherwise only vhosts who ask
490 * for a protocol get it enabled.
493 if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
496 #ifdef LWS_WITH_PLUGINS
500 for (n = 0; n < plugin->caps.count_protocols; n++) {
502 * for compatibility's sake, no pvo implies
503 * allow all protocols
505 if (f || lws_vhost_protocol_options(vh,
506 plugin->caps.protocols[n].name)) {
508 &plugin->caps.protocols[n],
509 sizeof(struct lws_protocols));
511 vh->count_protocols++;
514 plugin = plugin->list;
520 #ifdef LWS_WITH_PLUGINS
521 (context->plugin_list) ||
523 info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
524 vh->protocols = lwsp;
526 vh->protocols = info->protocols;
530 vh->same_vh_protocol_list = (struct lws **)
531 lws_zalloc(sizeof(struct lws *) * vh->count_protocols);
533 vh->mount_list = info->mounts;
535 #ifdef LWS_USE_UNIX_SOCK
536 if (LWS_UNIX_SOCK_ENABLED(context)) {
537 lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
538 vh->name, info->iface, vh->count_protocols);
541 lwsl_notice("Creating Vhost '%s' port %d, %d protocols, IPv6 %s\n",
542 vh->name, info->port, vh->count_protocols, LWS_IPV6_ENABLED(vh) ? "on" : "off");
544 mounts = info->mounts;
546 (void)mount_protocols[0];
547 lwsl_notice(" mounting %s%s to %s\n",
548 mount_protocols[mounts->origin_protocol],
549 mounts->origin, mounts->mountpoint);
551 /* convert interpreter protocol names to pointers */
552 pvo = mounts->interpret;
554 for (n = 0; n < vh->count_protocols; n++)
555 if (!strcmp(pvo->value, vh->protocols[n].name)) {
556 ((struct lws_protocol_vhost_options *)pvo)->value =
557 (const char *)(lws_intptr_t)n;
560 if (n == vh->count_protocols)
561 lwsl_err("ignoring unknown interpret protocol %s\n", pvo->value);
565 mounts = mounts->mount_next;
568 #ifndef LWS_NO_EXTENSIONS
569 #ifdef LWS_WITH_PLUGINS
570 if (context->plugin_extension_count) {
573 while (info->extensions && info->extensions[m].callback)
577 * give the vhost a unified list of extensions including the
578 * ones that came from plugins
580 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
582 context->plugin_extension_count + 1));
586 memcpy((struct lws_extension *)vh->extensions, info->extensions,
587 sizeof(struct lws_extension) * m);
588 plugin = context->plugin_list;
590 memcpy((struct lws_extension *)&vh->extensions[m],
591 plugin->caps.extensions,
592 sizeof(struct lws_extension) *
593 plugin->caps.count_extensions);
594 m += plugin->caps.count_extensions;
595 plugin = plugin->list;
599 vh->extensions = info->extensions;
602 vh->listen_port = info->port;
603 #if !defined(LWS_WITH_ESP8266)
604 vh->http_proxy_port = 0;
605 vh->http_proxy_address[0] = '\0';
606 #if defined(LWS_WITH_SOCKS5)
607 vh->socks_proxy_port = 0;
608 vh->socks_proxy_address[0] = '\0';
611 /* either use proxy from info, or try get it from env var */
614 if (info->http_proxy_address) {
615 /* override for backwards compatibility */
616 if (info->http_proxy_port)
617 vh->http_proxy_port = info->http_proxy_port;
618 lws_set_proxy(vh, info->http_proxy_address);
620 #ifdef LWS_HAVE_GETENV
621 p = getenv("http_proxy");
623 lws_set_proxy(vh, p);
626 #if defined(LWS_WITH_SOCKS5)
628 if (info->socks_proxy_address) {
629 /* override for backwards compatibility */
630 if (info->socks_proxy_port)
631 vh->socks_proxy_port = info->socks_proxy_port;
632 lws_set_socks(vh, info->socks_proxy_address);
634 #ifdef LWS_HAVE_GETENV
635 p = getenv("socks_proxy");
637 lws_set_socks(vh, p);
643 vh->ka_time = info->ka_time;
644 vh->ka_interval = info->ka_interval;
645 vh->ka_probes = info->ka_probes;
647 if (vh->options & LWS_SERVER_OPTION_STS)
648 lwsl_notice(" STS enabled\n");
650 #ifdef LWS_WITH_ACCESS_LOG
651 if (info->log_filepath) {
652 vh->log_fd = open(info->log_filepath, O_CREAT | O_APPEND | O_RDWR, 0600);
653 if (vh->log_fd == (int)LWS_INVALID_FILE) {
654 lwsl_err("unable to open log filepath %s\n",
659 if (context->uid != -1)
660 if (chown(info->log_filepath, context->uid,
662 lwsl_err("unable to chown log file %s\n",
666 vh->log_fd = (int)LWS_INVALID_FILE;
668 if (lws_context_init_server_ssl(info, vh))
670 if (lws_context_init_client_ssl(info, vh))
672 if (lws_context_init_server(info, vh)) {
673 lwsl_err("init server failed\n");
682 vh1 = &(*vh1)->vhost_next;
684 /* for the case we are adding a vhost much later, after server init */
686 if (context->protocol_init_done)
687 lws_protocol_init(context);
698 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
699 struct lws_vhost *vhost)
701 struct lws_context_creation_info i;
703 memcpy(&i, info, sizeof(i));
704 i.port = CONTEXT_PORT_NO_LISTEN;
706 return lws_context_init_client_ssl(&i, vhost);
709 LWS_VISIBLE struct lws_context *
710 lws_create_context(struct lws_context_creation_info *info)
712 struct lws_context *context = NULL;
713 struct lws_plat_file_ops *prev;
714 #ifndef LWS_NO_DAEMONIZE
715 int pid_daemon = get_daemonize_pid();
718 #if defined(__ANDROID__)
722 lwsl_notice("Initial logging level %d\n", log_level);
723 lwsl_notice("Libwebsockets version: %s\n", library_version);
725 lwsl_notice("Compiled with %s\n", GCC_VER);
729 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
730 lwsl_notice("IPV6 compiled in and enabled\n");
732 lwsl_notice("IPV6 compiled in but disabled\n");
734 lwsl_notice("IPV6 not compiled in\n");
736 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
737 lws_feature_status_libev(info);
738 lws_feature_status_libuv(info);
741 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
742 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
743 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
744 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
745 lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
746 #if defined(LWS_WITH_STATS)
747 lwsl_notice(" LWS_WITH_STATS : on\n");
750 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
752 if (lws_plat_context_early_init())
755 context = lws_zalloc(sizeof(struct lws_context));
757 lwsl_err("No memory for websocket context\n");
760 if (info->pt_serv_buf_size)
761 context->pt_serv_buf_size = info->pt_serv_buf_size;
763 context->pt_serv_buf_size = 4096;
765 /* default to just the platform fops implementation */
767 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
768 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
769 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
770 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
771 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
772 context->fops_platform.fi[0].sig = NULL;
775 * arrange a linear linked-list of fops starting from context->fops
778 * [ -> fops_zip (copied into context so .next settable) ]
782 context->fops = &context->fops_platform;
783 prev = (struct lws_plat_file_ops *)context->fops;
785 #if defined(LWS_WITH_ZIP_FOPS)
786 /* make a soft copy so we can set .next */
787 context->fops_zip = fops_zip;
788 prev->next = &context->fops_zip;
789 prev = (struct lws_plat_file_ops *)prev->next;
792 /* if user provided fops, tack them on the end of the list */
794 prev->next = info->fops;
796 context->reject_service_keywords = info->reject_service_keywords;
797 if (info->external_baggage_free_on_destroy)
798 context->external_baggage_free_on_destroy =
799 info->external_baggage_free_on_destroy;
801 context->time_up = time(NULL);
803 context->simultaneous_ssl_restriction = info->simultaneous_ssl_restriction;
805 #ifndef LWS_NO_DAEMONIZE
807 context->started_with_parent = pid_daemon;
808 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
811 #if defined(__ANDROID__)
812 n = getrlimit ( RLIMIT_NOFILE,&rt);
814 lwsl_err("Get RLIMIT_NOFILE failed!\n");
817 context->max_fds = rt.rlim_cur;
819 context->max_fds = getdtablesize();
822 if (info->count_threads)
823 context->count_threads = info->count_threads;
825 context->count_threads = 1;
827 if (context->count_threads > LWS_MAX_SMP)
828 context->count_threads = LWS_MAX_SMP;
830 context->token_limits = info->token_limits;
832 context->options = info->options;
834 if (info->timeout_secs)
835 context->timeout_secs = info->timeout_secs;
837 context->timeout_secs = AWAITING_TIMEOUT;
839 context->ws_ping_pong_interval = info->ws_ping_pong_interval;
841 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
843 if (info->max_http_header_data)
844 context->max_http_header_data = info->max_http_header_data;
846 if (info->max_http_header_data2)
847 context->max_http_header_data =
848 info->max_http_header_data2;
850 context->max_http_header_data = LWS_DEF_HEADER_LEN;
851 if (info->max_http_header_pool)
852 context->max_http_header_pool = info->max_http_header_pool;
854 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
857 * Allocate the per-thread storage for scratchpad buffers,
858 * and header data pool
860 for (n = 0; n < context->count_threads; n++) {
861 context->pt[n].serv_buf = lws_zalloc(context->pt_serv_buf_size);
862 if (!context->pt[n].serv_buf) {
868 context->pt[n].context = context;
870 context->pt[n].tid = n;
871 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
872 context->max_http_header_pool);
873 if (!context->pt[n].http_header_data)
876 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
877 context->max_http_header_pool);
878 for (m = 0; m < context->max_http_header_pool; m++)
879 context->pt[n].ah_pool[m].data =
880 (char *)context->pt[n].http_header_data +
881 (m * context->max_http_header_data);
882 if (!context->pt[n].ah_pool)
885 lws_pt_mutex_init(&context->pt[n]);
888 if (info->fd_limit_per_thread)
889 context->fd_limit_per_thread = info->fd_limit_per_thread;
891 context->fd_limit_per_thread = context->max_fds /
892 context->count_threads;
894 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
895 context->fd_limit_per_thread);
897 if (!info->ka_interval && info->ka_time > 0) {
898 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
903 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
904 * enable libev mediated SIGINT handling with a default handler of
905 * lws_sigint_cb. The handler can be overridden or disabled
906 * by invoking lws_sigint_cfg after creating the context, but
907 * before invoking lws_initloop:
909 context->use_ev_sigint = 1;
910 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
911 #endif /* LWS_USE_LIBEV */
913 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
914 * enable libev mediated SIGINT handling with a default handler of
915 * lws_sigint_cb. The handler can be overridden or disabled
916 * by invoking lws_sigint_cfg after creating the context, but
917 * before invoking lws_initloop:
919 context->use_ev_sigint = 1;
920 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
922 #ifdef LWS_USE_LIBEVENT
923 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
924 * enable libev mediated SIGINT handling with a default handler of
925 * lws_sigint_cb. The handler can be overridden or disabled
926 * by invoking lws_sigint_cfg after creating the context, but
927 * before invoking lws_initloop:
929 context->use_ev_sigint = 1;
930 context->lws_event_sigint_cb = &lws_event_sigint_cb;
931 #endif /* LWS_USE_LIBEVENT */
933 lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
934 (long)sizeof(struct lws_context) +
935 (context->count_threads * context->pt_serv_buf_size),
936 (long)sizeof(struct lws_context),
937 (long)context->count_threads,
938 context->pt_serv_buf_size);
940 lwsl_info(" mem: http hdr rsvd: %5lu bytes (%u thr x (%u + %lu) x %u))\n",
941 (long)(context->max_http_header_data +
942 sizeof(struct allocated_headers)) *
943 context->max_http_header_pool * context->count_threads,
944 context->count_threads,
945 context->max_http_header_data,
946 (long)sizeof(struct allocated_headers),
947 context->max_http_header_pool);
948 n = sizeof(struct lws_pollfd) * context->count_threads *
949 context->fd_limit_per_thread;
950 context->pt[0].fds = lws_zalloc(n);
951 if (context->pt[0].fds == NULL) {
952 lwsl_err("OOM allocating %d fds\n", context->max_fds);
955 lwsl_info(" mem: pollfd map: %5u\n", n);
957 if (info->server_string) {
958 context->server_string = info->server_string;
959 context->server_string_len = (short)
960 strlen(context->server_string);
964 /* each thread serves his own chunk of fds */
965 for (n = 1; n < (int)info->count_threads; n++)
966 context->pt[n].fds = context->pt[n - 1].fds +
967 context->fd_limit_per_thread;
970 if (lws_plat_init(context, info))
973 lws_context_init_ssl_library(info);
975 context->user_space = info->user;
978 * if he's not saying he'll make his own vhosts later then act
979 * compatibly and make a default vhost using the data in the info
981 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
982 if (!lws_create_vhost(context, info)) {
983 lwsl_err("Failed to create default vhost\n");
987 lws_context_init_extensions(info, context);
989 lwsl_notice(" mem: per-conn: %5lu bytes + protocol rx buf\n",
990 (unsigned long)sizeof(struct lws));
992 strcpy(context->canonical_hostname, "unknown");
993 lws_server_get_canonical_hostname(context, info);
995 context->uid = info->uid;
996 context->gid = info->gid;
998 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
999 memcpy(context->caps, info->caps, sizeof(context->caps));
1000 context->count_caps = info->count_caps;
1004 * drop any root privs for this process
1005 * to listen on port < 1023 we would have needed root, but now we are
1006 * listening, we don't want the power for anything else
1008 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
1009 lws_plat_drop_app_privileges(info);
1012 * give all extensions a chance to create any per-context
1013 * allocations they need
1015 if (info->port != CONTEXT_PORT_NO_LISTEN) {
1016 if (lws_ext_cb_all_exts(context, NULL,
1017 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1020 if (lws_ext_cb_all_exts(context, NULL,
1021 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1027 lws_context_destroy(context);
1031 LWS_VISIBLE LWS_EXTERN void
1032 lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
1034 struct lws_vhost *vh = context->vhost_list, *vh1;
1038 * "deprecation" means disable the context from accepting any new
1039 * connections and free up listen sockets to be used by a replacement
1042 * Otherwise the deprecated context remains operational, until its
1043 * number of connected sockets falls to zero, when it is deleted.
1046 /* for each vhost, close his listen socket */
1049 wsi = vh->lserv_wsi;
1051 wsi->socket_is_permanently_unusable = 1;
1052 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1053 wsi->context->deprecation_pending_listen_close_count++;
1055 * other vhosts can share the listen port, they
1056 * point to the same wsi. So zap those too.
1058 vh1 = context->vhost_list;
1060 if (vh1->lserv_wsi == wsi)
1061 vh1->lserv_wsi = NULL;
1062 vh1 = vh1->vhost_next;
1065 vh = vh->vhost_next;
1068 context->deprecated = 1;
1069 context->deprecation_cb = cb;
1072 LWS_VISIBLE LWS_EXTERN int
1073 lws_context_is_deprecated(struct lws_context *context)
1075 return context->deprecated;
1079 lws_context_destroy2(struct lws_context *context);
1082 lws_context_destroy(struct lws_context *context)
1084 const struct lws_protocols *protocol = NULL;
1085 struct lws_context_per_thread *pt;
1086 struct lws_vhost *vh = NULL;
1091 lwsl_notice("%s: ctx %p\n", __func__, context);
1094 if (context->being_destroyed1) {
1095 lwsl_notice("%s: ctx %p: already being destroyed\n", __func__, context);
1099 lwsl_notice("%s: ctx %p\n", __func__, context);
1101 m = context->count_threads;
1102 context->being_destroyed = 1;
1103 context->being_destroyed1 = 1;
1105 memset(&wsi, 0, sizeof(wsi));
1106 wsi.context = context;
1109 if (context->worst_latency_info[0])
1110 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1114 pt = &context->pt[m];
1116 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1117 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1121 lws_close_free_wsi(wsi,
1122 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1123 /* no protocol close */);
1126 lws_pt_mutex_destroy(pt);
1130 * give all extensions a chance to clean up any per-context
1131 * allocations they might have made
1134 n = lws_ext_cb_all_exts(context, NULL,
1135 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
1137 n = lws_ext_cb_all_exts(context, NULL,
1138 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
1141 * inform all the protocols that they are done and will have no more
1144 * We can't free things until after the event loop shuts down.
1146 if (context->protocol_init_done)
1147 vh = context->vhost_list;
1150 protocol = vh->protocols;
1153 while (n < vh->count_protocols) {
1154 wsi.protocol = protocol;
1155 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
1162 vh = vh->vhost_next;
1165 for (n = 0; n < context->count_threads; n++) {
1166 pt = &context->pt[n];
1168 lws_libev_destroyloop(context, n);
1169 lws_libuv_destroyloop(context, n);
1170 lws_libevent_destroyloop(context, n);
1172 lws_free_set_NULL(context->pt[n].serv_buf);
1174 lws_free(pt->ah_pool);
1175 if (pt->http_header_data)
1176 lws_free(pt->http_header_data);
1178 lws_plat_context_early_destroy(context);
1180 if (context->pt[0].fds)
1181 lws_free_set_NULL(context->pt[0].fds);
1183 if (!LWS_LIBUV_ENABLED(context))
1184 lws_context_destroy2(context);
1188 * call the second one after the event loop has been shut down cleanly
1192 lws_context_destroy2(struct lws_context *context)
1194 const struct lws_protocols *protocol = NULL;
1195 struct lws_vhost *vh = NULL, *vh1;
1198 lwsl_notice("%s: ctx %p\n", __func__, context);
1201 * free all the per-vhost allocations
1204 vh = context->vhost_list;
1206 protocol = vh->protocols;
1209 while (n < vh->count_protocols) {
1210 if (vh->protocol_vh_privs &&
1211 vh->protocol_vh_privs[n]) {
1212 // lwsl_notice(" %s: freeing per-vhost protocol data %p\n", __func__, vh->protocol_vh_privs[n]);
1213 lws_free(vh->protocol_vh_privs[n]);
1214 vh->protocol_vh_privs[n] = NULL;
1220 if (vh->protocol_vh_privs)
1221 lws_free(vh->protocol_vh_privs);
1222 lws_ssl_SSL_CTX_destroy(vh);
1223 lws_free(vh->same_vh_protocol_list);
1224 #ifdef LWS_WITH_PLUGINS
1225 if (context->plugin_list)
1226 lws_free((void *)vh->protocols);
1228 if (vh->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1229 lws_free((void *)vh->protocols);
1231 #ifdef LWS_WITH_PLUGINS
1232 #ifndef LWS_NO_EXTENSIONS
1233 if (context->plugin_extension_count)
1234 lws_free((void *)vh->extensions);
1237 #ifdef LWS_WITH_ACCESS_LOG
1238 if (vh->log_fd != (int)LWS_INVALID_FILE)
1242 vh1 = vh->vhost_next;
1247 lws_stats_log_dump(context);
1249 lws_ssl_context_destroy(context);
1250 lws_plat_context_late_destroy(context);
1252 if (context->external_baggage_free_on_destroy)
1253 free(context->external_baggage_free_on_destroy);