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