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;
230 case LWS_CALLBACK_HTTP:
231 #ifndef LWS_NO_SERVER
232 if (lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL))
235 if (lws_http_transaction_completed(wsi))
240 case LWS_CALLBACK_HTTP_WRITEABLE:
242 if (wsi->reason_bf & 1) {
243 if (lws_cgi_write_split_stdout_headers(wsi) < 0)
246 if (wsi->reason_bf & 8)
247 wsi->reason_bf &= ~8;
249 wsi->reason_bf &= ~1;
257 /* CGI IO events (POLLIN/OUT) appear here, our default policy is:
259 * - POST data goes on subprocess stdin
260 * - subprocess stdout goes on http via writeable callback
261 * - subprocess stderr goes to the logs
263 case LWS_CALLBACK_CGI:
264 args = (struct lws_cgi_args *)in;
265 switch (args->ch) { /* which of stdin/out/err ? */
267 /* TBD stdin rx flow control */
271 /* when writing to MASTER would not block */
272 lws_callback_on_writable(wsi);
275 n = read(lws_get_socket_fd(args->stdwsi[LWS_STDERR]),
276 buf, sizeof(buf) - 2);
278 if (buf[n - 1] != '\n')
281 lwsl_notice("CGI-stderr: %s\n", buf);
287 case LWS_CALLBACK_CGI_TERMINATED:
290 case LWS_CALLBACK_CGI_STDIN_DATA: /* POST body for stdin */
291 args = (struct lws_cgi_args *)in;
292 args->data[args->len] = '\0';
293 n = write(lws_get_socket_fd(args->stdwsi[LWS_STDIN]),
294 args->data, args->len);
296 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: "
297 "sent %d only %d went", n, args->len);
307 /* list of supported protocols and callbacks */
309 static const struct lws_protocols protocols_dummy[] = {
310 /* first protocol must always be HTTP handler */
313 "http-only", /* name */
314 lws_callback_http_dummy, /* callback */
315 0, /* per_session_data_size */
316 0, /* max frame size / rx buffer */
320 * the other protocols are provided by lws plugins
322 { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
325 #ifdef LWS_PLAT_OPTEE
326 #undef LWS_HAVE_GETENV
329 LWS_VISIBLE struct lws_vhost *
330 lws_create_vhost(struct lws_context *context,
331 struct lws_context_creation_info *info)
333 struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
334 **vh1 = &context->vhost_list;
335 const struct lws_http_mount *mounts;
336 const struct lws_protocol_vhost_options *pvo;
337 #ifdef LWS_WITH_PLUGINS
338 struct lws_plugin *plugin = context->plugin_list;
340 struct lws_protocols *lwsp;
341 int m, f = !info->pvo;
342 #ifdef LWS_HAVE_GETENV
350 if (!info->protocols)
351 info->protocols = &protocols_dummy[0];
353 vh->context = context;
354 if (!info->vhost_name)
355 vh->name = "default";
357 vh->name = info->vhost_name;
359 vh->iface = info->iface;
360 #if !defined(LWS_WITH_ESP8266) && !defined(LWS_WITH_ESP32) && !defined(OPTEE_TA) && !defined(WIN32)
361 vh->bind_iface = info->bind_iface;
364 for (vh->count_protocols = 0;
365 info->protocols[vh->count_protocols].callback;
366 vh->count_protocols++)
369 vh->options = info->options;
371 vh->headers = info->headers;
372 if (info->keepalive_timeout)
373 vh->keepalive_timeout = info->keepalive_timeout;
375 vh->keepalive_timeout = 5;
378 * give the vhost a unified list of protocols including the
379 * ones that came from plugins
381 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
382 (vh->count_protocols +
383 context->plugin_protocol_count + 1));
389 m = vh->count_protocols;
390 memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);
392 /* for compatibility, all protocols enabled on vhost if only
393 * the default vhost exists. Otherwise only vhosts who ask
394 * for a protocol get it enabled.
397 if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
400 #ifdef LWS_WITH_PLUGINS
404 for (n = 0; n < plugin->caps.count_protocols; n++) {
406 * for compatibility's sake, no pvo implies
407 * allow all protocols
409 if (f || lws_vhost_protocol_options(vh,
410 plugin->caps.protocols[n].name)) {
412 &plugin->caps.protocols[n],
413 sizeof(struct lws_protocols));
415 vh->count_protocols++;
418 plugin = plugin->list;
424 #ifdef LWS_WITH_PLUGINS
425 (context->plugin_list) ||
427 info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
428 vh->protocols = lwsp;
430 vh->protocols = info->protocols;
434 vh->same_vh_protocol_list = (struct lws **)
435 lws_zalloc(sizeof(struct lws *) * vh->count_protocols);
437 vh->mount_list = info->mounts;
439 #ifdef LWS_USE_UNIX_SOCK
440 if (LWS_UNIX_SOCK_ENABLED(context)) {
441 lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
442 vh->name, info->iface, vh->count_protocols);
445 lwsl_notice("Creating Vhost '%s' port %d, %d protocols, IPv6 %s\n",
446 vh->name, info->port, vh->count_protocols, LWS_IPV6_ENABLED(vh) ? "on" : "off");
448 mounts = info->mounts;
450 lwsl_notice(" mounting %s%s to %s\n",
451 mount_protocols[mounts->origin_protocol],
452 mounts->origin, mounts->mountpoint);
454 /* convert interpreter protocol names to pointers */
455 pvo = mounts->interpret;
457 for (n = 0; n < vh->count_protocols; n++)
458 if (!strcmp(pvo->value, vh->protocols[n].name)) {
459 ((struct lws_protocol_vhost_options *)pvo)->value =
460 (const char *)(long)n;
463 if (n == vh->count_protocols)
464 lwsl_err("ignoring unknown interpret protocol %s\n", pvo->value);
468 mounts = mounts->mount_next;
471 #ifndef LWS_NO_EXTENSIONS
472 #ifdef LWS_WITH_PLUGINS
473 if (context->plugin_extension_count) {
476 while (info->extensions && info->extensions[m].callback)
480 * give the vhost a unified list of extensions including the
481 * ones that came from plugins
483 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
485 context->plugin_extension_count + 1));
489 memcpy((struct lws_extension *)vh->extensions, info->extensions,
490 sizeof(struct lws_extension) * m);
491 plugin = context->plugin_list;
493 memcpy((struct lws_extension *)&vh->extensions[m],
494 plugin->caps.extensions,
495 sizeof(struct lws_extension) *
496 plugin->caps.count_extensions);
497 m += plugin->caps.count_extensions;
498 plugin = plugin->list;
502 vh->extensions = info->extensions;
505 vh->listen_port = info->port;
506 #if !defined(LWS_WITH_ESP8266)
507 vh->http_proxy_port = 0;
508 vh->http_proxy_address[0] = '\0';
509 #if defined(LWS_WITH_SOCKS5)
510 vh->socks_proxy_port = 0;
511 vh->socks_proxy_address[0] = '\0';
514 /* either use proxy from info, or try get it from env var */
517 if (info->http_proxy_address) {
518 /* override for backwards compatibility */
519 if (info->http_proxy_port)
520 vh->http_proxy_port = info->http_proxy_port;
521 lws_set_proxy(vh, info->http_proxy_address);
523 #ifdef LWS_HAVE_GETENV
524 p = getenv("http_proxy");
526 lws_set_proxy(vh, p);
529 #if defined(LWS_WITH_SOCKS5)
531 if (info->socks_proxy_address) {
532 /* override for backwards compatibility */
533 if (info->socks_proxy_port)
534 vh->socks_proxy_port = info->socks_proxy_port;
535 lws_set_socks(vh, info->socks_proxy_address);
537 #ifdef LWS_HAVE_GETENV
538 p = getenv("socks_proxy");
540 lws_set_socks(vh, p);
546 vh->ka_time = info->ka_time;
547 vh->ka_interval = info->ka_interval;
548 vh->ka_probes = info->ka_probes;
550 if (vh->options & LWS_SERVER_OPTION_STS)
551 lwsl_notice(" STS enabled\n");
553 #ifdef LWS_WITH_ACCESS_LOG
554 if (info->log_filepath) {
555 vh->log_fd = open(info->log_filepath, O_CREAT | O_APPEND | O_RDWR, 0600);
556 if (vh->log_fd == (int)LWS_INVALID_FILE) {
557 lwsl_err("unable to open log filepath %s\n",
562 if (context->uid != -1)
563 if (chown(info->log_filepath, context->uid,
565 lwsl_err("unable to chown log file %s\n",
569 vh->log_fd = (int)LWS_INVALID_FILE;
571 if (lws_context_init_server_ssl(info, vh))
573 if (lws_context_init_client_ssl(info, vh))
575 if (lws_context_init_server(info, vh)) {
576 lwsl_err("init server failed\n");
585 vh1 = &(*vh1)->vhost_next;
587 /* for the case we are adding a vhost much later, after server init */
589 if (context->protocol_init_done)
590 lws_protocol_init(context);
601 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
602 struct lws_vhost *vhost)
604 struct lws_context_creation_info i;
606 memcpy(&i, info, sizeof(i));
607 i.port = CONTEXT_PORT_NO_LISTEN;
609 return lws_context_init_client_ssl(&i, vhost);
612 LWS_VISIBLE struct lws_context *
613 lws_create_context(struct lws_context_creation_info *info)
615 struct lws_context *context = NULL;
616 struct lws_plat_file_ops *prev;
617 #ifndef LWS_NO_DAEMONIZE
618 int pid_daemon = get_daemonize_pid();
621 #if defined(__ANDROID__)
625 lwsl_notice("Initial logging level %d\n", log_level);
626 lwsl_notice("Libwebsockets version: %s\n", library_version);
628 lwsl_notice("Compiled with %s\n", GCC_VER);
632 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
633 lwsl_notice("IPV6 compiled in and enabled\n");
635 lwsl_notice("IPV6 compiled in but disabled\n");
637 lwsl_notice("IPV6 not compiled in\n");
639 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
640 lws_feature_status_libev(info);
641 lws_feature_status_libuv(info);
644 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
645 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
646 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
647 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
648 lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
649 #if defined(LWS_WITH_STATS)
650 lwsl_notice(" LWS_WITH_STATS : on\n");
653 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
655 if (lws_plat_context_early_init())
658 context = lws_zalloc(sizeof(struct lws_context));
660 lwsl_err("No memory for websocket context\n");
663 if (info->pt_serv_buf_size)
664 context->pt_serv_buf_size = info->pt_serv_buf_size;
666 context->pt_serv_buf_size = 4096;
668 /* default to just the platform fops implementation */
670 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
671 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
672 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
673 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
674 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
675 context->fops_platform.fi[0].sig = NULL;
678 * arrange a linear linked-list of fops starting from context->fops
681 * [ -> fops_zip (copied into context so .next settable) ]
685 context->fops = &context->fops_platform;
686 prev = (struct lws_plat_file_ops *)context->fops;
688 #if defined(LWS_WITH_ZIP_FOPS)
689 /* make a soft copy so we can set .next */
690 context->fops_zip = fops_zip;
691 prev->next = &context->fops_zip;
692 prev = (struct lws_plat_file_ops *)prev->next;
695 /* if user provided fops, tack them on the end of the list */
697 prev->next = info->fops;
699 context->reject_service_keywords = info->reject_service_keywords;
700 if (info->external_baggage_free_on_destroy)
701 context->external_baggage_free_on_destroy =
702 info->external_baggage_free_on_destroy;
704 context->time_up = time(NULL);
706 context->simultaneous_ssl_restriction = info->simultaneous_ssl_restriction;
708 #ifndef LWS_NO_DAEMONIZE
710 context->started_with_parent = pid_daemon;
711 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
714 #if defined(__ANDROID__)
715 n = getrlimit ( RLIMIT_NOFILE,&rt);
717 lwsl_err("Get RLIMIT_NOFILE failed!\n");
720 context->max_fds = rt.rlim_cur;
722 context->max_fds = getdtablesize();
725 if (info->count_threads)
726 context->count_threads = info->count_threads;
728 context->count_threads = 1;
730 if (context->count_threads > LWS_MAX_SMP)
731 context->count_threads = LWS_MAX_SMP;
733 context->token_limits = info->token_limits;
735 context->options = info->options;
737 if (info->timeout_secs)
738 context->timeout_secs = info->timeout_secs;
740 context->timeout_secs = AWAITING_TIMEOUT;
742 context->ws_ping_pong_interval = info->ws_ping_pong_interval;
744 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
746 if (info->max_http_header_data)
747 context->max_http_header_data = info->max_http_header_data;
749 if (info->max_http_header_data2)
750 context->max_http_header_data =
751 info->max_http_header_data2;
753 context->max_http_header_data = LWS_DEF_HEADER_LEN;
754 if (info->max_http_header_pool)
755 context->max_http_header_pool = info->max_http_header_pool;
757 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
760 * Allocate the per-thread storage for scratchpad buffers,
761 * and header data pool
763 for (n = 0; n < context->count_threads; n++) {
764 context->pt[n].serv_buf = lws_zalloc(context->pt_serv_buf_size);
765 if (!context->pt[n].serv_buf) {
771 context->pt[n].context = context;
773 context->pt[n].tid = n;
774 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
775 context->max_http_header_pool);
776 if (!context->pt[n].http_header_data)
779 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
780 context->max_http_header_pool);
781 for (m = 0; m < context->max_http_header_pool; m++)
782 context->pt[n].ah_pool[m].data =
783 (char *)context->pt[n].http_header_data +
784 (m * context->max_http_header_data);
785 if (!context->pt[n].ah_pool)
788 lws_pt_mutex_init(&context->pt[n]);
791 if (info->fd_limit_per_thread)
792 context->fd_limit_per_thread = info->fd_limit_per_thread;
794 context->fd_limit_per_thread = context->max_fds /
795 context->count_threads;
797 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
798 context->fd_limit_per_thread);
800 if (!info->ka_interval && info->ka_time > 0) {
801 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
806 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
807 * enable libev mediated SIGINT handling with a default handler of
808 * lws_sigint_cb. The handler can be overridden or disabled
809 * by invoking lws_sigint_cfg after creating the context, but
810 * before invoking lws_initloop:
812 context->use_ev_sigint = 1;
813 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
814 #endif /* LWS_USE_LIBEV */
816 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
817 * enable libev mediated SIGINT handling with a default handler of
818 * lws_sigint_cb. The handler can be overridden or disabled
819 * by invoking lws_sigint_cfg after creating the context, but
820 * before invoking lws_initloop:
822 context->use_ev_sigint = 1;
823 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
825 #ifdef LWS_USE_LIBEVENT
826 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
827 * enable libev mediated SIGINT handling with a default handler of
828 * lws_sigint_cb. The handler can be overridden or disabled
829 * by invoking lws_sigint_cfg after creating the context, but
830 * before invoking lws_initloop:
832 context->use_ev_sigint = 1;
833 context->lws_event_sigint_cb = &lws_event_sigint_cb;
834 #endif /* LWS_USE_LIBEVENT */
836 lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
837 (long)sizeof(struct lws_context) +
838 (context->count_threads * context->pt_serv_buf_size),
839 (long)sizeof(struct lws_context),
840 (long)context->count_threads,
841 context->pt_serv_buf_size);
843 lwsl_info(" mem: http hdr rsvd: %5lu bytes (%u thr x (%u + %lu) x %u))\n",
844 (long)(context->max_http_header_data +
845 sizeof(struct allocated_headers)) *
846 context->max_http_header_pool * context->count_threads,
847 context->count_threads,
848 context->max_http_header_data,
849 (long)sizeof(struct allocated_headers),
850 context->max_http_header_pool);
851 n = sizeof(struct lws_pollfd) * context->count_threads *
852 context->fd_limit_per_thread;
853 context->pt[0].fds = lws_zalloc(n);
854 if (context->pt[0].fds == NULL) {
855 lwsl_err("OOM allocating %d fds\n", context->max_fds);
858 lwsl_info(" mem: pollfd map: %5u\n", n);
860 if (info->server_string) {
861 context->server_string = info->server_string;
862 context->server_string_len = (short)
863 strlen(context->server_string);
867 /* each thread serves his own chunk of fds */
868 for (n = 1; n < (int)info->count_threads; n++)
869 context->pt[n].fds = context->pt[n - 1].fds +
870 context->fd_limit_per_thread;
873 if (lws_plat_init(context, info))
876 lws_context_init_ssl_library(info);
878 context->user_space = info->user;
881 * if he's not saying he'll make his own vhosts later then act
882 * compatibly and make a default vhost using the data in the info
884 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
885 if (!lws_create_vhost(context, info)) {
886 lwsl_err("Failed to create default vhost\n");
890 lws_context_init_extensions(info, context);
892 lwsl_notice(" mem: per-conn: %5lu bytes + protocol rx buf\n",
893 (unsigned long)sizeof(struct lws));
895 strcpy(context->canonical_hostname, "unknown");
896 lws_server_get_canonical_hostname(context, info);
898 context->uid = info->uid;
899 context->gid = info->gid;
901 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
902 memcpy(context->caps, info->caps, sizeof(context->caps));
903 context->count_caps = info->count_caps;
907 * drop any root privs for this process
908 * to listen on port < 1023 we would have needed root, but now we are
909 * listening, we don't want the power for anything else
911 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
912 lws_plat_drop_app_privileges(info);
915 * give all extensions a chance to create any per-context
916 * allocations they need
918 if (info->port != CONTEXT_PORT_NO_LISTEN) {
919 if (lws_ext_cb_all_exts(context, NULL,
920 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
923 if (lws_ext_cb_all_exts(context, NULL,
924 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
930 lws_context_destroy(context);
934 LWS_VISIBLE LWS_EXTERN void
935 lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
937 struct lws_vhost *vh = context->vhost_list, *vh1;
941 * "deprecation" means disable the context from accepting any new
942 * connections and free up listen sockets to be used by a replacement
945 * Otherwise the deprecated context remains operational, until its
946 * number of connected sockets falls to zero, when it is deleted.
949 /* for each vhost, close his listen socket */
954 wsi->socket_is_permanently_unusable = 1;
955 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
956 wsi->context->deprecation_pending_listen_close_count++;
958 * other vhosts can share the listen port, they
959 * point to the same wsi. So zap those too.
961 vh1 = context->vhost_list;
963 if (vh1->lserv_wsi == wsi)
964 vh1->lserv_wsi = NULL;
965 vh1 = vh1->vhost_next;
971 context->deprecated = 1;
972 context->deprecation_cb = cb;
975 LWS_VISIBLE LWS_EXTERN int
976 lws_context_is_deprecated(struct lws_context *context)
978 return context->deprecated;
982 lws_context_destroy2(struct lws_context *context);
985 lws_context_destroy(struct lws_context *context)
987 const struct lws_protocols *protocol = NULL;
988 struct lws_context_per_thread *pt;
989 struct lws_vhost *vh = NULL;
994 lwsl_notice("%s: ctx %p\n", __func__, context);
997 if (context->being_destroyed1) {
998 lwsl_notice("%s: ctx %p: already being destroyed\n", __func__, context);
1002 lwsl_notice("%s: ctx %p\n", __func__, context);
1004 m = context->count_threads;
1005 context->being_destroyed = 1;
1006 context->being_destroyed1 = 1;
1008 memset(&wsi, 0, sizeof(wsi));
1009 wsi.context = context;
1012 if (context->worst_latency_info[0])
1013 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1017 pt = &context->pt[m];
1019 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1020 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1024 lws_close_free_wsi(wsi,
1025 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1026 /* no protocol close */);
1029 lws_pt_mutex_destroy(pt);
1033 * give all extensions a chance to clean up any per-context
1034 * allocations they might have made
1037 n = lws_ext_cb_all_exts(context, NULL,
1038 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
1040 n = lws_ext_cb_all_exts(context, NULL,
1041 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
1044 * inform all the protocols that they are done and will have no more
1047 * We can't free things until after the event loop shuts down.
1049 if (context->protocol_init_done)
1050 vh = context->vhost_list;
1053 protocol = vh->protocols;
1056 while (n < vh->count_protocols) {
1057 wsi.protocol = protocol;
1058 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
1065 vh = vh->vhost_next;
1068 for (n = 0; n < context->count_threads; n++) {
1069 pt = &context->pt[n];
1071 lws_libev_destroyloop(context, n);
1072 lws_libuv_destroyloop(context, n);
1073 lws_libevent_destroyloop(context, n);
1075 lws_free_set_NULL(context->pt[n].serv_buf);
1077 lws_free(pt->ah_pool);
1078 if (pt->http_header_data)
1079 lws_free(pt->http_header_data);
1081 lws_plat_context_early_destroy(context);
1083 if (context->pt[0].fds)
1084 lws_free_set_NULL(context->pt[0].fds);
1086 if (!LWS_LIBUV_ENABLED(context))
1087 lws_context_destroy2(context);
1091 * call the second one after the event loop has been shut down cleanly
1095 lws_context_destroy2(struct lws_context *context)
1097 const struct lws_protocols *protocol = NULL;
1098 struct lws_vhost *vh = NULL, *vh1;
1101 lwsl_notice("%s: ctx %p\n", __func__, context);
1104 * free all the per-vhost allocations
1107 vh = context->vhost_list;
1109 protocol = vh->protocols;
1112 while (n < vh->count_protocols) {
1113 if (vh->protocol_vh_privs &&
1114 vh->protocol_vh_privs[n]) {
1115 // lwsl_notice(" %s: freeing per-vhost protocol data %p\n", __func__, vh->protocol_vh_privs[n]);
1116 lws_free(vh->protocol_vh_privs[n]);
1117 vh->protocol_vh_privs[n] = NULL;
1123 if (vh->protocol_vh_privs)
1124 lws_free(vh->protocol_vh_privs);
1125 lws_ssl_SSL_CTX_destroy(vh);
1126 lws_free(vh->same_vh_protocol_list);
1127 #ifdef LWS_WITH_PLUGINS
1128 if (context->plugin_list)
1129 lws_free((void *)vh->protocols);
1131 if (vh->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1132 lws_free((void *)vh->protocols);
1134 #ifdef LWS_WITH_PLUGINS
1135 #ifndef LWS_NO_EXTENSIONS
1136 if (context->plugin_extension_count)
1137 lws_free((void *)vh->extensions);
1140 #ifdef LWS_WITH_ACCESS_LOG
1141 if (vh->log_fd != (int)LWS_INVALID_FILE)
1145 vh1 = vh->vhost_next;
1150 lws_stats_log_dump(context);
1152 lws_ssl_context_destroy(context);
1153 lws_plat_context_late_destroy(context);
1155 if (context->external_baggage_free_on_destroy)
1156 free(context->external_baggage_free_on_destroy);