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;
473 if (info->timeout_secs_ah_idle)
474 vh->timeout_secs_ah_idle = info->timeout_secs_ah_idle;
476 vh->timeout_secs_ah_idle = 10;
479 * give the vhost a unified list of protocols including the
480 * ones that came from plugins
482 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
483 (vh->count_protocols +
484 context->plugin_protocol_count + 1));
490 m = vh->count_protocols;
491 memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);
493 /* for compatibility, all protocols enabled on vhost if only
494 * the default vhost exists. Otherwise only vhosts who ask
495 * for a protocol get it enabled.
498 if (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
501 #ifdef LWS_WITH_PLUGINS
505 for (n = 0; n < plugin->caps.count_protocols; n++) {
507 * for compatibility's sake, no pvo implies
508 * allow all protocols
510 if (f || lws_vhost_protocol_options(vh,
511 plugin->caps.protocols[n].name)) {
513 &plugin->caps.protocols[n],
514 sizeof(struct lws_protocols));
516 vh->count_protocols++;
519 plugin = plugin->list;
525 #ifdef LWS_WITH_PLUGINS
526 (context->plugin_list) ||
528 context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
529 vh->protocols = lwsp;
531 vh->protocols = info->protocols;
535 vh->same_vh_protocol_list = (struct lws **)
536 lws_zalloc(sizeof(struct lws *) * vh->count_protocols);
538 vh->mount_list = info->mounts;
540 #ifdef LWS_USE_UNIX_SOCK
541 if (LWS_UNIX_SOCK_ENABLED(context)) {
542 lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
543 vh->name, info->iface, vh->count_protocols);
546 lwsl_notice("Creating Vhost '%s' port %d, %d protocols, IPv6 %s\n",
547 vh->name, info->port, vh->count_protocols, LWS_IPV6_ENABLED(vh) ? "on" : "off");
549 mounts = info->mounts;
551 (void)mount_protocols[0];
552 lwsl_notice(" mounting %s%s to %s\n",
553 mount_protocols[mounts->origin_protocol],
554 mounts->origin, mounts->mountpoint);
556 /* convert interpreter protocol names to pointers */
557 pvo = mounts->interpret;
559 for (n = 0; n < vh->count_protocols; n++)
560 if (!strcmp(pvo->value, vh->protocols[n].name)) {
561 ((struct lws_protocol_vhost_options *)pvo)->value =
562 (const char *)(lws_intptr_t)n;
565 if (n == vh->count_protocols)
566 lwsl_err("ignoring unknown interpret protocol %s\n", pvo->value);
570 mounts = mounts->mount_next;
573 #ifndef LWS_NO_EXTENSIONS
574 #ifdef LWS_WITH_PLUGINS
575 if (context->plugin_extension_count) {
578 while (info->extensions && info->extensions[m].callback)
582 * give the vhost a unified list of extensions including the
583 * ones that came from plugins
585 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
587 context->plugin_extension_count + 1));
591 memcpy((struct lws_extension *)vh->extensions, info->extensions,
592 sizeof(struct lws_extension) * m);
593 plugin = context->plugin_list;
595 memcpy((struct lws_extension *)&vh->extensions[m],
596 plugin->caps.extensions,
597 sizeof(struct lws_extension) *
598 plugin->caps.count_extensions);
599 m += plugin->caps.count_extensions;
600 plugin = plugin->list;
604 vh->extensions = info->extensions;
607 vh->listen_port = info->port;
608 #if !defined(LWS_WITH_ESP8266)
609 vh->http_proxy_port = 0;
610 vh->http_proxy_address[0] = '\0';
611 #if defined(LWS_WITH_SOCKS5)
612 vh->socks_proxy_port = 0;
613 vh->socks_proxy_address[0] = '\0';
616 /* either use proxy from info, or try get it from env var */
619 if (info->http_proxy_address) {
620 /* override for backwards compatibility */
621 if (info->http_proxy_port)
622 vh->http_proxy_port = info->http_proxy_port;
623 lws_set_proxy(vh, info->http_proxy_address);
625 #ifdef LWS_HAVE_GETENV
626 p = getenv("http_proxy");
628 lws_set_proxy(vh, p);
631 #if defined(LWS_WITH_SOCKS5)
633 if (info->socks_proxy_address) {
634 /* override for backwards compatibility */
635 if (info->socks_proxy_port)
636 vh->socks_proxy_port = info->socks_proxy_port;
637 lws_set_socks(vh, info->socks_proxy_address);
639 #ifdef LWS_HAVE_GETENV
640 p = getenv("socks_proxy");
642 lws_set_socks(vh, p);
648 vh->ka_time = info->ka_time;
649 vh->ka_interval = info->ka_interval;
650 vh->ka_probes = info->ka_probes;
652 if (vh->options & LWS_SERVER_OPTION_STS)
653 lwsl_notice(" STS enabled\n");
655 #ifdef LWS_WITH_ACCESS_LOG
656 if (info->log_filepath) {
657 vh->log_fd = open(info->log_filepath, O_CREAT | O_APPEND | O_RDWR, 0600);
658 if (vh->log_fd == (int)LWS_INVALID_FILE) {
659 lwsl_err("unable to open log filepath %s\n",
664 if (context->uid != -1)
665 if (chown(info->log_filepath, context->uid,
667 lwsl_err("unable to chown log file %s\n",
671 vh->log_fd = (int)LWS_INVALID_FILE;
673 if (lws_context_init_server_ssl(info, vh))
675 if (lws_context_init_client_ssl(info, vh))
677 if (lws_context_init_server(info, vh)) {
678 lwsl_err("init server failed\n");
687 vh1 = &(*vh1)->vhost_next;
689 /* for the case we are adding a vhost much later, after server init */
691 if (context->protocol_init_done)
692 lws_protocol_init(context);
703 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
704 struct lws_vhost *vhost)
706 struct lws_context_creation_info i;
708 memcpy(&i, info, sizeof(i));
709 i.port = CONTEXT_PORT_NO_LISTEN;
711 return lws_context_init_client_ssl(&i, vhost);
714 LWS_VISIBLE struct lws_context *
715 lws_create_context(struct lws_context_creation_info *info)
717 struct lws_context *context = NULL;
718 struct lws_plat_file_ops *prev;
719 #ifndef LWS_NO_DAEMONIZE
720 int pid_daemon = get_daemonize_pid();
723 #if defined(__ANDROID__)
727 lwsl_notice("Initial logging level %d\n", log_level);
728 lwsl_notice("Libwebsockets version: %s\n", library_version);
730 lwsl_notice("Compiled with %s\n", GCC_VER);
734 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
735 lwsl_notice("IPV6 compiled in and enabled\n");
737 lwsl_notice("IPV6 compiled in but disabled\n");
739 lwsl_notice("IPV6 not compiled in\n");
741 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
742 lws_feature_status_libev(info);
743 lws_feature_status_libuv(info);
746 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
747 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
748 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
749 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
750 lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
751 #if defined(LWS_WITH_STATS)
752 lwsl_notice(" LWS_WITH_STATS : on\n");
755 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
757 if (lws_plat_context_early_init())
760 context = lws_zalloc(sizeof(struct lws_context));
762 lwsl_err("No memory for websocket context\n");
765 if (info->pt_serv_buf_size)
766 context->pt_serv_buf_size = info->pt_serv_buf_size;
768 context->pt_serv_buf_size = 4096;
770 /* default to just the platform fops implementation */
772 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
773 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
774 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
775 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
776 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
777 context->fops_platform.fi[0].sig = NULL;
780 * arrange a linear linked-list of fops starting from context->fops
783 * [ -> fops_zip (copied into context so .next settable) ]
787 context->fops = &context->fops_platform;
788 prev = (struct lws_plat_file_ops *)context->fops;
790 #if defined(LWS_WITH_ZIP_FOPS)
791 /* make a soft copy so we can set .next */
792 context->fops_zip = fops_zip;
793 prev->next = &context->fops_zip;
794 prev = (struct lws_plat_file_ops *)prev->next;
797 /* if user provided fops, tack them on the end of the list */
799 prev->next = info->fops;
801 context->reject_service_keywords = info->reject_service_keywords;
802 if (info->external_baggage_free_on_destroy)
803 context->external_baggage_free_on_destroy =
804 info->external_baggage_free_on_destroy;
806 context->time_up = time(NULL);
808 context->simultaneous_ssl_restriction = info->simultaneous_ssl_restriction;
810 #ifndef LWS_NO_DAEMONIZE
812 context->started_with_parent = pid_daemon;
813 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
816 #if defined(__ANDROID__)
817 n = getrlimit ( RLIMIT_NOFILE,&rt);
819 lwsl_err("Get RLIMIT_NOFILE failed!\n");
822 context->max_fds = rt.rlim_cur;
824 context->max_fds = getdtablesize();
827 if (info->count_threads)
828 context->count_threads = info->count_threads;
830 context->count_threads = 1;
832 if (context->count_threads > LWS_MAX_SMP)
833 context->count_threads = LWS_MAX_SMP;
835 context->token_limits = info->token_limits;
837 context->options = info->options;
839 if (info->timeout_secs)
840 context->timeout_secs = info->timeout_secs;
842 context->timeout_secs = AWAITING_TIMEOUT;
844 context->ws_ping_pong_interval = info->ws_ping_pong_interval;
846 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
848 if (info->max_http_header_data)
849 context->max_http_header_data = info->max_http_header_data;
851 if (info->max_http_header_data2)
852 context->max_http_header_data =
853 info->max_http_header_data2;
855 context->max_http_header_data = LWS_DEF_HEADER_LEN;
856 if (info->max_http_header_pool)
857 context->max_http_header_pool = info->max_http_header_pool;
859 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
862 * Allocate the per-thread storage for scratchpad buffers,
863 * and header data pool
865 for (n = 0; n < context->count_threads; n++) {
866 context->pt[n].serv_buf = lws_zalloc(context->pt_serv_buf_size);
867 if (!context->pt[n].serv_buf) {
873 context->pt[n].context = context;
875 context->pt[n].tid = n;
876 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
877 context->max_http_header_pool);
878 if (!context->pt[n].http_header_data)
881 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
882 context->max_http_header_pool);
883 for (m = 0; m < context->max_http_header_pool; m++)
884 context->pt[n].ah_pool[m].data =
885 (char *)context->pt[n].http_header_data +
886 (m * context->max_http_header_data);
887 if (!context->pt[n].ah_pool)
890 lws_pt_mutex_init(&context->pt[n]);
893 if (info->fd_limit_per_thread)
894 context->fd_limit_per_thread = info->fd_limit_per_thread;
896 context->fd_limit_per_thread = context->max_fds /
897 context->count_threads;
899 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
900 context->fd_limit_per_thread);
902 if (!info->ka_interval && info->ka_time > 0) {
903 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
908 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
909 * enable libev mediated SIGINT handling with a default handler of
910 * lws_sigint_cb. The handler can be overridden or disabled
911 * by invoking lws_sigint_cfg after creating the context, but
912 * before invoking lws_initloop:
914 context->use_ev_sigint = 1;
915 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
916 #endif /* LWS_USE_LIBEV */
918 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
919 * enable libev mediated SIGINT handling with a default handler of
920 * lws_sigint_cb. The handler can be overridden or disabled
921 * by invoking lws_sigint_cfg after creating the context, but
922 * before invoking lws_initloop:
924 context->use_ev_sigint = 1;
925 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
927 #ifdef LWS_USE_LIBEVENT
928 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
929 * enable libev mediated SIGINT handling with a default handler of
930 * lws_sigint_cb. The handler can be overridden or disabled
931 * by invoking lws_sigint_cfg after creating the context, but
932 * before invoking lws_initloop:
934 context->use_ev_sigint = 1;
935 context->lws_event_sigint_cb = &lws_event_sigint_cb;
936 #endif /* LWS_USE_LIBEVENT */
938 lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
939 (long)sizeof(struct lws_context) +
940 (context->count_threads * context->pt_serv_buf_size),
941 (long)sizeof(struct lws_context),
942 (long)context->count_threads,
943 context->pt_serv_buf_size);
945 lwsl_info(" mem: http hdr rsvd: %5lu bytes (%u thr x (%u + %lu) x %u))\n",
946 (long)(context->max_http_header_data +
947 sizeof(struct allocated_headers)) *
948 context->max_http_header_pool * context->count_threads,
949 context->count_threads,
950 context->max_http_header_data,
951 (long)sizeof(struct allocated_headers),
952 context->max_http_header_pool);
953 n = sizeof(struct lws_pollfd) * context->count_threads *
954 context->fd_limit_per_thread;
955 context->pt[0].fds = lws_zalloc(n);
956 if (context->pt[0].fds == NULL) {
957 lwsl_err("OOM allocating %d fds\n", context->max_fds);
960 lwsl_info(" mem: pollfd map: %5u\n", n);
962 if (info->server_string) {
963 context->server_string = info->server_string;
964 context->server_string_len = (short)
965 strlen(context->server_string);
969 /* each thread serves his own chunk of fds */
970 for (n = 1; n < (int)info->count_threads; n++)
971 context->pt[n].fds = context->pt[n - 1].fds +
972 context->fd_limit_per_thread;
975 if (lws_plat_init(context, info))
978 lws_context_init_ssl_library(info);
980 context->user_space = info->user;
983 * if he's not saying he'll make his own vhosts later then act
984 * compatibly and make a default vhost using the data in the info
986 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
987 if (!lws_create_vhost(context, info)) {
988 lwsl_err("Failed to create default vhost\n");
992 lws_context_init_extensions(info, context);
994 lwsl_notice(" mem: per-conn: %5lu bytes + protocol rx buf\n",
995 (unsigned long)sizeof(struct lws));
997 strcpy(context->canonical_hostname, "unknown");
998 lws_server_get_canonical_hostname(context, info);
1000 context->uid = info->uid;
1001 context->gid = info->gid;
1003 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
1004 memcpy(context->caps, info->caps, sizeof(context->caps));
1005 context->count_caps = info->count_caps;
1009 * drop any root privs for this process
1010 * to listen on port < 1023 we would have needed root, but now we are
1011 * listening, we don't want the power for anything else
1013 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
1014 lws_plat_drop_app_privileges(info);
1017 * give all extensions a chance to create any per-context
1018 * allocations they need
1020 if (info->port != CONTEXT_PORT_NO_LISTEN) {
1021 if (lws_ext_cb_all_exts(context, NULL,
1022 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1025 if (lws_ext_cb_all_exts(context, NULL,
1026 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1032 lws_context_destroy(context);
1036 LWS_VISIBLE LWS_EXTERN void
1037 lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
1039 struct lws_vhost *vh = context->vhost_list, *vh1;
1043 * "deprecation" means disable the context from accepting any new
1044 * connections and free up listen sockets to be used by a replacement
1047 * Otherwise the deprecated context remains operational, until its
1048 * number of connected sockets falls to zero, when it is deleted.
1051 /* for each vhost, close his listen socket */
1054 wsi = vh->lserv_wsi;
1056 wsi->socket_is_permanently_unusable = 1;
1057 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1058 wsi->context->deprecation_pending_listen_close_count++;
1060 * other vhosts can share the listen port, they
1061 * point to the same wsi. So zap those too.
1063 vh1 = context->vhost_list;
1065 if (vh1->lserv_wsi == wsi)
1066 vh1->lserv_wsi = NULL;
1067 vh1 = vh1->vhost_next;
1070 vh = vh->vhost_next;
1073 context->deprecated = 1;
1074 context->deprecation_cb = cb;
1077 LWS_VISIBLE LWS_EXTERN int
1078 lws_context_is_deprecated(struct lws_context *context)
1080 return context->deprecated;
1084 lws_context_destroy2(struct lws_context *context);
1088 lws_vhost_destroy1(struct lws_vhost *vh)
1090 const struct lws_protocols *protocol = NULL;
1091 struct lws_context_per_thread *pt;
1092 int n, m = vh->context->count_threads;
1093 struct lws_context *context = vh->context;
1096 lwsl_notice("%s\n", __func__);
1098 if (vh->being_destroyed)
1101 vh->being_destroyed = 1;
1104 * Are there other vhosts that are piggybacking on our listen socket?
1105 * If so we need to hand the listen socket off to one of the others
1106 * so it will remain open. If not, leave it attached to the closing
1107 * vhost and it will get closed.
1111 lws_start_foreach_ll(struct lws_vhost *, v, context->vhost_list) {
1113 !v->being_destroyed &&
1114 v->listen_port == vh->listen_port &&
1115 ((!v->iface && !vh->iface) ||
1116 (v->iface && vh->iface &&
1117 !strcmp(v->iface, vh->iface)))) {
1119 * this can only be a listen wsi, which is
1120 * restricted... it has no protocol or other
1121 * bindings or states. So we can simply
1122 * swap it to a vhost that has the same
1123 * iface + port, but is not closing.
1125 assert(v->lserv_wsi == NULL);
1126 v->lserv_wsi = vh->lserv_wsi;
1127 vh->lserv_wsi = NULL;
1128 v->lserv_wsi->vhost = v;
1130 lwsl_notice("%s: listen skt from %s to %s\n",
1131 __func__, vh->name, v->name);
1134 } lws_end_foreach_ll(v, vhost_next);
1137 * Forcibly close every wsi assoicated with this vhost. That will
1138 * include the listen socket if it is still associated with the closing
1143 pt = &context->pt[m];
1145 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1146 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1149 if (wsi->vhost != vh)
1152 lws_close_free_wsi(wsi,
1153 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1154 /* no protocol close */);
1160 * let the protocols destroy the per-vhost protocol objects
1163 memset(&wsi, 0, sizeof(wsi));
1164 wsi.context = vh->context;
1166 protocol = vh->protocols;
1169 while (n < vh->count_protocols) {
1170 wsi.protocol = protocol;
1171 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
1179 * remove vhost from context list of vhosts
1182 lws_start_foreach_llp(struct lws_vhost **, pv, context->vhost_list) {
1184 *pv = vh->vhost_next;
1187 } lws_end_foreach_llp(pv, vhost_next);
1189 /* add ourselves to the pending destruction list */
1191 vh->vhost_next = vh->context->vhost_pending_destruction_list;
1192 vh->context->vhost_pending_destruction_list = vh;
1196 lws_vhost_destroy2(struct lws_vhost *vh)
1198 const struct lws_protocols *protocol = NULL;
1199 struct lws_context *context = vh->context;
1200 struct lws_deferred_free *df;
1203 lwsl_notice("%s: %p\n", __func__, vh);
1205 /* if we are still on deferred free list, remove ourselves */
1207 lws_start_foreach_llp(struct lws_deferred_free **, pdf, context->deferred_free_list) {
1208 if ((*pdf)->payload == vh) {
1214 } lws_end_foreach_llp(pdf, next);
1216 /* remove ourselves from the pending destruction list */
1218 lws_start_foreach_llp(struct lws_vhost **, pv, context->vhost_pending_destruction_list) {
1220 *pv = (*pv)->vhost_next;
1223 } lws_end_foreach_llp(pv, vhost_next);
1226 * Free all the allocations associated with the vhost
1229 protocol = vh->protocols;
1232 while (n < vh->count_protocols) {
1233 if (vh->protocol_vh_privs &&
1234 vh->protocol_vh_privs[n]) {
1235 lws_free(vh->protocol_vh_privs[n]);
1236 vh->protocol_vh_privs[n] = NULL;
1242 if (vh->protocol_vh_privs)
1243 lws_free(vh->protocol_vh_privs);
1244 lws_ssl_SSL_CTX_destroy(vh);
1245 lws_free(vh->same_vh_protocol_list);
1246 #ifdef LWS_WITH_PLUGINS
1247 if (LWS_LIBUV_ENABLED(context)) {
1248 if (context->plugin_list)
1249 lws_free((void *)vh->protocols);
1253 if (context->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1254 lws_free((void *)vh->protocols);
1257 #ifdef LWS_WITH_PLUGINS
1258 #ifndef LWS_NO_EXTENSIONS
1259 if (context->plugin_extension_count)
1260 lws_free((void *)vh->extensions);
1263 #ifdef LWS_WITH_ACCESS_LOG
1264 if (vh->log_fd != (int)LWS_INVALID_FILE)
1269 * although async event callbacks may still come for wsi handles with
1270 * pending close in the case of asycn event library like libuv,
1271 * they do not refer to the vhost. So it's safe to free.
1274 lwsl_notice(" %s: Freeing vhost %p\n", __func__, vh);
1276 memset(vh, 0, sizeof(*vh));
1281 lws_check_deferred_free(struct lws_context *context, int force)
1283 struct lws_deferred_free *df;
1284 time_t now = lws_now_secs();
1286 lws_start_foreach_llp(struct lws_deferred_free **, pdf, context->deferred_free_list) {
1287 if (now > (*pdf)->deadline || force) {
1290 /* finalize vh destruction */
1291 lwsl_notice("doing deferred vh %p destroy\n", df->payload);
1292 lws_vhost_destroy2(df->payload);
1294 continue; /* after deletion we already point to next */
1296 } lws_end_foreach_llp(pdf, next);
1302 lws_vhost_destroy(struct lws_vhost *vh)
1304 struct lws_deferred_free *df = malloc(sizeof(*df));
1309 lws_vhost_destroy1(vh);
1311 /* part 2 is deferred to allow all the handle closes to complete */
1313 df->next = vh->context->deferred_free_list;
1314 df->deadline = lws_now_secs() + 5;
1316 vh->context->deferred_free_list = df;
1320 lws_context_destroy(struct lws_context *context)
1322 struct lws_context_per_thread *pt;
1323 struct lws_vhost *vh = NULL;
1328 lwsl_notice("%s: ctx %p\n", __func__, context);
1331 if (context->being_destroyed1) {
1332 lwsl_notice("%s: ctx %p: already being destroyed\n", __func__, context);
1336 lwsl_notice("%s: ctx %p\n", __func__, context);
1338 m = context->count_threads;
1339 context->being_destroyed = 1;
1340 context->being_destroyed1 = 1;
1342 memset(&wsi, 0, sizeof(wsi));
1343 wsi.context = context;
1346 if (context->worst_latency_info[0])
1347 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1351 pt = &context->pt[m];
1353 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1354 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1358 lws_close_free_wsi(wsi,
1359 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1360 /* no protocol close */);
1363 lws_pt_mutex_destroy(pt);
1367 * give all extensions a chance to clean up any per-context
1368 * allocations they might have made
1371 n = lws_ext_cb_all_exts(context, NULL,
1372 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
1374 n = lws_ext_cb_all_exts(context, NULL,
1375 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
1378 * inform all the protocols that they are done and will have no more
1381 * We can't free things until after the event loop shuts down.
1383 if (context->protocol_init_done)
1384 vh = context->vhost_list;
1386 lws_vhost_destroy1(vh);
1387 vh = vh->vhost_next;
1390 for (n = 0; n < context->count_threads; n++) {
1391 pt = &context->pt[n];
1393 lws_libev_destroyloop(context, n);
1394 lws_libuv_destroyloop(context, n);
1395 lws_libevent_destroyloop(context, n);
1397 lws_free_set_NULL(context->pt[n].serv_buf);
1399 lws_free(pt->ah_pool);
1400 if (pt->http_header_data)
1401 lws_free(pt->http_header_data);
1403 lws_plat_context_early_destroy(context);
1405 if (context->pt[0].fds)
1406 lws_free_set_NULL(context->pt[0].fds);
1408 if (!LWS_LIBUV_ENABLED(context))
1409 lws_context_destroy2(context);
1413 * call the second one after the event loop has been shut down cleanly
1417 lws_context_destroy2(struct lws_context *context)
1419 struct lws_vhost *vh = NULL, *vh1;
1421 lwsl_notice("%s: ctx %p\n", __func__, context);
1424 * free all the per-vhost allocations
1427 vh = context->vhost_list;
1429 vh1 = vh->vhost_next;
1430 lws_vhost_destroy2(vh);
1434 /* remove ourselves from the pending destruction list */
1436 while (context->vhost_pending_destruction_list)
1437 /* removes itself from list */
1438 lws_vhost_destroy2(context->vhost_pending_destruction_list);
1441 lws_stats_log_dump(context);
1443 lws_ssl_context_destroy(context);
1444 lws_plat_context_late_destroy(context);
1446 if (context->external_baggage_free_on_destroy)
1447 free(context->external_baggage_free_on_destroy);
1449 lws_check_deferred_free(context, 1);