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