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 #if !defined(LWS_WITH_NO_LOGS)
44 static const char * const mount_protocols[] = {
56 lws_protocol_vh_priv_zalloc(struct lws_vhost *vhost, const struct lws_protocols *prot,
61 /* allocate the vh priv array only on demand */
62 if (!vhost->protocol_vh_privs) {
63 vhost->protocol_vh_privs = (void **)lws_zalloc(
64 vhost->count_protocols * sizeof(void *));
65 if (!vhost->protocol_vh_privs)
69 while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
72 if (n == vhost->count_protocols) {
74 while (n < vhost->count_protocols &&
75 strcmp(vhost->protocols[n].name, prot->name))
78 if (n == vhost->count_protocols)
82 vhost->protocol_vh_privs[n] = lws_zalloc(size);
83 return vhost->protocol_vh_privs[n];
87 lws_protocol_vh_priv_get(struct lws_vhost *vhost, const struct lws_protocols *prot)
91 if (!vhost->protocol_vh_privs)
94 while (n < vhost->count_protocols && &vhost->protocols[n] != prot)
97 if (n == vhost->count_protocols) {
99 while (n < vhost->count_protocols &&
100 strcmp(vhost->protocols[n].name, prot->name))
103 if (n == vhost->count_protocols) {
104 lwsl_err("%s: unknown protocol %p\n", __func__, prot);
109 return vhost->protocol_vh_privs[n];
112 static const struct lws_protocol_vhost_options *
113 lws_vhost_protocol_options(struct lws_vhost *vh, const char *name)
115 const struct lws_protocol_vhost_options *pvo = vh->pvo;
118 // lwsl_notice("%s: '%s' '%s'\n", __func__, pvo->name, name);
119 if (!strcmp(pvo->name, name))
128 * inform every vhost that hasn't already done it, that
129 * his protocols are initializing
132 lws_protocol_init(struct lws_context *context)
134 struct lws_vhost *vh = context->vhost_list;
135 const struct lws_protocol_vhost_options *pvo, *pvo1;
139 memset(&wsi, 0, sizeof(wsi));
140 wsi.context = context;
142 lwsl_info("%s\n", __func__);
147 /* only do the protocol init once for a given vhost */
148 if (vh->created_vhost_protocols)
151 /* initialize supported protocols on this vhost */
153 for (n = 0; n < vh->count_protocols; n++) {
154 wsi.protocol = &vh->protocols[n];
155 if (!vh->protocols[n].name)
157 pvo = lws_vhost_protocol_options(vh,
158 vh->protocols[n].name);
161 * linked list of options specific to
168 lwsl_notice(" vh %s prot %s opt %s\n",
170 vh->protocols[n].name,
173 if (!strcmp(pvo->name, "default")) {
174 lwsl_notice("Setting default "
175 "protocol for vh %s to %s\n",
177 vh->protocols[n].name);
178 vh->default_protocol_index = n;
180 if (!strcmp(pvo->name, "raw")) {
181 lwsl_notice("Setting raw "
182 "protocol for vh %s to %s\n",
184 vh->protocols[n].name);
185 vh->raw_protocol_index = n;
194 * inform all the protocols that they are doing their one-time
195 * initialization if they want to.
197 * NOTE the wsi is all zeros except for the context, vh and
198 * protocol ptrs so lws_get_context(wsi) etc can work
200 if (vh->protocols[n].callback(&wsi,
201 LWS_CALLBACK_PROTOCOL_INIT, NULL,
206 vh->created_vhost_protocols = 1;
211 if (!context->protocol_init_done)
212 lws_finalize_startup(context);
214 context->protocol_init_done = 1;
220 lws_callback_http_dummy(struct lws *wsi, enum lws_callback_reasons reason,
221 void *user, void *in, size_t len)
224 struct lws_cgi_args *args;
226 #if defined(LWS_WITH_CGI) || defined(LWS_WITH_HTTP_PROXY)
233 case LWS_CALLBACK_HTTP:
234 #ifndef LWS_NO_SERVER
235 if (lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL))
238 if (lws_http_transaction_completed(wsi))
243 case LWS_CALLBACK_HTTP_WRITEABLE:
245 if (wsi->reason_bf & 1) {
246 if (lws_cgi_write_split_stdout_headers(wsi) < 0)
249 if (wsi->reason_bf & 8)
250 wsi->reason_bf &= ~8;
252 wsi->reason_bf &= ~1;
256 #if defined(LWS_WITH_HTTP_PROXY)
257 if (wsi->reason_bf & 2) {
258 char *px = buf + LWS_PRE;
259 int lenx = sizeof(buf) - LWS_PRE;
261 * our sink is writeable and our source has something
262 * to read. So read a lump of source material of
263 * suitable size to send or what's available, whichever
268 wsi->reason_bf &= ~2;
269 if (!lws_get_child(wsi))
271 if (lws_http_client_read(lws_get_child(wsi), &px, &lenx) < 0)
278 #if defined(LWS_WITH_HTTP_PROXY)
279 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP:
280 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP: wsi %p\n", wsi);
281 assert(lws_get_parent(wsi));
282 if (!lws_get_parent(wsi))
284 lws_get_parent(wsi)->reason_bf |= 2;
285 lws_callback_on_writable(lws_get_parent(wsi));
288 case LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ:
289 //lwsl_err("LWS_CALLBACK_RECEIVE_CLIENT_HTTP_READ len %d\n", (int)len);
290 assert(lws_get_parent(wsi));
291 n = lws_write(lws_get_parent(wsi), (unsigned char *)in,
292 len, LWS_WRITE_HTTP);
297 case LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP: {
298 unsigned char *p, *end;
299 char ctype[64], ctlen = 0;
301 //lwsl_err("LWS_CALLBACK_ESTABLISHED_CLIENT_HTTP\n");
303 p = (unsigned char *)buf + LWS_PRE;
304 end = p + sizeof(buf) - LWS_PRE;
306 if (lws_add_http_header_status(lws_get_parent(wsi), HTTP_STATUS_OK, &p, end))
308 if (lws_add_http_header_by_token(lws_get_parent(wsi),
309 WSI_TOKEN_HTTP_SERVER,
310 (unsigned char *)"libwebsockets",
314 ctlen = lws_hdr_copy(wsi, ctype, sizeof(ctype), WSI_TOKEN_HTTP_CONTENT_TYPE);
316 if (lws_add_http_header_by_token(lws_get_parent(wsi),
317 WSI_TOKEN_HTTP_CONTENT_TYPE,
318 (unsigned char *)ctype, ctlen, &p, end))
322 if (lws_add_http_header_content_length(lws_get_parent(wsi),
326 if (lws_finalize_http_header(lws_get_parent(wsi), &p, end))
330 // lwsl_info("%s\n", buf + LWS_PRE);
332 n = lws_write(lws_get_parent(wsi), (unsigned char *)buf + LWS_PRE,
333 p - ((unsigned char *)buf + LWS_PRE),
334 LWS_WRITE_HTTP_HEADERS);
343 /* CGI IO events (POLLIN/OUT) appear here, our default policy is:
345 * - POST data goes on subprocess stdin
346 * - subprocess stdout goes on http via writeable callback
347 * - subprocess stderr goes to the logs
349 case LWS_CALLBACK_CGI:
350 args = (struct lws_cgi_args *)in;
351 switch (args->ch) { /* which of stdin/out/err ? */
353 /* TBD stdin rx flow control */
357 /* when writing to MASTER would not block */
358 lws_callback_on_writable(wsi);
361 n = read(lws_get_socket_fd(args->stdwsi[LWS_STDERR]),
362 buf, sizeof(buf) - 2);
364 if (buf[n - 1] != '\n')
367 lwsl_notice("CGI-stderr: %s\n", buf);
373 case LWS_CALLBACK_CGI_TERMINATED:
376 case LWS_CALLBACK_CGI_STDIN_DATA: /* POST body for stdin */
377 args = (struct lws_cgi_args *)in;
378 args->data[args->len] = '\0';
379 n = write(lws_get_socket_fd(args->stdwsi[LWS_STDIN]),
380 args->data, args->len);
382 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: "
383 "sent %d only %d went", n, args->len);
393 /* list of supported protocols and callbacks */
395 static const struct lws_protocols protocols_dummy[] = {
396 /* first protocol must always be HTTP handler */
399 "http-only", /* name */
400 lws_callback_http_dummy, /* callback */
401 0, /* per_session_data_size */
402 0, /* max frame size / rx buffer */
406 * the other protocols are provided by lws plugins
408 { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
411 #ifdef LWS_PLAT_OPTEE
412 #undef LWS_HAVE_GETENV
415 LWS_VISIBLE struct lws_vhost *
416 lws_create_vhost(struct lws_context *context,
417 struct lws_context_creation_info *info)
419 struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
420 **vh1 = &context->vhost_list;
421 const struct lws_http_mount *mounts;
422 const struct lws_protocol_vhost_options *pvo;
423 #ifdef LWS_WITH_PLUGINS
424 struct lws_plugin *plugin = context->plugin_list;
426 struct lws_protocols *lwsp;
427 int m, f = !info->pvo;
428 #ifdef LWS_HAVE_GETENV
436 if (!info->protocols)
437 info->protocols = &protocols_dummy[0];
439 vh->context = context;
440 if (!info->vhost_name)
441 vh->name = "default";
443 vh->name = info->vhost_name;
445 vh->iface = info->iface;
446 #if !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32) && !defined(OPTEE_TA) && !defined(WIN32)
447 vh->bind_iface = info->bind_iface;
450 for (vh->count_protocols = 0;
451 info->protocols[vh->count_protocols].callback;
452 vh->count_protocols++)
455 vh->options = info->options;
457 vh->headers = info->headers;
458 if (info->keepalive_timeout)
459 vh->keepalive_timeout = info->keepalive_timeout;
461 vh->keepalive_timeout = 5;
464 * give the vhost a unified list of protocols including the
465 * ones that came from plugins
467 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
468 (vh->count_protocols +
469 context->plugin_protocol_count + 1));
475 m = vh->count_protocols;
476 memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);
478 /* for compatibility, all protocols enabled on vhost if only
479 * the default vhost exists. Otherwise only vhosts who ask
480 * for a protocol get it enabled.
483 if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
486 #ifdef LWS_WITH_PLUGINS
490 for (n = 0; n < plugin->caps.count_protocols; n++) {
492 * for compatibility's sake, no pvo implies
493 * allow all protocols
495 if (f || lws_vhost_protocol_options(vh,
496 plugin->caps.protocols[n].name)) {
498 &plugin->caps.protocols[n],
499 sizeof(struct lws_protocols));
501 vh->count_protocols++;
504 plugin = plugin->list;
510 #ifdef LWS_WITH_PLUGINS
511 (context->plugin_list) ||
513 info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
514 vh->protocols = lwsp;
516 vh->protocols = info->protocols;
520 vh->same_vh_protocol_list = (struct lws **)
521 lws_zalloc(sizeof(struct lws *) * vh->count_protocols);
523 vh->mount_list = info->mounts;
525 #ifdef LWS_USE_UNIX_SOCK
526 if (LWS_UNIX_SOCK_ENABLED(context)) {
527 lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
528 vh->name, info->iface, vh->count_protocols);
531 lwsl_notice("Creating Vhost '%s' port %d, %d protocols, IPv6 %s\n",
532 vh->name, info->port, vh->count_protocols, LWS_IPV6_ENABLED(vh) ? "on" : "off");
534 mounts = info->mounts;
536 lwsl_notice(" mounting %s%s to %s\n",
537 mount_protocols[mounts->origin_protocol],
538 mounts->origin, mounts->mountpoint);
540 /* convert interpreter protocol names to pointers */
541 pvo = mounts->interpret;
543 for (n = 0; n < vh->count_protocols; n++)
544 if (!strcmp(pvo->value, vh->protocols[n].name)) {
545 ((struct lws_protocol_vhost_options *)pvo)->value =
546 (const char *)(long)n;
549 if (n == vh->count_protocols)
550 lwsl_err("ignoring unknown interpret protocol %s\n", pvo->value);
554 mounts = mounts->mount_next;
557 #ifndef LWS_NO_EXTENSIONS
558 #ifdef LWS_WITH_PLUGINS
559 if (context->plugin_extension_count) {
562 while (info->extensions && info->extensions[m].callback)
566 * give the vhost a unified list of extensions including the
567 * ones that came from plugins
569 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
571 context->plugin_extension_count + 1));
575 memcpy((struct lws_extension *)vh->extensions, info->extensions,
576 sizeof(struct lws_extension) * m);
577 plugin = context->plugin_list;
579 memcpy((struct lws_extension *)&vh->extensions[m],
580 plugin->caps.extensions,
581 sizeof(struct lws_extension) *
582 plugin->caps.count_extensions);
583 m += plugin->caps.count_extensions;
584 plugin = plugin->list;
588 vh->extensions = info->extensions;
591 vh->listen_port = info->port;
592 #if !defined(LWS_WITH_ESP8266)
593 vh->http_proxy_port = 0;
594 vh->http_proxy_address[0] = '\0';
595 #if defined(LWS_WITH_SOCKS5)
596 vh->socks_proxy_port = 0;
597 vh->socks_proxy_address[0] = '\0';
600 /* either use proxy from info, or try get it from env var */
603 if (info->http_proxy_address) {
604 /* override for backwards compatibility */
605 if (info->http_proxy_port)
606 vh->http_proxy_port = info->http_proxy_port;
607 lws_set_proxy(vh, info->http_proxy_address);
609 #ifdef LWS_HAVE_GETENV
610 p = getenv("http_proxy");
612 lws_set_proxy(vh, p);
615 #if defined(LWS_WITH_SOCKS5)
617 if (info->socks_proxy_address) {
618 /* override for backwards compatibility */
619 if (info->socks_proxy_port)
620 vh->socks_proxy_port = info->socks_proxy_port;
621 lws_set_socks(vh, info->socks_proxy_address);
623 #ifdef LWS_HAVE_GETENV
624 p = getenv("socks_proxy");
626 lws_set_socks(vh, p);
632 vh->ka_time = info->ka_time;
633 vh->ka_interval = info->ka_interval;
634 vh->ka_probes = info->ka_probes;
636 if (vh->options & LWS_SERVER_OPTION_STS)
637 lwsl_notice(" STS enabled\n");
639 #ifdef LWS_WITH_ACCESS_LOG
640 if (info->log_filepath) {
641 vh->log_fd = open(info->log_filepath, O_CREAT | O_APPEND | O_RDWR, 0600);
642 if (vh->log_fd == (int)LWS_INVALID_FILE) {
643 lwsl_err("unable to open log filepath %s\n",
648 if (context->uid != -1)
649 if (chown(info->log_filepath, context->uid,
651 lwsl_err("unable to chown log file %s\n",
655 vh->log_fd = (int)LWS_INVALID_FILE;
657 if (lws_context_init_server_ssl(info, vh))
659 if (lws_context_init_client_ssl(info, vh))
661 if (lws_context_init_server(info, vh)) {
662 lwsl_err("init server failed\n");
671 vh1 = &(*vh1)->vhost_next;
673 /* for the case we are adding a vhost much later, after server init */
675 if (context->protocol_init_done)
676 lws_protocol_init(context);
687 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
688 struct lws_vhost *vhost)
690 struct lws_context_creation_info i;
692 memcpy(&i, info, sizeof(i));
693 i.port = CONTEXT_PORT_NO_LISTEN;
695 return lws_context_init_client_ssl(&i, vhost);
698 LWS_VISIBLE struct lws_context *
699 lws_create_context(struct lws_context_creation_info *info)
701 struct lws_context *context = NULL;
702 struct lws_plat_file_ops *prev;
703 #ifndef LWS_NO_DAEMONIZE
704 int pid_daemon = get_daemonize_pid();
707 #if defined(__ANDROID__)
711 lwsl_notice("Initial logging level %d\n", log_level);
712 lwsl_notice("Libwebsockets version: %s\n", library_version);
714 lwsl_notice("Compiled with %s\n", GCC_VER);
718 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
719 lwsl_notice("IPV6 compiled in and enabled\n");
721 lwsl_notice("IPV6 compiled in but disabled\n");
723 lwsl_notice("IPV6 not compiled in\n");
725 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
726 lws_feature_status_libev(info);
727 lws_feature_status_libuv(info);
730 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
731 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
732 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
733 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
734 lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
735 #if defined(LWS_WITH_STATS)
736 lwsl_notice(" LWS_WITH_STATS : on\n");
739 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
741 if (lws_plat_context_early_init())
744 context = lws_zalloc(sizeof(struct lws_context));
746 lwsl_err("No memory for websocket context\n");
749 if (info->pt_serv_buf_size)
750 context->pt_serv_buf_size = info->pt_serv_buf_size;
752 context->pt_serv_buf_size = 4096;
754 /* default to just the platform fops implementation */
756 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
757 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
758 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
759 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
760 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
761 context->fops_platform.fi[0].sig = NULL;
764 * arrange a linear linked-list of fops starting from context->fops
767 * [ -> fops_zip (copied into context so .next settable) ]
771 context->fops = &context->fops_platform;
772 prev = (struct lws_plat_file_ops *)context->fops;
774 #if defined(LWS_WITH_ZIP_FOPS)
775 /* make a soft copy so we can set .next */
776 context->fops_zip = fops_zip;
777 prev->next = &context->fops_zip;
778 prev = (struct lws_plat_file_ops *)prev->next;
781 /* if user provided fops, tack them on the end of the list */
783 prev->next = info->fops;
785 context->reject_service_keywords = info->reject_service_keywords;
786 if (info->external_baggage_free_on_destroy)
787 context->external_baggage_free_on_destroy =
788 info->external_baggage_free_on_destroy;
790 context->time_up = time(NULL);
792 context->simultaneous_ssl_restriction = info->simultaneous_ssl_restriction;
794 #ifndef LWS_NO_DAEMONIZE
796 context->started_with_parent = pid_daemon;
797 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
800 #if defined(__ANDROID__)
801 n = getrlimit ( RLIMIT_NOFILE,&rt);
803 lwsl_err("Get RLIMIT_NOFILE failed!\n");
806 context->max_fds = rt.rlim_cur;
808 context->max_fds = getdtablesize();
811 if (info->count_threads)
812 context->count_threads = info->count_threads;
814 context->count_threads = 1;
816 if (context->count_threads > LWS_MAX_SMP)
817 context->count_threads = LWS_MAX_SMP;
819 context->token_limits = info->token_limits;
821 context->options = info->options;
823 if (info->timeout_secs)
824 context->timeout_secs = info->timeout_secs;
826 context->timeout_secs = AWAITING_TIMEOUT;
828 context->ws_ping_pong_interval = info->ws_ping_pong_interval;
830 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
832 if (info->max_http_header_data)
833 context->max_http_header_data = info->max_http_header_data;
835 if (info->max_http_header_data2)
836 context->max_http_header_data =
837 info->max_http_header_data2;
839 context->max_http_header_data = LWS_DEF_HEADER_LEN;
840 if (info->max_http_header_pool)
841 context->max_http_header_pool = info->max_http_header_pool;
843 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
846 * Allocate the per-thread storage for scratchpad buffers,
847 * and header data pool
849 for (n = 0; n < context->count_threads; n++) {
850 context->pt[n].serv_buf = lws_zalloc(context->pt_serv_buf_size);
851 if (!context->pt[n].serv_buf) {
857 context->pt[n].context = context;
859 context->pt[n].tid = n;
860 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
861 context->max_http_header_pool);
862 if (!context->pt[n].http_header_data)
865 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
866 context->max_http_header_pool);
867 for (m = 0; m < context->max_http_header_pool; m++)
868 context->pt[n].ah_pool[m].data =
869 (char *)context->pt[n].http_header_data +
870 (m * context->max_http_header_data);
871 if (!context->pt[n].ah_pool)
874 lws_pt_mutex_init(&context->pt[n]);
877 if (info->fd_limit_per_thread)
878 context->fd_limit_per_thread = info->fd_limit_per_thread;
880 context->fd_limit_per_thread = context->max_fds /
881 context->count_threads;
883 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
884 context->fd_limit_per_thread);
886 if (!info->ka_interval && info->ka_time > 0) {
887 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
892 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
893 * enable libev mediated SIGINT handling with a default handler of
894 * lws_sigint_cb. The handler can be overridden or disabled
895 * by invoking lws_sigint_cfg after creating the context, but
896 * before invoking lws_initloop:
898 context->use_ev_sigint = 1;
899 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
900 #endif /* LWS_USE_LIBEV */
902 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
903 * enable libev mediated SIGINT handling with a default handler of
904 * lws_sigint_cb. The handler can be overridden or disabled
905 * by invoking lws_sigint_cfg after creating the context, but
906 * before invoking lws_initloop:
908 context->use_ev_sigint = 1;
909 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
911 #ifdef LWS_USE_LIBEVENT
912 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
913 * enable libev mediated SIGINT handling with a default handler of
914 * lws_sigint_cb. The handler can be overridden or disabled
915 * by invoking lws_sigint_cfg after creating the context, but
916 * before invoking lws_initloop:
918 context->use_ev_sigint = 1;
919 context->lws_event_sigint_cb = &lws_event_sigint_cb;
920 #endif /* LWS_USE_LIBEVENT */
922 lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
923 (long)sizeof(struct lws_context) +
924 (context->count_threads * context->pt_serv_buf_size),
925 (long)sizeof(struct lws_context),
926 (long)context->count_threads,
927 context->pt_serv_buf_size);
929 lwsl_info(" mem: http hdr rsvd: %5lu bytes (%u thr x (%u + %lu) x %u))\n",
930 (long)(context->max_http_header_data +
931 sizeof(struct allocated_headers)) *
932 context->max_http_header_pool * context->count_threads,
933 context->count_threads,
934 context->max_http_header_data,
935 (long)sizeof(struct allocated_headers),
936 context->max_http_header_pool);
937 n = sizeof(struct lws_pollfd) * context->count_threads *
938 context->fd_limit_per_thread;
939 context->pt[0].fds = lws_zalloc(n);
940 if (context->pt[0].fds == NULL) {
941 lwsl_err("OOM allocating %d fds\n", context->max_fds);
944 lwsl_info(" mem: pollfd map: %5u\n", n);
946 if (info->server_string) {
947 context->server_string = info->server_string;
948 context->server_string_len = (short)
949 strlen(context->server_string);
953 /* each thread serves his own chunk of fds */
954 for (n = 1; n < (int)info->count_threads; n++)
955 context->pt[n].fds = context->pt[n - 1].fds +
956 context->fd_limit_per_thread;
959 if (lws_plat_init(context, info))
962 lws_context_init_ssl_library(info);
964 context->user_space = info->user;
967 * if he's not saying he'll make his own vhosts later then act
968 * compatibly and make a default vhost using the data in the info
970 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
971 if (!lws_create_vhost(context, info)) {
972 lwsl_err("Failed to create default vhost\n");
976 lws_context_init_extensions(info, context);
978 lwsl_notice(" mem: per-conn: %5lu bytes + protocol rx buf\n",
979 (unsigned long)sizeof(struct lws));
981 strcpy(context->canonical_hostname, "unknown");
982 lws_server_get_canonical_hostname(context, info);
984 context->uid = info->uid;
985 context->gid = info->gid;
987 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
988 memcpy(context->caps, info->caps, sizeof(context->caps));
989 context->count_caps = info->count_caps;
993 * drop any root privs for this process
994 * to listen on port < 1023 we would have needed root, but now we are
995 * listening, we don't want the power for anything else
997 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
998 lws_plat_drop_app_privileges(info);
1001 * give all extensions a chance to create any per-context
1002 * allocations they need
1004 if (info->port != CONTEXT_PORT_NO_LISTEN) {
1005 if (lws_ext_cb_all_exts(context, NULL,
1006 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1009 if (lws_ext_cb_all_exts(context, NULL,
1010 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
1016 lws_context_destroy(context);
1020 LWS_VISIBLE LWS_EXTERN void
1021 lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
1023 struct lws_vhost *vh = context->vhost_list, *vh1;
1027 * "deprecation" means disable the context from accepting any new
1028 * connections and free up listen sockets to be used by a replacement
1031 * Otherwise the deprecated context remains operational, until its
1032 * number of connected sockets falls to zero, when it is deleted.
1035 /* for each vhost, close his listen socket */
1038 wsi = vh->lserv_wsi;
1040 wsi->socket_is_permanently_unusable = 1;
1041 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
1042 wsi->context->deprecation_pending_listen_close_count++;
1044 * other vhosts can share the listen port, they
1045 * point to the same wsi. So zap those too.
1047 vh1 = context->vhost_list;
1049 if (vh1->lserv_wsi == wsi)
1050 vh1->lserv_wsi = NULL;
1051 vh1 = vh1->vhost_next;
1054 vh = vh->vhost_next;
1057 context->deprecated = 1;
1058 context->deprecation_cb = cb;
1061 LWS_VISIBLE LWS_EXTERN int
1062 lws_context_is_deprecated(struct lws_context *context)
1064 return context->deprecated;
1068 lws_context_destroy2(struct lws_context *context);
1071 lws_context_destroy(struct lws_context *context)
1073 const struct lws_protocols *protocol = NULL;
1074 struct lws_context_per_thread *pt;
1075 struct lws_vhost *vh = NULL;
1080 lwsl_notice("%s: ctx %p\n", __func__, context);
1083 if (context->being_destroyed1) {
1084 lwsl_notice("%s: ctx %p: already being destroyed\n", __func__, context);
1088 lwsl_notice("%s: ctx %p\n", __func__, context);
1090 m = context->count_threads;
1091 context->being_destroyed = 1;
1092 context->being_destroyed1 = 1;
1094 memset(&wsi, 0, sizeof(wsi));
1095 wsi.context = context;
1098 if (context->worst_latency_info[0])
1099 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1103 pt = &context->pt[m];
1105 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1106 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1110 lws_close_free_wsi(wsi,
1111 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1112 /* no protocol close */);
1115 lws_pt_mutex_destroy(pt);
1119 * give all extensions a chance to clean up any per-context
1120 * allocations they might have made
1123 n = lws_ext_cb_all_exts(context, NULL,
1124 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
1126 n = lws_ext_cb_all_exts(context, NULL,
1127 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
1130 * inform all the protocols that they are done and will have no more
1133 * We can't free things until after the event loop shuts down.
1135 if (context->protocol_init_done)
1136 vh = context->vhost_list;
1139 protocol = vh->protocols;
1142 while (n < vh->count_protocols) {
1143 wsi.protocol = protocol;
1144 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
1151 vh = vh->vhost_next;
1154 for (n = 0; n < context->count_threads; n++) {
1155 pt = &context->pt[n];
1157 lws_libev_destroyloop(context, n);
1158 lws_libuv_destroyloop(context, n);
1159 lws_libevent_destroyloop(context, n);
1161 lws_free_set_NULL(context->pt[n].serv_buf);
1163 lws_free(pt->ah_pool);
1164 if (pt->http_header_data)
1165 lws_free(pt->http_header_data);
1167 lws_plat_context_early_destroy(context);
1169 if (context->pt[0].fds)
1170 lws_free_set_NULL(context->pt[0].fds);
1172 if (!LWS_LIBUV_ENABLED(context))
1173 lws_context_destroy2(context);
1177 * call the second one after the event loop has been shut down cleanly
1181 lws_context_destroy2(struct lws_context *context)
1183 const struct lws_protocols *protocol = NULL;
1184 struct lws_vhost *vh = NULL, *vh1;
1187 lwsl_notice("%s: ctx %p\n", __func__, context);
1190 * free all the per-vhost allocations
1193 vh = context->vhost_list;
1195 protocol = vh->protocols;
1198 while (n < vh->count_protocols) {
1199 if (vh->protocol_vh_privs &&
1200 vh->protocol_vh_privs[n]) {
1201 // lwsl_notice(" %s: freeing per-vhost protocol data %p\n", __func__, vh->protocol_vh_privs[n]);
1202 lws_free(vh->protocol_vh_privs[n]);
1203 vh->protocol_vh_privs[n] = NULL;
1209 if (vh->protocol_vh_privs)
1210 lws_free(vh->protocol_vh_privs);
1211 lws_ssl_SSL_CTX_destroy(vh);
1212 lws_free(vh->same_vh_protocol_list);
1213 #ifdef LWS_WITH_PLUGINS
1214 if (context->plugin_list)
1215 lws_free((void *)vh->protocols);
1217 if (vh->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1218 lws_free((void *)vh->protocols);
1220 #ifdef LWS_WITH_PLUGINS
1221 #ifndef LWS_NO_EXTENSIONS
1222 if (context->plugin_extension_count)
1223 lws_free((void *)vh->extensions);
1226 #ifdef LWS_WITH_ACCESS_LOG
1227 if (vh->log_fd != (int)LWS_INVALID_FILE)
1231 vh1 = vh->vhost_next;
1236 lws_stats_log_dump(context);
1238 lws_ssl_context_destroy(context);
1239 lws_plat_context_late_destroy(context);
1241 if (context->external_baggage_free_on_destroy)
1242 free(context->external_baggage_free_on_destroy);