/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 | 116 | { |
45 | 116 | #if defined(LWS_HAVE_CLOCK_GETTIME) |
46 | 116 | struct timespec ts; |
47 | | |
48 | 116 | if (clock_gettime(CLOCK_MONOTONIC, &ts)) |
49 | 0 | return 0; |
50 | | |
51 | 116 | return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) + |
52 | 116 | ((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 | 116 | } |
61 | | |
62 | | size_t |
63 | | lws_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 | | /* coverity[tainted_scalar] */ |
70 | 0 | return (size_t)read(context->fd_random, (char *)buf, len); |
71 | 0 | #endif |
72 | 0 | } |
73 | | |
74 | | void lwsl_emit_syslog(int level, const char *line) |
75 | 0 | { |
76 | 0 | int syslog_level = LOG_DEBUG; |
77 | |
|
78 | 0 | switch (level) { |
79 | 0 | case LLL_ERR: |
80 | 0 | syslog_level = LOG_ERR; |
81 | 0 | break; |
82 | 0 | case LLL_WARN: |
83 | 0 | syslog_level = LOG_WARNING; |
84 | 0 | break; |
85 | 0 | case LLL_NOTICE: |
86 | 0 | syslog_level = LOG_NOTICE; |
87 | 0 | break; |
88 | 0 | case LLL_INFO: |
89 | 0 | syslog_level = LOG_INFO; |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | syslog(syslog_level, "%s", line); |
93 | 0 | } |
94 | | |
95 | | |
96 | | int |
97 | | lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf, |
98 | | size_t len) |
99 | 0 | { |
100 | 0 | ssize_t n; |
101 | |
|
102 | 0 | n = write(fd, buf, len); |
103 | |
|
104 | 0 | if (n < 0 || fsync(fd)) |
105 | 0 | return 1; |
106 | 0 | if (lseek(fd, 0, SEEK_SET) < 0) |
107 | 0 | return 1; |
108 | | |
109 | 0 | return (size_t)n != len; |
110 | 0 | } |
111 | | |
112 | | |
113 | | int |
114 | | lws_plat_recommended_rsa_bits(void) |
115 | 0 | { |
116 | 0 | return 4096; |
117 | 0 | } |
118 | | |
119 | | /* |
120 | | * Platform-specific ntpclient server configuration |
121 | | */ |
122 | | |
123 | | int |
124 | | lws_plat_ntpclient_config(struct lws_context *context) |
125 | 0 | { |
126 | 0 | #if defined(LWS_HAVE_GETENV) |
127 | 0 | char *ntpsrv = getenv("LWS_NTP_SERVER"); |
128 | |
|
129 | 0 | if (ntpsrv && strlen(ntpsrv) < 64) { |
130 | 0 | lws_system_blob_t *blob = lws_system_get_blob(context, |
131 | 0 | LWS_SYSBLOB_TYPE_NTP_SERVER, 0); |
132 | 0 | if (!blob) |
133 | 0 | return 0; |
134 | | |
135 | 0 | lws_system_blob_direct_set(blob, (const uint8_t *)ntpsrv, |
136 | 0 | strlen(ntpsrv)); |
137 | 0 | return 1; |
138 | 0 | } |
139 | 0 | #endif |
140 | 0 | return 0; |
141 | 0 | } |
142 | | |