b9f317bb78fa6a3f9104849ff55edce859a947f0
[platform/upstream/libwebsockets.git] / lib / lws-plat-unix.c
1 #include "private-libwebsockets.h"
2
3 #include <pwd.h>
4 #include <grp.h>
5
6 #ifdef LWS_WITH_PLUGINS
7 #include <dlfcn.h>
8 #endif
9 #include <dirent.h>
10
11
12 /*
13  * included from libwebsockets.c for unix builds
14  */
15
16 unsigned long long time_in_microseconds(void)
17 {
18         struct timeval tv;
19         gettimeofday(&tv, NULL);
20         return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
21 }
22
23 LWS_VISIBLE int
24 lws_get_random(struct lws_context *context, void *buf, int len)
25 {
26         return read(context->fd_random, (char *)buf, len);
27 }
28
29 LWS_VISIBLE int
30 lws_send_pipe_choked(struct lws *wsi)
31 {
32         struct lws_pollfd fds;
33
34         /* treat the fact we got a truncated send pending as if we're choked */
35         if (wsi->trunc_len)
36                 return 1;
37
38         fds.fd = wsi->desc.sockfd;
39         fds.events = POLLOUT;
40         fds.revents = 0;
41
42         if (poll(&fds, 1, 0) != 1)
43                 return 1;
44
45         if ((fds.revents & POLLOUT) == 0)
46                 return 1;
47
48         /* okay to send another packet without blocking */
49
50         return 0;
51 }
52
53 LWS_VISIBLE int
54 lws_poll_listen_fd(struct lws_pollfd *fd)
55 {
56         return poll(fd, 1, 0);
57 }
58
59 LWS_VISIBLE void
60 lws_cancel_service_pt(struct lws *wsi)
61 {
62         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
63         char buf = 0;
64
65         if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
66                 lwsl_err("Cannot write to dummy pipe");
67 }
68
69 LWS_VISIBLE void
70 lws_cancel_service(struct lws_context *context)
71 {
72         struct lws_context_per_thread *pt = &context->pt[0];
73         char buf = 0, m = context->count_threads;
74
75         while (m--) {
76                 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77                         lwsl_err("Cannot write to dummy pipe");
78                 pt++;
79         }
80 }
81
82 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
83 {
84         int syslog_level = LOG_DEBUG;
85
86         switch (level) {
87         case LLL_ERR:
88                 syslog_level = LOG_ERR;
89                 break;
90         case LLL_WARN:
91                 syslog_level = LOG_WARNING;
92                 break;
93         case LLL_NOTICE:
94                 syslog_level = LOG_NOTICE;
95                 break;
96         case LLL_INFO:
97                 syslog_level = LOG_INFO;
98                 break;
99         }
100         syslog(syslog_level, "%s", line);
101 }
102
103 LWS_VISIBLE LWS_EXTERN int
104 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
105 {
106         struct lws_context_per_thread *pt;
107         int n = -1, m, c;
108         char buf;
109
110         /* stay dead once we are dead */
111
112         if (!context || !context->vhost_list)
113                 return 1;
114
115         pt = &context->pt[tsi];
116
117         lws_stats_atomic_bump(context, pt, LWSSTATS_C_SERVICE_ENTRY, 1);
118
119         if (timeout_ms < 0)
120                 goto faked_service;
121
122         lws_libev_run(context, tsi);
123         lws_libuv_run(context, tsi);
124         lws_libevent_run(context, tsi);
125
126         if (!context->service_tid_detected) {
127                 struct lws _lws;
128
129                 memset(&_lws, 0, sizeof(_lws));
130                 _lws.context = context;
131
132                 context->service_tid_detected =
133                         context->vhost_list->protocols[0].callback(
134                         &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
135         }
136         context->service_tid = context->service_tid_detected;
137
138         /*
139          * is there anybody with pending stuff that needs service forcing?
140          */
141         if (!lws_service_adjust_timeout(context, 1, tsi)) {
142                 /* -1 timeout means just do forced service */
143                 _lws_plat_service_tsi(context, -1, pt->tid);
144                 /* still somebody left who wants forced service? */
145                 if (!lws_service_adjust_timeout(context, 1, pt->tid))
146                         /* yes... come back again quickly */
147                         timeout_ms = 0;
148         }
149
150         n = poll(pt->fds, pt->fds_count, timeout_ms);
151
152 #ifdef LWS_OPENSSL_SUPPORT
153         if (!pt->rx_draining_ext_list &&
154             !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
155 #else
156         if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
157 #endif
158                 lws_service_fd_tsi(context, NULL, tsi);
159                 return 0;
160         }
161
162 faked_service:
163         m = lws_service_flag_pending(context, tsi);
164         if (m)
165                 c = -1; /* unknown limit */
166         else
167                 if (n < 0) {
168                         if (LWS_ERRNO != LWS_EINTR)
169                                 return -1;
170                         return 0;
171                 } else
172                         c = n;
173
174         /* any socket with events to service? */
175         for (n = 0; n < pt->fds_count && c; n++) {
176                 if (!pt->fds[n].revents)
177                         continue;
178
179                 c--;
180
181                 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
182                         if (read(pt->fds[n].fd, &buf, 1) != 1)
183                                 lwsl_err("Cannot read from dummy pipe.");
184                         continue;
185                 }
186
187                 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
188                 if (m < 0)
189                         return -1;
190                 /* if something closed, retry this slot */
191                 if (m)
192                         n--;
193         }
194
195         return 0;
196 }
197
198 LWS_VISIBLE int
199 lws_plat_check_connection_error(struct lws *wsi)
200 {
201         return 0;
202 }
203
204 LWS_VISIBLE int
205 lws_plat_service(struct lws_context *context, int timeout_ms)
206 {
207         return _lws_plat_service_tsi(context, timeout_ms, 0);
208 }
209
210 LWS_VISIBLE int
211 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
212 {
213         int optval = 1;
214         socklen_t optlen = sizeof(optval);
215
216 #if defined(__APPLE__) || \
217     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
218     defined(__NetBSD__) || \
219     defined(__OpenBSD__)
220         struct protoent *tcp_proto;
221 #endif
222
223         if (vhost->ka_time) {
224                 /* enable keepalive on this socket */
225                 optval = 1;
226                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
227                                (const void *)&optval, optlen) < 0)
228                         return 1;
229
230 #if defined(__APPLE__) || \
231     defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
232     defined(__NetBSD__) || \
233         defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
234
235                 /*
236                  * didn't find a way to set these per-socket, need to
237                  * tune kernel systemwide values
238                  */
239 #else
240                 /* set the keepalive conditions we want on it too */
241                 optval = vhost->ka_time;
242                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
243                                (const void *)&optval, optlen) < 0)
244                         return 1;
245
246                 optval = vhost->ka_interval;
247                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
248                                (const void *)&optval, optlen) < 0)
249                         return 1;
250
251                 optval = vhost->ka_probes;
252                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
253                                (const void *)&optval, optlen) < 0)
254                         return 1;
255 #endif
256         }
257
258         /* Disable Nagle */
259         optval = 1;
260 #if defined (__sun)
261         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
262                 return 1;
263 #elif !defined(__APPLE__) && \
264       !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
265       !defined(__NetBSD__) && \
266       !defined(__OpenBSD__)
267         if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
268                 return 1;
269 #else
270         tcp_proto = getprotobyname("TCP");
271         if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
272                 return 1;
273 #endif
274
275         /* We are nonblocking... */
276         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
277                 return 1;
278
279         return 0;
280 }
281
282 LWS_VISIBLE void
283 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
284 {
285         if (info->gid != -1)
286                 if (setgid(info->gid))
287                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
288
289         if (info->uid != -1) {
290                 struct passwd *p = getpwuid(info->uid);
291
292                 if (p) {
293                         initgroups(p->pw_name, info->gid);
294                         if (setuid(info->uid))
295                                 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
296                         else
297                                 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
298                 } else
299                         lwsl_warn("getpwuid: unable to find uid %d", info->uid);
300         }
301 }
302
303 #ifdef LWS_WITH_PLUGINS
304
305 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
306
307 /* libuv.c implements these in a cross-platform way */
308
309 #else
310
311 static int filter(const struct dirent *ent)
312 {
313         if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
314                 return 0;
315
316         return 1;
317 }
318
319 LWS_VISIBLE int
320 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
321 {
322         struct lws_plugin_capability lcaps;
323         struct lws_plugin *plugin;
324         lws_plugin_init_func initfunc;
325         struct dirent **namelist;
326         int n, i, m, ret = 0;
327         char path[256];
328         void *l;
329
330         lwsl_notice("  Plugins:\n");
331
332         while (d && *d) {
333                 n = scandir(*d, &namelist, filter, alphasort);
334                 if (n < 0) {
335                         lwsl_err("Scandir on %s failed\n", *d);
336                         return 1;
337                 }
338
339                 for (i = 0; i < n; i++) {
340                         if (strlen(namelist[i]->d_name) < 7)
341                                 goto inval;
342
343                         lwsl_notice("   %s\n", namelist[i]->d_name);
344
345                         lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
346                                  namelist[i]->d_name);
347                         l = dlopen(path, RTLD_NOW);
348                         if (!l) {
349                                 lwsl_err("Error loading DSO: %s\n", dlerror());
350                                 while (i++ < n)
351                                         free(namelist[i]);
352                                 goto bail;
353                         }
354                         /* we could open it, can we get his init function? */
355                         m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
356                                      namelist[i]->d_name + 3 /* snip lib... */);
357                         path[m - 3] = '\0'; /* snip the .so */
358                         initfunc = dlsym(l, path);
359                         if (!initfunc) {
360                                 lwsl_err("Failed to get init on %s: %s",
361                                                 namelist[i]->d_name, dlerror());
362                                 dlclose(l);
363                         }
364                         lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
365                         m = initfunc(context, &lcaps);
366                         if (m) {
367                                 lwsl_err("Initializing %s failed %d\n",
368                                         namelist[i]->d_name, m);
369                                 dlclose(l);
370                                 goto skip;
371                         }
372
373                         plugin = lws_malloc(sizeof(*plugin));
374                         if (!plugin) {
375                                 lwsl_err("OOM\n");
376                                 goto bail;
377                         }
378                         plugin->list = context->plugin_list;
379                         context->plugin_list = plugin;
380                         strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
381                         plugin->name[sizeof(plugin->name) - 1] = '\0';
382                         plugin->l = l;
383                         plugin->caps = lcaps;
384                         context->plugin_protocol_count += lcaps.count_protocols;
385                         context->plugin_extension_count += lcaps.count_extensions;
386
387                         free(namelist[i]);
388                         continue;
389
390         skip:
391                         dlclose(l);
392         inval:
393                         free(namelist[i]);
394                 }
395                 free(namelist);
396                 d++;
397         }
398
399 bail:
400         free(namelist);
401
402         return ret;
403 }
404
405 LWS_VISIBLE int
406 lws_plat_plugins_destroy(struct lws_context * context)
407 {
408         struct lws_plugin *plugin = context->plugin_list, *p;
409         lws_plugin_destroy_func func;
410         char path[256];
411         int m;
412
413         if (!plugin)
414                 return 0;
415
416         lwsl_notice("%s\n", __func__);
417
418         while (plugin) {
419                 p = plugin;
420                 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
421                 path[m - 3] = '\0';
422                 func = dlsym(plugin->l, path);
423                 if (!func) {
424                         lwsl_err("Failed to get destroy on %s: %s",
425                                         plugin->name, dlerror());
426                         goto next;
427                 }
428                 m = func(context);
429                 if (m)
430                         lwsl_err("Initializing %s failed %d\n",
431                                 plugin->name, m);
432 next:
433                 dlclose(p->l);
434                 plugin = p->list;
435                 p->list = NULL;
436                 free(p);
437         }
438
439         context->plugin_list = NULL;
440
441         return 0;
442 }
443
444 #endif
445 #endif
446
447
448 #if 0
449 static void
450 sigabrt_handler(int x)
451 {
452         printf("%s\n", __func__);
453         //*(char *)0 = 0;
454 }
455 #endif
456
457 LWS_VISIBLE int
458 lws_plat_context_early_init(void)
459 {
460 #if !defined(LWS_AVOID_SIGPIPE_IGN)
461         signal(SIGPIPE, SIG_IGN);
462 #endif
463
464 //      signal(SIGABRT, sigabrt_handler);
465
466         return 0;
467 }
468
469 LWS_VISIBLE void
470 lws_plat_context_early_destroy(struct lws_context *context)
471 {
472 }
473
474 LWS_VISIBLE void
475 lws_plat_context_late_destroy(struct lws_context *context)
476 {
477         struct lws_context_per_thread *pt = &context->pt[0];
478         int m = context->count_threads;
479
480 #ifdef LWS_WITH_PLUGINS
481         if (context->plugin_list)
482                 lws_plat_plugins_destroy(context);
483 #endif
484
485         if (context->lws_lookup)
486                 lws_free(context->lws_lookup);
487
488         while (m--) {
489                 if (pt->dummy_pipe_fds[0])
490                         close(pt->dummy_pipe_fds[0]);
491                 if (pt->dummy_pipe_fds[1])
492                         close(pt->dummy_pipe_fds[1]);
493                 pt++;
494         }
495         if (!context->fd_random)
496                 lwsl_err("ZERO RANDOM FD\n");
497         if (context->fd_random != LWS_INVALID_FILE)
498                 close(context->fd_random);
499 }
500
501 /* cast a struct sockaddr_in6 * into addr for ipv6 */
502
503 LWS_VISIBLE int
504 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
505                     size_t addrlen)
506 {
507         int rc = -1;
508
509         struct ifaddrs *ifr;
510         struct ifaddrs *ifc;
511 #ifdef LWS_USE_IPV6
512         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
513 #endif
514
515         getifaddrs(&ifr);
516         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
517                 if (!ifc->ifa_addr)
518                         continue;
519
520                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
521
522                 if (strcmp(ifc->ifa_name, ifname))
523                         continue;
524
525                 switch (ifc->ifa_addr->sa_family) {
526                 case AF_INET:
527 #ifdef LWS_USE_IPV6
528                         if (ipv6) {
529                                 /* map IPv4 to IPv6 */
530                                 bzero((char *)&addr6->sin6_addr,
531                                                 sizeof(struct in6_addr));
532                                 addr6->sin6_addr.s6_addr[10] = 0xff;
533                                 addr6->sin6_addr.s6_addr[11] = 0xff;
534                                 memcpy(&addr6->sin6_addr.s6_addr[12],
535                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
536                                                         sizeof(struct in_addr));
537                         } else
538 #endif
539                                 memcpy(addr,
540                                         (struct sockaddr_in *)ifc->ifa_addr,
541                                                     sizeof(struct sockaddr_in));
542                         break;
543 #ifdef LWS_USE_IPV6
544                 case AF_INET6:
545                         memcpy(&addr6->sin6_addr,
546                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
547                                                        sizeof(struct in6_addr));
548                         break;
549 #endif
550                 default:
551                         continue;
552                 }
553                 rc = 0;
554         }
555
556         freeifaddrs(ifr);
557
558         if (rc == -1) {
559                 /* check if bind to IP address */
560 #ifdef LWS_USE_IPV6
561                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
562                         rc = 0;
563                 else
564 #endif
565                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
566                         rc = 0;
567         }
568
569         return rc;
570 }
571
572 LWS_VISIBLE void
573 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
574 {
575         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
576
577         lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
578         lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
579         lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
580
581         pt->fds[pt->fds_count++].revents = 0;
582 }
583
584 LWS_VISIBLE void
585 lws_plat_delete_socket_from_fds(struct lws_context *context,
586                                                 struct lws *wsi, int m)
587 {
588         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
589
590         lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
591         lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
592         lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
593
594         pt->fds_count--;
595 }
596
597 LWS_VISIBLE void
598 lws_plat_service_periodic(struct lws_context *context)
599 {
600         /* if our parent went down, don't linger around */
601         if (context->started_with_parent &&
602             kill(context->started_with_parent, 0) < 0)
603                 kill(getpid(), SIGTERM);
604 }
605
606 LWS_VISIBLE int
607 lws_plat_change_pollfd(struct lws_context *context,
608                       struct lws *wsi, struct lws_pollfd *pfd)
609 {
610         return 0;
611 }
612
613 LWS_VISIBLE const char *
614 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
615 {
616         return inet_ntop(af, src, dst, cnt);
617 }
618
619 LWS_VISIBLE lws_fop_fd_t
620 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
621                     const char *vpath, lws_fop_flags_t *flags)
622 {
623         struct stat stat_buf;
624         int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
625         lws_fop_fd_t fop_fd;
626
627         if (ret < 0)
628                 return NULL;
629
630         if (fstat(ret, &stat_buf) < 0)
631                 goto bail;
632
633         fop_fd = malloc(sizeof(*fop_fd));
634         if (!fop_fd)
635                 goto bail;
636
637         fop_fd->fops = fops;
638         fop_fd->flags = *flags;
639         fop_fd->fd = ret;
640         fop_fd->filesystem_priv = NULL; /* we don't use it */
641         fop_fd->len = stat_buf.st_size;
642         fop_fd->pos = 0;
643
644         return fop_fd;
645
646 bail:
647         close(ret);
648         return NULL;
649 }
650
651 LWS_VISIBLE int
652 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
653 {
654         int fd = (*fop_fd)->fd;
655
656         free(*fop_fd);
657         *fop_fd = NULL;
658
659         return close(fd);
660 }
661
662 LWS_VISIBLE lws_fileofs_t
663 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
664 {
665         lws_fileofs_t r;
666
667         if (offset > 0 && offset > fop_fd->len - fop_fd->pos)
668                 offset = fop_fd->len - fop_fd->pos;
669
670         if ((lws_fileofs_t)fop_fd->pos + offset < 0)
671                 offset = -fop_fd->pos;
672
673         r = lseek(fop_fd->fd, offset, SEEK_CUR);
674
675         if (r >= 0)
676                 fop_fd->pos = r;
677         else
678                 lwsl_err("error seeking from cur %ld, offset %ld\n",
679                         (long)fop_fd->pos, (long)offset);
680
681         return r;
682 }
683
684 LWS_VISIBLE int
685 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
686                     uint8_t *buf, lws_filepos_t len)
687 {
688         long n;
689
690         n = read((int)fop_fd->fd, buf, len);
691         if (n == -1) {
692                 *amount = 0;
693                 return -1;
694         }
695         fop_fd->pos += n;
696         lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__, n,
697                   (long)len, (long)fop_fd->pos, (long)fop_fd->len);
698         *amount = n;
699
700         return 0;
701 }
702
703 LWS_VISIBLE int
704 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
705                      uint8_t *buf, lws_filepos_t len)
706 {
707         long n;
708
709         n = write((int)fop_fd->fd, buf, len);
710         if (n == -1) {
711                 *amount = 0;
712                 return -1;
713         }
714
715         fop_fd->pos += n;
716         *amount = n;
717
718         return 0;
719 }
720
721
722 LWS_VISIBLE int
723 lws_plat_init(struct lws_context *context,
724               struct lws_context_creation_info *info)
725 {
726         struct lws_context_per_thread *pt = &context->pt[0];
727         int n = context->count_threads, fd;
728
729         /* master context has the global fd lookup array */
730         context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
731                                          context->max_fds);
732         if (context->lws_lookup == NULL) {
733                 lwsl_err("OOM on lws_lookup array for %d connections\n",
734                          context->max_fds);
735                 return 1;
736         }
737
738         lwsl_notice(" mem: platform fd map: %5lu bytes\n",
739                     (unsigned long)(sizeof(struct lws *) * context->max_fds));
740         fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
741
742         context->fd_random = fd;
743         if (context->fd_random < 0) {
744                 lwsl_err("Unable to open random device %s %d\n",
745                          SYSTEM_RANDOM_FILEPATH, context->fd_random);
746                 return 1;
747         }
748
749         if (!lws_libev_init_fd_table(context) &&
750             !lws_libuv_init_fd_table(context) &&
751             !lws_libevent_init_fd_table(context)) {
752                 /* otherwise libev handled it instead */
753
754                 while (n--) {
755                         if (pipe(pt->dummy_pipe_fds)) {
756                                 lwsl_err("Unable to create pipe\n");
757                                 return 1;
758                         }
759
760                         /* use the read end of pipe as first item */
761                         pt->fds[0].fd = pt->dummy_pipe_fds[0];
762                         pt->fds[0].events = LWS_POLLIN;
763                         pt->fds[0].revents = 0;
764                         pt->fds_count = 1;
765                         pt++;
766                 }
767         }
768
769 #ifdef LWS_WITH_PLUGINS
770         if (info->plugin_dirs)
771                 lws_plat_plugins_init(context, info->plugin_dirs);
772 #endif
773
774         return 0;
775 }