Lines Matching defs:context

92 	struct lws_context *context = lws_container_of(mgr, struct lws_context,
105 for (n = 0; n < context->count_threads; n++)
106 lws_system_do_attach(&context->pt[n]);
115 if (!lws_dhcpc_status(context, NULL))
123 lws_ntpc_trigger(context);
139 context->netlink &&
140 !context->nl_initial_done) {
141 lwsl_cx_info(context, "waiting for netlink coldplug");
155 ab0 = lws_system_get_blob(context, LWS_SYSBLOB_TYPE_AUTH, 0);
156 ab1 = lws_system_get_blob(context, LWS_SYSBLOB_TYPE_AUTH, 1);
159 context->pss_policies && ab0 && ab1 &&
162 lwsl_cx_info(context,
168 if (!lws_ss_sys_auth_api_amazon_com(context))
179 if (lws_system_cpd_state_get(context) == LWS_CPD_INTERNET_OK)
196 context->pss_policies && !context->policy_updated) {
198 if (context->hss_fetch_policy)
201 lwsl_cx_debug(context, "starting policy fetch");
206 if (!lws_ss_sys_fetch_policy(context))
219 if (context->protocol_init_done)
225 lwsl_cx_info(context, "doing protocol init on POLICY_VALID\n");
227 return lws_protocol_init(context);
233 struct lws_context *context = lws_container_of(sul, struct lws_context,
237 lws_state_transition_steps(&context->mgr_system,
371 struct lws_context *context = NULL;
455 /* pt fakewsi and the pt serv buf allocations ride after the context */
485 * the context object, so we can overallocate it correctly
529 * set the context event loops ops struct
602 context = lws_zalloc(size, "context");
603 if (!context || lws_fi(&info->fic, "ctx_createfail_oom_ctx")) {
605 lws_free(context);
612 // NOTE: we need to init this fields because they may be used in logger when context destroying
613 context->mgr_system.state_names = system_state_names;
614 context->mgr_system.context = context;
618 context->event_loop_ops = plev->ops;
619 context->us_wait_resolution = us_wait_resolution;
625 ci.cx = context;
629 context->trust_cache = lws_cache_create(&ci);
635 context->evlib_ctx = (uint8_t *)context + size -
639 context->evlib_plugin_list = evlib_plugin_list;
643 context->uid = info->uid;
644 context->gid = info->gid;
645 context->username = info->username;
646 context->groupname = info->groupname;
648 context->name = info->vhost_name;
650 context->log_cx = info->log_cx;
652 context->log_cx = &log_cx;
653 lwsl_refcount_cx(context->log_cx, 1);
655 context->system_ops = info->system_ops;
656 context->pt_serv_buf_size = (unsigned int)s1;
657 context->protocols_copy = info->protocols;
659 context->vh_idle_grace_ms = info->vh_idle_grace_ms ?
664 context->fic.name = "ctx";
667 * This moves all the lws_fi_t from info->fi to the context fi,
670 lws_fi_import(&context->fic, &info->fic);
675 context->smd_ttl_us = info->smd_ttl_us ? info->smd_ttl_us :
681 context->smd_queue_depth = (uint16_t)(info->smd_queue_depth ?
691 context->lcg[LWSLCG_WSI].tag_prefix = "wsi";
692 context->lcg[LWSLCG_VHOST].tag_prefix = "vh";
693 context->lcg[LWSLCG_WSI_SERVER].tag_prefix = "wsisrv"; /* adopted */
696 context->lcg[LWSLCG_WSI_MUX].tag_prefix = "mux"; /* a mux child wsi */
700 context->lcg[LWSLCG_WSI_CLIENT].tag_prefix = "wsicli";
705 context->lcg[LWSLCG_SS_CLIENT].tag_prefix = "SScli";
708 context->lcg[LWSLCG_SS_SERVER].tag_prefix = "SSsrv";
711 context->lcg[LWSLCG_WSI_SS_CLIENT].tag_prefix = "wsiSScli";
714 context->lcg[LWSLCG_WSI_SS_SERVER].tag_prefix = "wsiSSsrv";
724 context->metrics_policies = info->metrics_policies;
725 context->metrics_prefix = info->metrics_prefix;
727 context->mt_service = lws_metric_create(context,
733 context->mt_conn_dns = lws_metric_create(context,
737 context->mt_conn_tcp = lws_metric_create(context,
741 context->mt_conn_tls = lws_metric_create(context,
746 context->mt_http_txn = lws_metric_create(context,
752 context->mth_conn_failures = lws_metric_create(context,
756 context->mt_adns_cache = lws_metric_create(context,
762 context->mth_ss_conn = lws_metric_create(context, LWSMTFL_REPORT_HIST,
766 context->mt_ss_cliprox_conn = lws_metric_create(context,
769 context->mt_ss_cliprox_paylat = lws_metric_create(context,
773 context->mt_ss_proxcli_paylat = lws_metric_create(context,
782 context->mth_srv = lws_metric_create(context,
795 lwsl_cx_notice(context, "LWS: %s, MbedTLS-%s %s%s", library_version, mbedtls_version, opts_str, s);
797 lwsl_cx_notice(context, "LWS: %s, %s%s", library_version, opts_str, s);
801 lwsl_cx_info(context, "Event loop: %s", plev->ops->name);
810 context->lcg[LWSLCG_SSP_CLIENT].tag_prefix = "SSPcli";
813 context->lcg[LWSLCG_SSP_ONWARD].tag_prefix = "SSPonw";
816 context->lcg[LWSLCG_WSI_SSP_CLIENT].tag_prefix = "wsiSSPcli";
819 context->lcg[LWSLCG_WSI_SSP_ONWARD].tag_prefix = "wsiSSPonw";
826 context->pss_policies = info->pss_policies;
830 context->ss_proxy_bind = info->ss_proxy_bind;
831 context->ss_proxy_port = info->ss_proxy_port;
832 context->ss_proxy_address = info->ss_proxy_address;
833 if (context->ss_proxy_bind && context->ss_proxy_address)
834 lwsl_cx_notice(context, "ss proxy bind '%s', port %d, ads '%s'",
835 context->ss_proxy_bind, context->ss_proxy_port,
836 context->ss_proxy_address);
840 context->undestroyed_threads = count_threads;
841 context->count_threads = count_threads;
845 lwsl_cx_warn(context, "WITHOUT_EXTENSIONS but exts ptr set");
851 context->pss_policies_json = info->pss_policies_json;
854 context->pss_plugins = info->pss_plugins;
859 if (lws_plat_drop_app_privileges(context, 0) ||
860 lws_fi(&context->fic, "ctx_createfail_privdrop"))
865 context->tls_ops = &tls_ops_mbedtls;
869 context->tls_ops = &tls_ops_openssl;
874 lws_mutex_refcount_init(&context->mr);
879 context->last_free_heap = xPortGetFreeHeapSize();
881 context->last_free_heap = esp_get_free_heap_size();
888 context->fops_platform.LWS_FOP_OPEN = _lws_plat_file_open;
889 context->fops_platform.LWS_FOP_CLOSE = _lws_plat_file_close;
890 context->fops_platform.LWS_FOP_SEEK_CUR = _lws_plat_file_seek_cur;
891 context->fops_platform.LWS_FOP_READ = _lws_plat_file_read;
892 context->fops_platform.LWS_FOP_WRITE = _lws_plat_file_write;
893 context->fops_platform.fi[0].sig = NULL;
896 * arrange a linear linked-list of fops starting from context->fops
899 * [ -> fops_zip (copied into context so .next settable) ]
903 context->fops = &context->fops_platform;
904 prev = (struct lws_plat_file_ops *)context->fops;
908 context->fops_zip = fops_zip;
909 prev->next = &context->fops_zip;
919 context->reject_service_keywords = info->reject_service_keywords;
922 context->external_baggage_free_on_destroy =
925 context->time_up = lws_now_usecs();
927 context->pcontext_finalize = info->pcontext;
930 context->simultaneous_ssl_restriction =
932 context->simultaneous_ssl_handshake_restriction =
936 context->options = info->options;
955 context->started_with_parent = pid_daemon;
956 lwsl_cx_info(context, " Started with daemon pid %u",
963 lwsl_cx_err(context, "Get RLIMIT_NOFILE failed!");
967 context->max_fds = (unsigned int)rt.rlim_cur;
970 context->max_fds = getdtablesize();
975 context->max_fds = 2560;
978 lwsl_cx_warn(context, "unreasonable ulimit -n workaround");
981 context->max_fds = (unsigned int)l;
984 if ((int)context->max_fds < 0 ||
985 lws_fi(&context->fic, "ctx_createfail_maxfds")) {
986 lwsl_cx_err(context, "problem getting process max files");
998 unsigned int mf = lpf * context->count_threads;
1000 if (mf < context->max_fds) {
1001 context->max_fds_unrelated_to_ulimit = 1;
1002 context->max_fds = mf;
1007 context->token_limits = info->token_limits;
1012 time(&context->tls.last_cert_check_s);
1014 context->tls.alpn_default = info->alpn;
1016 char *p = context->tls.alpn_discovered, first = 1;
1023 (context->tls.alpn_discovered +
1024 sizeof(context->tls.alpn_discovered) -
1030 context->tls.alpn_default = context->tls.alpn_discovered;
1036 context->timeout_secs = info->timeout_secs;
1039 context->timeout_secs = 15;
1043 context->max_http_header_data = info->max_http_header_data;
1046 context->max_http_header_data =
1049 context->max_http_header_data = LWS_DEF_HEADER_LEN;
1052 context->max_http_header_pool = info->max_http_header_pool;
1055 context->max_http_header_pool =
1058 context->max_http_header_pool = context->max_fds;
1062 context->fd_limit_per_thread = lpf;
1064 if (context->count_threads)
1065 context->fd_limit_per_thread = context->max_fds /
1066 context->count_threads;
1069 lws_mutex_init(context->smd.lock_messages);
1070 lws_mutex_init(context->smd.lock_peers);
1074 if (!lws_smd_register(context, context, 0, LWSSMDCL_NETWORK,
1076 lwsl_cx_err(context, "early smd register failed");
1082 !lws_smd_register(context, info->early_smd_opaque, 0,
1085 lwsl_cx_err(context, "early smd register failed");
1092 context->default_retry.retry_ms_table = default_backoff_table;
1093 context->default_retry.conceal_count =
1094 context->default_retry.retry_ms_table_count =
1096 context->default_retry.jitter_percent = 20;
1097 context->default_retry.secs_since_valid_ping = 300;
1098 context->default_retry.secs_since_valid_hangup = 310;
1102 context->default_retry.secs_since_valid_ping =
1104 context->default_retry.secs_since_valid_hangup =
1112 u = (uint8_t *)&context[1];
1113 for (n = 0; n < context->count_threads; n++) {
1114 context->pt[n].serv_buf = u;
1115 u += context->pt_serv_buf_size;
1117 context->pt[n].context = context;
1118 context->pt[n].tid = (uint8_t)n;
1126 * context.
1128 context->pt[n].fake_wsi = (struct lws *)u;
1131 memset(context->pt[n].fake_wsi, 0, sizeof(struct lws));
1134 context->pt[n].evlib_pt = u;
1138 context->pt[n].http.ah_list = NULL;
1139 context->pt[n].http.ah_pool_length = 0;
1141 lws_pt_mutex_init(&context->pt[n]);
1143 lws_seq_pt_init(&context->pt[n]);
1149 pt_init_destroy(context, info,
1150 &context->pt[n], 0);
1155 lwsl_cx_err(context, "info->ka_interval can't be 0 if ka_time used");
1165 context->pl_hash_elements =
1166 (context->count_threads * context->fd_limit_per_thread) / 16;
1167 context->pl_hash_table = lws_zalloc(sizeof(struct lws_peer *) *
1168 context->pl_hash_elements, "peer limits hash table");
1170 context->ip_limit_ah = info->ip_limit_ah;
1171 context->ip_limit_wsi = info->ip_limit_wsi;
1172 context->pl_notify_cb = info->pl_notify_cb;
1179 n = (int)(sizeof(struct lws_pollfd) * context->count_threads *
1180 context->fd_limit_per_thread);
1181 context->pt[0].fds = lws_zalloc((unsigned int)n, "fds table");
1182 if (context->pt[0].fds == NULL ||
1183 lws_fi(&context->fic, "ctx_createfail_oom_fds")) {
1185 lws_free(context->pt[0].fds);
1187 lwsl_cx_err(context, "OOM allocating %d fds\n", context->max_fds);
1192 lwsl_cx_info(context, "ctx: %5luB (%ld ctx + pt(%ld thr x %d)), "
1195 (context->count_threads * context->pt_serv_buf_size),
1197 (long)context->count_threads,
1198 context->pt_serv_buf_size,
1199 context->fd_limit_per_thread, n);
1202 lwsl_cx_info(context, " http: ah_data: %u, ah: %lu, max count %u",
1203 context->max_http_header_data,
1205 context->max_http_header_pool);
1210 context->server_string = info->server_string;
1211 context->server_string_len = (short)
1212 strlen(context->server_string);
1218 for (n = 1; n < (int)context->count_threads; n++)
1219 context->pt[n].fds = context->pt[n - 1].fds +
1220 context->fd_limit_per_thread;
1229 if (lws_plat_init(context, info) ||
1230 lws_fi(&context->fic, "ctx_createfail_plat_init"))
1235 if (lws_fi(&context->fic, "ctx_createfail_evlib_init"))
1238 if (context->event_loop_ops->init_context)
1239 if (context->event_loop_ops->init_context(context, info))
1242 if (lws_fi(&context->fic, "ctx_createfail_evlib_pt"))
1245 if (context->event_loop_ops->init_pt)
1246 for (n = 0; n < context->count_threads; n++) {
1252 if (context->event_loop_ops->init_pt(context, lp, n))
1256 lws_context_lock(context, __func__);
1257 n = __lws_create_event_pipes(context);
1258 lws_context_unlock(context);
1262 for (n = 0; n < context->count_threads; n++) {
1266 pt_init_destroy(context, info,
1267 &context->pt[n], 0);
1272 lws_context_init_ssl_library(context, info);
1274 context->user_space = info->user;
1277 strcpy(context->canonical_hostname, "unknown");
1279 lws_server_get_canonical_hostname(context, info);
1284 memcpy(context->caps, info->caps, sizeof(context->caps));
1285 context->count_caps = info->count_caps;
1328 if (lws_fi(&context->fic, "ctx_createfail_sys_vh"))
1331 vh = lws_create_vhost(context, &ii);
1333 lwsl_cx_err(context, "failed to create system vhost");
1337 context->vhost_system = vh;
1340 lws_fi(&context->fic, "ctx_createfail_sys_vh_init")) {
1341 lwsl_cx_err(context, "failed to init system vhost");
1345 lws_async_dns_init(context);
1357 context->mgr_system.name = "system";
1358 context->mgr_system.state = LWS_SYSTATE_CONTEXT_CREATED;
1359 context->mgr_system.parent = context;
1361 context->mgr_system.smd_class = LWSSMDCL_SYSTEM_STATE;
1364 context->protocols_notify.name = "prot_init";
1365 context->protocols_notify.notify_cb = lws_state_notify_protocol_init;
1367 lws_state_reg_notifier(&context->mgr_system, &context->protocols_notify);
1372 * reaching 'operational', before we returned from context creation.
1375 lws_state_reg_notifier_list(&context->mgr_system,
1384 if (!lws_create_vhost(context, info) ||
1385 lws_fi(&context->fic, "ctx_createfail_def_vh")) {
1386 lwsl_cx_err(context, "Failed to create default vhost");
1389 lws_free_set_NULL(context->pl_hash_table);
1399 ci.cx = context;
1404 context->nsc = lws_cache_create(&ci);
1405 if (!context->nsc)
1410 ci.parent = context->nsc;
1415 context->l1 = lws_cache_create(&ci);
1416 if (!context->l1) {
1417 lwsl_cx_err(context, "Failed to init cookiejar");
1426 if (context->pss_policies_json) {
1428 * You must create your context with the explicit vhosts flag
1434 if (lws_ss_policy_parse_begin(context, 0) ||
1435 lws_fi(&context->fic, "ctx_createfail_ss_pol1")) {
1437 lws_ss_policy_parse_abandon(context);
1442 n = lws_ss_policy_parse(context,
1443 (uint8_t *)context->pss_policies_json,
1444 strlen(context->pss_policies_json));
1446 lws_fi(&context->fic, "ctx_createfail_ss_pol2")) {
1447 lws_ss_policy_parse_abandon(context);
1451 if (lws_ss_policy_set(context, "hardcoded") ||
1452 lws_fi(&context->fic, "ctx_createfail_ss_pol3")) {
1453 lwsl_cx_err(context, "policy set failed");
1458 if (context->pss_policies) {
1461 if (lws_ss_policy_set(context, "hardcoded") ||
1462 lws_fi(&context->fic, "ctx_createfail_ss_pol3")) {
1463 lwsl_cx_err(context, "policy set failed");
1470 lws_context_init_extensions(info, context);
1472 lwsl_cx_info(context, " mem: per-conn: %5lu bytes + protocol rx buf",
1481 if (lws_plat_drop_app_privileges(context, 1) ||
1482 lws_fi(&context->fic, "ctx_createfail_privdrop"))
1489 * code that called us can set its copy of context, which it may be
1495 lws_sul_schedule(context, 0, &context->sul_system_state,
1499 /* expedite post-context init (eg, protocols) */
1500 lws_cancel_service(context);
1503 return context;
1513 lws_context_destroy(context);
1519 lws_context_destroy(context);
1521 return fatal_exit_defer ? context : NULL;
1528 if (context)
1529 lwsl_cx_err(context, "Requested event library support not configured");
1534 if (context) {
1536 _lws_smd_destroy(context);
1541 if (context) {
1543 lws_metrics_destroy(context);
1545 lws_fi_destroy(&context->fic);
1548 if (context) {
1549 lwsl_refcount_cx(context->log_cx, -1);
1550 lws_free(context);
1639 * When using an event loop, the context destruction is in three separate
1658 * destroys the context itself, setting what was info.pcontext to NULL.
1669 lws_ctx_t ctx = pt->context;
1699 wsi.a.context = pt->context;
1744 * - mark context as starting destroy process
1755 * - if all pt down, call into evlib to advance context destroy
1762 * - destroy and free the actual context
1766 lws_context_destroy(struct lws_context *context)
1779 if (!context || context->inside_context_destroy)
1782 pcontext_finalize = context->pcontext_finalize;
1784 lws_context_lock(context, __func__);
1785 context->inside_context_destroy = 1;
1787 lwsl_cx_info(context, "destroy_state %d", context->destroy_state);
1789 switch (context->destroy_state) {
1795 lwsl_cx_info(context, "starting context destroy flow");
1796 context->being_destroyed = 1;
1809 if (context->protocol_init_done)
1810 vh = context->vhost_list;
1820 lws_plat_context_early_destroy(context);
1822 context->service_no_longer_possible = 1;
1823 context->requested_stop_internal_loops = 1;
1841 pt = context->pt;
1842 for (n = 0; n < context->count_threads; n++) {
1859 struct lws *wsi = wsi_from_fd(context,
1864 lwsl_cx_debug(context,
1881 pt_init_destroy(context, NULL,
1890 if (context->event_loop_ops->destroy_pt) {
1891 lwsl_cx_info(context,
1893 context->event_loop_ops->destroy_pt(context, n);
1903 context->destroy_state = LWSCD_PT_WAS_DEFERRED;
1904 lwsl_cx_notice(context, "destroy from inside service");
1905 lws_cancel_service(context);
1909 context->destroy_state = LWSCD_PT_WAIT_ALL_DESTROYED;
1931 if (context->event_loop_ops->destroy_context1) {
1932 lwsl_cx_info(context, "do evlib destroy_context1 and wait");
1933 context->event_loop_ops->destroy_context1(context);
1943 lwsl_cx_info(context, "manually destroying pts");
1945 pt = context->pt;
1946 for (n = 0; n < context->count_threads; n++, pt++) {
1957 for (n = 0; n < context->count_threads; n++)
1958 if (!context->pt[n].is_destroyed &&
1959 !context->pt[n].event_loop_pt_unused)
1962 lwsl_cx_info(context, "PT_WAIT_ALL_DESTROYED: %d alive", alive);
1972 * again at the original app thread and do the context
1979 if (context->event_loop_ops->destroy_context2)
1985 context->event_loop_ops->destroy_context2(context);
1986 context->requested_stop_internal_loops = 1;
1995 lws_state_transition(&context->mgr_system, LWS_SYSTATE_POLICY_INVALID);
2003 vh = context->vhost_list;
2013 while (context->vhost_pending_destruction_list)
2015 __lws_vhost_destroy2(context->vhost_pending_destruction_list);
2019 lws_ssl_context_destroy(context);
2021 lws_plat_context_late_destroy(context);
2024 if (context->pl_hash_table)
2025 for (nu = 0; nu < context->pl_hash_elements; nu++) {
2026 if (!context->pl_hash_table[nu])
2029 context->pl_hash_table[nu]) {
2036 lws_free(context->pl_hash_table);
2041 for (n = 0; n < context->count_threads; n++) {
2042 struct lws_context_per_thread *pt = &context->pt[n];
2051 pt_init_destroy(context, NULL, pt, 1);
2057 pt_init_destroy(context, NULL,
2065 lwsl_cx_info(context, "pt destroy %d", n);
2070 context->destroy_state = LWSCD_FINALIZATION;
2074 if (context->pt[0].event_loop_foreign &&
2075 context->event_loop_ops->destroy_context1) {
2077 lwsl_cx_info(context,
2078 "leaving final context destruction"
2083 if (context->event_loop_ops->destroy_context1 &&
2084 !context->pt[0].event_loop_foreign) {
2085 lwsl_cx_notice(context, "waiting for internal loop exit");
2095 lws_metrics_dump(context);
2098 context->evlib_finalize_destroy_after_int_loops_stop = 1;
2101 if (context->event_loop_ops->destroy_context2)
2102 context->event_loop_ops->destroy_context2(context);
2104 lws_state_transition_steps(&context->mgr_system,
2111 for (n = 0; n < context->count_threads; n++) {
2112 struct lws_context_per_thread *pt = &context->pt[n];
2121 pt_init_destroy(context, NULL, pt, 1);
2126 pt_init_destroy(context, NULL, pt, 1);
2134 lwsl_cx_info(context, "pt %d fully destroyed",
2135 (int)(pt - pt->context->pt));
2141 * clean up the context and things hanging off it
2145 lws_cache_destroy(&context->trust_cache);
2146 lws_tls_jit_trust_inflight_destroy_all(context);
2150 lws_cache_destroy(&context->nsc);
2151 lws_cache_destroy(&context->l1);
2155 _lws_smd_destroy(context);
2159 lws_async_dns_deinit(&context->async_dns);
2162 lws_dhcpc_remove(context, NULL);
2165 if (context->pt[0].fds)
2166 lws_free_set_NULL(context->pt[0].fds);
2168 lws_context_deinit_ssl_library(context);
2171 if (context->latencies_fd != -1)
2172 compatible_close(context->latencies_fd);
2177 lws_system_get_blob(context, (lws_system_blob_item_t)n, 0));
2182 while (context->server_der_list) {
2183 struct lws_ss_x509 *x = context->server_der_list;
2185 context->server_der_list = x->next;
2189 if (context->ac_policy)
2190 lwsac_free(&context->ac_policy);
2197 lws_context_unlock(context);
2200 lws_mutex_refcount_destroy(&context->mr);
2204 lws_metrics_destroy(context);
2207 if (context->external_baggage_free_on_destroy)
2208 free(context->external_baggage_free_on_destroy);
2212 context->last_free_heap = xPortGetFreeHeapSize();
2214 context->last_free_heap = esp_get_free_heap_size();
2219 if (context->evlib_plugin_list)
2220 lws_plugins_destroy(&context->evlib_plugin_list,
2225 lws_fi_destroy(&context->fic);
2228 lwsl_refcount_cx(context->log_cx, -1);
2230 lws_free(context);
2241 lwsl_cx_info(context, "leaving");
2242 context->inside_context_destroy = 0;
2243 lws_context_unlock(context);
2247 lws_context_is_being_destroyed(struct lws_context *context)
2249 return !!context->being_destroyed;
2257 return mgr->context;