1 #include "private-libwebsockets.h"
7 * included from libwebsockets.c for unix builds
10 unsigned long long time_in_microseconds(void)
13 gettimeofday(&tv, NULL);
14 return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
18 lws_get_random(struct lws_context *context, void *buf, int len)
20 return read(context->fd_random, (char *)buf, len);
24 lws_send_pipe_choked(struct lws *wsi)
26 struct lws_pollfd fds;
28 /* treat the fact we got a truncated send pending as if we're choked */
36 if (poll(&fds, 1, 0) != 1)
39 if ((fds.revents & POLLOUT) == 0)
42 /* okay to send another packet without blocking */
48 lws_poll_listen_fd(struct lws_pollfd *fd)
50 return poll(fd, 1, 0);
54 * This is just used to interrupt poll waiting
55 * we don't have to do anything with it.
63 * lws_cancel_service_pt() - Cancel servicing of pending socket activity
65 * @wsi: Cancel service on the thread this wsi is serviced by
67 * This function let a call to lws_service() waiting for a timeout
71 lws_cancel_service_pt(struct lws *wsi)
73 struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
76 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
77 lwsl_err("Cannot write to dummy pipe");
81 * lws_cancel_service() - Cancel ALL servicing of pending socket activity
82 * @context: Websocket context
84 * This function let a call to lws_service() waiting for a timeout
88 lws_cancel_service(struct lws_context *context)
90 struct lws_context_per_thread *pt = &context->pt[0];
91 char buf = 0, m = context->count_threads;
94 if (write(pt->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
95 lwsl_err("Cannot write to dummy pipe");
100 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
102 int syslog_level = LOG_DEBUG;
106 syslog_level = LOG_ERR;
109 syslog_level = LOG_WARNING;
112 syslog_level = LOG_NOTICE;
115 syslog_level = LOG_INFO;
118 syslog(syslog_level, "%s", line);
122 lws_plat_service_tsi(struct lws_context *context, int timeout_ms, int tsi)
124 struct lws_context_per_thread *pt = &context->pt[tsi];
128 /* stay dead once we are dead */
133 lws_libev_run(context, tsi);
134 lws_libuv_run(context, tsi);
136 if (!context->service_tid_detected) {
139 memset(&_lws, 0, sizeof(_lws));
140 _lws.context = context;
142 context->service_tid_detected = context->protocols[0].callback(
143 &_lws, LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
145 context->service_tid = context->service_tid_detected;
147 timeout_ms = lws_service_adjust_timeout(context, timeout_ms, tsi);
149 n = poll(pt->fds, pt->fds_count, timeout_ms);
151 #ifdef LWS_OPENSSL_SUPPORT
152 if (!pt->rx_draining_ext_list &&
153 !lws_ssl_anybody_has_buffered_read_tsi(context, tsi) && !n) {
155 if (!pt->rx_draining_ext_list && !n) /* poll timeout */ {
157 lws_service_fd_tsi(context, NULL, tsi);
162 if (LWS_ERRNO != LWS_EINTR)
169 lws_service_flag_pending(context, tsi);
171 /* any socket with events to service? */
172 for (n = 0; n < pt->fds_count && c; n++) {
173 if (!pt->fds[n].revents)
178 if (pt->fds[n].fd == pt->dummy_pipe_fds[0]) {
179 if (read(pt->fds[n].fd, &buf, 1) != 1)
180 lwsl_err("Cannot read from dummy pipe.");
184 m = lws_service_fd_tsi(context, &pt->fds[n], tsi);
187 /* if something closed, retry this slot */
196 lws_plat_service(struct lws_context *context, int timeout_ms)
198 return lws_plat_service_tsi(context, timeout_ms, 0);
202 lws_plat_set_socket_options(struct lws_context *context, int fd)
205 socklen_t optlen = sizeof(optval);
207 #if defined(__APPLE__) || \
208 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
209 defined(__NetBSD__) || \
211 struct protoent *tcp_proto;
214 if (context->ka_time) {
215 /* enable keepalive on this socket */
217 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
218 (const void *)&optval, optlen) < 0)
221 #if defined(__APPLE__) || \
222 defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
223 defined(__NetBSD__) || \
224 defined(__CYGWIN__) || defined(__OpenBSD__)
227 * didn't find a way to set these per-socket, need to
228 * tune kernel systemwide values
231 /* set the keepalive conditions we want on it too */
232 optval = context->ka_time;
233 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
234 (const void *)&optval, optlen) < 0)
237 optval = context->ka_interval;
238 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
239 (const void *)&optval, optlen) < 0)
242 optval = context->ka_probes;
243 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
244 (const void *)&optval, optlen) < 0)
251 #if !defined(__APPLE__) && \
252 !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && \
253 !defined(__NetBSD__) && \
254 !defined(__OpenBSD__)
255 if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
258 tcp_proto = getprotobyname("TCP");
259 if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
263 /* We are nonblocking... */
264 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
271 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
273 if (info->uid != -1) {
274 struct passwd *p = getpwuid(info->uid);
277 initgroups(p->pw_name, info->gid);
278 if (setuid(info->uid))
279 lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
281 lwsl_notice(" Set privs to user '%s'\n", p->pw_name);
283 lwsl_warn("getpwuid: unable to find uid %d", info->uid);
286 if (setgid(info->gid))
287 lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
292 sigpipe_handler(int x)
297 lws_plat_context_early_init(void)
301 signal(SIGUSR2, lws_sigusr2);
303 sigaddset(&mask, SIGUSR2);
305 sigprocmask(SIG_BLOCK, &mask, NULL);
307 signal(SIGPIPE, sigpipe_handler);
313 lws_plat_context_early_destroy(struct lws_context *context)
318 lws_plat_context_late_destroy(struct lws_context *context)
320 struct lws_context_per_thread *pt = &context->pt[0];
321 int m = context->count_threads;
323 if (context->lws_lookup)
324 lws_free(context->lws_lookup);
327 close(pt->dummy_pipe_fds[0]);
328 close(pt->dummy_pipe_fds[1]);
331 close(context->fd_random);
334 /* cast a struct sockaddr_in6 * into addr for ipv6 */
337 lws_interface_to_sa(int ipv6, const char *ifname, struct sockaddr_in *addr,
345 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
349 for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
353 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
355 if (strcmp(ifc->ifa_name, ifname))
358 switch (ifc->ifa_addr->sa_family) {
362 /* map IPv4 to IPv6 */
363 bzero((char *)&addr6->sin6_addr,
364 sizeof(struct in6_addr));
365 addr6->sin6_addr.s6_addr[10] = 0xff;
366 addr6->sin6_addr.s6_addr[11] = 0xff;
367 memcpy(&addr6->sin6_addr.s6_addr[12],
368 &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
369 sizeof(struct in_addr));
373 (struct sockaddr_in *)ifc->ifa_addr,
374 sizeof(struct sockaddr_in));
378 memcpy(&addr6->sin6_addr,
379 &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
380 sizeof(struct in6_addr));
392 /* check if bind to IP adddress */
394 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
398 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
406 lws_plat_insert_socket_into_fds(struct lws_context *context, struct lws *wsi)
408 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
410 lws_libev_io(wsi, LWS_EV_START | LWS_EV_READ);
411 lws_libuv_io(wsi, LWS_EV_START | LWS_EV_READ);
413 pt->fds[pt->fds_count++].revents = 0;
417 lws_plat_delete_socket_from_fds(struct lws_context *context,
418 struct lws *wsi, int m)
420 struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
425 lws_plat_service_periodic(struct lws_context *context)
427 /* if our parent went down, don't linger around */
428 if (context->started_with_parent &&
429 kill(context->started_with_parent, 0) < 0)
430 kill(getpid(), SIGTERM);
434 lws_plat_change_pollfd(struct lws_context *context,
435 struct lws *wsi, struct lws_pollfd *pfd)
440 LWS_VISIBLE const char *
441 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
443 return inet_ntop(af, src, dst, cnt);
446 static lws_filefd_type
447 _lws_plat_file_open(struct lws *wsi, const char *filename,
448 unsigned long *filelen, int flags)
450 struct stat stat_buf;
451 int ret = open(filename, flags, 0664);
454 return LWS_INVALID_FILE;
456 if (fstat(ret, &stat_buf) < 0) {
458 return LWS_INVALID_FILE;
460 *filelen = stat_buf.st_size;
465 _lws_plat_file_close(struct lws *wsi, lws_filefd_type fd)
471 _lws_plat_file_seek_cur(struct lws *wsi, lws_filefd_type fd, long offset)
473 return lseek(fd, offset, SEEK_CUR);
477 _lws_plat_file_read(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
478 unsigned char *buf, unsigned long len)
482 n = read((int)fd, buf, len);
494 _lws_plat_file_write(struct lws *wsi, lws_filefd_type fd, unsigned long *amount,
495 unsigned char *buf, unsigned long len)
499 n = write((int)fd, buf, len);
511 lws_plat_init(struct lws_context *context,
512 struct lws_context_creation_info *info)
514 struct lws_context_per_thread *pt = &context->pt[0];
515 int n = context->count_threads, fd;
517 /* master context has the global fd lookup array */
518 context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
520 if (context->lws_lookup == NULL) {
521 lwsl_err("OOM on lws_lookup array for %d connections\n",
526 lwsl_notice(" mem: platform fd map: %5u bytes\n",
527 sizeof(struct lws *) * context->max_fds);
528 fd = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
530 context->fd_random = fd;
531 if (context->fd_random < 0) {
532 lwsl_err("Unable to open random device %s %d\n",
533 SYSTEM_RANDOM_FILEPATH, context->fd_random);
537 if (!lws_libev_init_fd_table(context) &&
538 !lws_libuv_init_fd_table(context)) {
539 /* otherwise libev handled it instead */
542 if (pipe(pt->dummy_pipe_fds)) {
543 lwsl_err("Unable to create pipe\n");
547 /* use the read end of pipe as first item */
548 pt->fds[0].fd = pt->dummy_pipe_fds[0];
549 pt->fds[0].events = LWS_POLLIN;
550 pt->fds[0].revents = 0;
556 context->fops.open = _lws_plat_file_open;
557 context->fops.close = _lws_plat_file_close;
558 context->fops.seek_cur = _lws_plat_file_seek_cur;
559 context->fops.read = _lws_plat_file_read;
560 context->fops.write = _lws_plat_file_write;