Coverage Report

Created: 2023-03-26 07:20

/src/libwebsockets/lib/core/context.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to
8
 * deal in the Software without restriction, including without limitation the
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
 * sell copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
 * IN THE SOFTWARE.
23
 */
24
25
#include "private-lib-core.h"
26
27
#ifndef LWS_BUILD_HASH
28
#define LWS_BUILD_HASH "unknown-build-hash"
29
#endif
30
31
static const char *library_version = LWS_LIBRARY_VERSION;
32
33
#if defined(LWS_WITH_MBEDTLS)
34
extern const char *mbedtls_client_preload_filepath;
35
#endif
36
37
#if defined(LWS_HAVE_SYS_RESOURCE_H)
38
/* for setrlimit */
39
#include <sys/resource.h>
40
#endif
41
42
#if defined(LWS_WITH_NETWORK)
43
/* in ms */
44
static uint32_t default_backoff_table[] = { 1000, 3000, 9000, 17000 };
45
#endif
46
47
/**
48
 * lws_get_library_version: get version and git hash library built from
49
 *
50
 *  returns a const char * to a string like "1.1 178d78c"
51
 *  representing the library version followed by the git head hash it
52
 *  was built from
53
 */
54
const char *
55
lws_get_library_version(void)
56
0
{
57
0
  return library_version;
58
0
}
59
60
#if defined(LWS_WITH_NETWORK)
61
62
#if defined(LWS_WITH_SYS_STATE)
63
64
static const char * system_state_names[] = {
65
  "undef",
66
  "CONTEXT_CREATED",
67
  "INITIALIZED",
68
  "IFACE_COLDPLUG",
69
  "DHCP",
70
  "CPD_PRE_TIME",
71
  "TIME_VALID",
72
  "CPD_POST_TIME",
73
  "POLICY_VALID",
74
  "REGISTERED",
75
  "AUTH1",
76
  "AUTH2",
77
  "ONE_TIME_UPDATES",
78
  "OPERATIONAL",
79
  "POLICY_INVALID",
80
  "DESTROYING",
81
  "AWAITING_MODAL_UPDATING",
82
  "MODAL_UPDATING"
83
};
84
85
86
/*
87
 * Handle provoking protocol init when we pass through the right system state
88
 */
89
90
static int
91
lws_state_notify_protocol_init(struct lws_state_manager *mgr,
92
             struct lws_state_notify_link *link, int current,
93
             int target)
94
0
{
95
0
  struct lws_context *context = lws_container_of(mgr, struct lws_context,
96
0
                   mgr_system);
97
0
#if defined(LWS_WITH_SECURE_STREAMS) && \
98
0
    defined(LWS_WITH_SECURE_STREAMS_SYS_AUTH_API_AMAZON_COM)
99
0
  lws_system_blob_t *ab0, *ab1;
100
0
#endif
101
0
  int n;
102
103
  /*
104
   * Deal with any attachments that were waiting for the right state
105
   * to come along
106
   */
107
108
0
  for (n = 0; n < context->count_threads; n++)
109
0
    lws_system_do_attach(&context->pt[n]);
110
111
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
112
  if (target == LWS_SYSTATE_DHCP) {
113
    /*
114
     * Don't let it past here until at least one iface has been
115
     * configured for operation with DHCP
116
     */
117
118
    if (!lws_dhcpc_status(context, NULL))
119
      return 1;
120
  }
121
#endif
122
123
#if defined(LWS_WITH_SYS_NTPCLIENT)
124
  if (target == LWS_SYSTATE_TIME_VALID &&
125
      lws_now_secs() < 1594017754) /* 06:42 Mon Jul 6 2020 UTC */ {
126
    lws_ntpc_trigger(context);
127
128
    return 1;
129
  }
130
#endif
131
132
#if defined(LWS_WITH_OTA)
133
  if (target == LWS_SYSTATE_OPERATIONAL) {
134
    uint16_t b;
135
136
    /*
137
     * We add jitter, so possibly large numbers of devices don't
138
     * all wake up and check for updates at the same moment after a
139
     * power outage
140
     */
141
142
    lws_get_random(context, &b, 2);
143
    lws_sul_schedule(context, 0, &context->sul_ota_periodic,
144
         lws_ota_periodic_cb, (/* 30 + */ (b % 1000) *
145
              LWS_US_PER_MS));
146
  }
147
#endif
148
149
0
#if defined(LWS_WITH_NETLINK)
150
  /*
151
   * If we're going to use netlink routing data for DNS, we have to
152
   * wait to collect it asynchronously from the platform first.  Netlink
153
   * role init starts a ctx sul for 350ms (reset to 100ms each time some
154
   * new netlink data comes) that sets nl_initial_done and tries to move
155
   * us to OPERATIONAL
156
   */
157
158
0
  if (target == LWS_SYSTATE_IFACE_COLDPLUG &&
159
0
      context->netlink &&
160
0
      !context->nl_initial_done) {
161
0
    lwsl_cx_info(context, "waiting for netlink coldplug");
162
163
0
    return 1;
164
0
  }
165
0
#endif
166
167
0
#if defined(LWS_WITH_SECURE_STREAMS) && \
168
0
    defined(LWS_WITH_SECURE_STREAMS_SYS_AUTH_API_AMAZON_COM)
169
  /*
170
   * Skip this if we are running something without the policy for it
171
   *
172
   * If root token is empty, skip too.
173
   */
174
175
0
  ab0 = lws_system_get_blob(context, LWS_SYSBLOB_TYPE_AUTH, 0);
176
0
  ab1 = lws_system_get_blob(context, LWS_SYSBLOB_TYPE_AUTH, 1);
177
178
0
  if (target == LWS_SYSTATE_AUTH1 &&
179
0
      context->pss_policies && ab0 && ab1 &&
180
0
      !lws_system_blob_get_size(ab0) &&
181
0
      lws_system_blob_get_size(ab1)) {
182
0
    lwsl_cx_info(context,
183
0
           "AUTH1 state triggering api.amazon.com auth");
184
    /*
185
     * Start trying to acquire it if it's not already in progress
186
     * returns nonzero if we determine it's not needed
187
     */
188
0
    if (!lws_ss_sys_auth_api_amazon_com(context))
189
0
      return 1;
190
0
  }
191
0
#endif
192
193
0
#if defined(LWS_WITH_SECURE_STREAMS)
194
#if defined(LWS_WITH_DRIVERS)
195
  /*
196
   * See if we should do the SS Captive Portal Detection
197
   */
198
  if (target == LWS_SYSTATE_CPD_PRE_TIME) {
199
    if (lws_system_cpd_state_get(context) == LWS_CPD_INTERNET_OK)
200
      return 0; /* allow it */
201
202
    /*
203
     * Don't allow it to move past here until we get an IP and
204
     * CPD passes, driven by SMD
205
     */
206
207
    return 1;
208
  }
209
#endif
210
211
0
#if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
212
  /*
213
   * Skip this if we are running something without the policy for it
214
   */
215
0
  if (target == LWS_SYSTATE_POLICY_VALID &&
216
0
      context->pss_policies && !context->policy_updated) {
217
218
0
    if (context->hss_fetch_policy)
219
0
      return 1;
220
221
0
    lwsl_cx_debug(context, "starting policy fetch");
222
    /*
223
     * Start trying to acquire it if it's not already in progress
224
     * returns nonzero if we determine it's not needed
225
     */
226
0
    if (!lws_ss_sys_fetch_policy(context))
227
      /* we have it */
228
0
      return 0;
229
230
    /* deny while we fetch it */
231
232
0
    return 1;
233
0
  }
234
0
#endif
235
0
#endif
236
237
  /* protocol part */
238
239
0
  if (context->protocol_init_done)
240
0
    return 0;
241
242
0
  if (target != LWS_SYSTATE_POLICY_VALID)
243
0
    return 0;
244
245
0
  lwsl_cx_info(context, "doing protocol init on POLICY_VALID\n");
246
247
0
  return lws_protocol_init(context);
248
0
}
249
250
static void
251
lws_context_creation_completion_cb(lws_sorted_usec_list_t *sul)
252
0
{
253
0
  struct lws_context *context = lws_container_of(sul, struct lws_context,
254
0
                   sul_system_state);
255
256
  /* if nothing is there to intercept anything, go all the way */
257
0
  lws_state_transition_steps(&context->mgr_system,
258
0
           LWS_SYSTATE_OPERATIONAL);
259
0
}
260
#endif /* WITH_SYS_STATE */
261
262
#if defined(LWS_WITH_SYS_SMD)
263
static int
264
lws_system_smd_cb(void *opaque, lws_smd_class_t _class, lws_usec_t timestamp,
265
      void *buf, size_t len)
266
0
{
267
0
  struct lws_context *cx = (struct lws_context *)opaque;
268
269
0
  if (_class != LWSSMDCL_NETWORK)
270
0
    return 0;
271
272
  /* something external requested CPD check */
273
274
0
  if (!lws_json_simple_strcmp(buf, len, "\"trigger\":", "cpdcheck"))
275
0
    lws_system_cpd_start(cx);
276
0
  else
277
    /*
278
     * IP acquisition on any interface triggers captive portal
279
     * check on default route
280
     */
281
0
    if (!lws_json_simple_strcmp(buf, len, "\"type\":", "ipacq"))
282
0
      lws_system_cpd_start(cx);
283
284
#if defined(LWS_WITH_SYS_NTPCLIENT)
285
  /*
286
   * Captive portal detect showing internet workable triggers NTP Client
287
   */
288
  if (!lws_json_simple_strcmp(buf, len, "\"type\":", "cps") &&
289
      !lws_json_simple_strcmp(buf, len, "\"result\":", "OK") &&
290
      lws_now_secs() < 1594017754) /* 06:42 Mon Jul 6 2020 UTC */
291
    lws_ntpc_trigger(cx);
292
#endif
293
294
#if defined(LWS_WITH_SYS_DHCP_CLIENT) && 0
295
  /*
296
   * Any network interface linkup triggers DHCP
297
   */
298
  if (!lws_json_simple_strcmp(buf, len, "\"type\":", "linkup"))
299
    lws_ntpc_trigger(cx);
300
301
#endif
302
303
#if defined(LWS_WITH_DRIVERS) && defined(LWS_WITH_NETWORK)
304
  lws_netdev_smd_cb(opaque, _class, timestamp, buf, len);
305
#endif
306
307
0
  return 0;
308
0
}
309
#endif
310
311
312
313
#endif /* NETWORK */
314
315
#if !defined(LWS_WITH_NO_LOGS)
316
317
static const char * const opts_str =
318
#if defined(LWS_WITH_NETWORK)
319
      "NET "
320
#else
321
      "NoNET "
322
#endif
323
#if defined(LWS_WITH_CLIENT)
324
      "CLI "
325
#endif
326
#if defined(LWS_WITH_SERVER)
327
      "SRV "
328
#endif
329
#if defined(LWS_ROLE_H1)
330
      "H1 "
331
#endif
332
#if defined(LWS_ROLE_H2)
333
      "H2 "
334
#endif
335
#if defined(LWS_ROLE_WS)
336
      "WS "
337
#endif
338
#if defined(LWS_ROLE_MQTT)
339
      "MQTT "
340
#endif
341
#if defined(LWS_WITH_SECURE_STREAMS) && !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
342
      "SS-JSON-POL "
343
#endif
344
#if defined(LWS_WITH_SECURE_STREAMS) && defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
345
      "SS-STATIC-POL "
346
#endif
347
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
348
      "SSPROX "
349
#endif
350
351
#if defined(LWS_WITH_MBEDTLS)
352
      "MbedTLS "
353
#endif
354
#if defined(LWS_WITH_CONMON)
355
      "ConMon "
356
#endif
357
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
358
      "FLTINJ "
359
#endif
360
#if defined(LWS_WITH_SYS_ASYNC_DNS)
361
      "ASYNC_DNS "
362
#endif
363
#if defined(LWS_WITH_SYS_NTPCLIENT)
364
      "NTPCLIENT "
365
#endif
366
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
367
      "DHCP_CLIENT "
368
#endif
369
;
370
371
#endif
372
373
#if defined(LWS_WITH_EVLIB_PLUGINS) && defined(LWS_WITH_EVENT_LIBS)
374
static const struct lws_evlib_map {
375
  uint64_t  flag;
376
  const char  *name;
377
} map[] = {
378
  { LWS_SERVER_OPTION_LIBUV,    "evlib_uv" },
379
  { LWS_SERVER_OPTION_LIBEVENT, "evlib_event" },
380
  { LWS_SERVER_OPTION_GLIB,     "evlib_glib" },
381
  { LWS_SERVER_OPTION_LIBEV,    "evlib_ev" },
382
  { LWS_SERVER_OPTION_SDEVENT,  "evlib_sd" },
383
  { LWS_SERVER_OPTION_ULOOP,    "evlib_uloop" },
384
};
385
static const char * const dlist[] = {
386
  ".",        /* Priority 1: plugins in cwd */
387
  LWS_INSTALL_LIBDIR,   /* Priority 2: plugins in install dir */
388
  NULL
389
};
390
#endif
391
392
struct lws_context *
393
lws_create_context(const struct lws_context_creation_info *info)
394
0
{
395
0
  struct lws_context *context = NULL;
396
0
#if !defined(LWS_WITH_NO_LOGS)
397
0
  const char *s = "IPv6-absent";
398
0
#endif
399
0
#if defined(LWS_WITH_FILE_OPS)
400
0
  struct lws_plat_file_ops *prev;
401
0
#endif
402
#ifndef LWS_NO_DAEMONIZE
403
  pid_t pid_daemon = get_daemonize_pid();
404
#endif
405
0
#if defined(LWS_WITH_NETWORK)
406
0
  const lws_plugin_evlib_t *plev = NULL;
407
0
  unsigned short count_threads = 1;
408
0
  uint8_t *u;
409
0
  uint16_t us_wait_resolution = 0;
410
0
#if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
411
0
  struct lws_cache_creation_info ci;
412
0
#endif
413
414
#if defined(__ANDROID__)
415
  struct rlimit rt;
416
#endif
417
0
  size_t
418
#if defined(LWS_PLAT_FREERTOS)
419
    /* smaller default, can set in info->pt_serv_buf_size */
420
    s1 = 2048,
421
#else
422
0
    s1 = 4096,
423
0
#endif
424
0
    size = sizeof(struct lws_context);
425
0
#endif
426
0
#if !defined(LWS_PLAT_BAREMETAL) && defined(LWS_WITH_NETWORK)
427
0
  int n;
428
0
#endif
429
0
  unsigned int lpf = info->fd_limit_per_thread;
430
#if defined(LWS_WITH_EVLIB_PLUGINS) && defined(LWS_WITH_EVENT_LIBS)
431
  struct lws_plugin   *evlib_plugin_list = NULL;
432
#if defined(_DEBUG) && !defined(LWS_WITH_NO_LOGS)
433
  char    *ld_env;
434
#endif
435
#endif
436
#if defined(LWS_WITH_LIBUV)
437
  char fatal_exit_defer = 0;
438
#endif
439
440
441
0
  if (lws_fi(&info->fic, "ctx_createfail1"))
442
0
    goto early_bail;
443
444
0
  if (lpf) {
445
0
    lpf+= 2;
446
#if defined(LWS_WITH_SYS_ASYNC_DNS)
447
    lpf++;
448
#endif
449
#if defined(LWS_WITH_SYS_NTPCLIENT)
450
    lpf++;
451
#endif
452
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
453
    lpf++;
454
#endif
455
0
  }
456
457
#if defined(LWS_WITH_IPV6) && !defined(LWS_WITH_NO_LOGS)
458
  if (!lws_check_opt(info->options, LWS_SERVER_OPTION_DISABLE_IPV6))
459
    s = "IPV6-on";
460
  else
461
    s = "IPV6-off";
462
#endif
463
464
0
  if (lws_plat_context_early_init())
465
0
    goto early_bail;
466
467
0
#if defined(LWS_WITH_NETWORK)
468
0
  if (info->count_threads)
469
0
    count_threads = (unsigned short)info->count_threads;
470
471
0
  if (count_threads > LWS_MAX_SMP)
472
0
    count_threads = LWS_MAX_SMP;
473
474
0
  if (info->pt_serv_buf_size)
475
0
    s1 = info->pt_serv_buf_size;
476
477
  /* pt fakewsi and the pt serv buf allocations ride after the context */
478
0
  size += count_threads * s1;
479
0
#if !defined(LWS_PLAT_FREERTOS)
480
0
  size += (count_threads * sizeof(struct lws));
481
0
#endif
482
483
0
  if (info->event_lib_custom) {
484
0
    plev = info->event_lib_custom;
485
0
    us_wait_resolution = 0;
486
0
  }
487
0
#if defined(LWS_WITH_POLL)
488
0
  else {
489
0
    extern const lws_plugin_evlib_t evlib_poll;
490
0
    plev = &evlib_poll;
491
0
#if !defined(LWS_PLAT_FREERTOS)
492
    /*
493
     * ... freertos has us-resolution select()...
494
     * others are to ms-resolution poll()
495
     */
496
0
    us_wait_resolution = 1000;
497
0
#endif
498
0
  }
499
0
#endif
500
501
#if defined(LWS_WITH_EVLIB_PLUGINS) && defined(LWS_WITH_EVENT_LIBS)
502
503
  /*
504
   * New style dynamically loaded event lib support
505
   *
506
   * We have to pick and load the event lib plugin before we allocate
507
   * the context object, so we can overallocate it correctly
508
   */
509
510
#if defined(_DEBUG) && !defined(LWS_WITH_NO_LOGS)
511
  ld_env = getenv("LD_LIBRARY_PATH");
512
  lwsl_info("%s: ev lib path %s, '%s'\n", __func__,
513
      LWS_INSTALL_LIBDIR, ld_env);
514
#endif
515
516
  for (n = 0; n < (int)LWS_ARRAY_SIZE(map); n++) {
517
    char ok = 0;
518
519
    if (!lws_check_opt(info->options, map[n].flag))
520
      continue;
521
522
    if (!lws_plugins_init(&evlib_plugin_list,
523
             dlist, "lws_evlib_plugin",
524
             map[n].name, NULL, NULL))
525
      ok = 1;
526
527
    if (!ok || lws_fi(&info->fic, "ctx_createfail_plugin_init")) {
528
      lwsl_err("%s: failed to load %s\n", __func__,
529
          map[n].name);
530
      goto bail;
531
    }
532
533
#if defined(LWS_WITH_LIBUV)
534
    if (!n) /* libuv */
535
      fatal_exit_defer = !!info->foreign_loops;
536
#endif
537
538
    if (!evlib_plugin_list ||
539
        lws_fi(&info->fic, "ctx_createfail_evlib_plugin")) {
540
      lwsl_err("%s: unable to load evlib plugin %s\n",
541
          __func__, map[n].name);
542
543
      goto bail;
544
    }
545
    plev = (const lws_plugin_evlib_t *)evlib_plugin_list->hdr;
546
    break;
547
  }
548
#else
549
#if defined(LWS_WITH_EVENT_LIBS)
550
  /*
551
   * set the context event loops ops struct
552
   *
553
   * after this, all event_loop actions use the generic ops
554
   */
555
556
  /*
557
   * oldstyle built-in event lib support
558
   *
559
   * We have composed them into the libwebsockets lib itself, we can
560
   * just pick the ops we want and done
561
   */
562
563
#if defined(LWS_WITH_LIBUV)
564
  if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBUV)) {
565
    extern const lws_plugin_evlib_t evlib_uv;
566
    plev = &evlib_uv;
567
    fatal_exit_defer = !!info->foreign_loops;
568
    us_wait_resolution = 0;
569
  }
570
#endif
571
572
#if defined(LWS_WITH_LIBEVENT)
573
  if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEVENT)) {
574
    extern const lws_plugin_evlib_t evlib_event;
575
    plev = &evlib_event;
576
    us_wait_resolution = 0;
577
  }
578
#endif
579
580
#if defined(LWS_WITH_GLIB)
581
  if (lws_check_opt(info->options, LWS_SERVER_OPTION_GLIB)) {
582
    extern const lws_plugin_evlib_t evlib_glib;
583
    plev = &evlib_glib;
584
    us_wait_resolution = 0;
585
  }
586
#endif
587
588
#if defined(LWS_WITH_LIBEV)
589
  if (lws_check_opt(info->options, LWS_SERVER_OPTION_LIBEV)) {
590
    extern const lws_plugin_evlib_t evlib_ev;
591
    plev = &evlib_ev;
592
    us_wait_resolution = 0;
593
  }
594
#endif
595
596
#if defined(LWS_WITH_SDEVENT)
597
    if (lws_check_opt(info->options, LWS_SERVER_OPTION_SDEVENT)) {
598
        extern const lws_plugin_evlib_t evlib_sd;
599
        plev = &evlib_sd;
600
        us_wait_resolution = 0;
601
    }
602
#endif
603
604
#if defined(LWS_WITH_ULOOP)
605
    if (lws_check_opt(info->options, LWS_SERVER_OPTION_ULOOP)) {
606
        extern const lws_plugin_evlib_t evlib_uloop;
607
        plev = &evlib_uloop;
608
        us_wait_resolution = 0;
609
    }
610
#endif
611
612
#endif /* with event libs */
613
614
0
#endif /* not with ev plugins */
615
616
0
  if (!plev || lws_fi(&info->fic, "ctx_createfail_evlib_sel"))
617
0
    goto fail_event_libs;
618
619
0
#if defined(LWS_WITH_NETWORK)
620
0
  size += (size_t)plev->ops->evlib_size_ctx /* the ctx evlib priv */ +
621
0
    (count_threads * (size_t)plev->ops->evlib_size_pt) /* the pt evlib priv */;
622
0
#endif
623
624
0
  context = lws_zalloc(size, "context");
625
0
  if (!context || lws_fi(&info->fic, "ctx_createfail_oom_ctx")) {
626
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
627
    lws_free(context);
628
#endif
629
0
    lwsl_err("OOM");
630
0
    goto early_bail;
631
0
  }
632
633
0
#if defined(LWS_WITH_SYS_STATE)
634
   // NOTE: we need to init this fields because they may be used in logger when context destroying
635
0
  context->mgr_system.state_names = system_state_names;
636
0
  context->mgr_system.context = context;
637
0
#endif
638
639
0
#if defined(LWS_WITH_NETWORK)
640
0
  context->event_loop_ops = plev->ops;
641
0
  context->us_wait_resolution = us_wait_resolution;
642
#if defined(LWS_WITH_TLS_JIT_TRUST)
643
  {
644
    struct lws_cache_creation_info ci;
645
646
    memset(&ci, 0, sizeof(ci));
647
    ci.cx = context;
648
    ci.ops = &lws_cache_ops_heap;
649
    ci.name = "jitt";
650
    ci.max_footprint = info->jitt_cache_max_footprint;
651
    context->trust_cache = lws_cache_create(&ci);
652
  }
653
#endif
654
0
#endif
655
#if defined(LWS_WITH_EVENT_LIBS)
656
  /* at the very end */
657
  context->evlib_ctx = (uint8_t *)context + size -
658
          plev->ops->evlib_size_ctx;
659
#endif
660
#if defined(LWS_WITH_EVLIB_PLUGINS) && defined(LWS_WITH_EVENT_LIBS)
661
  context->evlib_plugin_list = evlib_plugin_list;
662
#endif
663
664
0
#if !defined(LWS_PLAT_FREERTOS)
665
0
  context->uid = info->uid;
666
0
  context->gid = info->gid;
667
0
  context->username = info->username;
668
0
  context->groupname = info->groupname;
669
0
#endif
670
0
  context->name     = info->vhost_name;
671
0
  if (info->log_cx)
672
0
    context->log_cx = info->log_cx;
673
0
  else
674
0
    context->log_cx = &log_cx;
675
0
  lwsl_refcount_cx(context->log_cx, 1);
676
677
0
  context->system_ops = info->system_ops;
678
0
  context->pt_serv_buf_size = (unsigned int)s1;
679
0
  context->protocols_copy = info->protocols;
680
#if defined(LWS_WITH_TLS_JIT_TRUST)
681
  context->vh_idle_grace_ms = info->vh_idle_grace_ms ?
682
      info->vh_idle_grace_ms : 5000;
683
#endif
684
685
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
686
  context->fic.name = "ctx";
687
  if (info->fic.fi_owner.count)
688
    /*
689
     * This moves all the lws_fi_t from info->fi to the context fi,
690
     * leaving it empty, so no injection added to default vhost
691
     */
692
    lws_fi_import(&context->fic, &info->fic);
693
#endif
694
695
696
0
#if defined(LWS_WITH_SYS_SMD)
697
0
  context->smd_ttl_us = info->smd_ttl_us ? info->smd_ttl_us :
698
#if defined(LWS_PLAT_FREERTOS)
699
      5000000;
700
#else
701
0
      2000000;
702
0
#endif
703
0
  context->smd_queue_depth = (uint16_t)(info->smd_queue_depth ?
704
0
            info->smd_queue_depth :
705
#if defined(LWS_PLAT_FREERTOS)
706
            20);
707
#else
708
0
            40);
709
0
#endif
710
0
#endif
711
712
0
#if defined(LWS_WITH_NETWORK)
713
0
  context->lcg[LWSLCG_WSI].tag_prefix = "wsi";
714
0
  context->lcg[LWSLCG_VHOST].tag_prefix = "vh";
715
0
  context->lcg[LWSLCG_WSI_SERVER].tag_prefix = "wsisrv"; /* adopted */
716
717
0
#if defined(LWS_ROLE_H2) || defined(LWS_ROLE_MQTT)
718
0
  context->lcg[LWSLCG_WSI_MUX].tag_prefix = "mux"; /* a mux child wsi */
719
0
#endif
720
721
0
#if defined(LWS_WITH_CLIENT)
722
0
  context->lcg[LWSLCG_WSI_CLIENT].tag_prefix = "wsicli";
723
0
#endif
724
725
0
#if defined(LWS_WITH_SECURE_STREAMS)
726
0
#if defined(LWS_WITH_CLIENT)
727
0
  context->lcg[LWSLCG_SS_CLIENT].tag_prefix = "SScli";
728
0
#endif
729
0
#if defined(LWS_WITH_SERVER)
730
0
  context->lcg[LWSLCG_SS_SERVER].tag_prefix = "SSsrv";
731
0
#endif
732
0
#if defined(LWS_WITH_CLIENT)
733
0
  context->lcg[LWSLCG_WSI_SS_CLIENT].tag_prefix = "wsiSScli";
734
0
#endif
735
0
#if defined(LWS_WITH_SERVER)
736
0
  context->lcg[LWSLCG_WSI_SS_SERVER].tag_prefix = "wsiSSsrv";
737
0
#endif
738
0
#endif
739
0
#endif
740
741
#if defined(LWS_WITH_SYS_METRICS)
742
  /*
743
   * If we're not using secure streams, we can still pass in a linked-
744
   * list of metrics policies
745
   */
746
  context->metrics_policies = info->metrics_policies;
747
  context->metrics_prefix = info->metrics_prefix;
748
749
  context->mt_service = lws_metric_create(context,
750
          LWSMTFL_REPORT_DUTY_WALLCLOCK_US |
751
          LWSMTFL_REPORT_ONLY_GO, "cpu.svc");
752
753
#if defined(LWS_WITH_CLIENT)
754
755
  context->mt_conn_dns = lws_metric_create(context,
756
             LWSMTFL_REPORT_MEAN |
757
             LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
758
             "n.cn.dns");
759
  context->mt_conn_tcp = lws_metric_create(context,
760
             LWSMTFL_REPORT_MEAN |
761
             LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
762
             "n.cn.tcp");
763
  context->mt_conn_tls = lws_metric_create(context,
764
             LWSMTFL_REPORT_MEAN |
765
             LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
766
             "n.cn.tls");
767
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
768
  context->mt_http_txn = lws_metric_create(context,
769
             LWSMTFL_REPORT_MEAN |
770
             LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
771
             "n.http.txn");
772
#endif
773
774
  context->mth_conn_failures = lws_metric_create(context,
775
          LWSMTFL_REPORT_HIST, "n.cn.failures");
776
777
#if defined(LWS_WITH_SYS_ASYNC_DNS)
778
  context->mt_adns_cache = lws_metric_create(context,
779
               LWSMTFL_REPORT_MEAN |
780
               LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
781
               "n.cn.adns");
782
#endif
783
#if defined(LWS_WITH_SECURE_STREAMS)
784
  context->mth_ss_conn = lws_metric_create(context, LWSMTFL_REPORT_HIST,
785
             "n.ss.conn");
786
#endif
787
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
788
  context->mt_ss_cliprox_conn = lws_metric_create(context,
789
      LWSMTFL_REPORT_HIST,
790
              "n.ss.cliprox.conn");
791
  context->mt_ss_cliprox_paylat = lws_metric_create(context,
792
                LWSMTFL_REPORT_MEAN |
793
                LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
794
                "n.ss.cliprox.paylat");
795
  context->mt_ss_proxcli_paylat = lws_metric_create(context,
796
                LWSMTFL_REPORT_MEAN |
797
                LWSMTFL_REPORT_DUTY_WALLCLOCK_US,
798
                "n.ss.proxcli.paylat");
799
#endif
800
801
#endif /* network + metrics + client */
802
803
#if defined(LWS_WITH_SERVER)
804
  context->mth_srv = lws_metric_create(context,
805
               LWSMTFL_REPORT_HIST, "n.srv");
806
#endif /* network + metrics + server */
807
808
#endif /* network + metrics */
809
810
0
#endif /* network */
811
812
0
  lwsl_cx_notice(context, "LWS: %s, %s%s", library_version, opts_str, s);
813
0
#if defined(LWS_WITH_NETWORK)
814
0
  lwsl_cx_info(context, "Event loop: %s", plev->ops->name);
815
0
#endif
816
817
  /*
818
   * Proxy group
819
   */
820
821
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
822
#if defined(LWS_WITH_CLIENT)
823
  context->lcg[LWSLCG_SSP_CLIENT].tag_prefix = "SSPcli";
824
#endif
825
#if defined(LWS_WITH_SERVER)
826
  context->lcg[LWSLCG_SSP_ONWARD].tag_prefix = "SSPonw";
827
#endif
828
#if defined(LWS_WITH_CLIENT)
829
  context->lcg[LWSLCG_WSI_SSP_CLIENT].tag_prefix = "wsiSSPcli";
830
#endif
831
#if defined(LWS_WITH_SERVER)
832
  context->lcg[LWSLCG_WSI_SSP_ONWARD].tag_prefix = "wsiSSPonw";
833
#endif
834
#endif
835
836
0
#if defined(LWS_WITH_SERVER)
837
0
  context->lcg[LWSLCG_WSI_SSP_SINK].tag_prefix = "SSsink";
838
0
  context->lcg[LWSLCG_WSI_SSP_SOURCE].tag_prefix = "SSsrc";
839
0
#endif
840
841
#if defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
842
  /* directly use the user-provided policy object list */
843
  context->pss_policies = info->pss_policies;
844
#endif
845
846
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) && defined(LWS_WITH_CLIENT)
847
  context->ss_proxy_bind = info->ss_proxy_bind;
848
  context->ss_proxy_port = info->ss_proxy_port;
849
  context->ss_proxy_address = info->ss_proxy_address;
850
851
  if (info->txp_ops_ssproxy)
852
    context->txp_ppath.ops_onw = info->txp_ops_ssproxy;
853
  else
854
    context->txp_ppath.ops_onw = &txp_ops_ssproxy_wsi;
855
  if (info->txp_ops_sspc)
856
    context->txp_cpath.ops_onw = info->txp_ops_sspc;
857
  else
858
    context->txp_cpath.ops_onw = &txp_ops_sspc_wsi;
859
860
  context->txp_ssproxy_info = info->txp_ssproxy_info;
861
862
  if (context->ss_proxy_bind && context->ss_proxy_address)
863
    lwsl_cx_notice(context, "ss proxy bind '%s', port %d, ads '%s'",
864
      context->ss_proxy_bind, context->ss_proxy_port,
865
      context->ss_proxy_address);
866
#endif
867
868
0
#if defined(LWS_WITH_NETWORK)
869
0
  context->undestroyed_threads = count_threads;
870
0
  context->count_threads = count_threads;
871
872
0
#if defined(LWS_ROLE_WS) && defined(LWS_WITHOUT_EXTENSIONS)
873
0
        if (info->extensions)
874
0
                lwsl_cx_warn(context, "WITHOUT_EXTENSIONS but exts ptr set");
875
0
#endif
876
0
#endif /* network */
877
878
0
#if defined(LWS_WITH_SECURE_STREAMS)
879
0
#if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
880
0
  context->pss_policies_json = info->pss_policies_json;
881
0
#endif
882
0
#endif
883
884
  /* if he gave us names, set the uid / gid */
885
0
  if (lws_plat_drop_app_privileges(context, 0) ||
886
0
      lws_fi(&context->fic, "ctx_createfail_privdrop"))
887
0
    goto free_context_fail2;
888
889
0
#if defined(LWS_WITH_TLS) && defined(LWS_WITH_NETWORK)
890
#if defined(LWS_WITH_MBEDTLS)
891
  context->tls_ops = &tls_ops_mbedtls;
892
893
  mbedtls_client_preload_filepath = info->mbedtls_client_preload_filepath;
894
#else
895
0
  context->tls_ops = &tls_ops_openssl;
896
0
#endif
897
0
#endif
898
899
#if LWS_MAX_SMP > 1
900
  lws_mutex_refcount_init(&context->mr);
901
#endif
902
903
#if defined(LWS_PLAT_FREERTOS)
904
#if defined(LWS_AMAZON_RTOS)
905
  context->last_free_heap = xPortGetFreeHeapSize();
906
#else
907
  context->last_free_heap = esp_get_free_heap_size();
908
#endif
909
#endif
910
911
0
#if defined(LWS_WITH_FILE_OPS)
912
  /* default to just the platform fops implementation */
913
914
0
  context->fops_platform.LWS_FOP_OPEN  = _lws_plat_file_open;
915
0
  context->fops_platform.LWS_FOP_CLOSE  = _lws_plat_file_close;
916
0
  context->fops_platform.LWS_FOP_SEEK_CUR  = _lws_plat_file_seek_cur;
917
0
  context->fops_platform.LWS_FOP_READ  = _lws_plat_file_read;
918
0
  context->fops_platform.LWS_FOP_WRITE  = _lws_plat_file_write;
919
0
  context->fops_platform.fi[0].sig  = NULL;
920
0
  context->fops_platform.cx   = context;
921
922
  /*
923
   *  arrange a linear linked-list of fops starting from context->fops
924
   *
925
   * platform fops
926
   * [ -> fops_zip (copied into context so .next settable) ]
927
   * [ -> info->fops ]
928
   */
929
930
0
  context->fops = &context->fops_platform;
931
0
  prev = (struct lws_plat_file_ops *)context->fops;
932
933
#if defined(LWS_WITH_ZIP_FOPS)
934
  /* make a soft copy so we can set .next */
935
  context->fops_zip = fops_zip;
936
  prev->next = &context->fops_zip;
937
  context->fops_zip.cx = context;
938
  prev = (struct lws_plat_file_ops *)prev->next;
939
#endif
940
941
  /* if user provided fops, tack them on the end of the list */
942
0
  if (info->fops)
943
0
    prev->next = info->fops;
944
0
#endif
945
946
0
#if defined(LWS_WITH_SERVER)
947
0
  context->reject_service_keywords = info->reject_service_keywords;
948
0
#endif
949
0
  if (info->external_baggage_free_on_destroy)
950
0
    context->external_baggage_free_on_destroy =
951
0
      info->external_baggage_free_on_destroy;
952
0
#if defined(LWS_WITH_NETWORK)
953
0
  context->time_up = lws_now_usecs();
954
0
#endif
955
0
  context->pcontext_finalize = info->pcontext;
956
957
0
#if defined(LWS_WITH_TLS) && defined(LWS_WITH_NETWORK)
958
0
  context->simultaneous_ssl_restriction =
959
0
      info->simultaneous_ssl_restriction;
960
0
  context->simultaneous_ssl_handshake_restriction =
961
0
      info->simultaneous_ssl_handshake_restriction;
962
0
#endif
963
964
0
  context->options = info->options;
965
966
0
#if !defined(LWS_PLAT_FREERTOS) && !defined(LWS_PLAT_OPTEE) && !defined(WIN32) && !defined(LWS_PLAT_BAREMETAL)
967
  /*
968
   * If asked, try to set the rlimit / ulimit for process sockets / files.
969
   * We read the effective limit in a moment, so we will find out the
970
   * real limit according to system constraints then.
971
   */
972
0
  if (info->rlimit_nofile) {
973
0
    struct rlimit rl;
974
975
0
    rl.rlim_cur = (unsigned int)info->rlimit_nofile;
976
0
    rl.rlim_max = (unsigned int)info->rlimit_nofile;
977
0
    setrlimit(RLIMIT_NOFILE, &rl);
978
0
  }
979
0
#endif
980
981
#ifndef LWS_NO_DAEMONIZE
982
  if (pid_daemon) {
983
    context->started_with_parent = pid_daemon;
984
    lwsl_cx_info(context, " Started with daemon pid %u",
985
        (unsigned int)pid_daemon);
986
  }
987
#endif
988
#if defined(__ANDROID__)
989
  n = getrlimit(RLIMIT_NOFILE, &rt);
990
  if (n == -1) {
991
    lwsl_cx_err(context, "Get RLIMIT_NOFILE failed!");
992
993
    goto free_context_fail2;
994
  }
995
  context->max_fds = (unsigned int)rt.rlim_cur;
996
#else
997
#if defined(WIN32) || defined(_WIN32) || defined(LWS_AMAZON_RTOS) || defined(LWS_ESP_PLATFORM)
998
  context->max_fds = getdtablesize();
999
#else
1000
0
  {
1001
0
    long l = sysconf(_SC_OPEN_MAX);
1002
1003
0
    context->max_fds = 2560;
1004
1005
0
    if (l > 10000000)
1006
0
      lwsl_cx_warn(context, "unreasonable ulimit -n workaround");
1007
0
    else
1008
0
      if (l != -1l)
1009
0
        context->max_fds = (unsigned int)l;
1010
0
  }
1011
0
#endif
1012
0
  if ((int)context->max_fds < 0 ||
1013
0
       lws_fi(&context->fic, "ctx_createfail_maxfds")) {
1014
0
    lwsl_cx_err(context, "problem getting process max files");
1015
1016
0
    goto free_context_fail2;
1017
0
  }
1018
0
#endif
1019
1020
  /*
1021
   * deal with any max_fds override, if it's reducing (setting it to
1022
   * more than ulimit -n is meaningless).  The platform init will
1023
   * figure out what if this is something it can deal with.
1024
   */
1025
0
  if (info->fd_limit_per_thread) {
1026
0
    unsigned int mf = lpf * context->count_threads;
1027
1028
0
    if (mf < context->max_fds) {
1029
0
      context->max_fds_unrelated_to_ulimit = 1;
1030
0
      context->max_fds = mf;
1031
0
    }
1032
0
  }
1033
1034
0
#if defined(LWS_WITH_NETWORK)
1035
0
  context->token_limits = info->token_limits;
1036
0
#endif
1037
1038
1039
0
#if defined(LWS_WITH_TLS) && defined(LWS_WITH_NETWORK)
1040
0
  time(&context->tls.last_cert_check_s);
1041
0
  if (info->alpn)
1042
0
    context->tls.alpn_default = info->alpn;
1043
0
  else {
1044
0
    char *p = context->tls.alpn_discovered, first = 1;
1045
1046
0
    LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) {
1047
0
      if (ar->alpn) {
1048
0
        if (!first)
1049
0
          *p++ = ',';
1050
0
        p += lws_snprintf(p, (unsigned int)(
1051
0
          (context->tls.alpn_discovered +
1052
0
          sizeof(context->tls.alpn_discovered) -
1053
0
          2) - p), "%s", ar->alpn);
1054
0
        first = 0;
1055
0
      }
1056
0
    } LWS_FOR_EVERY_AVAILABLE_ROLE_END;
1057
1058
0
    context->tls.alpn_default = context->tls.alpn_discovered;
1059
0
  }
1060
1061
0
#endif
1062
1063
0
  context->timeout_secs = 15;
1064
1065
0
#if defined(LWS_WITH_NETWORK)
1066
#if defined(WIN32)
1067
  if (!info->win32_connect_check_interval_usec)
1068
    context->win32_connect_check_interval_usec = 1000;
1069
  else
1070
    context->win32_connect_check_interval_usec =
1071
        info->win32_connect_check_interval_usec;
1072
#endif
1073
0
  if (info->timeout_secs)
1074
0
    context->timeout_secs = info->timeout_secs;
1075
0
#endif /* WITH_NETWORK */
1076
1077
0
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
1078
0
  if (info->max_http_header_data)
1079
0
    context->max_http_header_data = info->max_http_header_data;
1080
0
  else
1081
0
    if (info->max_http_header_data2)
1082
0
      context->max_http_header_data =
1083
0
          (unsigned short)info->max_http_header_data2;
1084
0
    else
1085
0
      context->max_http_header_data = LWS_DEF_HEADER_LEN;
1086
1087
0
  if (info->max_http_header_pool)
1088
0
    context->max_http_header_pool = info->max_http_header_pool;
1089
0
  else
1090
0
    if (info->max_http_header_pool2)
1091
0
      context->max_http_header_pool =
1092
0
          (unsigned short)info->max_http_header_pool2;
1093
0
    else
1094
0
      context->max_http_header_pool = context->max_fds;
1095
0
#endif
1096
1097
0
  if (info->fd_limit_per_thread)
1098
0
    context->fd_limit_per_thread = lpf;
1099
0
  else
1100
0
    if (context->count_threads)
1101
0
      context->fd_limit_per_thread = context->max_fds /
1102
0
              context->count_threads;
1103
1104
0
#if defined(LWS_WITH_SYS_SMD)
1105
0
  lws_mutex_init(context->smd.lock_messages);
1106
0
  lws_mutex_init(context->smd.lock_peers);
1107
1108
  /* lws_system smd participant */
1109
1110
0
  if (!lws_smd_register(context, context, 0, LWSSMDCL_NETWORK,
1111
0
            lws_system_smd_cb)) {
1112
0
    lwsl_cx_err(context, "early smd register failed");
1113
0
  }
1114
1115
  /* user smd participant */
1116
1117
0
  if (info->early_smd_cb &&
1118
0
      !lws_smd_register(context, info->early_smd_opaque, 0,
1119
0
            info->early_smd_class_filter,
1120
0
            info->early_smd_cb)) {
1121
0
    lwsl_cx_err(context, "early smd register failed");
1122
0
  }
1123
0
#endif
1124
1125
0
#if !defined(LWS_PLAT_BAREMETAL) && defined(LWS_WITH_NETWORK)
1126
0
  n = 0;
1127
0
#endif
1128
0
#if defined(LWS_WITH_NETWORK)
1129
1130
0
  context->default_retry.retry_ms_table = default_backoff_table;
1131
0
  context->default_retry.conceal_count =
1132
0
      context->default_retry.retry_ms_table_count =
1133
0
          LWS_ARRAY_SIZE(default_backoff_table);
1134
0
  context->default_retry.jitter_percent = 20;
1135
0
  context->default_retry.secs_since_valid_ping = 300;
1136
0
  context->default_retry.secs_since_valid_hangup = 310;
1137
1138
0
  if (info->retry_and_idle_policy &&
1139
0
      info->retry_and_idle_policy->secs_since_valid_ping) {
1140
0
    context->default_retry.secs_since_valid_ping =
1141
0
        info->retry_and_idle_policy->secs_since_valid_ping;
1142
0
    context->default_retry.secs_since_valid_hangup =
1143
0
        info->retry_and_idle_policy->secs_since_valid_hangup;
1144
0
  }
1145
1146
  /*
1147
   * Allocate the per-thread storage for scratchpad buffers,
1148
   * and header data pool
1149
   */
1150
0
  u = (uint8_t *)&context[1];
1151
0
  for (n = 0; n < context->count_threads; n++) {
1152
0
    context->pt[n].serv_buf = u;
1153
0
    u += context->pt_serv_buf_size;
1154
1155
0
    context->pt[n].context = context;
1156
0
    context->pt[n].tid = (uint8_t)n;
1157
1158
0
#if !defined(LWS_PLAT_FREERTOS)
1159
    /*
1160
     * We overallocated for a fakewsi (can't compose it in the
1161
     * pt because size isn't known at that time).  point to it
1162
     * and zero it down.  Fakewsis are needed to make callbacks work
1163
     * when the source of the callback is not actually from a wsi
1164
     * context.
1165
     */
1166
0
    context->pt[n].fake_wsi = (struct lws *)u;
1167
0
    u += sizeof(struct lws);
1168
1169
0
    memset(context->pt[n].fake_wsi, 0, sizeof(struct lws));
1170
0
#endif
1171
1172
0
    context->pt[n].evlib_pt = u;
1173
0
    u += plev->ops->evlib_size_pt;
1174
1175
0
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
1176
0
    context->pt[n].http.ah_list = NULL;
1177
0
    context->pt[n].http.ah_pool_length = 0;
1178
0
#endif
1179
0
    lws_pt_mutex_init(&context->pt[n]);
1180
1181
#if defined(LWS_WITH_CGI)
1182
    if (lws_rops_fidx(&role_ops_cgi, LWS_ROPS_pt_init_destroy))
1183
      (lws_rops_func_fidx(&role_ops_cgi, LWS_ROPS_pt_init_destroy)).
1184
        pt_init_destroy(context, info,
1185
            &context->pt[n], 0);
1186
#endif
1187
0
  }
1188
1189
0
  if (!info->ka_interval && info->ka_time > 0) {
1190
0
    lwsl_cx_err(context, "info->ka_interval can't be 0 if ka_time used");
1191
0
    goto free_context_fail;
1192
0
  }
1193
1194
#if defined(LWS_WITH_PEER_LIMITS)
1195
  /* scale the peer hash table according to the max fds for the process,
1196
   * so that the max list depth averages 16.  Eg, 1024 fd -> 64,
1197
   * 102400 fd -> 6400
1198
   */
1199
1200
  context->pl_hash_elements =
1201
    (context->count_threads * context->fd_limit_per_thread) / 16;
1202
  context->pl_hash_table = lws_zalloc(sizeof(struct lws_peer *) *
1203
      context->pl_hash_elements, "peer limits hash table");
1204
1205
  context->ip_limit_ah = info->ip_limit_ah;
1206
  context->ip_limit_wsi = info->ip_limit_wsi;
1207
  context->pl_notify_cb = info->pl_notify_cb;
1208
#endif
1209
1210
  /*
1211
   * fds table contains pollfd structs for as many pollfds as we can
1212
   * handle... spread across as many service threads as we have going
1213
   */
1214
0
  n = (int)(sizeof(struct lws_pollfd) * context->count_threads *
1215
0
      context->fd_limit_per_thread);
1216
0
  context->pt[0].fds = lws_zalloc((unsigned int)n, "fds table");
1217
0
  if (context->pt[0].fds == NULL ||
1218
0
      lws_fi(&context->fic, "ctx_createfail_oom_fds")) {
1219
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
1220
    lws_free(context->pt[0].fds);
1221
#endif
1222
0
    lwsl_cx_err(context, "OOM allocating %d fds\n", context->max_fds);
1223
0
    goto free_context_fail;
1224
0
  }
1225
0
#endif
1226
1227
0
  lwsl_cx_info(context, "ctx: %5luB (%ld ctx + pt(%ld thr x %d)), "
1228
0
      "pt-fds: %d",
1229
0
      (long)sizeof(struct lws_context) +
1230
0
      (context->count_threads * context->pt_serv_buf_size),
1231
0
      (long)sizeof(struct lws_context),
1232
0
      (long)context->count_threads,
1233
0
      context->pt_serv_buf_size,
1234
0
      context->fd_limit_per_thread);
1235
1236
0
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
1237
0
  lwsl_cx_info(context, " http: ah_data: %u, ah: %lu, max count %u",
1238
0
        context->max_http_header_data,
1239
0
        (long)sizeof(struct allocated_headers),
1240
0
        context->max_http_header_pool);
1241
0
#endif
1242
1243
0
#if defined(LWS_WITH_SERVER)
1244
0
  if (info->server_string) {
1245
0
    context->server_string = info->server_string;
1246
0
    context->server_string_len = (short)
1247
0
        strlen(context->server_string);
1248
0
  }
1249
0
#endif
1250
1251
#if LWS_MAX_SMP > 1
1252
  /* each thread serves his own chunk of fds */
1253
  for (n = 1; n < (int)context->count_threads; n++)
1254
    context->pt[n].fds = context->pt[n - 1].fds +
1255
             context->fd_limit_per_thread;
1256
#endif
1257
1258
1259
  /*
1260
   * Past here, we may have added handles to the event lib
1261
   * loop and if libuv,  have to take care about how to unpick them...
1262
   */
1263
1264
0
  if (lws_plat_init(context, info) ||
1265
0
      lws_fi(&context->fic, "ctx_createfail_plat_init"))
1266
0
    goto bail_libuv_aware;
1267
1268
0
#if defined(LWS_WITH_NETWORK)
1269
1270
0
  if (lws_fi(&context->fic, "ctx_createfail_evlib_init"))
1271
0
    goto bail_libuv_aware;
1272
1273
0
  if (context->event_loop_ops->init_context)
1274
0
    if (context->event_loop_ops->init_context(context, info))
1275
0
      goto bail_libuv_aware;
1276
1277
0
  if (lws_fi(&context->fic, "ctx_createfail_evlib_pt"))
1278
0
    goto bail_libuv_aware;
1279
1280
0
  if (context->event_loop_ops->init_pt)
1281
0
    for (n = 0; n < context->count_threads; n++) {
1282
0
      void *lp = NULL;
1283
1284
0
      if (info->foreign_loops)
1285
0
        lp = info->foreign_loops[n];
1286
1287
0
      if (context->event_loop_ops->init_pt(context, lp, n))
1288
0
        goto bail_libuv_aware;
1289
0
    }
1290
1291
0
  lws_context_lock(context, __func__);
1292
0
  n = __lws_create_event_pipes(context);
1293
0
  lws_context_unlock(context);
1294
0
  if (n)
1295
0
    goto bail_libuv_aware;
1296
1297
0
  for (n = 0; n < context->count_threads; n++) {
1298
0
    LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) {
1299
0
      if (lws_rops_fidx(ar, LWS_ROPS_pt_init_destroy))
1300
0
        (lws_rops_func_fidx(ar, LWS_ROPS_pt_init_destroy)).
1301
0
          pt_init_destroy(context, info,
1302
0
              &context->pt[n], 0);
1303
0
    } LWS_FOR_EVERY_AVAILABLE_ROLE_END;
1304
0
  }
1305
0
#endif
1306
1307
0
  lws_context_init_ssl_library(context, info);
1308
1309
0
  context->user_space = info->user;
1310
1311
0
#if defined(LWS_WITH_SERVER)
1312
0
  strcpy(context->canonical_hostname, "unknown");
1313
0
#if defined(LWS_WITH_NETWORK)
1314
0
  lws_server_get_canonical_hostname(context, info);
1315
0
#endif
1316
0
#endif
1317
1318
#if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
1319
  memcpy(context->caps, info->caps, sizeof(context->caps));
1320
  context->count_caps = info->count_caps;
1321
#endif
1322
1323
1324
0
#if defined(LWS_WITH_NETWORK)
1325
1326
#if defined(LWS_WITH_SYS_ASYNC_DNS) || defined(LWS_WITH_SYS_NTPCLIENT) || \
1327
  defined(LWS_WITH_SYS_DHCP_CLIENT)
1328
  {
1329
    /*
1330
     * system vhost
1331
     */
1332
1333
    struct lws_context_creation_info ii;
1334
    const struct lws_protocols *pp[4];
1335
    struct lws_vhost *vh;
1336
#if defined(LWS_WITH_SYS_ASYNC_DNS)
1337
    extern const struct lws_protocols lws_async_dns_protocol;
1338
#endif
1339
#if defined(LWS_WITH_SYS_NTPCLIENT)
1340
    extern const struct lws_protocols lws_system_protocol_ntpc;
1341
#endif
1342
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
1343
    extern const struct lws_protocols lws_system_protocol_dhcpc4;
1344
#endif
1345
1346
    n = 0;
1347
#if defined(LWS_WITH_SYS_ASYNC_DNS)
1348
    pp[n++] = &lws_async_dns_protocol;
1349
#endif
1350
#if defined(LWS_WITH_SYS_NTPCLIENT)
1351
    pp[n++] = &lws_system_protocol_ntpc;
1352
#endif
1353
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
1354
    pp[n++] = &lws_system_protocol_dhcpc4;
1355
#endif
1356
    pp[n] = NULL;
1357
1358
    memset(&ii, 0, sizeof(ii));
1359
    ii.vhost_name = "system";
1360
    ii.pprotocols = pp;
1361
    ii.port = CONTEXT_PORT_NO_LISTEN;
1362
1363
    if (lws_fi(&context->fic, "ctx_createfail_sys_vh"))
1364
      vh = NULL;
1365
    else
1366
      vh = lws_create_vhost(context, &ii);
1367
    if (!vh) {
1368
      lwsl_cx_err(context, "failed to create system vhost");
1369
      goto bail_libuv_aware;
1370
    }
1371
1372
    context->vhost_system = vh;
1373
1374
    if (lws_protocol_init_vhost(vh, NULL) ||
1375
        lws_fi(&context->fic, "ctx_createfail_sys_vh_init")) {
1376
      lwsl_cx_err(context, "failed to init system vhost");
1377
      goto bail_libuv_aware;
1378
    }
1379
#if defined(LWS_WITH_SYS_ASYNC_DNS)
1380
    lws_async_dns_init(context);
1381
      //goto bail_libuv_aware;
1382
#endif
1383
  }
1384
1385
#endif
1386
1387
0
#if defined(LWS_WITH_SYS_STATE)
1388
  /*
1389
   * init the lws_state mgr for the system state
1390
   */
1391
1392
0
  context->mgr_system.name    = "system";
1393
0
  context->mgr_system.state   = LWS_SYSTATE_CONTEXT_CREATED;
1394
0
  context->mgr_system.parent    = context;
1395
0
#if defined(LWS_WITH_SYS_SMD)
1396
0
  context->mgr_system.smd_class   = LWSSMDCL_SYSTEM_STATE;
1397
0
#endif
1398
1399
0
  context->protocols_notify.name    = "prot_init";
1400
0
  context->protocols_notify.notify_cb = lws_state_notify_protocol_init;
1401
1402
0
  lws_state_reg_notifier(&context->mgr_system, &context->protocols_notify);
1403
1404
  /*
1405
   * insert user notifiers here so they can participate with vetoing us
1406
   * trying to jump straight to operational, or at least observe us
1407
   * reaching 'operational', before we returned from context creation.
1408
   */
1409
1410
0
  lws_state_reg_notifier_list(&context->mgr_system,
1411
0
            info->register_notifier_list);
1412
0
#endif
1413
1414
  /*
1415
   * if he's not saying he'll make his own vhosts later then act
1416
   * compatibly and make a default vhost using the data in the info
1417
   */
1418
0
  if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)
1419
0
      || info->pprotocols) {
1420
0
    if (!lws_create_vhost(context, info) ||
1421
0
        lws_fi(&context->fic, "ctx_createfail_def_vh")) {
1422
0
      lwsl_cx_err(context, "Failed to create default vhost");
1423
1424
#if defined(LWS_WITH_PEER_LIMITS)
1425
      lws_free_set_NULL(context->pl_hash_table);
1426
#endif
1427
0
      goto bail;
1428
0
    }
1429
0
  }
1430
1431
0
#if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
1432
0
  if (info->http_nsc_filepath) {
1433
0
    memset(&ci, 0, sizeof(ci));
1434
1435
0
    ci.cx        = context;
1436
0
    ci.ops         = &lws_cache_ops_nscookiejar;
1437
0
    ci.name        = "NSC";
1438
0
    ci.u.nscookiejar.filepath  = info->http_nsc_filepath;
1439
1440
0
    context->nsc = lws_cache_create(&ci);
1441
0
    if (!context->nsc)
1442
0
      goto bail;
1443
1444
0
    ci.ops        = &lws_cache_ops_heap;
1445
0
    ci.name       = "L1";
1446
0
    ci.parent     = context->nsc;
1447
0
    ci.max_footprint    = info->http_nsc_heap_max_footprint;
1448
0
    ci.max_items      = info->http_nsc_heap_max_items;
1449
0
    ci.max_payload      = info->http_nsc_heap_max_payload;
1450
1451
0
    context->l1 = lws_cache_create(&ci);
1452
0
    if (!context->l1) {
1453
0
      lwsl_cx_err(context, "Failed to init cookiejar");
1454
0
      goto bail;
1455
0
    }
1456
0
  }
1457
0
#endif
1458
1459
#if defined(LWS_WITH_SYS_ASYNC_DNS)
1460
  if (info->async_dns_servers) {
1461
    const char **dsrv = info->async_dns_servers;
1462
    while (*dsrv) {
1463
      lws_sockaddr46 sa46;
1464
      if (!lws_sa46_parse_numeric_address(*dsrv, &sa46)) {
1465
        lwsl_cx_info(context, "Adding DNS %s", *dsrv);
1466
        lws_async_dns_server_add(context, &sa46);
1467
      }
1468
      dsrv++;
1469
    }
1470
  }
1471
#endif
1472
1473
0
#if defined(LWS_WITH_SECURE_STREAMS)
1474
1475
0
#if !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
1476
1477
    /*
1478
     * You must create your context with the explicit vhosts flag
1479
     * in order to use secure streams
1480
     */
1481
0
  if (lws_check_opt(info->options,
1482
0
           LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
1483
1484
0
    if (!context->pss_policies_json)
1485
0
      context->pss_policies_json =
1486
0
  "{\n"
1487
0
    "\"release\": \"1\",\n"
1488
0
    "\"product\": \"lws_default\",\n"
1489
0
    "\"schema-version\": 1,\n"
1490
0
    "\"retry\": [{\n"
1491
0
      "\"default\": {\n"
1492
0
        "\"backoff\": [1000, 2000, 3000, 5000, 10000],\n"
1493
0
        "\"conceal\": 5,\n"
1494
0
        "\"jitterpc\": 20,\n"
1495
0
        "\"svalidping\": 30,\n"
1496
0
        "\"svalidhup\": 35\n"
1497
0
      "}\n"
1498
0
    "}],\n"
1499
0
    "\"s\": [\n"
1500
0
      "{\n"
1501
0
        "\"__default\": {\n"
1502
0
          "\"endpoint\": \"${endpoint}\",\n"
1503
0
          "\"port\": 443,\n"
1504
0
#if defined(LWS_WITH_HTTP2)
1505
0
          "\"protocol\": \"h2\",\n"
1506
#else
1507
          "\"protocol\": \"h1\",\n"
1508
#endif
1509
0
          "\"http_method\": \"GET\",\n"
1510
0
          "\"http_url\": \"\",\n"
1511
0
          "\"metadata\": [{\n"
1512
0
            "\"endpoint\":"      "\"\",\n"
1513
0
            "\"acc\":"      "\"accept\",\n"
1514
0
            "\"ua\":" "\"user-agent\"\n"
1515
0
          "}],\n"
1516
0
          "\"tls\": true,\n"
1517
0
          "\"allow_redirects\": true,\n"
1518
0
          "\"nghttp2_quirk_end_stream\": true,\n"
1519
0
          "\"h2q_oflow_txcr\": true,\n"
1520
0
          "\"direct_proto_str\": true,\n"
1521
0
          "\"opportunistic\": true,\n"
1522
0
          "\"retry\": \"default\",\n"
1523
0
          "\"timeout_ms\": 2000\n"
1524
0
        "},\n"
1525
0
                        "\"captive_portal_detect\": {"
1526
0
                            "\"endpoint\":"         "\"connectivitycheck.android.com\","
1527
0
                            "\"http_url\":"         "\"generate_204\","
1528
0
                            "\"port\":"             "80,"
1529
0
                            "\"protocol\":"         "\"h1\","
1530
0
                            "\"http_method\":"      "\"GET\","
1531
0
                            "\"opportunistic\":"    "true,"
1532
0
                            "\"http_expect\":"      "204,"
1533
0
                            "\"http_fail_redirect\": true\n"
1534
0
        "}\n"
1535
0
      "}\n"
1536
0
    "]\n"
1537
0
  "}\n";
1538
1539
0
    if (lws_ss_policy_parse_begin(context, 0) ||
1540
0
        lws_fi(&context->fic, "ctx_createfail_ss_pol1")) {
1541
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
1542
      lws_ss_policy_parse_abandon(context);
1543
#endif
1544
0
      goto bail_libuv_aware;
1545
0
    }
1546
1547
0
    n = lws_ss_policy_parse(context,
1548
0
          (uint8_t *)context->pss_policies_json,
1549
0
          strlen(context->pss_policies_json));
1550
0
    if ((n != LEJP_CONTINUE && n < 0) ||
1551
0
        lws_fi(&context->fic, "ctx_createfail_ss_pol2")) {
1552
0
      lws_ss_policy_parse_abandon(context);
1553
0
      goto bail_libuv_aware;
1554
0
    }
1555
1556
0
    if (lws_ss_policy_set(context, "hardcoded") ||
1557
0
        lws_fi(&context->fic, "ctx_createfail_ss_pol3")) {
1558
0
      lwsl_cx_err(context, "policy set failed");
1559
0
      goto bail_libuv_aware;
1560
0
    }
1561
0
  }
1562
#else
1563
  if (context->pss_policies) {
1564
    /* user code set the policy objects directly, no parsing step */
1565
1566
    /* you must set this content option to use SS */
1567
    assert(lws_check_opt(info->options,
1568
               LWS_SERVER_OPTION_EXPLICIT_VHOSTS));
1569
1570
    if (lws_ss_policy_set(context, "hardcoded") ||
1571
        lws_fi(&context->fic, "ctx_createfail_ss_pol3")) {
1572
      lwsl_cx_err(context, "policy set failed");
1573
      goto bail_libuv_aware;
1574
    }
1575
  }
1576
#endif
1577
0
#endif
1578
1579
0
  lws_context_init_extensions(info, context);
1580
1581
0
  lwsl_cx_info(context, " mem: per-conn:        %5lu bytes + protocol rx buf",
1582
0
        (unsigned long)sizeof(struct lws));
1583
1584
  /*
1585
   * drop any root privs for this process
1586
   * to listen on port < 1023 we would have needed root, but now we are
1587
   * listening, we don't want the power for anything else
1588
   */
1589
0
  if (!lws_check_opt(info->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS))
1590
0
    if (lws_plat_drop_app_privileges(context, 1) ||
1591
0
        lws_fi(&context->fic, "ctx_createfail_privdrop"))
1592
0
      goto bail_libuv_aware;
1593
1594
0
#if defined(LWS_WITH_SYS_STATE)
1595
  /*
1596
   * We want to move on the syste, state as far as it can go towards
1597
   * OPERATIONAL now.  But we have to return from here first so the user
1598
   * code that called us can set its copy of context, which it may be
1599
   * relying on to perform operations triggered by the state change.
1600
   *
1601
   * We set up a sul to come back immediately and do the state change.
1602
   */
1603
1604
0
  lws_sul_schedule(context, 0, &context->sul_system_state,
1605
0
       lws_context_creation_completion_cb, 1);
1606
0
#endif
1607
1608
  /* expedite post-context init (eg, protocols) */
1609
0
  lws_cancel_service(context);
1610
0
#endif
1611
1612
0
  return context;
1613
1614
0
early_bail:
1615
0
  lws_fi_destroy(&info->fic);
1616
1617
0
  return NULL;
1618
1619
0
#if defined(LWS_WITH_NETWORK)
1620
0
bail:
1621
0
  lws_fi_destroy(&info->fic);
1622
0
  lws_context_destroy(context);
1623
1624
0
  return NULL;
1625
0
#endif
1626
1627
0
bail_libuv_aware:
1628
0
  lws_context_destroy(context);
1629
#if defined(LWS_WITH_LIBUV)
1630
  return fatal_exit_defer ? context : NULL;
1631
#else
1632
0
  return NULL;
1633
0
#endif
1634
1635
0
#if defined(LWS_WITH_NETWORK)
1636
0
fail_event_libs:
1637
0
  if (context)
1638
0
  lwsl_cx_err(context, "Requested event library support not configured");
1639
0
#endif
1640
1641
0
#if defined(LWS_WITH_NETWORK)
1642
0
free_context_fail:
1643
0
  if (context) {
1644
0
#if defined(LWS_WITH_SYS_SMD)
1645
0
    _lws_smd_destroy(context);
1646
0
#endif
1647
0
  }
1648
0
#endif
1649
0
free_context_fail2:
1650
0
  if (context) {
1651
#if defined(LWS_WITH_SYS_METRICS)
1652
    lws_metrics_destroy(context);
1653
#endif
1654
0
    lws_fi_destroy(&context->fic);
1655
0
  }
1656
0
  lws_fi_destroy(&info->fic);
1657
0
  if (context) {
1658
0
    lwsl_refcount_cx(context->log_cx, -1);
1659
0
    lws_free(context);
1660
0
  }
1661
1662
0
  return NULL;
1663
0
}
1664
1665
#if defined(LWS_WITH_NETWORK)
1666
int
1667
lws_system_cpd_start(struct lws_context *cx)
1668
0
{
1669
0
  cx->captive_portal_detect = LWS_CPD_UNKNOWN;
1670
1671
  /* if there's a platform implementation, use it */
1672
1673
0
  if (lws_system_get_ops(cx) &&
1674
0
      lws_system_get_ops(cx)->captive_portal_detect_request)
1675
0
    return lws_system_get_ops(cx)->captive_portal_detect_request(cx);
1676
1677
0
#if defined(LWS_WITH_SECURE_STREAMS)
1678
  /*
1679
   * Otherwise try to use SS "captive_portal_detect" if that's enabled
1680
   */
1681
0
  return lws_ss_sys_cpd(cx);
1682
#else
1683
  return 0;
1684
#endif
1685
0
}
1686
1687
static void
1688
lws_system_deferred_cb(lws_sorted_usec_list_t *sul)
1689
0
{
1690
0
  struct lws_context *cx =
1691
0
         lws_container_of(sul, struct lws_context, sul_cpd_defer);
1692
1693
0
  lws_system_cpd_start(cx);
1694
0
}
1695
1696
void
1697
lws_system_cpd_start_defer(struct lws_context *cx, lws_usec_t defer_us)
1698
0
{
1699
0
  lws_sul_schedule(cx, 0, &cx->sul_cpd_defer,
1700
0
       lws_system_deferred_cb, defer_us);
1701
0
}
1702
1703
#if (defined(LWS_WITH_SYS_STATE) && defined(LWS_WITH_SYS_SMD)) || !defined(LWS_WITH_NO_LOGS)
1704
static const char *cname[] = { "Unknown", "OK", "Captive", "No internet" };
1705
#endif
1706
1707
void
1708
lws_system_cpd_set(struct lws_context *cx, lws_cpd_result_t result)
1709
0
{
1710
0
  if (cx->captive_portal_detect != LWS_CPD_UNKNOWN)
1711
0
    return;
1712
1713
0
#if !defined(LWS_WITH_NO_LOGS)
1714
0
  lwsl_cx_notice(cx, "setting CPD result %s", cname[result]);
1715
0
#endif
1716
1717
0
  cx->captive_portal_detect = (uint8_t)result;
1718
1719
0
#if defined(LWS_WITH_SYS_STATE)
1720
0
#if defined(LWS_WITH_SYS_SMD)
1721
0
  lws_smd_msg_printf(cx, LWSSMDCL_NETWORK,
1722
0
         "{\"type\":\"cpd\",\"result\":\"%s\"}",
1723
0
         cname[cx->captive_portal_detect]);
1724
0
#endif
1725
1726
  /* if nothing is there to intercept anything, go all the way */
1727
0
  if (cx->mgr_system.state != LWS_SYSTATE_POLICY_INVALID)
1728
0
    lws_state_transition_steps(&cx->mgr_system,
1729
0
             LWS_SYSTATE_OPERATIONAL);
1730
0
#endif
1731
0
}
1732
1733
lws_cpd_result_t
1734
lws_system_cpd_state_get(struct lws_context *cx)
1735
0
{
1736
0
  return (lws_cpd_result_t)cx->captive_portal_detect;
1737
0
}
1738
1739
#endif
1740
1741
int
1742
lws_context_is_deprecated(struct lws_context *cx)
1743
0
{
1744
0
  return cx->deprecated;
1745
0
}
1746
1747
/*
1748
 * When using an event loop, the context destruction is in three separate
1749
 * parts.  This is to cover both internal and foreign event loops cleanly.
1750
 *
1751
 *  - lws_context_destroy() simply starts a soft close of all wsi and
1752
 *     related allocations.  The event loop continues.
1753
 *
1754
 *     As the closes complete in the event loop, reference counting is used
1755
 *     to determine when everything is closed.  It then calls
1756
 *     lws_context_destroy2().
1757
 *
1758
 *  - lws_context_destroy2() cleans up the rest of the higher-level logical
1759
 *     lws pieces like vhosts.  If the loop was foreign, it then proceeds to
1760
 *     lws_context_destroy3().  If it the loop is internal, it stops the
1761
 *     internal loops and waits for lws_context_destroy() to be called again
1762
 *     outside the event loop (since we cannot destroy the loop from
1763
 *     within the loop).  That will cause lws_context_destroy3() to run
1764
 *     directly.
1765
 *
1766
 *  - lws_context_destroy3() destroys any internal event loops and then
1767
 *     destroys the context itself, setting what was info.pcontext to NULL.
1768
 */
1769
1770
1771
#if defined(LWS_WITH_NETWORK)
1772
static void
1773
lws_pt_destroy(struct lws_context_per_thread *pt)
1774
0
{
1775
0
  volatile struct lws_foreign_thread_pollfd *ftp, *next;
1776
0
  volatile struct lws_context_per_thread *vpt;
1777
#if defined(LWS_WITH_CGI)
1778
  lws_ctx_t ctx = pt->context;
1779
1780
    if (lws_rops_fidx(&role_ops_cgi, LWS_ROPS_pt_init_destroy))
1781
      (lws_rops_func_fidx(&role_ops_cgi, LWS_ROPS_pt_init_destroy)).
1782
        pt_init_destroy(ctx, NULL, pt, 1);
1783
#endif
1784
0
  vpt = (volatile struct lws_context_per_thread *)pt;
1785
0
  ftp = vpt->foreign_pfd_list;
1786
0
  while (ftp) {
1787
0
    next = ftp->next;
1788
0
    lws_free((void *)ftp);
1789
0
    ftp = next;
1790
0
  }
1791
0
  vpt->foreign_pfd_list = NULL;
1792
1793
0
  lws_pt_lock(pt, __func__);
1794
1795
0
  if (pt->pipe_wsi) {
1796
0
    lws_destroy_event_pipe(pt->pipe_wsi);
1797
0
    pt->pipe_wsi = NULL;
1798
0
  }
1799
1800
0
  if (pt->dummy_pipe_fds[0]
1801
0
#if !defined(WIN32)
1802
0
      && (int)pt->dummy_pipe_fds[0] != -1
1803
0
#endif
1804
0
  ) {
1805
0
    struct lws wsi;
1806
1807
0
    memset(&wsi, 0, sizeof(wsi));
1808
0
    wsi.a.context = pt->context;
1809
0
    wsi.tsi = (char)pt->tid;
1810
0
    lws_plat_pipe_close(&wsi);
1811
0
  }
1812
1813
0
#if defined(LWS_WITH_SECURE_STREAMS)
1814
0
  while (pt->ss_owner.head)
1815
0
    lws_ss_destroy_dll(pt->ss_owner.head, NULL);
1816
1817
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API) && defined(LWS_WITH_CLIENT)
1818
  lws_dll2_foreach_safe(&pt->ss_client_owner, NULL, lws_sspc_destroy_dll);
1819
#endif
1820
1821
0
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
1822
0
    while (pt->http.ah_list)
1823
0
      _lws_destroy_ah(pt, pt->http.ah_list);
1824
0
#endif
1825
1826
0
#endif
1827
1828
0
  lws_pt_unlock(pt);
1829
0
  pt->pipe_wsi = NULL;
1830
1831
0
}
1832
#endif
1833
1834
/*
1835
 * Context destruction is now a state machine that's aware of SMP pts and
1836
 * various event lib approaches.
1837
 *
1838
 * lws_context_destroy() expects to be called at the end of the user code's
1839
 * usage of it.  But it can also be called non-finally, as a way to stop
1840
 * service and exit the outer user service loop, and then complete in the
1841
 * final call.
1842
 *
1843
 * For libuv, with async close, it must decide by refcounting the hamdles on
1844
 * the loop if it has extricated itself from the loop and can be destroyed.
1845
 *
1846
 * The various entry states for the staged destroy
1847
 *
1848
 * LWSCD_NO_DESTROY: begin destroy process
1849
 *  - mark context as starting destroy process
1850
 *  - start vhost destroy
1851
 *  - stop any further user protocol service
1852
 *
1853
 * LWSCD_PT_WAS_DEFERRED: come back here if any pt inside service
1854
 *  - Check for pts that are inside service loop, mark deferral needed if so
1855
 *  - If not, close all wsi on the pt loop and start logical pt destroy
1856
 *  - If any deferred, set state to LWSCD_PT_WAS_DEFERRED and exit
1857
 *
1858
 * LWSCD_PT_WAIT_ALL_DESTROYED: come back here for async loop / pt closes
1859
 *  - exit if any pt not marked as unused, or destroyed
1860
 *  - if all pt down, call into evlib to advance context destroy
1861
 *  - finalize vhost destruction
1862
 *  - finalize pt destruction
1863
 *  - if foreign loops, set state to LWSCD_FINALIZATION and exit
1864
 *
1865
 * LWSCD_FINALIZATION: come back here at final lws_destroy_context() call
1866
 *  - destroy sundries
1867
 *  - destroy and free the actual context
1868
 */
1869
1870
void
1871
lws_context_destroy(struct lws_context *context)
1872
0
{
1873
0
  struct lws_context **pcontext_finalize;
1874
0
#if defined(LWS_WITH_NETWORK)
1875
0
  struct lws_context_per_thread *pt;
1876
0
  struct lws_vhost *vh = NULL, *vh1;
1877
0
  int alive = 0, deferred_pt = 0;
1878
0
#endif
1879
#if defined(LWS_WITH_PEER_LIMITS)
1880
  uint32_t nu;
1881
#endif
1882
0
  int n;
1883
1884
0
  if (!context || context->inside_context_destroy)
1885
0
    return;
1886
1887
0
  pcontext_finalize = context->pcontext_finalize;
1888
1889
0
  lws_context_lock(context, __func__);
1890
0
  context->inside_context_destroy = 1;
1891
1892
0
  lwsl_cx_info(context, "destroy_state %d", context->destroy_state);
1893
1894
0
  switch (context->destroy_state) {
1895
0
  case LWSCD_NO_DESTROY:
1896
    /*
1897
     * We're getting started
1898
     */
1899
1900
0
    lwsl_cx_info(context, "starting context destroy flow");
1901
0
    context->being_destroyed = 1;
1902
1903
0
#if defined(LWS_WITH_NETWORK)
1904
1905
    /*
1906
     * Close any vhost listen wsi
1907
     *
1908
     * inform all the protocols that they are done and will have no
1909
     * more callbacks.
1910
     *
1911
     * We can't free things until after the event loop shuts down.
1912
     */
1913
1914
0
    if (context->protocol_init_done)
1915
0
      vh = context->vhost_list;
1916
1917
0
    while (vh) {
1918
0
      lwsl_vhost_info(vh, "start close");
1919
0
      vh1 = vh->vhost_next;
1920
0
      lws_vhost_destroy1(vh);
1921
0
      vh = vh1;
1922
0
    }
1923
0
#endif
1924
1925
0
    lws_plat_context_early_destroy(context);
1926
1927
0
    context->service_no_longer_possible = 1;
1928
0
    context->requested_stop_internal_loops = 1;
1929
1930
    /* fallthru */
1931
1932
0
  case LWSCD_PT_WAS_DEFERRED:
1933
1934
0
#if defined(LWS_WITH_NETWORK)
1935
1936
    /*
1937
     * We want to mark the pts as their destruction having been
1938
     * initiated, so they will reject any new wsi, and iterate all
1939
     * existing pt wsi starting to close them.
1940
     *
1941
     * If the event loop has async close, we have to return after
1942
     * this and try again when all the loops stop after all the
1943
     * refcounted wsi are gone.
1944
     */
1945
1946
0
    pt = context->pt;
1947
0
    for (n = 0; n < context->count_threads; n++) {
1948
0
      lws_pt_lock(pt, __func__);
1949
1950
      /* evlib will realize it needs to destroy pt */
1951
0
      pt->destroy_self = 1;
1952
1953
0
      if (pt->inside_lws_service) {
1954
0
        pt->event_loop_pt_unused = 1;
1955
0
        deferred_pt = 1;
1956
0
        goto next;
1957
0
      }
1958
1959
      /*
1960
       * Close every handle in the fds
1961
       */
1962
1963
0
      while (pt->fds_count) {
1964
0
        struct lws *wsi = wsi_from_fd(context,
1965
0
                    pt->fds[0].fd);
1966
1967
0
        if (wsi) {
1968
1969
0
          lwsl_cx_debug(context,
1970
0
            "pt %d: closing wsi %p: role %s",
1971
0
            n, wsi, wsi->role_ops->name);
1972
1973
0
          lws_close_free_wsi(wsi,
1974
0
            LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY,
1975
0
            "ctx destroy"
1976
0
            /* no protocol close */);
1977
1978
0
          if (pt->pipe_wsi == wsi)
1979
0
            pt->pipe_wsi = NULL;
1980
0
        }
1981
0
      }
1982
1983
#if defined(LWS_WITH_CGI)
1984
      (lws_rops_func_fidx(&role_ops_cgi,
1985
              LWS_ROPS_pt_init_destroy)).
1986
              pt_init_destroy(context, NULL,
1987
                  pt, 1);
1988
#endif
1989
1990
      /*
1991
       * This closes handles that belong to the evlib pt
1992
       * footprint, eg, timers, idle
1993
       */
1994
1995
0
      if (context->event_loop_ops->destroy_pt) {
1996
0
        lwsl_cx_info(context,
1997
0
               "calling evlib destroy_pt %d\n", n);
1998
0
        context->event_loop_ops->destroy_pt(context, n);
1999
0
      }
2000
2001
0
next:
2002
0
      lws_pt_unlock(pt);
2003
2004
0
      pt++;
2005
0
    }
2006
2007
0
    if (deferred_pt) {
2008
0
      context->destroy_state = LWSCD_PT_WAS_DEFERRED;
2009
0
      lwsl_cx_notice(context, "destroy from inside service");
2010
0
      lws_cancel_service(context);
2011
0
      goto bail;
2012
0
    }
2013
0
#endif
2014
0
    context->destroy_state = LWSCD_PT_WAIT_ALL_DESTROYED;
2015
2016
    /*
2017
     * We have different needs depending if foreign loop or not.
2018
     *
2019
     * 1) If foreign loop, we really want to advance the
2020
     *    destroy_context() past here, and block only for libuv-
2021
     *    style async close completion.
2022
     *
2023
     * 2a) If poll, and we exited by ourselves and are calling a
2024
     *     final destroy_context() outside of any service already,
2025
     *     we want to advance all the way in one step.
2026
     *
2027
     * 2b) If poll, and we are reacting to a SIGINT, service
2028
     *     thread(s) may be in poll wait or servicing.  We can't
2029
     *     advance the destroy_context() to the point it's freeing
2030
     *     things; we have to leave that for the final
2031
     *     destroy_context() after the service thread(s) are
2032
     *     finished calling for service.
2033
     */
2034
2035
0
#if defined(LWS_WITH_NETWORK)
2036
2037
#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)
2038
    lws_ss_proxy_destroy(context);
2039
#endif
2040
2041
0
    if (context->event_loop_ops->destroy_context1) {
2042
0
      lwsl_cx_info(context, "do evlib destroy_context1 and wait");
2043
0
      context->event_loop_ops->destroy_context1(context);
2044
2045
0
      goto bail;
2046
0
    }
2047
2048
    /*
2049
     * ...if the more typical sync close, we can clean up the pts
2050
     * now ourselves...
2051
     */
2052
2053
0
    lwsl_cx_info(context, "manually destroying pts");
2054
2055
0
    pt = context->pt;
2056
0
    for (n = 0; n < context->count_threads; n++, pt++) {
2057
0
      pt->event_loop_pt_unused = 1;
2058
0
      lws_pt_destroy(pt);
2059
0
    }
2060
0
#endif
2061
    /* fallthru */
2062
2063
0
  case LWSCD_PT_WAIT_ALL_DESTROYED:
2064
2065
0
#if defined(LWS_WITH_NETWORK)
2066
2067
0
    for (n = 0; n < context->count_threads; n++)
2068
0
      if (!context->pt[n].is_destroyed &&
2069
0
          !context->pt[n].event_loop_pt_unused)
2070
0
        alive++;
2071
2072
0
    lwsl_cx_info(context, "PT_WAIT_ALL_DESTROYED: %d alive", alive);
2073
2074
0
    if (alive)
2075
0
      break;
2076
2077
    /*
2078
     * With foreign loops, removing all our fds from the loop
2079
     * means there are no more ways for the foreign loop to give
2080
     * us any further CPU once we leave here... so we must make
2081
     * sure related service threads are exiting so we can pick up
2082
     * again at the original app thread and do the context
2083
     * destroy completion
2084
     */
2085
2086
    /*
2087
     * evlib specific loop destroy?
2088
     */
2089
0
    if (context->event_loop_ops->destroy_context2)
2090
      /*
2091
       * He returns nonzero to indicate the evlib must
2092
       * continue around the loop before destroy of it is
2093
       * completed so it can be freed
2094
       */
2095
0
      context->event_loop_ops->destroy_context2(context);
2096
0
    context->requested_stop_internal_loops = 1;
2097
0
#endif
2098
2099
    /*
2100
     * Every pt and wsi that may depend on the logical vhosts
2101
     * is destroyed.  We can remove the logical vhosts.
2102
     */
2103
2104
0
#if defined(LWS_WITH_SYS_STATE) && defined(LWS_WITH_NETWORK)
2105
0
  lws_state_transition(&context->mgr_system, LWS_SYSTATE_POLICY_INVALID);
2106
0
#endif
2107
2108
0
#if defined(LWS_WITH_NETWORK)
2109
    /*
2110
     * free all the per-vhost allocations
2111
     */
2112
2113
0
    vh = context->vhost_list;
2114
0
    while (vh) {
2115
0
      vh1 = vh->vhost_next;
2116
    //  lwsl_vhost_debug(vh, "vh %s destroy2", vh->name);
2117
0
      __lws_vhost_destroy2(vh);
2118
0
      vh = vh1;
2119
0
    }
2120
2121
    /* remove ourselves from the pending destruction list */
2122
2123
0
    while (context->vhost_pending_destruction_list)
2124
      /* removes itself from list */
2125
0
      __lws_vhost_destroy2(context->vhost_pending_destruction_list);
2126
0
#endif
2127
2128
0
#if defined(LWS_WITH_NETWORK)
2129
0
    lws_ssl_context_destroy(context);
2130
0
#endif
2131
0
    lws_plat_context_late_destroy(context);
2132
2133
#if defined(LWS_WITH_PEER_LIMITS)
2134
    if (context->pl_hash_table)
2135
      for (nu = 0; nu < context->pl_hash_elements; nu++)  {
2136
        if (!context->pl_hash_table[nu])
2137
          continue;
2138
        lws_start_foreach_llp(struct lws_peer **, peer,
2139
                  context->pl_hash_table[nu]) {
2140
          struct lws_peer *df = *peer;
2141
          *peer = df->next;
2142
          lws_free(df);
2143
          continue;
2144
        } lws_end_foreach_llp(peer, next);
2145
      }
2146
    lws_free(context->pl_hash_table);
2147
#endif
2148
2149
0
#if defined(LWS_WITH_NETWORK)
2150
2151
0
    for (n = 0; n < context->count_threads; n++) {
2152
0
      struct lws_context_per_thread *pt = &context->pt[n];
2153
2154
0
      (void)pt;
2155
2156
0
      LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) {
2157
0
        if (lws_rops_fidx(ar, LWS_ROPS_pt_init_destroy))
2158
0
          (lws_rops_func_fidx(ar, LWS_ROPS_pt_init_destroy)).
2159
0
            pt_init_destroy(context, NULL, pt, 1);
2160
0
      } LWS_FOR_EVERY_AVAILABLE_ROLE_END;
2161
2162
#if defined(LWS_WITH_CGI)
2163
      lws_rops_func_fidx(&role_ops_cgi,
2164
             LWS_ROPS_pt_init_destroy).
2165
                  pt_init_destroy(context, NULL,
2166
                      pt, 1);
2167
#endif
2168
2169
0
#if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
2170
0
      while (pt->http.ah_list)
2171
0
        _lws_destroy_ah(pt, pt->http.ah_list);
2172
0
#endif
2173
0
      lwsl_cx_info(context, "pt destroy %d", n);
2174
0
      lws_pt_destroy(pt);
2175
0
    }
2176
0
#endif /* NETWORK */
2177
2178
0
    context->destroy_state = LWSCD_FINALIZATION;
2179
2180
0
#if defined(LWS_WITH_NETWORK)
2181
2182
0
    if (context->pt[0].event_loop_foreign &&
2183
0
        context->event_loop_ops->destroy_context1) {
2184
2185
0
      lwsl_cx_info(context,
2186
0
            "leaving final context destruction"
2187
0
          " for final call");
2188
0
      goto bail;
2189
0
    }
2190
2191
0
    if (context->event_loop_ops->destroy_context1 &&
2192
0
        !context->pt[0].event_loop_foreign) {
2193
0
      lwsl_cx_notice(context, "waiting for internal loop exit");
2194
2195
0
      goto bail;
2196
0
    }
2197
0
#endif
2198
    /* fallthru */
2199
2200
0
  case LWSCD_FINALIZATION:
2201
2202
#if defined(LWS_WITH_SYS_METRICS)
2203
    lws_metrics_dump(context);
2204
#endif
2205
2206
0
    context->evlib_finalize_destroy_after_int_loops_stop = 1;
2207
2208
0
#if defined(LWS_WITH_NETWORK)
2209
0
    if (context->event_loop_ops->destroy_context2)
2210
0
      context->event_loop_ops->destroy_context2(context);
2211
0
#if defined(LWS_WITH_SYS_STATE)
2212
0
    lws_state_transition_steps(&context->mgr_system,
2213
0
             LWS_SYSTATE_CONTEXT_DESTROYING);
2214
0
#endif
2215
    /*
2216
     * finalize destroy of pt and things hanging off it
2217
     */
2218
2219
0
    for (n = 0; n < context->count_threads; n++) {
2220
0
      struct lws_context_per_thread *pt = &context->pt[n];
2221
2222
      /*
2223
       * Destroy the pt-roles
2224
       */
2225
2226
0
      LWS_FOR_EVERY_AVAILABLE_ROLE_START(ar) {
2227
0
        if (lws_rops_fidx(ar, LWS_ROPS_pt_init_destroy))
2228
0
          (lws_rops_func_fidx(ar, LWS_ROPS_pt_init_destroy)).
2229
0
              pt_init_destroy(context, NULL, pt, 1);
2230
0
      } LWS_FOR_EVERY_AVAILABLE_ROLE_END;
2231
2232
    #if defined(LWS_WITH_CGI)
2233
      lws_rops_func_fidx(&role_ops_cgi, LWS_ROPS_pt_init_destroy).
2234
            pt_init_destroy(context, NULL, pt, 1);
2235
    #endif
2236
2237
0
      lws_pt_mutex_destroy(pt);
2238
0
      assert(!pt->is_destroyed);
2239
0
      pt->destroy_self = 0;
2240
0
      pt->is_destroyed = 1;
2241
2242
0
      lwsl_cx_info(context, "pt %d fully destroyed",
2243
0
          (int)(pt - pt->context->pt));
2244
0
    }
2245
2246
    /*
2247
     * wsis are gone, pts are gone, vhosts are gone.
2248
     *
2249
     * clean up the context and things hanging off it
2250
     */
2251
2252
#if defined(LWS_WITH_TLS_JIT_TRUST)
2253
    lws_cache_destroy(&context->trust_cache);
2254
    lws_tls_jit_trust_inflight_destroy_all(context);
2255
#endif
2256
2257
0
#if defined(LWS_WITH_CACHE_NSCOOKIEJAR) && defined(LWS_WITH_CLIENT)
2258
0
    lws_cache_destroy(&context->nsc);
2259
0
    lws_cache_destroy(&context->l1);
2260
0
#endif
2261
2262
0
#if defined(LWS_WITH_SYS_SMD)
2263
0
    _lws_smd_destroy(context);
2264
0
#endif
2265
2266
#if defined(LWS_WITH_SYS_ASYNC_DNS)
2267
    lws_async_dns_deinit(&context->async_dns);
2268
#endif
2269
#if defined(LWS_WITH_SYS_DHCP_CLIENT)
2270
    lws_dhcpc_remove(context, NULL);
2271
#endif
2272
2273
0
#if defined(LWS_WITH_DLO)
2274
0
    lws_fonts_destroy(context);
2275
0
    lws_dlo_file_destroy(context);
2276
0
#endif
2277
2278
0
    if (context->pt[0].fds)
2279
0
      lws_free_set_NULL(context->pt[0].fds);
2280
0
#endif
2281
0
    lws_context_deinit_ssl_library(context);
2282
2283
#if defined(LWS_WITH_DETAILED_LATENCIES)
2284
    if (context->latencies_fd != -1)
2285
      compatible_close(context->latencies_fd);
2286
#endif
2287
2288
0
    for (n = 0; n < LWS_SYSBLOB_TYPE_COUNT; n++)
2289
0
      lws_system_blob_destroy(
2290
0
          lws_system_get_blob(context, (lws_system_blob_item_t)n, 0));
2291
2292
0
#if defined(LWS_WITH_NETWORK) && defined(LWS_WITH_SECURE_STREAMS) && \
2293
0
  !defined(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY)
2294
2295
0
    while (context->server_der_list) {
2296
0
      struct lws_ss_x509 *x = context->server_der_list;
2297
2298
0
      context->server_der_list = x->next;
2299
0
      lws_free((void *)x->ca_der);
2300
0
    }
2301
2302
0
    if (context->ac_policy)
2303
0
      lwsac_free(&context->ac_policy);
2304
2305
0
#if defined(LWS_WITH_SERVER)
2306
    /* ... for every sink... */
2307
0
    lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
2308
0
              lws_dll2_get_head(&context->sinks)) {
2309
0
      lws_ss_sinks_t *sn = lws_container_of(d, lws_ss_sinks_t,
2310
0
                    list);
2311
2312
0
      assert(!sn->accepts.count);
2313
2314
0
      lws_dll2_remove(&sn->list);
2315
0
      lws_free(sn);
2316
2317
0
    } lws_end_foreach_dll_safe(d, d1);
2318
0
#endif
2319
0
#endif
2320
2321
    /*
2322
     * Context lock is about to go away
2323
     */
2324
2325
0
    lws_context_unlock(context);
2326
2327
#if LWS_MAX_SMP > 1
2328
    lws_mutex_refcount_destroy(&context->mr);
2329
#endif
2330
2331
#if defined(LWS_WITH_SYS_METRICS) && defined(LWS_WITH_NETWORK)
2332
    lws_metrics_destroy(context);
2333
#endif
2334
2335
0
    if (context->external_baggage_free_on_destroy)
2336
0
      free(context->external_baggage_free_on_destroy);
2337
2338
#if defined(LWS_PLAT_FREERTOS)
2339
#if defined(LWS_AMAZON_RTOS)
2340
    context->last_free_heap = xPortGetFreeHeapSize();
2341
#else
2342
    context->last_free_heap = esp_get_free_heap_size();
2343
#endif
2344
#endif
2345
2346
#if defined(LWS_WITH_EVLIB_PLUGINS) && defined(LWS_WITH_EVENT_LIBS)
2347
    if (context->evlib_plugin_list)
2348
      lws_plugins_destroy(&context->evlib_plugin_list,
2349
              NULL, NULL);
2350
#endif
2351
2352
#if defined(LWS_WITH_SYS_FAULT_INJECTION)
2353
    lws_fi_destroy(&context->fic);
2354
#endif
2355
2356
0
    lwsl_refcount_cx(context->log_cx, -1);
2357
2358
0
    lws_free(context);
2359
2360
0
    if (pcontext_finalize)
2361
0
      *pcontext_finalize = NULL;
2362
2363
0
    return;
2364
0
  }
2365
2366
0
#if defined(LWS_WITH_NETWORK)
2367
0
bail:
2368
0
#endif
2369
0
  lwsl_cx_info(context, "leaving");
2370
0
  context->inside_context_destroy = 0;
2371
0
  lws_context_unlock(context);
2372
0
}
2373
2374
int
2375
lws_context_is_being_destroyed(struct lws_context *context)
2376
0
{
2377
0
  return !!context->being_destroyed;
2378
0
}
2379
2380
#if defined(LWS_WITH_SYS_STATE)
2381
struct lws_context *
2382
lws_system_context_from_system_mgr(lws_state_manager_t *mgr)
2383
0
{
2384
0
#if defined(LWS_WITH_NETWORK)
2385
0
  return mgr->context;
2386
#else
2387
  return NULL;
2388
#endif
2389
0
}
2390
#endif
2391
2392
void
2393
lws_log_prepend_context(struct lws_log_cx *cx, void *obj, char **p, char *e)
2394
0
{
2395
0
  struct lws_context *lcx = (struct lws_context *)obj;
2396
2397
0
  if (lcx->name)
2398
0
    *p += lws_snprintf(*p, lws_ptr_diff_size_t(e, (*p)), "%s: ",
2399
0
           lcx->name);
2400
0
}
2401
2402
struct lws_log_cx *
2403
lwsl_context_get_cx(struct lws_context *cx)
2404
0
{
2405
0
  if (!cx)
2406
0
    return NULL;
2407
2408
0
  return cx->log_cx;
2409
0
}