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