a894e801f6481c503fe4c42f1aba3d9d20fdcc0d
[platform/upstream/libwebsockets.git] / lib / lws-plat-unix.c
1 #include "private-libwebsockets.h"
2
3 /*
4  * included from libwebsockets.c for unix builds
5  */
6
7 unsigned long long time_in_microseconds(void)
8 {
9         struct timeval tv;
10         gettimeofday(&tv, NULL);
11         return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
12 }
13
14 LWS_VISIBLE int libwebsockets_get_random(struct libwebsocket_context *context,
15                                                              void *buf, int len)
16 {
17         return read(context->fd_random, (char *)buf, len);
18 }
19
20 LWS_VISIBLE int lws_send_pipe_choked(struct libwebsocket *wsi)
21 {
22         struct libwebsocket_pollfd fds;
23
24         /* treat the fact we got a truncated send pending as if we're choked */
25         if (wsi->truncated_send_len)
26                 return 1;
27
28         fds.fd = wsi->sock;
29         fds.events = POLLOUT;
30         fds.revents = 0;
31
32         if (poll(&fds, 1, 0) != 1)
33                 return 1;
34
35         if ((fds.revents & POLLOUT) == 0)
36                 return 1;
37
38         /* okay to send another packet without blocking */
39
40         return 0;
41 }
42
43 LWS_VISIBLE int
44 lws_poll_listen_fd(struct libwebsocket_pollfd *fd)
45 {
46         return poll(fd, 1, 0);
47 }
48
49 /*
50  * This is just used to interrupt poll waiting
51  * we don't have to do anything with it.
52  */
53 static void lws_sigusr2(int sig)
54 {
55 }
56
57 /**
58  * libwebsocket_cancel_service() - Cancel servicing of pending websocket activity
59  * @context:    Websocket context
60  *
61  *      This function let a call to libwebsocket_service() waiting for a timeout
62  *      immediately return.
63  */
64 LWS_VISIBLE void
65 libwebsocket_cancel_service(struct libwebsocket_context *context)
66 {
67         char buf = 0;
68
69         if (write(context->dummy_pipe_fds[1], &buf, sizeof(buf)) != 1)
70                 lwsl_err("Cannot write to dummy pipe");
71 }
72
73 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
74 {
75         int syslog_level = LOG_DEBUG;
76
77         switch (level) {
78         case LLL_ERR:
79                 syslog_level = LOG_ERR;
80                 break;
81         case LLL_WARN:
82                 syslog_level = LOG_WARNING;
83                 break;
84         case LLL_NOTICE:
85                 syslog_level = LOG_NOTICE;
86                 break;
87         case LLL_INFO:
88                 syslog_level = LOG_INFO;
89                 break;
90         }
91         syslog(syslog_level, "%s", line);
92 }
93
94 LWS_VISIBLE int
95 lws_plat_service(struct libwebsocket_context *context, int timeout_ms)
96 {
97         int n;
98         int m;
99         char buf;
100         struct libwebsocket *wsi, *wsi_next;
101
102         /* stay dead once we are dead */
103
104         if (!context)
105                 return 1;
106
107         lws_libev_run(context);
108
109         context->service_tid = context->protocols[0].callback(context, NULL,
110                                      LWS_CALLBACK_GET_THREAD_ID, NULL, NULL, 0);
111
112 #ifdef LWS_OPENSSL_SUPPORT
113         /* if we know we have non-network pending data, do not wait in poll */
114         if (lws_ssl_anybody_has_buffered_read(context))
115                 timeout_ms = 0;
116 #endif
117         n = poll(context->fds, context->fds_count, timeout_ms);
118         context->service_tid = 0;
119
120 #ifdef LWS_OPENSSL_SUPPORT
121         if (!lws_ssl_anybody_has_buffered_read(context) && n == 0) {
122 #else
123         if (n == 0) /* poll timeout */ {
124 #endif
125                 libwebsocket_service_fd(context, NULL);
126                 return 0;
127         }
128
129         if (n < 0) {
130                 if (LWS_ERRNO != LWS_EINTR)
131                         return -1;
132                 return 0;
133         }
134
135 #ifdef LWS_OPENSSL_SUPPORT
136         /*
137          * For all guys with buffered SSL read data already saved up, if they
138          * are not flowcontrolled, fake their POLLIN status so they'll get
139          * service to use up the buffered incoming data, even though their
140          * network socket may have nothing
141          */
142
143         wsi = context->pending_read_list;
144         while (wsi) {
145                 wsi_next = wsi->pending_read_list_next;
146                 context->fds[wsi->sock].revents |=
147                                 context->fds[wsi->sock].events & POLLIN;
148                 if (context->fds[wsi->sock].revents & POLLIN) {
149                         /*
150                          * he's going to get serviced now, take him off the
151                          * list of guys with buffered SSL.  If he still has some
152                          * at the end of the service, he'll get put back on the
153                          * list then.
154                          */
155                         lws_ssl_remove_wsi_from_buffered_list(context, wsi);
156                 }
157                 wsi = wsi_next;
158         }
159 #endif
160
161         /* any socket with events to service? */
162
163         for (n = 0; n < context->fds_count; n++) {
164
165                 if (!context->fds[n].revents)
166                         continue;
167
168                 if (context->fds[n].fd == context->dummy_pipe_fds[0]) {
169                         if (read(context->fds[n].fd, &buf, 1) != 1)
170                                 lwsl_err("Cannot read from dummy pipe.");
171                         continue;
172                 }
173
174                 m = libwebsocket_service_fd(context, &context->fds[n]);
175                 if (m < 0)
176                         return -1;
177                 /* if something closed, retry this slot */
178                 if (m)
179                         n--;
180         }
181
182         return 0;
183 }
184
185 LWS_VISIBLE int
186 lws_plat_set_socket_options(struct libwebsocket_context *context, int fd)
187 {
188         int optval = 1;
189         socklen_t optlen = sizeof(optval);
190
191 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
192     defined(__OpenBSD__)
193         struct protoent *tcp_proto;
194 #endif
195
196         if (context->ka_time) {
197                 /* enable keepalive on this socket */
198                 optval = 1;
199                 if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE,
200                                              (const void *)&optval, optlen) < 0)
201                         return 1;
202
203 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \
204         defined(__CYGWIN__) || defined(__OpenBSD__)
205
206                 /*
207                  * didn't find a way to set these per-socket, need to
208                  * tune kernel systemwide values
209                  */
210 #else
211                 /* set the keepalive conditions we want on it too */
212                 optval = context->ka_time;
213                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE,
214                                              (const void *)&optval, optlen) < 0)
215                         return 1;
216
217                 optval = context->ka_interval;
218                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL,
219                                              (const void *)&optval, optlen) < 0)
220                         return 1;
221
222                 optval = context->ka_probes;
223                 if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT,
224                                              (const void *)&optval, optlen) < 0)
225                         return 1;
226 #endif
227         }
228
229         /* Disable Nagle */
230         optval = 1;
231 #if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && \
232     !defined(__OpenBSD__)
233         if (setsockopt(fd, SOL_TCP, TCP_NODELAY, (const void *)&optval, optlen) < 0)
234                 return 1;
235 #else
236         tcp_proto = getprotobyname("TCP");
237         if (setsockopt(fd, tcp_proto->p_proto, TCP_NODELAY, &optval, optlen) < 0)
238                 return 1;
239 #endif
240
241         /* We are nonblocking... */
242         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
243                 return 1;
244
245         return 0;
246 }
247
248 LWS_VISIBLE void
249 lws_plat_drop_app_privileges(struct lws_context_creation_info *info)
250 {
251         if (info->gid != -1)
252                 if (setgid(info->gid))
253                         lwsl_warn("setgid: %s\n", strerror(LWS_ERRNO));
254         if (info->uid != -1)
255                 if (setuid(info->uid))
256                         lwsl_warn("setuid: %s\n", strerror(LWS_ERRNO));
257 }
258
259 LWS_VISIBLE int
260 lws_plat_init_fd_tables(struct libwebsocket_context *context)
261 {
262         if (lws_libev_init_fd_table(context))
263                 /* libev handled it instead */
264                 return 0;
265
266         if (pipe(context->dummy_pipe_fds)) {
267                 lwsl_err("Unable to create pipe\n");
268                 return 1;
269         }
270
271         /* use the read end of pipe as first item */
272         context->fds[0].fd = context->dummy_pipe_fds[0];
273         context->fds[0].events = LWS_POLLIN;
274         context->fds[0].revents = 0;
275         context->fds_count = 1;
276
277         context->fd_random = open(SYSTEM_RANDOM_FILEPATH, O_RDONLY);
278         if (context->fd_random < 0) {
279                 lwsl_err("Unable to open random device %s %d\n",
280                                     SYSTEM_RANDOM_FILEPATH, context->fd_random);
281                 return 1;
282         }
283
284         return 0;
285 }
286
287 static void sigpipe_handler(int x)
288 {
289 }
290
291
292 LWS_VISIBLE int
293 lws_plat_context_early_init(void)
294 {
295         sigset_t mask;
296
297         signal(SIGUSR2, lws_sigusr2);
298         sigemptyset(&mask);
299         sigaddset(&mask, SIGUSR2);
300
301         sigprocmask(SIG_BLOCK, &mask, NULL);
302
303         signal(SIGPIPE, sigpipe_handler);
304
305         return 0;
306 }
307
308 LWS_VISIBLE void
309 lws_plat_context_early_destroy(struct libwebsocket_context *context)
310 {
311 }
312
313 LWS_VISIBLE void
314 lws_plat_context_late_destroy(struct libwebsocket_context *context)
315 {
316         close(context->dummy_pipe_fds[0]);
317         close(context->dummy_pipe_fds[1]);
318         close(context->fd_random);
319 }
320
321 /* cast a struct sockaddr_in6 * into addr for ipv6 */
322
323 LWS_VISIBLE int
324 interface_to_sa(struct libwebsocket_context *context,
325                 const char *ifname, struct sockaddr_in *addr, size_t addrlen)
326 {
327         int rc = -1;
328
329         struct ifaddrs *ifr;
330         struct ifaddrs *ifc;
331 #ifdef LWS_USE_IPV6
332         struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
333 #endif
334
335         getifaddrs(&ifr);
336         for (ifc = ifr; ifc != NULL && rc; ifc = ifc->ifa_next) {
337                 if (!ifc->ifa_addr)
338                         continue;
339
340                 lwsl_info(" interface %s vs %s\n", ifc->ifa_name, ifname);
341
342                 if (strcmp(ifc->ifa_name, ifname))
343                         continue;
344
345                 switch (ifc->ifa_addr->sa_family) {
346                 case AF_INET:
347 #ifdef LWS_USE_IPV6
348                         if (LWS_IPV6_ENABLED(context)) {
349                                 /* map IPv4 to IPv6 */
350                                 bzero((char *)&addr6->sin6_addr,
351                                                 sizeof(struct in6_addr));
352                                 addr6->sin6_addr.s6_addr[10] = 0xff;
353                                 addr6->sin6_addr.s6_addr[11] = 0xff;
354                                 memcpy(&addr6->sin6_addr.s6_addr[12],
355                                         &((struct sockaddr_in *)ifc->ifa_addr)->sin_addr,
356                                                         sizeof(struct in_addr));
357                         } else
358 #endif
359                                 memcpy(addr,
360                                         (struct sockaddr_in *)ifc->ifa_addr,
361                                                     sizeof(struct sockaddr_in));
362                         break;
363 #ifdef LWS_USE_IPV6
364                 case AF_INET6:
365                         memcpy(&addr6->sin6_addr,
366                           &((struct sockaddr_in6 *)ifc->ifa_addr)->sin6_addr,
367                                                        sizeof(struct in6_addr));
368                         break;
369 #endif
370                 default:
371                         continue;
372                 }
373                 rc = 0;
374         }
375
376         freeifaddrs(ifr);
377
378         if (rc == -1) {
379                 /* check if bind to IP adddress */
380 #ifdef LWS_USE_IPV6
381                 if (inet_pton(AF_INET6, ifname, &addr6->sin6_addr) == 1)
382                         rc = 0;
383                 else
384 #endif
385                 if (inet_pton(AF_INET, ifname, &addr->sin_addr) == 1)
386                         rc = 0;
387         }
388
389         return rc;
390 }
391
392 LWS_VISIBLE void
393 lws_plat_insert_socket_into_fds(struct libwebsocket_context *context,
394                                                        struct libwebsocket *wsi)
395 {
396         lws_libev_io(context, wsi, LWS_EV_START | LWS_EV_READ);
397         context->fds[context->fds_count++].revents = 0;
398 }
399
400 LWS_VISIBLE void
401 lws_plat_delete_socket_from_fds(struct libwebsocket_context *context,
402                                                 struct libwebsocket *wsi, int m)
403 {
404 }
405
406 LWS_VISIBLE void
407 lws_plat_service_periodic(struct libwebsocket_context *context)
408 {
409         /* if our parent went down, don't linger around */
410         if (context->started_with_parent &&
411                               kill(context->started_with_parent, 0) < 0)
412                 kill(getpid(), SIGTERM);
413 }
414
415 LWS_VISIBLE int
416 lws_plat_change_pollfd(struct libwebsocket_context *context,
417                       struct libwebsocket *wsi, struct libwebsocket_pollfd *pfd)
418 {
419         return 0;
420 }
421
422 LWS_VISIBLE int
423 lws_plat_open_file(const char* filename, unsigned long* filelen)
424 {
425         struct stat stat_buf;
426         int ret = open(filename, O_RDONLY);
427
428         if (ret < 0)
429                 return LWS_INVALID_FILE;
430
431         if (fstat(ret, &stat_buf) < 0) {
432                 close(ret);
433                 return LWS_INVALID_FILE;
434         }
435         *filelen = stat_buf.st_size;
436         return ret;
437 }
438
439 #ifdef LWS_USE_IPV6
440 LWS_VISIBLE const char *
441 lws_plat_inet_ntop(int af, const void *src, char *dst, int cnt)
442 {
443         return inet_ntop(af, src, dst, cnt);
444 }
445 #endif