07b7edc8f012916d61cc7db2f9b6b4544700c144
[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 #if defined(SO_BINDTODEVICE)
259         if (vhost->bind_iface) {
260                 lwsl_info("binding listen skt to %s using SO_BINDTODEVICE\n", vhost->iface);
261                 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, vhost->iface,
262                                 strlen(vhost->iface)) < 0) {
263                         lwsl_warn("Failed to bind to device %s\n", vhost->iface);
264                         return 1;
265                 }
266         }
267 #endif
268
269         /* Disable Nagle */
270         optval = 1;
271 #if defined (__sun)
272         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
273                 return 1;
274 #elif !defined(__APPLE__) && \
275       !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) &&        \
276       !defined(__NetBSD__) && \
277       !defined(__OpenBSD__)
278         if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
279                 return 1;
280 #else
281         tcp_proto = getprotobyname("TCP");
282         if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
283                 return 1;
284 #endif
285
286         /* We are nonblocking... */
287         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
288                 return 1;
289
290         return 0;
291 }
292
293 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
294 static void
295 _lws_plat_apply_caps(int mode, cap_value_t *cv, int count)
296 {
297         cap_t caps;
298
299         if (!count)
300                 return;
301
302         caps = cap_get_proc();
303
304         cap_set_flag(caps, mode, count, cv, CAP_SET);
305         cap_set_proc(caps);
306         prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
307         cap_free(caps);
308 }
309 #endif
310
311 LWS_VISIBLE void
312 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
313 {
314 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
315         int n;
316 #endif
317
318         if (info->gid != -1)
319                 if (setgid(info->gid))
320                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
321
322         if (info->uid != -1) {
323                 struct passwd *p = getpwuid(info->uid);
324
325                 if (p) {
326
327 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
328                         _lws_plat_apply_caps(CAP_PERMITTED, info->caps, info->count_caps);
329 #endif
330
331                         initgroups(p->pw_name, info->gid);
332                         if (setuid(info->uid))
333                                 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
334                         else
335                                 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
336
337 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
338                         _lws_plat_apply_caps(CAP_EFFECTIVE, info->caps, info->count_caps);
339
340                         if (info->count_caps)
341                                 for (n = 0; n < info->count_caps; n++)
342                                         lwsl_notice("   RETAINING CAPABILITY %d\n", (int)info->caps[n]);
343 #endif
344
345                 } else
346                         lwsl_warn("getpwuid: unable to find uid %d", info->uid);
347         }
348 }
349
350 #ifdef LWS_WITH_PLUGINS
351
352 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
353
354 /* libuv.c implements these in a cross-platform way */
355
356 #else
357
358 static int filter(const struct dirent *ent)
359 {
360         if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
361                 return 0;
362
363         return 1;
364 }
365
366 LWS_VISIBLE int
367 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
368 {
369         struct lws_plugin_capability lcaps;
370         struct lws_plugin *plugin;
371         lws_plugin_init_func initfunc;
372         struct dirent **namelist;
373         int n, i, m, ret = 0;
374         char path[256];
375         void *l;
376
377         lwsl_notice("  Plugins:\n");
378
379         while (d && *d) {
380                 n = scandir(*d, &namelist, filter, alphasort);
381                 if (n < 0) {
382                         lwsl_err("Scandir on %s failed\n", *d);
383                         return 1;
384                 }
385
386                 for (i = 0; i < n; i++) {
387                         if (strlen(namelist[i]->d_name) < 7)
388                                 goto inval;
389
390                         lwsl_notice("   %s\n", namelist[i]->d_name);
391
392                         lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
393                                  namelist[i]->d_name);
394                         l = dlopen(path, RTLD_NOW);
395                         if (!l) {
396                                 lwsl_err("Error loading DSO: %s\n", dlerror());
397                                 while (i++ < n)
398                                         free(namelist[i]);
399                                 goto bail;
400                         }
401                         /* we could open it, can we get his init function? */
402                         m = lws_snprintf(path, sizeof(path) - 1, "init_%s",
403                                      namelist[i]->d_name + 3 /* snip lib... */);
404                         path[m - 3] = '\0'; /* snip the .so */
405                         initfunc = dlsym(l, path);
406                         if (!initfunc) {
407                                 lwsl_err("Failed to get init on %s: %s",
408                                                 namelist[i]->d_name, dlerror());
409                                 dlclose(l);
410                         }
411                         lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
412                         m = initfunc(context, &lcaps);
413                         if (m) {
414                                 lwsl_err("Initializing %s failed %d\n",
415                                         namelist[i]->d_name, m);
416                                 dlclose(l);
417                                 goto skip;
418                         }
419
420                         plugin = lws_malloc(sizeof(*plugin));
421                         if (!plugin) {
422                                 lwsl_err("OOM\n");
423                                 goto bail;
424                         }
425                         plugin->list = context->plugin_list;
426                         context->plugin_list = plugin;
427                         strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
428                         plugin->name[sizeof(plugin->name) - 1] = '\0';
429                         plugin->l = l;
430                         plugin->caps = lcaps;
431                         context->plugin_protocol_count += lcaps.count_protocols;
432                         context->plugin_extension_count += lcaps.count_extensions;
433
434                         free(namelist[i]);
435                         continue;
436
437         skip:
438                         dlclose(l);
439         inval:
440                         free(namelist[i]);
441                 }
442                 free(namelist);
443                 d++;
444         }
445
446 bail:
447         free(namelist);
448
449         return ret;
450 }
451
452 LWS_VISIBLE int
453 lws_plat_plugins_destroy(struct lws_context * context)
454 {
455         struct lws_plugin *plugin = context->plugin_list, *p;
456         lws_plugin_destroy_func func;
457         char path[256];
458         int m;
459
460         if (!plugin)
461                 return 0;
462
463         lwsl_notice("%s\n", __func__);
464
465         while (plugin) {
466                 p = plugin;
467                 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
468                 path[m - 3] = '\0';
469                 func = dlsym(plugin->l, path);
470                 if (!func) {
471                         lwsl_err("Failed to get destroy on %s: %s",
472                                         plugin->name, dlerror());
473                         goto next;
474                 }
475                 m = func(context);
476                 if (m)
477                         lwsl_err("Initializing %s failed %d\n",
478                                 plugin->name, m);
479 next:
480                 dlclose(p->l);
481                 plugin = p->list;
482                 p->list = NULL;
483                 free(p);
484         }
485
486         context->plugin_list = NULL;
487
488         return 0;
489 }
490
491 #endif
492 #endif
493
494
495 #if 0
496 static void
497 sigabrt_handler(int x)
498 {
499         printf("%s\n", __func__);
500         //*(char *)0 = 0;
501 }
502 #endif
503
504 LWS_VISIBLE int
505 lws_plat_context_early_init(void)
506 {
507 #if !defined(LWS_AVOID_SIGPIPE_IGN)
508         signal(SIGPIPE, SIG_IGN);
509 #endif
510
511 //      signal(SIGABRT, sigabrt_handler);
512
513         return 0;
514 }
515
516 LWS_VISIBLE void
517 lws_plat_context_early_destroy(struct lws_context *context)
518 {
519 }
520
521 LWS_VISIBLE void
522 lws_plat_context_late_destroy(struct lws_context *context)
523 {
524         struct lws_context_per_thread *pt = &context->pt[0];
525         int m = context->count_threads;
526
527 #ifdef LWS_WITH_PLUGINS
528         if (context->plugin_list)
529                 lws_plat_plugins_destroy(context);
530 #endif
531
532         if (context->lws_lookup)
533                 lws_free(context->lws_lookup);
534
535         while (m--) {
536                 if (pt->dummy_pipe_fds[0])
537                         close(pt->dummy_pipe_fds[0]);
538                 if (pt->dummy_pipe_fds[1])
539                         close(pt->dummy_pipe_fds[1]);
540                 pt++;
541         }
542         if (!context->fd_random)
543                 lwsl_err("ZERO RANDOM FD\n");
544         if (context->fd_random != LWS_INVALID_FILE)
545                 close(context->fd_random);
546 }
547
548 /* cast a struct sockaddr_in6 * into addr for ipv6 */
549
550 LWS_VISIBLE int
551 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
552                     size_t addrlen)
553 {
554         int rc = -1;
555
556         struct ifaddrs *ifr;
557         struct ifaddrs *ifc;
558 #ifdef LWS_USE_IPV6
559         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
560 #endif
561
562         getifaddrs(&ifr);
563         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
564                 if (!ifc->ifa_addr)
565                         continue;
566
567                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
568
569                 if (strcmp(ifc->ifa_name, ifname))
570                         continue;
571
572                 switch (ifc->ifa_addr->sa_family) {
573                 case AF_INET:
574 #ifdef LWS_USE_IPV6
575                         if (ipv6) {
576                                 /* map IPv4 to IPv6 */
577                                 bzero((char *)&addr6->sin6_addr,
578                                                 sizeof(struct in6_addr));
579                                 addr6->sin6_addr.s6_addr[10] = 0xff;
580                                 addr6->sin6_addr.s6_addr[11] = 0xff;
581                                 memcpy(&addr6->sin6_addr.s6_addr[12],
582                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
583                                                         sizeof(struct in_addr));
584                         } else
585 #endif
586                                 memcpy(addr,
587                                         (struct sockaddr_in *)ifc->ifa_addr,
588                                                     sizeof(struct sockaddr_in));
589                         break;
590 #ifdef LWS_USE_IPV6
591                 case AF_INET6:
592                         memcpy(&addr6->sin6_addr,
593                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
594                                                        sizeof(struct in6_addr));
595                         break;
596 #endif
597                 default:
598                         continue;
599                 }
600                 rc = 0;
601         }
602
603         freeifaddrs(ifr);
604
605         if (rc == -1) {
606                 /* check if bind to IP address */
607 #ifdef LWS_USE_IPV6
608                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
609                         rc = 0;
610                 else
611 #endif
612                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
613                         rc = 0;
614         }
615
616         return rc;
617 }
618
619 LWS_VISIBLE void
620 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
621 {
622         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
623
624         lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
625         lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
626         lws_libevent_io(wsi, LWS_EV_START | LWS_EV_READ);
627
628         pt->fds[pt->fds_count++].revents = 0;
629 }
630
631 LWS_VISIBLE void
632 lws_plat_delete_socket_from_fds(struct lws_context *context,
633                                                 struct lws *wsi, int m)
634 {
635         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
636
637         lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
638         lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
639         lws_libevent_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
640
641         pt->fds_count--;
642 }
643
644 LWS_VISIBLE void
645 lws_plat_service_periodic(struct lws_context *context)
646 {
647         /* if our parent went down, don't linger around */
648         if (context->started_with_parent &&
649             kill(context->started_with_parent, 0) < 0)
650                 kill(getpid(), SIGTERM);
651 }
652
653 LWS_VISIBLE int
654 lws_plat_change_pollfd(struct lws_context *context,
655                       struct lws *wsi, struct lws_pollfd *pfd)
656 {
657         return 0;
658 }
659
660 LWS_VISIBLE const char *
661 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
662 {
663         return inet_ntop(af, src, dst, cnt);
664 }
665
666 LWS_VISIBLE int
667 lws_plat_inet_pton(int af, const char *src, void *dst)
668 {
669         return inet_pton(af, src, dst);
670 }
671
672 LWS_VISIBLE lws_fop_fd_t
673 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
674                     const char *vpath, lws_fop_flags_t *flags)
675 {
676         struct stat stat_buf;
677         int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
678         lws_fop_fd_t fop_fd;
679
680         if (ret < 0)
681                 return NULL;
682
683         if (fstat(ret, &stat_buf) < 0)
684                 goto bail;
685
686         fop_fd = malloc(sizeof(*fop_fd));
687         if (!fop_fd)
688                 goto bail;
689
690         fop_fd->fops = fops;
691         fop_fd->flags = *flags;
692         fop_fd->fd = ret;
693         fop_fd->filesystem_priv = NULL; /* we don't use it */
694         fop_fd->len = stat_buf.st_size;
695         fop_fd->pos = 0;
696
697         return fop_fd;
698
699 bail:
700         close(ret);
701         return NULL;
702 }
703
704 LWS_VISIBLE int
705 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
706 {
707         int fd = (*fop_fd)->fd;
708
709         free(*fop_fd);
710         *fop_fd = NULL;
711
712         return close(fd);
713 }
714
715 LWS_VISIBLE lws_fileofs_t
716 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
717 {
718         lws_fileofs_t r;
719
720         if (offset > 0 && offset > fop_fd->len - fop_fd->pos)
721                 offset = fop_fd->len - fop_fd->pos;
722
723         if ((lws_fileofs_t)fop_fd->pos + offset < 0)
724                 offset = -fop_fd->pos;
725
726         r = lseek(fop_fd->fd, offset, SEEK_CUR);
727
728         if (r >= 0)
729                 fop_fd->pos = r;
730         else
731                 lwsl_err("error seeking from cur %ld, offset %ld\n",
732                         (long)fop_fd->pos, (long)offset);
733
734         return r;
735 }
736
737 LWS_VISIBLE int
738 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
739                     uint8_t *buf, lws_filepos_t len)
740 {
741         long n;
742
743         n = read((int)fop_fd->fd, buf, len);
744         if (n == -1) {
745                 *amount = 0;
746                 return -1;
747         }
748         fop_fd->pos += n;
749         lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__, n,
750                   (long)len, (long)fop_fd->pos, (long)fop_fd->len);
751         *amount = n;
752
753         return 0;
754 }
755
756 LWS_VISIBLE int
757 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
758                      uint8_t *buf, lws_filepos_t len)
759 {
760         long n;
761
762         n = write((int)fop_fd->fd, buf, len);
763         if (n == -1) {
764                 *amount = 0;
765                 return -1;
766         }
767
768         fop_fd->pos += n;
769         *amount = n;
770
771         return 0;
772 }
773
774
775 LWS_VISIBLE int
776 lws_plat_init(struct lws_context *context,
777               struct lws_context_creation_info *info)
778 {
779         struct lws_context_per_thread *pt = &context->pt[0];
780         int n = context->count_threads, fd;
781
782         /* master context has the global fd lookup array */
783         context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
784                                          context->max_fds);
785         if (context->lws_lookup == NULL) {
786                 lwsl_err("OOM on lws_lookup array for %d connections\n",
787                          context->max_fds);
788                 return 1;
789         }
790
791         lwsl_notice(" mem: platform fd map: %5lu bytes\n",
792                     (unsigned long)(sizeof(struct lws *) * context->max_fds));
793         fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
794
795         context->fd_random = fd;
796         if (context->fd_random < 0) {
797                 lwsl_err("Unable to open random device %s %d\n",
798                          SYSTEM_RANDOM_FILEPATH, context->fd_random);
799                 return 1;
800         }
801
802         if (!lws_libev_init_fd_table(context) &&
803             !lws_libuv_init_fd_table(context) &&
804             !lws_libevent_init_fd_table(context)) {
805                 /* otherwise libev handled it instead */
806
807                 while (n--) {
808                         if (pipe(pt->dummy_pipe_fds)) {
809                                 lwsl_err("Unable to create pipe\n");
810                                 return 1;
811                         }
812
813                         /* use the read end of pipe as first item */
814                         pt->fds[0].fd = pt->dummy_pipe_fds[0];
815                         pt->fds[0].events = LWS_POLLIN;
816                         pt->fds[0].revents = 0;
817                         pt->fds_count = 1;
818                         pt++;
819                 }
820         }
821
822 #ifdef LWS_WITH_PLUGINS
823         if (info->plugin_dirs)
824                 lws_plat_plugins_init(context, info->plugin_dirs);
825 #endif
826
827         return 0;
828 }