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 */
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);
104 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
106 struct lws_context_per_thread *pt = &context->pt[tsi];
110 /* stay dead once we are dead */
112 if (!context || !context->vhost_list)
118 lws_libev_run(context, tsi);
119 lws_libuv_run(context, tsi);
121 if (!context->service_tid_detected) {
124 memset(&_lws, 0, sizeof(_lws));
125 _lws.context = context;
127 context->service_tid_detected =
128 context->vhost_list->protocols[0].callback(
129 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
131 context->service_tid = context->service_tid_detected;
133 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
135 n = poll(pt->fds, pt->fds_count, timeout_ms);
137 #ifdef LWS_OPENSSL_SUPPORT
138 if (!pt->rx_draining_ext_list &&
139 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
141 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
143 lws_service_fd_tsi(context, NULL, tsi);
148 m = lws_service_flag_pending(context, tsi);
150 c = -1; /* unknown limit */
153 if (LWS_ERRNO != LWS_EINTR)
159 /* any socket with events to service? */
160 for (n = 0; n < pt->fds_count && c; n++) {
161 if (!pt->fds[n].revents)
166 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
167 if (read(pt->fds[n].fd, &buf, 1) != 1)
168 lwsl_err("Cannot read from dummy pipe.");
172 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
175 /* if something closed, retry this slot */
184 lws_plat_check_connection_error(struct lws *wsi)
190 lws_plat_service(struct lws_context *context, int timeout_ms)
192 return lws_plat_service_tsi(context, timeout_ms, 0);
196 lws_plat_set_socket_options(struct lws_vhost *vhost, int fd)
199 socklen_t optlen = sizeof(optval);
201 #if defined(__APPLE__) || \
202 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
203 defined(__NetBSD__) || \
205 struct protoent *tcp_proto;
208 if (vhost->ka_time) {
209 /* enable keepalive on this socket */
211 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
212 (const void *)&optval, optlen) < 0)
215 #if defined(__APPLE__) || \
216 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
217 defined(__NetBSD__) || \
218 defined(__CYGWIN__) || defined(__OpenBSD__)
221 * didn't find a way to set these per-socket, need to
222 * tune kernel systemwide values
225 /* set the keepalive conditions we want on it too */
226 optval = vhost->ka_time;
227 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
228 (const void *)&optval, optlen) < 0)
231 optval = vhost->ka_interval;
232 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
233 (const void *)&optval, optlen) < 0)
236 optval = vhost->ka_probes;
237 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
238 (const void *)&optval, optlen) < 0)
245 #if !defined(__APPLE__) && \
246 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
247 !defined(__NetBSD__) && \
248 !defined(__OpenBSD__)
249 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
252 tcp_proto = getprotobyname("TCP");
253 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
257 /* We are nonblocking... */
258 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
265 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
268 if (setgid(info->gid))
269 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
271 if (info->uid != -1) {
272 struct passwd *p = getpwuid(info->uid);
275 initgroups(p->pw_name, info->gid);
276 if (setuid(info->uid))
277 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
279 lwsl_notice("Set privs to user '%s'\n", p->pw_name);
281 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
285 #ifdef LWS_WITH_PLUGINS
287 #if defined(LWS_USE_LIBUV) && UV_VERSION_MAJOR > 0
289 /* libuv.c implements these in a cross-platform way */
293 static int filter(const struct dirent *ent)
295 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
302 lws_plat_plugins_init(struct lws_context * context, const char * const *d)
304 struct lws_plugin_capability lcaps;
305 struct lws_plugin *plugin;
306 lws_plugin_init_func initfunc;
307 struct dirent **namelist;
308 int n, i, m, ret = 0;
312 lwsl_notice(" Plugins:\n");
315 n = scandir(*d, &namelist, filter, alphasort);
317 lwsl_err("Scandir on %s failed\n", *d);
321 for (i = 0; i < n; i++) {
322 if (strlen(namelist[i]->d_name) < 7)
325 lwsl_notice(" %s\n", namelist[i]->d_name);
327 snprintf(path, sizeof(path) - 1, "%s/%s", *d,
328 namelist[i]->d_name);
329 l = dlopen(path, RTLD_NOW);
331 lwsl_err("Error loading DSO: %s\n", dlerror());
336 /* we could open it, can we get his init function? */
337 m = snprintf(path, sizeof(path) - 1, "init_%s",
338 namelist[i]->d_name + 3 /* snip lib... */);
339 path[m - 3] = '\0'; /* snip the .so */
340 initfunc = dlsym(l, path);
342 lwsl_err("Failed to get init on %s: %s",
343 namelist[i]->d_name, dlerror());
346 lcaps.api_magic = LWS_PLUGIN_API_MAGIC;
347 m = initfunc(context, &lcaps);
349 lwsl_err("Initializing %s failed %d\n",
350 namelist[i]->d_name, m);
355 plugin = lws_malloc(sizeof(*plugin));
360 plugin->list = context->plugin_list;
361 context->plugin_list = plugin;
362 strncpy(plugin->name, namelist[i]->d_name, sizeof(plugin->name) - 1);
363 plugin->name[sizeof(plugin->name) - 1] = '\0';
365 plugin->caps = lcaps;
366 context->plugin_protocol_count += lcaps.count_protocols;
367 context->plugin_extension_count += lcaps.count_extensions;
388 lws_plat_plugins_destroy(struct lws_context * context)
390 struct lws_plugin *plugin = context->plugin_list, *p;
391 lws_plugin_destroy_func func;
398 lwsl_notice("%s\n", __func__);
402 m = snprintf(path, sizeof(path) - 1, "destroy_%s", plugin->name + 3);
404 func = dlsym(plugin->l, path);
406 lwsl_err("Failed to get destroy on %s: %s",
407 plugin->name, dlerror());
412 lwsl_err("Initializing %s failed %d\n",
421 context->plugin_list = NULL;
432 sigabrt_handler(int x)
434 printf("%s\n", __func__);
440 lws_plat_context_early_init(void)
442 signal(SIGPIPE, SIG_IGN);
444 // signal(SIGABRT, sigabrt_handler);
450 lws_plat_context_early_destroy(struct lws_context *context)
455 lws_plat_context_late_destroy(struct lws_context *context)
457 struct lws_context_per_thread *pt = &context->pt[0];
458 int m = context->count_threads;
460 #ifdef LWS_WITH_PLUGINS
461 if (context->plugin_list)
462 lws_plat_plugins_destroy(context);
465 if (context->lws_lookup)
466 lws_free(context->lws_lookup);
469 close(pt->dummy_pipe_fds[0]);
470 close(pt->dummy_pipe_fds[1]);
473 close(context->fd_random);
476 /* cast a struct sockaddr_in6 * into addr for ipv6 */
479 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
487 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
491 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
495 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
497 if (strcmp(ifc->ifa_name, ifname))
500 switch (ifc->ifa_addr->sa_family) {
504 /* map IPv4 to IPv6 */
505 bzero((char *)&addr6->sin6_addr,
506 sizeof(struct in6_addr));
507 addr6->sin6_addr.s6_addr[10] = 0xff;
508 addr6->sin6_addr.s6_addr[11] = 0xff;
509 memcpy(&addr6->sin6_addr.s6_addr[12],
510 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
511 sizeof(struct in_addr));
515 (struct sockaddr_in *)ifc->ifa_addr,
516 sizeof(struct sockaddr_in));
520 memcpy(&addr6->sin6_addr,
521 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
522 sizeof(struct in6_addr));
534 /* check if bind to IP adddress */
536 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
540 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
548 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
550 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
552 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
553 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
555 pt->fds[pt->fds_count++].revents = 0;
559 lws_plat_delete_socket_from_fds(struct lws_context *context,
560 struct lws *wsi, int m)
562 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
564 lws_libev_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
565 lws_libuv_io(wsi, LWS_EV_STOP | LWS_EV_READ | LWS_EV_WRITE);
571 lws_plat_service_periodic(struct lws_context *context)
573 /* if our parent went down, don't linger around */
574 if (context->started_with_parent &&
575 kill(context->started_with_parent, 0) < 0)
576 kill(getpid(), SIGTERM);
580 lws_plat_change_pollfd(struct lws_context *context,
581 struct lws *wsi, struct lws_pollfd *pfd)
586 LWS_VISIBLE const char *
587 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
589 return inet_ntop(af, src, dst, cnt);
592 static lws_filefd_type
593 _lws_plat_file_open(struct lws *wsi, const char *filename,
594 unsigned long *filelen, int flags)
596 struct stat stat_buf;
597 int ret = open(filename, flags, 0664);
600 return LWS_INVALID_FILE;
602 if (fstat(ret, &stat_buf) < 0) {
604 return LWS_INVALID_FILE;
606 *filelen = stat_buf.st_size;
611 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
617 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
619 return lseek(fd, offset, SEEK_CUR);
623 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
624 unsigned char *buf, unsigned long len)
628 n = read((int)fd, buf, len);
640 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
641 unsigned char *buf, unsigned long len)
645 n = write((int)fd, buf, len);
658 lws_plat_init(struct lws_context *context,
659 struct lws_context_creation_info *info)
661 struct lws_context_per_thread *pt = &context->pt[0];
662 int n = context->count_threads, fd;
664 /* master context has the global fd lookup array */
665 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
667 if (context->lws_lookup == NULL) {
668 lwsl_err("OOM on lws_lookup array for %d connections\n",
673 lwsl_notice(" mem: platform fd map: %5u bytes\n",
674 sizeof(struct lws *) * context->max_fds);
675 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
677 context->fd_random = fd;
678 if (context->fd_random < 0) {
679 lwsl_err("Unable to open random device %s %d\n",
680 SYSTEM_RANDOM_FILEPATH, context->fd_random);
684 if (!lws_libev_init_fd_table(context) &&
685 !lws_libuv_init_fd_table(context)) {
686 /* otherwise libev handled it instead */
689 if (pipe(pt->dummy_pipe_fds)) {
690 lwsl_err("Unable to create pipe\n");
694 /* use the read end of pipe as first item */
695 pt->fds[0].fd = pt->dummy_pipe_fds[0];
696 pt->fds[0].events = LWS_POLLIN;
697 pt->fds[0].revents = 0;
703 context->fops.open = _lws_plat_file_open;
704 context->fops.close = _lws_plat_file_close;
705 context->fops.seek_cur = _lws_plat_file_seek_cur;
706 context->fops.read = _lws_plat_file_read;
707 context->fops.write = _lws_plat_file_write;
709 #ifdef LWS_WITH_PLUGINS
710 if (info->plugin_dirs)
711 lws_plat_plugins_init(context, info->plugin_dirs);