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 lws_cancel_service_pt(struct lws *wsi)
60 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
63 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
64 lwsl_err("Cannot write to dummy pipe");
68 lws_cancel_service(struct lws_context *context)
70 struct lws_context_per_thread *pt = &context->pt[0];
71 char buf = 0, m = context->count_threads;
74 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
75 lwsl_err("Cannot write to dummy pipe");
80 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
82 int syslog_level = LOG_DEBUG;
86 syslog_level = LOG_ERR;
89 syslog_level = LOG_WARNING;
92 syslog_level = LOG_NOTICE;
95 syslog_level = LOG_INFO;
98 syslog(syslog_level, "%s", line);
102 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
104 struct lws_context_per_thread *pt = &context->pt[tsi];
108 /* stay dead once we are dead */
110 if (!context || !context->vhost_list)
116 lws_libev_run(context, tsi);
117 lws_libuv_run(context, tsi);
119 if (!context->service_tid_detected) {
122 memset(&_lws, 0, sizeof(_lws));
123 _lws.context = context;
125 context->service_tid_detected =
126 context->vhost_list->protocols[0].callback(
127 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
129 context->service_tid = context->service_tid_detected;
131 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
133 n = poll(pt->fds, pt->fds_count, timeout_ms);
135 #ifdef LWS_OPENSSL_SUPPORT
136 if (!pt->rx_draining_ext_list &&
137 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
139 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
141 lws_service_fd_tsi(context, NULL, tsi);
146 m = lws_service_flag_pending(context, tsi);
148 c = -1; /* unknown limit */
151 if (LWS_ERRNO != LWS_EINTR)
157 /* any socket with events to service? */
158 for (n = 0; n < pt->fds_count && c; n++) {
159 if (!pt->fds[n].revents)
164 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
165 if (read(pt->fds[n].fd, &buf, 1) != 1)
166 lwsl_err("Cannot read from dummy pipe.");
170 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
173 /* if something closed, retry this slot */
182 lws_plat_service(struct lws_context *context, int timeout_ms)
184 return lws_plat_service_tsi(context, timeout_ms, 0);
188 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
191 socklen_t optlen = sizeof(optval);
193 #if defined(__APPLE__) || \
194 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
195 defined(__NetBSD__) || \
197 struct protoent *tcp_proto;
200 if (vhost->ka_time) {
201 /* enable keepalive on this socket */
203 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
204 (const void *)&optval, optlen) < 0)
207 #if defined(__APPLE__) || \
208 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
209 defined(__NetBSD__) || \
210 defined(__CYGWIN__) || defined(__OpenBSD__)
213 * didn't find a way to set these per-socket, need to
214 * tune kernel systemwide values
217 /* set the keepalive conditions we want on it too */
218 optval = vhost->ka_time;
219 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
220 (const void *)&optval, optlen) < 0)
223 optval = vhost->ka_interval;
224 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
225 (const void *)&optval, optlen) < 0)
228 optval = vhost->ka_probes;
229 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
230 (const void *)&optval, optlen) < 0)
237 #if !defined(__APPLE__) && \
238 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
239 !defined(__NetBSD__) && \
240 !defined(__OpenBSD__)
241 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
244 tcp_proto = getprotobyname("TCP");
245 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
249 /* We are nonblocking... */
250 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
257 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
260 if (setgid(info->gid))
261 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
263 if (info->uid != -1) {
264 struct passwd *p = getpwuid(info->uid);
267 initgroups(p->pw_name, info->gid);
268 if (setuid(info->uid))
269 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
271 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
273 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
277 #ifdef LWS_WITH_PLUGINS
279 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
281 /* libuv.c implements these in a cross-platform way */
285 static int filter(const struct dirent *ent)
287 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
294 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
296 struct lws_plugin_capability lcaps;
297 struct lws_plugin *plugin;
298 lws_plugin_init_func initfunc;
299 struct dirent **namelist;
300 int n, i, m, ret = 0;
304 lwsl_notice(" Plugins:\n");
307 n = scandir(*d, &namelist, filter, alphasort);
309 lwsl_err("Scandir on %s failed\n", *d);
313 for (i = 0; i < n; i++) {
314 if (strlen(namelist[i]->d_name) < 7)
317 lwsl_notice(" %s\n", namelist[i]->d_name);
319 snprintf(path, sizeof(path) - 1, "%s/%s", *d,
320 namelist[i]->d_name);
321 l = dlopen(path, RTLD_NOW);
323 lwsl_err("Error loading DSO: %s\n", dlerror());
328 /* we could open it, can we get his init function? */
329 m = snprintf(path, sizeof(path) - 1, "init_%s",
330 namelist[i]->d_name + 3 /* snip lib... */);
331 path[m - 3] = '\0'; /* snip the .so */
332 initfunc = dlsym(l, path);
334 lwsl_err("Failed to get init on %s: %s",
335 namelist[i]->d_name, dlerror());
338 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
339 m = initfunc(context, &lcaps);
341 lwsl_err("Initializing %s failed %d\n",
342 namelist[i]->d_name, m);
347 plugin = lws_malloc(sizeof(*plugin));
352 plugin->list = context->plugin_list;
353 context->plugin_list = plugin;
354 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
355 plugin->name[sizeof(plugin->name) - 1] = '\0';
357 plugin->caps = lcaps;
358 context->plugin_protocol_count += lcaps.count_protocols;
359 context->plugin_extension_count += lcaps.count_extensions;
380 lws_plat_plugins_destroy(struct lws_context * context)
382 struct lws_plugin *plugin = context->plugin_list, *p;
383 lws_plugin_destroy_func func;
390 lwsl_notice("%s\n", __func__);
394 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
396 func = dlsym(plugin->l, path);
398 lwsl_err("Failed to get destroy on %s: %s",
399 plugin->name, dlerror());
404 lwsl_err("Initializing %s failed %d\n",
413 context->plugin_list = NULL;
424 sigabrt_handler(int x)
426 printf("%s\n", __func__);
432 lws_plat_context_early_init(void)
434 signal(SIGPIPE, SIG_IGN);
436 // signal(SIGABRT, sigabrt_handler);
442 lws_plat_context_early_destroy(struct lws_context *context)
447 lws_plat_context_late_destroy(struct lws_context *context)
449 struct lws_context_per_thread *pt = &context->pt[0];
450 int m = context->count_threads;
452 #ifdef LWS_WITH_PLUGINS
453 if (context->plugin_list)
454 lws_plat_plugins_destroy(context);
457 if (context->lws_lookup)
458 lws_free(context->lws_lookup);
461 close(pt->dummy_pipe_fds[0]);
462 close(pt->dummy_pipe_fds[1]);
465 close(context->fd_random);
468 /* cast a struct sockaddr_in6 * into addr for ipv6 */
471 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
479 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
483 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
487 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
489 if (strcmp(ifc->ifa_name, ifname))
492 switch (ifc->ifa_addr->sa_family) {
496 /* map IPv4 to IPv6 */
497 bzero((char *)&addr6->sin6_addr,
498 sizeof(struct in6_addr));
499 addr6->sin6_addr.s6_addr[10] = 0xff;
500 addr6->sin6_addr.s6_addr[11] = 0xff;
501 memcpy(&addr6->sin6_addr.s6_addr[12],
502 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
503 sizeof(struct in_addr));
507 (struct sockaddr_in *)ifc->ifa_addr,
508 sizeof(struct sockaddr_in));
512 memcpy(&addr6->sin6_addr,
513 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
514 sizeof(struct in6_addr));
526 /* check if bind to IP adddress */
528 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
532 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
540 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
542 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
544 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
545 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
547 pt->fds[pt->fds_count++].revents = 0;
551 lws_plat_delete_socket_from_fds(struct lws_context *context,
552 struct lws *wsi, int m)
554 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
556 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
557 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
563 lws_plat_service_periodic(struct lws_context *context)
565 /* if our parent went down, don't linger around */
566 if (context->started_with_parent &&
567 kill(context->started_with_parent, 0) < 0)
568 kill(getpid(), SIGTERM);
572 lws_plat_change_pollfd(struct lws_context *context,
573 struct lws *wsi, struct lws_pollfd *pfd)
578 LWS_VISIBLE const char *
579 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
581 return inet_ntop(af, src, dst, cnt);
584 static lws_filefd_type
585 _lws_plat_file_open(struct lws *wsi, const char *filename,
586 unsigned long *filelen, int flags)
588 struct stat stat_buf;
589 int ret = open(filename, flags, 0664);
592 return LWS_INVALID_FILE;
594 if (fstat(ret, &stat_buf) < 0) {
596 return LWS_INVALID_FILE;
598 *filelen = stat_buf.st_size;
603 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
609 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
611 return lseek(fd, offset, SEEK_CUR);
615 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
616 unsigned char *buf, unsigned long len)
620 n = read((int)fd, buf, len);
632 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
633 unsigned char *buf, unsigned long len)
637 n = write((int)fd, buf, len);
650 lws_plat_init(struct lws_context *context,
651 struct lws_context_creation_info *info)
653 struct lws_context_per_thread *pt = &context->pt[0];
654 int n = context->count_threads, fd;
656 /* master context has the global fd lookup array */
657 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
659 if (context->lws_lookup == NULL) {
660 lwsl_err("OOM on lws_lookup array for %d connections\n",
665 lwsl_notice(" mem: platform fd map: %5u bytes\n",
666 sizeof(struct lws *) * context->max_fds);
667 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
669 context->fd_random = fd;
670 if (context->fd_random < 0) {
671 lwsl_err("Unable to open random device %s %d\n",
672 SYSTEM_RANDOM_FILEPATH, context->fd_random);
676 if (!lws_libev_init_fd_table(context) &&
677 !lws_libuv_init_fd_table(context)) {
678 /* otherwise libev handled it instead */
681 if (pipe(pt->dummy_pipe_fds)) {
682 lwsl_err("Unable to create pipe\n");
686 /* use the read end of pipe as first item */
687 pt->fds[0].fd = pt->dummy_pipe_fds[0];
688 pt->fds[0].events = LWS_POLLIN;
689 pt->fds[0].revents = 0;
695 context->fops.open = _lws_plat_file_open;
696 context->fops.close = _lws_plat_file_close;
697 context->fops.seek_cur = _lws_plat_file_seek_cur;
698 context->fops.read = _lws_plat_file_read;
699 context->fops.write = _lws_plat_file_write;
701 #ifdef LWS_WITH_PLUGINS
702 if (info->plugin_dirs)
703 lws_plat_plugins_init(context, info->plugin_dirs);