1 #include "private-libwebsockets.h"
6 #ifdef LWS_WITH_PLUGINS
13 * included from libwebsockets.c for unix builds
16 unsigned long long time_in_microseconds(void)
19 gettimeofday(&tv, NULL);
20 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
24 lws_get_random(struct lws_context *context, void *buf, int len)
26 return read(context->fd_random, (char *)buf, len);
30 lws_send_pipe_choked(struct lws *wsi)
32 struct lws_pollfd fds;
34 /* treat the fact we got a truncated send pending as if we're choked */
38 fds.fd = wsi->desc.sockfd;
42 if (poll(&fds, 1, 0) != 1)
45 if ((fds.revents & POLLOUT) == 0)
48 /* okay to send another packet without blocking */
54 lws_poll_listen_fd(struct lws_pollfd *fd)
56 return poll(fd, 1, 0);
60 lws_cancel_service_pt(struct lws *wsi)
62 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
65 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
66 lwsl_err("Cannot write to dummy pipe");
70 lws_cancel_service(struct lws_context *context)
72 struct lws_context_per_thread *pt = &context->pt[0];
73 char buf = 0, m = context->count_threads;
76 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77 lwsl_err("Cannot write to dummy pipe");
82 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
84 int syslog_level = LOG_DEBUG;
88 syslog_level = LOG_ERR;
91 syslog_level = LOG_WARNING;
94 syslog_level = LOG_NOTICE;
97 syslog_level = LOG_INFO;
100 syslog(syslog_level, "%s", line);
103 LWS_VISIBLE LWS_EXTERN int
104 _lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
106 struct lws_context_per_thread *pt;
110 /* stay dead once we are dead */
112 if (!context || !context->vhost_list)
115 pt = &context->pt[tsi];
117 lws_stats_atomic_bump(context, pt, LWSSTATS_C_SERVICE_ENTRY, 1);
122 lws_libev_run(context, tsi);
123 lws_libuv_run(context, tsi);
124 lws_libevent_run(context, tsi);
126 if (!context->service_tid_detected) {
129 memset(&_lws, 0, sizeof(_lws));
130 _lws.context = context;
132 context->service_tid_detected =
133 context->vhost_list->protocols[0].callback(
134 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
136 context->service_tid = context->service_tid_detected;
139 * is there anybody with pending stuff that needs service forcing?
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 */
150 n = poll(pt->fds, pt->fds_count, timeout_ms);
152 #ifdef LWS_OPENSSL_SUPPORT
153 if (!pt->rx_draining_ext_list &&
154 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
156 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
158 lws_service_fd_tsi(context, NULL, tsi);
163 m = lws_service_flag_pending(context, tsi);
165 c = -1; /* unknown limit */
168 if (LWS_ERRNO != LWS_EINTR)
174 /* any socket with events to service? */
175 for (n = 0; n < pt->fds_count && c; n++) {
176 if (!pt->fds[n].revents)
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.");
187 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
190 /* if something closed, retry this slot */
199 lws_plat_check_connection_error(struct lws *wsi)
205 lws_plat_service(struct lws_context *context, int timeout_ms)
207 return _lws_plat_service_tsi(context, timeout_ms, 0);
211 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
214 socklen_t optlen = sizeof(optval);
216 #if defined(__APPLE__) || \
217 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
218 defined(__NetBSD__) || \
220 struct protoent *tcp_proto;
223 if (vhost->ka_time) {
224 /* enable keepalive on this socket */
226 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
227 (const void *)&optval, optlen) < 0)
230 #if defined(__APPLE__) || \
231 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
232 defined(__NetBSD__) || \
233 defined(__CYGWIN__) || defined(__OpenBSD__) || defined (__sun)
236 * didn't find a way to set these per-socket, need to
237 * tune kernel systemwide values
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)
246 optval = vhost->ka_interval;
247 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
248 (const void *)&optval, optlen) < 0)
251 optval = vhost->ka_probes;
252 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
253 (const void *)&optval, optlen) < 0)
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);
272 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
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)
281 tcp_proto = getprotobyname("TCP");
282 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
286 /* We are nonblocking... */
287 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
293 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
295 _lws_plat_apply_caps(int mode, cap_value_t *cv, int count)
302 caps = cap_get_proc();
304 cap_set_flag(caps, mode, count, cv, CAP_SET);
306 prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
312 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
314 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
319 if (setgid(info->gid))
320 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
322 if (info->uid != -1) {
323 struct passwd *p = getpwuid(info->uid);
327 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
328 _lws_plat_apply_caps(CAP_PERMITTED, info->caps, info->count_caps);
331 initgroups(p->pw_name, info->gid);
332 if (setuid(info->uid))
333 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
335 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
337 #if defined(LWS_HAVE_SYS_CAPABILITY_H) && defined(LWS_HAVE_LIBCAP)
338 _lws_plat_apply_caps(CAP_EFFECTIVE, info->caps, info->count_caps);
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]);
346 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
350 #ifdef LWS_WITH_PLUGINS
352 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
354 /* libuv.c implements these in a cross-platform way */
358 static int filter(const struct dirent *ent)
360 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
367 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
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;
377 lwsl_notice(" Plugins:\n");
380 n = scandir(*d, &namelist, filter, alphasort);
382 lwsl_err("Scandir on %s failed\n", *d);
386 for (i = 0; i < n; i++) {
387 if (strlen(namelist[i]->d_name) < 7)
390 lwsl_notice(" %s\n", namelist[i]->d_name);
392 lws_snprintf(path, sizeof(path) - 1, "%s/%s", *d,
393 namelist[i]->d_name);
394 l = dlopen(path, RTLD_NOW);
396 lwsl_err("Error loading DSO: %s\n", dlerror());
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);
407 lwsl_err("Failed to get init on %s: %s",
408 namelist[i]->d_name, dlerror());
411 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
412 m = initfunc(context, &lcaps);
414 lwsl_err("Initializing %s failed %d\n",
415 namelist[i]->d_name, m);
420 plugin = lws_malloc(sizeof(*plugin));
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';
430 plugin->caps = lcaps;
431 context->plugin_protocol_count += lcaps.count_protocols;
432 context->plugin_extension_count += lcaps.count_extensions;
453 lws_plat_plugins_destroy(struct lws_context * context)
455 struct lws_plugin *plugin = context->plugin_list, *p;
456 lws_plugin_destroy_func func;
463 lwsl_notice("%s\n", __func__);
467 m = lws_snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
469 func = dlsym(plugin->l, path);
471 lwsl_err("Failed to get destroy on %s: %s",
472 plugin->name, dlerror());
477 lwsl_err("Initializing %s failed %d\n",
486 context->plugin_list = NULL;
497 sigabrt_handler(int x)
499 printf("%s\n", __func__);
505 lws_plat_context_early_init(void)
507 #if !defined(LWS_AVOID_SIGPIPE_IGN)
508 signal(SIGPIPE, SIG_IGN);
511 // signal(SIGABRT, sigabrt_handler);
517 lws_plat_context_early_destroy(struct lws_context *context)
522 lws_plat_context_late_destroy(struct lws_context *context)
524 struct lws_context_per_thread *pt = &context->pt[0];
525 int m = context->count_threads;
527 #ifdef LWS_WITH_PLUGINS
528 if (context->plugin_list)
529 lws_plat_plugins_destroy(context);
532 if (context->lws_lookup)
533 lws_free(context->lws_lookup);
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]);
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);
548 /* cast a struct sockaddr_in6 * into addr for ipv6 */
551 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
559 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
563 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
567 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
569 if (strcmp(ifc->ifa_name, ifname))
572 switch (ifc->ifa_addr->sa_family) {
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));
587 (struct sockaddr_in *)ifc->ifa_addr,
588 sizeof(struct sockaddr_in));
592 memcpy(&addr6->sin6_addr,
593 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
594 sizeof(struct in6_addr));
606 /* check if bind to IP address */
608 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
612 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
620 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
622 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
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);
628 pt->fds[pt->fds_count++].revents = 0;
632 lws_plat_delete_socket_from_fds(struct lws_context *context,
633 struct lws *wsi, int m)
635 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
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);
645 lws_plat_service_periodic(struct lws_context *context)
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);
654 lws_plat_change_pollfd(struct lws_context *context,
655 struct lws *wsi, struct lws_pollfd *pfd)
660 LWS_VISIBLE const char *
661 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
663 return inet_ntop(af, src, dst, cnt);
667 lws_plat_inet_pton(int af, const char *src, void *dst)
669 return inet_pton(af, src, dst);
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)
676 struct stat stat_buf;
677 int ret = open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
683 if (fstat(ret, &stat_buf) < 0)
686 fop_fd = malloc(sizeof(*fop_fd));
691 fop_fd->flags = *flags;
693 fop_fd->filesystem_priv = NULL; /* we don't use it */
694 fop_fd->len = stat_buf.st_size;
705 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
707 int fd = (*fop_fd)->fd;
715 LWS_VISIBLE lws_fileofs_t
716 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
720 if (offset > 0 && offset > fop_fd->len - fop_fd->pos)
721 offset = fop_fd->len - fop_fd->pos;
723 if ((lws_fileofs_t)fop_fd->pos + offset < 0)
724 offset = -fop_fd->pos;
726 r = lseek(fop_fd->fd, offset, SEEK_CUR);
731 lwsl_err("error seeking from cur %ld, offset %ld\n",
732 (long)fop_fd->pos, (long)offset);
738 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
739 uint8_t *buf, lws_filepos_t len)
743 n = read((int)fop_fd->fd, buf, len);
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);
757 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
758 uint8_t *buf, lws_filepos_t len)
762 n = write((int)fop_fd->fd, buf, len);
776 lws_plat_init(struct lws_context *context,
777 struct lws_context_creation_info *info)
779 struct lws_context_per_thread *pt = &context->pt[0];
780 int n = context->count_threads, fd;
782 /* master context has the global fd lookup array */
783 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
785 if (context->lws_lookup == NULL) {
786 lwsl_err("OOM on lws_lookup array for %d connections\n",
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);
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);
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 */
808 if (pipe(pt->dummy_pipe_fds)) {
809 lwsl_err("Unable to create pipe\n");
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;
822 #ifdef LWS_WITH_PLUGINS
823 if (info->plugin_dirs)
824 lws_plat_plugins_init(context, info->plugin_dirs);