1 #include "private-libwebsockets.h"
11 * included from libwebsockets.c for unix builds
14 unsigned long long time_in_microseconds(void)
17 gettimeofday(&tv, NULL);
18 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
22 lws_get_random(struct lws_context *context, void *buf, int len)
24 return read(context->fd_random, (char *)buf, len);
28 lws_send_pipe_choked(struct lws *wsi)
30 struct lws_pollfd fds;
32 /* treat the fact we got a truncated send pending as if we're choked */
40 if (poll(&fds, 1, 0) != 1)
43 if ((fds.revents & POLLOUT) == 0)
46 /* okay to send another packet without blocking */
52 lws_poll_listen_fd(struct lws_pollfd *fd)
54 return poll(fd, 1, 0);
58 * This is just used to interrupt poll waiting
59 * we don't have to do anything with it.
67 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
69 * @wsi: Cancel service on the thread this wsi is serviced by
71 * This function let a call to lws_service() waiting for a timeout
75 lws_cancel_service_pt(struct lws *wsi)
77 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
80 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
81 lwsl_err("Cannot write to dummy pipe");
85 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
86 * @context: Websocket context
88 * This function let a call to lws_service() waiting for a timeout
92 lws_cancel_service(struct lws_context *context)
94 struct lws_context_per_thread *pt = &context->pt[0];
95 char buf = 0, m = context->count_threads;
98 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
99 lwsl_err("Cannot write to dummy pipe");
104 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
106 int syslog_level = LOG_DEBUG;
110 syslog_level = LOG_ERR;
113 syslog_level = LOG_WARNING;
116 syslog_level = LOG_NOTICE;
119 syslog_level = LOG_INFO;
122 syslog(syslog_level, "%s", line);
126 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
128 struct lws_context_per_thread *pt = &context->pt[tsi];
132 /* stay dead once we are dead */
134 if (!context || !context->vhost_list)
140 lws_libev_run(context, tsi);
141 lws_libuv_run(context, tsi);
143 if (!context->service_tid_detected) {
146 memset(&_lws, 0, sizeof(_lws));
147 _lws.context = context;
149 context->service_tid_detected =
150 context->vhost_list->protocols[0].callback(
151 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
153 context->service_tid = context->service_tid_detected;
155 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
157 n = poll(pt->fds, pt->fds_count, timeout_ms);
159 #ifdef LWS_OPENSSL_SUPPORT
160 if (!pt->rx_draining_ext_list &&
161 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
163 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
165 lws_service_fd_tsi(context, NULL, tsi);
170 m = lws_service_flag_pending(context, tsi);
172 c = -1; /* unknown limit */
175 if (LWS_ERRNO != LWS_EINTR)
181 /* any socket with events to service? */
182 for (n = 0; n < pt->fds_count && c; n++) {
183 if (!pt->fds[n].revents)
188 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
189 if (read(pt->fds[n].fd, &buf, 1) != 1)
190 lwsl_err("Cannot read from dummy pipe.");
194 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
197 /* if something closed, retry this slot */
206 lws_plat_service(struct lws_context *context, int timeout_ms)
208 return lws_plat_service_tsi(context, timeout_ms, 0);
212 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
215 socklen_t optlen = sizeof(optval);
217 #if defined(__APPLE__) || \
218 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
219 defined(__NetBSD__) || \
221 struct protoent *tcp_proto;
224 if (vhost->ka_time) {
225 /* enable keepalive on this socket */
227 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
228 (const void *)&optval, optlen) < 0)
231 #if defined(__APPLE__) || \
232 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
233 defined(__NetBSD__) || \
234 defined(__CYGWIN__) || defined(__OpenBSD__)
237 * didn't find a way to set these per-socket, need to
238 * tune kernel systemwide values
241 /* set the keepalive conditions we want on it too */
242 optval = vhost->ka_time;
243 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
244 (const void *)&optval, optlen) < 0)
247 optval = vhost->ka_interval;
248 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
249 (const void *)&optval, optlen) < 0)
252 optval = vhost->ka_probes;
253 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
254 (const void *)&optval, optlen) < 0)
261 #if !defined(__APPLE__) && \
262 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
263 !defined(__NetBSD__) && \
264 !defined(__OpenBSD__)
265 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
268 tcp_proto = getprotobyname("TCP");
269 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
273 /* We are nonblocking... */
274 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
281 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
284 if (setgid(info->gid))
285 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
287 if (info->uid != -1) {
288 struct passwd *p = getpwuid(info->uid);
291 initgroups(p->pw_name, info->gid);
292 if (setuid(info->uid))
293 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
295 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
297 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
301 #ifdef LWS_WITH_PLUGINS
303 static int filter(const struct dirent *ent)
305 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
312 lws_plat_plugins_init(struct lws_context * context, const char *d)
314 struct lws_plugin_capability lcaps;
315 struct lws_plugin *plugin;
316 lws_plugin_init_func initfunc;
317 struct dirent **namelist;
318 int n, i, m, ret = 0;
323 n = scandir(d, &namelist, filter, alphasort);
325 lwsl_err("Scandir on %d failed\n", d);
329 lwsl_notice(" Plugins:\n");
331 for (i = 0; i < n; i++) {
332 if (strlen(namelist[i]->d_name) < 7)
335 lwsl_notice(" %s\n", namelist[i]->d_name);
337 snprintf(path, sizeof(path) - 1, "%s/%s", d,
338 namelist[i]->d_name);
339 l = dlopen(path, RTLD_NOW);
341 lwsl_err("Error loading DSO: %s\n", dlerror());
346 /* we could open it, can we get his init function? */
347 m = snprintf(path, sizeof(path) - 1, "init_%s",
348 namelist[i]->d_name + 3 /* snip lib... */);
349 path[m - 3] = '\0'; /* snip the .so */
350 initfunc = dlsym(l, path);
352 lwsl_err("Failed to get init on %s: %s",
353 namelist[i]->d_name, dlerror());
356 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
357 m = initfunc(context, &lcaps);
359 lwsl_err("Initializing %s failed %d\n",
360 namelist[i]->d_name, m);
365 plugin = lws_malloc(sizeof(*plugin));
370 plugin->list = context->plugin_list;
371 context->plugin_list = plugin;
372 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
373 plugin->name[sizeof(plugin->name) - 1] = '\0';
375 plugin->caps = lcaps;
376 context->plugin_protocol_count += lcaps.count_protocols;
377 context->plugin_extension_count += lcaps.count_extensions;
395 lws_plat_plugins_destroy(struct lws_context * context)
397 struct lws_plugin *plugin = context->plugin_list, *p;
398 lws_plugin_destroy_func func;
405 lwsl_notice("%s\n", __func__);
409 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
411 func = dlsym(plugin->l, path);
413 lwsl_err("Failed to get destroy on %s: %s",
414 plugin->name, dlerror());
419 lwsl_err("Initializing %s failed %d\n",
428 context->plugin_list = NULL;
437 sigpipe_handler(int x)
442 lws_plat_context_early_init(void)
446 signal(SIGUSR2, lws_sigusr2);
448 sigaddset(&mask, SIGUSR2);
450 sigprocmask(SIG_BLOCK, &mask, NULL);
452 signal(SIGPIPE, sigpipe_handler);
458 lws_plat_context_early_destroy(struct lws_context *context)
463 lws_plat_context_late_destroy(struct lws_context *context)
465 struct lws_context_per_thread *pt = &context->pt[0];
466 int m = context->count_threads;
468 #ifdef LWS_WITH_PLUGINS
469 if (context->plugin_list)
470 lws_plat_plugins_destroy(context);
473 if (context->lws_lookup)
474 lws_free(context->lws_lookup);
477 close(pt->dummy_pipe_fds[0]);
478 close(pt->dummy_pipe_fds[1]);
481 close(context->fd_random);
484 /* cast a struct sockaddr_in6 * into addr for ipv6 */
487 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
495 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
499 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
503 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
505 if (strcmp(ifc->ifa_name, ifname))
508 switch (ifc->ifa_addr->sa_family) {
512 /* map IPv4 to IPv6 */
513 bzero((char *)&addr6->sin6_addr,
514 sizeof(struct in6_addr));
515 addr6->sin6_addr.s6_addr[10] = 0xff;
516 addr6->sin6_addr.s6_addr[11] = 0xff;
517 memcpy(&addr6->sin6_addr.s6_addr[12],
518 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
519 sizeof(struct in_addr));
523 (struct sockaddr_in *)ifc->ifa_addr,
524 sizeof(struct sockaddr_in));
528 memcpy(&addr6->sin6_addr,
529 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
530 sizeof(struct in6_addr));
542 /* check if bind to IP adddress */
544 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
548 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
556 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
558 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
560 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
561 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
563 pt->fds[pt->fds_count++].revents = 0;
567 lws_plat_delete_socket_from_fds(struct lws_context *context,
568 struct lws *wsi, int m)
570 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
575 lws_plat_service_periodic(struct lws_context *context)
577 /* if our parent went down, don't linger around */
578 if (context->started_with_parent &&
579 kill(context->started_with_parent, 0) < 0)
580 kill(getpid(), SIGTERM);
584 lws_plat_change_pollfd(struct lws_context *context,
585 struct lws *wsi, struct lws_pollfd *pfd)
590 LWS_VISIBLE const char *
591 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
593 return inet_ntop(af, src, dst, cnt);
596 static lws_filefd_type
597 _lws_plat_file_open(struct lws *wsi, const char *filename,
598 unsigned long *filelen, int flags)
600 struct stat stat_buf;
601 int ret = open(filename, flags, 0664);
604 return LWS_INVALID_FILE;
606 if (fstat(ret, &stat_buf) < 0) {
608 return LWS_INVALID_FILE;
610 *filelen = stat_buf.st_size;
615 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
621 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
623 return lseek(fd, offset, SEEK_CUR);
627 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
628 unsigned char *buf, unsigned long len)
632 n = read((int)fd, buf, len);
644 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
645 unsigned char *buf, unsigned long len)
649 n = write((int)fd, buf, len);
662 lws_plat_init(struct lws_context *context,
663 struct lws_context_creation_info *info)
665 struct lws_context_per_thread *pt = &context->pt[0];
666 int n = context->count_threads, fd;
668 /* master context has the global fd lookup array */
669 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
671 if (context->lws_lookup == NULL) {
672 lwsl_err("OOM on lws_lookup array for %d connections\n",
677 lwsl_notice(" mem: platform fd map: %5u bytes\n",
678 sizeof(struct lws *) * context->max_fds);
679 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
681 context->fd_random = fd;
682 if (context->fd_random < 0) {
683 lwsl_err("Unable to open random device %s %d\n",
684 SYSTEM_RANDOM_FILEPATH, context->fd_random);
688 if (!lws_libev_init_fd_table(context) &&
689 !lws_libuv_init_fd_table(context)) {
690 /* otherwise libev handled it instead */
693 if (pipe(pt->dummy_pipe_fds)) {
694 lwsl_err("Unable to create pipe\n");
698 /* use the read end of pipe as first item */
699 pt->fds[0].fd = pt->dummy_pipe_fds[0];
700 pt->fds[0].events = LWS_POLLIN;
701 pt->fds[0].revents = 0;
707 context->fops.open = _lws_plat_file_open;
708 context->fops.close = _lws_plat_file_close;
709 context->fops.seek_cur = _lws_plat_file_seek_cur;
710 context->fops.read = _lws_plat_file_read;
711 context->fops.write = _lws_plat_file_write;
713 #ifdef LWS_WITH_PLUGINS
714 if (info->plugins_dir)
715 lws_plat_plugins_init(context, info->plugins_dir);