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;
228 case LWS_CALLBACK_HTTP:
229 #ifndef LWS_NO_SERVER
230 if (lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL))
233 if (lws_http_transaction_completed(wsi))
238 case LWS_CALLBACK_HTTP_WRITEABLE:
240 if (wsi->reason_bf & 1) {
241 if (lws_cgi_write_split_stdout_headers(wsi) < 0)
244 if (wsi->reason_bf & 8)
245 wsi->reason_bf &= ~8;
247 wsi->reason_bf &= ~1;
255 /* CGI IO events (POLLIN/OUT) appear here, our default policy is:
257 * - POST data goes on subprocess stdin
258 * - subprocess stdout goes on http via writeable callback
259 * - subprocess stderr goes to the logs
261 case LWS_CALLBACK_CGI:
262 args = (struct lws_cgi_args *)in;
263 switch (args->ch) { /* which of stdin/out/err ? */
265 /* TBD stdin rx flow control */
269 /* when writing to MASTER would not block */
270 lws_callback_on_writable(wsi);
273 n = read(lws_get_socket_fd(args->stdwsi[LWS_STDERR]),
274 buf, sizeof(buf) - 2);
276 if (buf[n - 1] != '\n')
279 lwsl_notice("CGI-stderr: %s\n", buf);
285 case LWS_CALLBACK_CGI_TERMINATED:
288 case LWS_CALLBACK_CGI_STDIN_DATA: /* POST body for stdin */
289 args = (struct lws_cgi_args *)in;
290 args->data[args->len] = '\0';
291 n = write(lws_get_socket_fd(args->stdwsi[LWS_STDIN]),
292 args->data, args->len);
294 lwsl_notice("LWS_CALLBACK_CGI_STDIN_DATA: "
295 "sent %d only %d went", n, args->len);
305 /* list of supported protocols and callbacks */
307 static const struct lws_protocols protocols_dummy[] = {
308 /* first protocol must always be HTTP handler */
311 "http-only", /* name */
312 lws_callback_http_dummy, /* callback */
313 0, /* per_session_data_size */
314 0, /* max frame size / rx buffer */
318 * the other protocols are provided by lws plugins
320 { NULL, NULL, 0, 0, 0, NULL, 0} /* terminator */
323 #ifdef LWS_PLAT_OPTEE
324 #undef LWS_HAVE_GETENV
327 LWS_VISIBLE struct lws_vhost *
328 lws_create_vhost(struct lws_context *context,
329 struct lws_context_creation_info *info)
331 struct lws_vhost *vh = lws_zalloc(sizeof(*vh)),
332 **vh1 = &context->vhost_list;
333 const struct lws_http_mount *mounts;
334 const struct lws_protocol_vhost_options *pvo;
335 #ifdef LWS_WITH_PLUGINS
336 struct lws_plugin *plugin = context->plugin_list;
338 struct lws_protocols *lwsp;
339 int m, f = !info->pvo;
340 #ifdef LWS_HAVE_GETENV
348 if (!info->protocols)
349 info->protocols = &protocols_dummy[0];
351 vh->context = context;
352 if (!info->vhost_name)
353 vh->name = "default";
355 vh->name = info->vhost_name;
357 vh->iface = info->iface;
358 for (vh->count_protocols = 0;
359 info->protocols[vh->count_protocols].callback;
360 vh->count_protocols++)
363 vh->options = info->options;
365 vh->headers = info->headers;
366 if (info->keepalive_timeout)
367 vh->keepalive_timeout = info->keepalive_timeout;
369 vh->keepalive_timeout = 5;
372 * give the vhost a unified list of protocols including the
373 * ones that came from plugins
375 lwsp = lws_zalloc(sizeof(struct lws_protocols) *
376 (vh->count_protocols +
377 context->plugin_protocol_count + 1));
383 m = vh->count_protocols;
384 memcpy(lwsp, info->protocols, sizeof(struct lws_protocols) * m);
386 /* for compatibility, all protocols enabled on vhost if only
387 * the default vhost exists. Otherwise only vhosts who ask
388 * for a protocol get it enabled.
391 if (info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
394 #ifdef LWS_WITH_PLUGINS
398 for (n = 0; n < plugin->caps.count_protocols; n++) {
400 * for compatibility's sake, no pvo implies
401 * allow all protocols
403 if (f || lws_vhost_protocol_options(vh,
404 plugin->caps.protocols[n].name)) {
406 &plugin->caps.protocols[n],
407 sizeof(struct lws_protocols));
409 vh->count_protocols++;
412 plugin = plugin->list;
418 #ifdef LWS_WITH_PLUGINS
419 (context->plugin_list) ||
421 info->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
422 vh->protocols = lwsp;
424 vh->protocols = info->protocols;
428 vh->same_vh_protocol_list = (struct lws **)
429 lws_zalloc(sizeof(struct lws *) * vh->count_protocols);
431 vh->mount_list = info->mounts;
433 #ifdef LWS_USE_UNIX_SOCK
434 if (LWS_UNIX_SOCK_ENABLED(context)) {
435 lwsl_notice("Creating Vhost '%s' path \"%s\", %d protocols\n",
436 vh->name, info->iface, vh->count_protocols);
439 lwsl_notice("Creating Vhost '%s' port %d, %d protocols, IPv6 %s\n",
440 vh->name, info->port, vh->count_protocols, LWS_IPV6_ENABLED(vh) ? "on" : "off");
442 mounts = info->mounts;
444 lwsl_notice(" mounting %s%s to %s\n",
445 mount_protocols[mounts->origin_protocol],
446 mounts->origin, mounts->mountpoint);
448 /* convert interpreter protocol names to pointers */
449 pvo = mounts->interpret;
451 for (n = 0; n < vh->count_protocols; n++)
452 if (!strcmp(pvo->value, vh->protocols[n].name)) {
453 ((struct lws_protocol_vhost_options *)pvo)->value =
454 (const char *)(long)n;
457 if (n == vh->count_protocols)
458 lwsl_err("ignoring unknown interpret protocol %s\n", pvo->value);
462 mounts = mounts->mount_next;
465 #ifndef LWS_NO_EXTENSIONS
466 #ifdef LWS_WITH_PLUGINS
467 if (context->plugin_extension_count) {
470 while (info->extensions && info->extensions[m].callback)
474 * give the vhost a unified list of extensions including the
475 * ones that came from plugins
477 vh->extensions = lws_zalloc(sizeof(struct lws_extension) *
479 context->plugin_extension_count + 1));
483 memcpy((struct lws_extension *)vh->extensions, info->extensions,
484 sizeof(struct lws_extension) * m);
485 plugin = context->plugin_list;
487 memcpy((struct lws_extension *)&vh->extensions[m],
488 plugin->caps.extensions,
489 sizeof(struct lws_extension) *
490 plugin->caps.count_extensions);
491 m += plugin->caps.count_extensions;
492 plugin = plugin->list;
496 vh->extensions = info->extensions;
499 vh->listen_port = info->port;
500 #if !defined(LWS_WITH_ESP8266)
501 vh->http_proxy_port = 0;
502 vh->http_proxy_address[0] = '\0';
503 #if defined(LWS_WITH_SOCKS5)
504 vh->socks_proxy_port = 0;
505 vh->socks_proxy_address[0] = '\0';
508 /* either use proxy from info, or try get it from env var */
511 if (info->http_proxy_address) {
512 /* override for backwards compatibility */
513 if (info->http_proxy_port)
514 vh->http_proxy_port = info->http_proxy_port;
515 lws_set_proxy(vh, info->http_proxy_address);
517 #ifdef LWS_HAVE_GETENV
518 p = getenv("http_proxy");
520 lws_set_proxy(vh, p);
523 #if defined(LWS_WITH_SOCKS5)
525 if (info->socks_proxy_address) {
526 /* override for backwards compatibility */
527 if (info->socks_proxy_port)
528 vh->socks_proxy_port = info->socks_proxy_port;
529 lws_set_socks(vh, info->socks_proxy_address);
531 #ifdef LWS_HAVE_GETENV
532 p = getenv("socks_proxy");
534 lws_set_socks(vh, p);
540 vh->ka_time = info->ka_time;
541 vh->ka_interval = info->ka_interval;
542 vh->ka_probes = info->ka_probes;
544 if (vh->options & LWS_SERVER_OPTION_STS)
545 lwsl_notice(" STS enabled\n");
547 #ifdef LWS_WITH_ACCESS_LOG
548 if (info->log_filepath) {
549 vh->log_fd = open(info->log_filepath, O_CREAT | O_APPEND | O_RDWR, 0600);
550 if (vh->log_fd == (int)LWS_INVALID_FILE) {
551 lwsl_err("unable to open log filepath %s\n",
556 if (context->uid != -1)
557 if (chown(info->log_filepath, context->uid,
559 lwsl_err("unable to chown log file %s\n",
563 vh->log_fd = (int)LWS_INVALID_FILE;
565 if (lws_context_init_server_ssl(info, vh))
567 if (lws_context_init_client_ssl(info, vh))
569 if (lws_context_init_server(info, vh)) {
570 lwsl_err("init server failed\n");
579 vh1 = &(*vh1)->vhost_next;
581 /* for the case we are adding a vhost much later, after server init */
583 if (context->protocol_init_done)
584 lws_protocol_init(context);
595 lws_init_vhost_client_ssl(const struct lws_context_creation_info *info,
596 struct lws_vhost *vhost)
598 struct lws_context_creation_info i;
600 memcpy(&i, info, sizeof(i));
601 i.port = CONTEXT_PORT_NO_LISTEN;
603 return lws_context_init_client_ssl(&i, vhost);
606 LWS_VISIBLE struct lws_context *
607 lws_create_context(struct lws_context_creation_info *info)
609 struct lws_context *context = NULL;
610 struct lws_plat_file_ops *prev;
611 #ifndef LWS_NO_DAEMONIZE
612 int pid_daemon = get_daemonize_pid();
615 #if defined(__ANDROID__)
619 lwsl_notice("Initial logging level %d\n", log_level);
620 lwsl_notice("Libwebsockets version: %s\n", library_version);
622 lwsl_notice("Compiled with %s\n", GCC_VER);
626 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
627 lwsl_notice("IPV6 compiled in and enabled\n");
629 lwsl_notice("IPV6 compiled in but disabled\n");
631 lwsl_notice("IPV6 not compiled in\n");
633 #if !defined(LWS_PLAT_OPTEE) && !defined(LWS_PLAT_ESP32)
634 lws_feature_status_libev(info);
635 lws_feature_status_libuv(info);
638 lwsl_info(" LWS_DEF_HEADER_LEN : %u\n", LWS_DEF_HEADER_LEN);
639 lwsl_info(" LWS_MAX_PROTOCOLS : %u\n", LWS_MAX_PROTOCOLS);
640 lwsl_info(" LWS_MAX_SMP : %u\n", LWS_MAX_SMP);
641 lwsl_info(" SPEC_LATEST_SUPPORTED : %u\n", SPEC_LATEST_SUPPORTED);
642 lwsl_info(" sizeof (*info) : %ld\n", (long)sizeof(*info));
643 #if defined(LWS_WITH_STATS)
644 lwsl_notice(" LWS_WITH_STATS : on\n");
647 lwsl_info(" SYSTEM_RANDOM_FILEPATH: '%s'\n", SYSTEM_RANDOM_FILEPATH);
649 if (lws_plat_context_early_init())
652 context = lws_zalloc(sizeof(struct lws_context));
654 lwsl_err("No memory for websocket context\n");
657 if (info->pt_serv_buf_size)
658 context->pt_serv_buf_size = info->pt_serv_buf_size;
660 context->pt_serv_buf_size = 4096;
662 /* default to just the platform fops implementation */
664 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
665 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
666 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
667 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
668 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
669 context->fops_platform.fi[0].sig = NULL;
672 * arrange a linear linked-list of fops starting from context->fops
675 * [ -> fops_zip (copied into context so .next settable) ]
679 context->fops = &context->fops_platform;
680 prev = (struct lws_plat_file_ops *)context->fops;
682 #if defined(LWS_WITH_ZIP_FOPS)
683 /* make a soft copy so we can set .next */
684 context->fops_zip = fops_zip;
685 prev->next = &context->fops_zip;
686 prev = (struct lws_plat_file_ops *)prev->next;
689 /* if user provided fops, tack them on the end of the list */
691 prev->next = info->fops;
693 context->reject_service_keywords = info->reject_service_keywords;
694 if (info->external_baggage_free_on_destroy)
695 context->external_baggage_free_on_destroy =
696 info->external_baggage_free_on_destroy;
698 context->time_up = time(NULL);
700 context->simultaneous_ssl_restriction = info->simultaneous_ssl_restriction;
702 #ifndef LWS_NO_DAEMONIZE
704 context->started_with_parent = pid_daemon;
705 lwsl_notice(" Started with daemon pid %d\n", pid_daemon);
708 #if defined(__ANDROID__)
709 n = getrlimit ( RLIMIT_NOFILE,&rt);
711 lwsl_err("Get RLIMIT_NOFILE failed!\n");
714 context->max_fds = rt.rlim_cur;
716 context->max_fds = getdtablesize();
719 if (info->count_threads)
720 context->count_threads = info->count_threads;
722 context->count_threads = 1;
724 if (context->count_threads > LWS_MAX_SMP)
725 context->count_threads = LWS_MAX_SMP;
727 context->token_limits = info->token_limits;
729 context->options = info->options;
731 if (info->timeout_secs)
732 context->timeout_secs = info->timeout_secs;
734 context->timeout_secs = AWAITING_TIMEOUT;
736 context->ws_ping_pong_interval = info->ws_ping_pong_interval;
738 lwsl_info(" default timeout (secs): %u\n", context->timeout_secs);
740 if (info->max_http_header_data)
741 context->max_http_header_data = info->max_http_header_data;
743 if (info->max_http_header_data2)
744 context->max_http_header_data =
745 info->max_http_header_data2;
747 context->max_http_header_data = LWS_DEF_HEADER_LEN;
748 if (info->max_http_header_pool)
749 context->max_http_header_pool = info->max_http_header_pool;
751 context->max_http_header_pool = LWS_DEF_HEADER_POOL;
754 * Allocate the per-thread storage for scratchpad buffers,
755 * and header data pool
757 for (n = 0; n < context->count_threads; n++) {
758 context->pt[n].serv_buf = lws_zalloc(context->pt_serv_buf_size);
759 if (!context->pt[n].serv_buf) {
765 context->pt[n].context = context;
767 context->pt[n].tid = n;
768 context->pt[n].http_header_data = lws_malloc(context->max_http_header_data *
769 context->max_http_header_pool);
770 if (!context->pt[n].http_header_data)
773 context->pt[n].ah_pool = lws_zalloc(sizeof(struct allocated_headers) *
774 context->max_http_header_pool);
775 for (m = 0; m < context->max_http_header_pool; m++)
776 context->pt[n].ah_pool[m].data =
777 (char *)context->pt[n].http_header_data +
778 (m * context->max_http_header_data);
779 if (!context->pt[n].ah_pool)
782 lws_pt_mutex_init(&context->pt[n]);
785 if (info->fd_limit_per_thread)
786 context->fd_limit_per_thread = info->fd_limit_per_thread;
788 context->fd_limit_per_thread = context->max_fds /
789 context->count_threads;
791 lwsl_notice(" Threads: %d each %d fds\n", context->count_threads,
792 context->fd_limit_per_thread);
794 if (!info->ka_interval && info->ka_time > 0) {
795 lwsl_err("info->ka_interval can't be 0 if ka_time used\n");
800 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
801 * enable libev mediated SIGINT handling with a default handler of
802 * lws_sigint_cb. The handler can be overridden or disabled
803 * by invoking lws_sigint_cfg after creating the context, but
804 * before invoking lws_initloop:
806 context->use_ev_sigint = 1;
807 context->lws_ev_sigint_cb = &lws_ev_sigint_cb;
808 #endif /* LWS_USE_LIBEV */
810 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
811 * enable libev mediated SIGINT handling with a default handler of
812 * lws_sigint_cb. The handler can be overridden or disabled
813 * by invoking lws_sigint_cfg after creating the context, but
814 * before invoking lws_initloop:
816 context->use_ev_sigint = 1;
817 context->lws_uv_sigint_cb = &lws_uv_sigint_cb;
819 #ifdef LWS_USE_LIBEVENT
820 /* (Issue #264) In order to *avoid breaking backwards compatibility*, we
821 * enable libev mediated SIGINT handling with a default handler of
822 * lws_sigint_cb. The handler can be overridden or disabled
823 * by invoking lws_sigint_cfg after creating the context, but
824 * before invoking lws_initloop:
826 context->use_ev_sigint = 1;
827 context->lws_event_sigint_cb = &lws_event_sigint_cb;
828 #endif /* LWS_USE_LIBEVENT */
830 lwsl_info(" mem: context: %5lu bytes (%ld ctx + (%ld thr x %d))\n",
831 (long)sizeof(struct lws_context) +
832 (context->count_threads * context->pt_serv_buf_size),
833 (long)sizeof(struct lws_context),
834 (long)context->count_threads,
835 context->pt_serv_buf_size);
837 lwsl_info(" mem: http hdr rsvd: %5lu bytes (%u thr x (%u + %lu) x %u))\n",
838 (long)(context->max_http_header_data +
839 sizeof(struct allocated_headers)) *
840 context->max_http_header_pool * context->count_threads,
841 context->count_threads,
842 context->max_http_header_data,
843 (long)sizeof(struct allocated_headers),
844 context->max_http_header_pool);
845 n = sizeof(struct lws_pollfd) * context->count_threads *
846 context->fd_limit_per_thread;
847 context->pt[0].fds = lws_zalloc(n);
848 if (context->pt[0].fds == NULL) {
849 lwsl_err("OOM allocating %d fds\n", context->max_fds);
852 lwsl_info(" mem: pollfd map: %5u\n", n);
854 if (info->server_string) {
855 context->server_string = info->server_string;
856 context->server_string_len = (short)
857 strlen(context->server_string);
861 /* each thread serves his own chunk of fds */
862 for (n = 1; n < (int)info->count_threads; n++)
863 context->pt[n].fds = context->pt[n - 1].fds +
864 context->fd_limit_per_thread;
867 if (lws_plat_init(context, info))
870 lws_context_init_ssl_library(info);
872 context->user_space = info->user;
875 * if he's not saying he'll make his own vhosts later then act
876 * compatibly and make a default vhost using the data in the info
878 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
879 if (!lws_create_vhost(context, info)) {
880 lwsl_err("Failed to create default vhost\n");
884 lws_context_init_extensions(info, context);
886 lwsl_notice(" mem: per-conn: %5lu bytes + protocol rx buf\n",
887 (unsigned long)sizeof(struct lws));
889 strcpy(context->canonical_hostname, "unknown");
890 lws_server_get_canonical_hostname(context, info);
892 context->uid = info->uid;
893 context->gid = info->gid;
895 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
896 memcpy(context->caps, info->caps, sizeof(context->caps));
897 context->count_caps = info->count_caps;
901 * drop any root privs for this process
902 * to listen on port < 1023 we would have needed root, but now we are
903 * listening, we don't want the power for anything else
905 if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
906 lws_plat_drop_app_privileges(info);
909 * give all extensions a chance to create any per-context
910 * allocations they need
912 if (info->port != CONTEXT_PORT_NO_LISTEN) {
913 if (lws_ext_cb_all_exts(context, NULL,
914 LWS_EXT_CB_SERVER_CONTEXT_CONSTRUCT, NULL, 0) < 0)
917 if (lws_ext_cb_all_exts(context, NULL,
918 LWS_EXT_CB_CLIENT_CONTEXT_CONSTRUCT, NULL, 0) < 0)
924 lws_context_destroy(context);
928 LWS_VISIBLE LWS_EXTERN void
929 lws_context_deprecate(struct lws_context *context, lws_reload_func cb)
931 struct lws_vhost *vh = context->vhost_list, *vh1;
935 * "deprecation" means disable the context from accepting any new
936 * connections and free up listen sockets to be used by a replacement
939 * Otherwise the deprecated context remains operational, until its
940 * number of connected sockets falls to zero, when it is deleted.
943 /* for each vhost, close his listen socket */
948 wsi->socket_is_permanently_unusable = 1;
949 lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS);
950 wsi->context->deprecation_pending_listen_close_count++;
952 * other vhosts can share the listen port, they
953 * point to the same wsi. So zap those too.
955 vh1 = context->vhost_list;
957 if (vh1->lserv_wsi == wsi)
958 vh1->lserv_wsi = NULL;
959 vh1 = vh1->vhost_next;
965 context->deprecated = 1;
966 context->deprecation_cb = cb;
969 LWS_VISIBLE LWS_EXTERN int
970 lws_context_is_deprecated(struct lws_context *context)
972 return context->deprecated;
976 lws_context_destroy2(struct lws_context *context);
979 lws_context_destroy(struct lws_context *context)
981 const struct lws_protocols *protocol = NULL;
982 struct lws_context_per_thread *pt;
983 struct lws_vhost *vh = NULL;
988 lwsl_notice("%s: ctx %p\n", __func__, context);
991 if (context->being_destroyed1) {
992 lwsl_notice("%s: ctx %p: already being destroyed\n", __func__, context);
996 lwsl_notice("%s: ctx %p\n", __func__, context);
998 m = context->count_threads;
999 context->being_destroyed = 1;
1000 context->being_destroyed1 = 1;
1002 memset(&wsi, 0, sizeof(wsi));
1003 wsi.context = context;
1006 if (context->worst_latency_info[0])
1007 lwsl_notice("Worst latency: %s\n", context->worst_latency_info);
1011 pt = &context->pt[m];
1013 for (n = 0; (unsigned int)n < context->pt[m].fds_count; n++) {
1014 struct lws *wsi = wsi_from_fd(context, pt->fds[n].fd);
1018 lws_close_free_wsi(wsi,
1019 LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
1020 /* no protocol close */);
1023 lws_pt_mutex_destroy(pt);
1027 * give all extensions a chance to clean up any per-context
1028 * allocations they might have made
1031 n = lws_ext_cb_all_exts(context, NULL,
1032 LWS_EXT_CB_SERVER_CONTEXT_DESTRUCT, NULL, 0);
1034 n = lws_ext_cb_all_exts(context, NULL,
1035 LWS_EXT_CB_CLIENT_CONTEXT_DESTRUCT, NULL, 0);
1038 * inform all the protocols that they are done and will have no more
1041 * We can't free things until after the event loop shuts down.
1043 if (context->protocol_init_done)
1044 vh = context->vhost_list;
1047 protocol = vh->protocols;
1050 while (n < vh->count_protocols) {
1051 wsi.protocol = protocol;
1052 protocol->callback(&wsi, LWS_CALLBACK_PROTOCOL_DESTROY,
1059 vh = vh->vhost_next;
1062 for (n = 0; n < context->count_threads; n++) {
1063 pt = &context->pt[n];
1065 lws_libev_destroyloop(context, n);
1066 lws_libuv_destroyloop(context, n);
1067 lws_libevent_destroyloop(context, n);
1069 lws_free_set_NULL(context->pt[n].serv_buf);
1071 lws_free(pt->ah_pool);
1072 if (pt->http_header_data)
1073 lws_free(pt->http_header_data);
1075 lws_plat_context_early_destroy(context);
1077 if (context->pt[0].fds)
1078 lws_free_set_NULL(context->pt[0].fds);
1080 if (!LWS_LIBUV_ENABLED(context))
1081 lws_context_destroy2(context);
1085 * call the second one after the event loop has been shut down cleanly
1089 lws_context_destroy2(struct lws_context *context)
1091 const struct lws_protocols *protocol = NULL;
1092 struct lws_vhost *vh = NULL, *vh1;
1095 lwsl_notice("%s: ctx %p\n", __func__, context);
1098 * free all the per-vhost allocations
1101 vh = context->vhost_list;
1103 protocol = vh->protocols;
1106 while (n < vh->count_protocols) {
1107 if (vh->protocol_vh_privs &&
1108 vh->protocol_vh_privs[n]) {
1109 // lwsl_notice(" %s: freeing per-vhost protocol data %p\n", __func__, vh->protocol_vh_privs[n]);
1110 lws_free(vh->protocol_vh_privs[n]);
1111 vh->protocol_vh_privs[n] = NULL;
1117 if (vh->protocol_vh_privs)
1118 lws_free(vh->protocol_vh_privs);
1119 lws_ssl_SSL_CTX_destroy(vh);
1120 lws_free(vh->same_vh_protocol_list);
1121 #ifdef LWS_WITH_PLUGINS
1122 if (context->plugin_list)
1123 lws_free((void *)vh->protocols);
1125 if (vh->options & LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1126 lws_free((void *)vh->protocols);
1128 #ifdef LWS_WITH_PLUGINS
1129 #ifndef LWS_NO_EXTENSIONS
1130 if (context->plugin_extension_count)
1131 lws_free((void *)vh->extensions);
1134 #ifdef LWS_WITH_ACCESS_LOG
1135 if (vh->log_fd != (int)LWS_INVALID_FILE)
1139 vh1 = vh->vhost_next;
1144 lws_stats_log_dump(context);
1146 lws_ssl_context_destroy(context);
1147 lws_plat_context_late_destroy(context);
1149 if (context->external_baggage_free_on_destroy)
1150 free(context->external_baggage_free_on_destroy);