/src/libwebsockets/lib/plat/unix/unix-misc.c
Line | Count | Source |
1 | | /* |
2 | | * libwebsockets - small server side websockets and web server implementation |
3 | | * |
4 | | * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com> |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | * of this software and associated documentation files (the "Software"), to |
8 | | * deal in the Software without restriction, including without limitation the |
9 | | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
10 | | * sell copies of the Software, and to permit persons to whom the Software is |
11 | | * furnished to do so, subject to the following conditions: |
12 | | * |
13 | | * The above copyright notice and this permission notice shall be included in |
14 | | * all copies or substantial portions of the Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
22 | | * IN THE SOFTWARE. |
23 | | */ |
24 | | |
25 | | #if !defined(_GNU_SOURCE) |
26 | | #define _GNU_SOURCE |
27 | | #endif |
28 | | #include "private-lib-core.h" |
29 | | |
30 | | /* |
31 | | * Normally you don't want this, use lws_sul instead inside the event loop. |
32 | | * But sometimes for drivers it makes sense, so there's an internal-only |
33 | | * crossplatform api for it. |
34 | | */ |
35 | | |
36 | | void |
37 | | lws_msleep(unsigned int ms) |
38 | 0 | { |
39 | 0 | usleep((unsigned int)(ms * LWS_US_PER_MS)); |
40 | 0 | } |
41 | | |
42 | | lws_usec_t |
43 | | lws_now_usecs(void) |
44 | 281 | { |
45 | 281 | #if defined(LWS_HAVE_CLOCK_GETTIME) |
46 | 281 | struct timespec ts; |
47 | | |
48 | 281 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
49 | 0 | return 0; |
50 | | |
51 | 281 | return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) + |
52 | 281 | ((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US); |
53 | | #else |
54 | | struct timeval now; |
55 | | |
56 | | gettimeofday(&now, NULL); |
57 | | return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) + |
58 | | (lws_usec_t)now.tv_usec; |
59 | | #endif |
60 | 281 | } |
61 | | |
62 | | size_t |
63 | | lws_plat_get_random(struct lws_context *context, void *buf, size_t len) |
64 | 0 | { |
65 | | #if defined(__COVERITY__) |
66 | | memset(buf, 0, len); |
67 | | return len; |
68 | | #else |
69 | 0 | uint8_t *p = (uint8_t *)buf; |
70 | 0 | size_t done = 0; |
71 | | |
72 | | /* |
73 | | * read() on the random source may return short (eg, a signal arrived |
74 | | * during it), and may fail. Callers are entitled to believe that a |
75 | | * return of len means len good random bytes... so loop, and on any |
76 | | * hard failure destroy whatever partial content we produced and |
77 | | * return the real count, ie, 0. |
78 | | */ |
79 | |
|
80 | 0 | while (done < len) { |
81 | | /* coverity[tainted_scalar] */ |
82 | 0 | ssize_t n = read(context->fd_random, p + done, len - done); |
83 | |
|
84 | 0 | if (n > 0) { |
85 | 0 | done += (size_t)n; |
86 | 0 | continue; |
87 | 0 | } |
88 | | |
89 | 0 | if (n < 0 && errno == EINTR) |
90 | 0 | continue; |
91 | | |
92 | | /* 0 = EOF, or a hard error... we cannot fulfil this */ |
93 | | |
94 | 0 | lws_explicit_bzero(buf, len); |
95 | |
|
96 | 0 | return 0; |
97 | 0 | } |
98 | | |
99 | 0 | return done; |
100 | 0 | #endif |
101 | 0 | } |
102 | | |
103 | | void lwsl_emit_syslog(int level, const char *line) |
104 | 0 | { |
105 | 0 | int syslog_level = LOG_DEBUG; |
106 | |
|
107 | 0 | switch (level) { |
108 | 0 | case LLL_ERR: |
109 | 0 | syslog_level = LOG_ERR; |
110 | 0 | break; |
111 | 0 | case LLL_WARN: |
112 | 0 | syslog_level = LOG_WARNING; |
113 | 0 | break; |
114 | 0 | case LLL_NOTICE: |
115 | 0 | syslog_level = LOG_NOTICE; |
116 | 0 | break; |
117 | 0 | case LLL_INFO: |
118 | 0 | syslog_level = LOG_INFO; |
119 | 0 | break; |
120 | 0 | } |
121 | 0 | syslog(syslog_level, "%s", line); |
122 | 0 | } |
123 | | |
124 | | |
125 | | int |
126 | | lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf, |
127 | | size_t len) |
128 | 0 | { |
129 | 0 | ssize_t n; |
130 | |
|
131 | 0 | n = write(fd, buf, len); |
132 | |
|
133 | 0 | if (n < 0 || fsync(fd)) |
134 | 0 | return 1; |
135 | 0 | if (lseek(fd, 0, SEEK_SET) < 0) |
136 | 0 | return 1; |
137 | | |
138 | 0 | return (size_t)n != len; |
139 | 0 | } |
140 | | |
141 | | |
142 | | int |
143 | | lws_plat_recommended_rsa_bits(void) |
144 | 0 | { |
145 | 0 | return 4096; |
146 | 0 | } |