lws-plat-unix.c 16 KB

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