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 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
305 /* libuv.c implements these in a cross-platform way */
309 static int filter(const struct dirent *ent)
311 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
318 lws_plat_plugins_init(struct lws_context * context, const char *d)
320 struct lws_plugin_capability lcaps;
321 struct lws_plugin *plugin;
322 lws_plugin_init_func initfunc;
323 struct dirent **namelist;
324 int n, i, m, ret = 0;
329 n = scandir(d, &namelist, filter, alphasort);
331 lwsl_err("Scandir on %s failed\n", d);
335 lwsl_notice(" Plugins:\n");
337 for (i = 0; i < n; i++) {
338 if (strlen(namelist[i]->d_name) < 7)
341 lwsl_notice(" %s\n", namelist[i]->d_name);
343 snprintf(path, sizeof(path) - 1, "%s/%s", d,
344 namelist[i]->d_name);
345 l = dlopen(path, RTLD_NOW);
347 lwsl_err("Error loading DSO: %s\n", dlerror());
352 /* we could open it, can we get his init function? */
353 m = snprintf(path, sizeof(path) - 1, "init_%s",
354 namelist[i]->d_name + 3 /* snip lib... */);
355 path[m - 3] = '\0'; /* snip the .so */
356 initfunc = dlsym(l, path);
358 lwsl_err("Failed to get init on %s: %s",
359 namelist[i]->d_name, dlerror());
362 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
363 m = initfunc(context, &lcaps);
365 lwsl_err("Initializing %s failed %d\n",
366 namelist[i]->d_name, m);
371 plugin = lws_malloc(sizeof(*plugin));
376 plugin->list = context->plugin_list;
377 context->plugin_list = plugin;
378 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
379 plugin->name[sizeof(plugin->name) - 1] = '\0';
381 plugin->caps = lcaps;
382 context->plugin_protocol_count += lcaps.count_protocols;
383 context->plugin_extension_count += lcaps.count_extensions;
401 lws_plat_plugins_destroy(struct lws_context * context)
403 struct lws_plugin *plugin = context->plugin_list, *p;
404 lws_plugin_destroy_func func;
411 lwsl_notice("%s\n", __func__);
415 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
417 func = dlsym(plugin->l, path);
419 lwsl_err("Failed to get destroy on %s: %s",
420 plugin->name, dlerror());
425 lwsl_err("Initializing %s failed %d\n",
434 context->plugin_list = NULL;
444 sigpipe_handler(int x)
449 lws_plat_context_early_init(void)
453 signal(SIGUSR2, lws_sigusr2);
455 sigaddset(&mask, SIGUSR2);
457 sigprocmask(SIG_BLOCK, &mask, NULL);
459 signal(SIGPIPE, sigpipe_handler);
465 lws_plat_context_early_destroy(struct lws_context *context)
470 lws_plat_context_late_destroy(struct lws_context *context)
472 struct lws_context_per_thread *pt = &context->pt[0];
473 int m = context->count_threads;
475 #ifdef LWS_WITH_PLUGINS
476 if (context->plugin_list)
477 lws_plat_plugins_destroy(context);
480 if (context->lws_lookup)
481 lws_free(context->lws_lookup);
484 close(pt->dummy_pipe_fds[0]);
485 close(pt->dummy_pipe_fds[1]);
488 close(context->fd_random);
491 /* cast a struct sockaddr_in6 * into addr for ipv6 */
494 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
502 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
506 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
510 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
512 if (strcmp(ifc->ifa_name, ifname))
515 switch (ifc->ifa_addr->sa_family) {
519 /* map IPv4 to IPv6 */
520 bzero((char *)&addr6->sin6_addr,
521 sizeof(struct in6_addr));
522 addr6->sin6_addr.s6_addr[10] = 0xff;
523 addr6->sin6_addr.s6_addr[11] = 0xff;
524 memcpy(&addr6->sin6_addr.s6_addr[12],
525 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
526 sizeof(struct in_addr));
530 (struct sockaddr_in *)ifc->ifa_addr,
531 sizeof(struct sockaddr_in));
535 memcpy(&addr6->sin6_addr,
536 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
537 sizeof(struct in6_addr));
549 /* check if bind to IP adddress */
551 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
555 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
563 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
565 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
567 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
568 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
570 pt->fds[pt->fds_count++].revents = 0;
574 lws_plat_delete_socket_from_fds(struct lws_context *context,
575 struct lws *wsi, int m)
577 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
582 lws_plat_service_periodic(struct lws_context *context)
584 /* if our parent went down, don't linger around */
585 if (context->started_with_parent &&
586 kill(context->started_with_parent, 0) < 0)
587 kill(getpid(), SIGTERM);
591 lws_plat_change_pollfd(struct lws_context *context,
592 struct lws *wsi, struct lws_pollfd *pfd)
597 LWS_VISIBLE const char *
598 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
600 return inet_ntop(af, src, dst, cnt);
603 static lws_filefd_type
604 _lws_plat_file_open(struct lws *wsi, const char *filename,
605 unsigned long *filelen, int flags)
607 struct stat stat_buf;
608 int ret = open(filename, flags, 0664);
611 return LWS_INVALID_FILE;
613 if (fstat(ret, &stat_buf) < 0) {
615 return LWS_INVALID_FILE;
617 *filelen = stat_buf.st_size;
622 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
628 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
630 return lseek(fd, offset, SEEK_CUR);
634 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
635 unsigned char *buf, unsigned long len)
639 n = read((int)fd, buf, len);
651 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
652 unsigned char *buf, unsigned long len)
656 n = write((int)fd, buf, len);
669 lws_plat_init(struct lws_context *context,
670 struct lws_context_creation_info *info)
672 struct lws_context_per_thread *pt = &context->pt[0];
673 int n = context->count_threads, fd;
675 /* master context has the global fd lookup array */
676 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
678 if (context->lws_lookup == NULL) {
679 lwsl_err("OOM on lws_lookup array for %d connections\n",
684 lwsl_notice(" mem: platform fd map: %5u bytes\n",
685 sizeof(struct lws *) * context->max_fds);
686 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
688 context->fd_random = fd;
689 if (context->fd_random < 0) {
690 lwsl_err("Unable to open random device %s %d\n",
691 SYSTEM_RANDOM_FILEPATH, context->fd_random);
695 if (!lws_libev_init_fd_table(context) &&
696 !lws_libuv_init_fd_table(context)) {
697 /* otherwise libev handled it instead */
700 if (pipe(pt->dummy_pipe_fds)) {
701 lwsl_err("Unable to create pipe\n");
705 /* use the read end of pipe as first item */
706 pt->fds[0].fd = pt->dummy_pipe_fds[0];
707 pt->fds[0].events = LWS_POLLIN;
708 pt->fds[0].revents = 0;
714 context->fops.open = _lws_plat_file_open;
715 context->fops.close = _lws_plat_file_close;
716 context->fops.seek_cur = _lws_plat_file_seek_cur;
717 context->fops.read = _lws_plat_file_read;
718 context->fops.write = _lws_plat_file_write;
720 #ifdef LWS_WITH_PLUGINS
721 if (info->plugins_dir)
722 lws_plat_plugins_init(context, info->plugins_dir);