1 /*
2 * OS specific functions
3 * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #ifndef OS_H
16 #define OS_H
17 #include <zephyr/kernel.h>
18 #include "esp_types.h"
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "esp_err.h"
23 #include "supplicant_opt.h"
24
25 typedef long os_time_t;
26
27 /**
28 * os_sleep - Sleep (sec, usec)
29 * @sec: Number of seconds to sleep
30 * @usec: Number of microseconds to sleep
31 */
32 void os_sleep(os_time_t sec, os_time_t usec);
33
34 struct os_time {
35 os_time_t sec;
36 os_time_t usec;
37 };
38
39 #define os_reltime os_time
40
41 struct os_tm {
42 int sec; /* 0..59 or 60 for leap seconds */
43 int min; /* 0..59 */
44 int hour; /* 0..23 */
45 int day; /* 1..31 */
46 int month; /* 1..12 */
47 int year; /* Four digit year */
48 };
49
50 /**
51 * os_get_time - Get current time (sec, usec)
52 * @t: Pointer to buffer for the time
53 * Returns: 0 on success, -1 on failure
54 */
55 int os_get_time(struct os_time *t);
56 #define os_get_reltime os_get_time
57
58 /* Helper macros for handling struct os_time */
59
60 #define os_time_before(a, b) \
61 ((a)->sec < (b)->sec || \
62 ((a)->sec == (b)->sec && (a)->usec < (b)->usec))
63
64 #define os_reltime_before os_time_before
65 #define os_time_sub(a, b, res) do { \
66 (res)->sec = (a)->sec - (b)->sec; \
67 (res)->usec = (a)->usec - (b)->usec; \
68 if ((res)->usec < 0) { \
69 (res)->sec--; \
70 (res)->usec += 1000000; \
71 } \
72 } while (0)
73 #define os_reltime_sub os_time_sub
74
75 /**
76 * os_mktime - Convert broken-down time into seconds since 1970-01-01
77 * @year: Four digit year
78 * @month: Month (1 .. 12)
79 * @day: Day of month (1 .. 31)
80 * @hour: Hour (0 .. 23)
81 * @min: Minute (0 .. 59)
82 * @sec: Second (0 .. 60)
83 * @t: Buffer for returning calendar time representation (seconds since
84 * 1970-01-01 00:00:00)
85 * Returns: 0 on success, -1 on failure
86 *
87 * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
88 * which is used by POSIX mktime().
89 */
90 int os_mktime(int year, int month, int day, int hour, int min, int sec,
91 os_time_t *t);
92
93 int os_gmtime(os_time_t t, struct os_tm *tm);
94
95 /**
96 * os_daemonize - Run in the background (detach from the controlling terminal)
97 * @pid_file: File name to write the process ID to or %NULL to skip this
98 * Returns: 0 on success, -1 on failure
99 */
100 int os_daemonize(const char *pid_file);
101
102 /**
103 * os_daemonize_terminate - Stop running in the background (remove pid file)
104 * @pid_file: File name to write the process ID to or %NULL to skip this
105 */
106 void os_daemonize_terminate(const char *pid_file);
107
108 /**
109 * os_get_random - Get cryptographically strong pseudo random data
110 * @buf: Buffer for pseudo random data
111 * @len: Length of the buffer
112 * Returns: 0 on success, -1 on failure
113 */
114 int os_get_random(unsigned char *buf, size_t len);
115
116 /**
117 * os_random - Get pseudo random value (not necessarily very strong)
118 * Returns: Pseudo random value
119 */
120 unsigned long os_random(void);
121
122 /**
123 * os_rel2abs_path - Get an absolute path for a file
124 * @rel_path: Relative path to a file
125 * Returns: Absolute path for the file or %NULL on failure
126 *
127 * This function tries to convert a relative path of a file to an absolute path
128 * in order for the file to be found even if current working directory has
129 * changed. The returned value is allocated and caller is responsible for
130 * freeing it. It is acceptable to just return the same path in an allocated
131 * buffer, e.g., return strdup(rel_path). This function is only used to find
132 * configuration files when os_daemonize() may have changed the current working
133 * directory and relative path would be pointing to a different location.
134 */
135 char * os_rel2abs_path(const char *rel_path);
136
137 /**
138 * os_program_init - Program initialization (called at start)
139 * Returns: 0 on success, -1 on failure
140 *
141 * This function is called when a programs starts. If there are any OS specific
142 * processing that is needed, it can be placed here. It is also acceptable to
143 * just return 0 if not special processing is needed.
144 */
145 int os_program_init(void);
146
147 /**
148 * os_program_deinit - Program deinitialization (called just before exit)
149 *
150 * This function is called just before a program exists. If there are any OS
151 * specific processing, e.g., freeing resourced allocated in os_program_init(),
152 * it should be done here. It is also acceptable for this function to do
153 * nothing.
154 */
155 void os_program_deinit(void);
156
157 /**
158 * os_setenv - Set environment variable
159 * @name: Name of the variable
160 * @value: Value to set to the variable
161 * @overwrite: Whether existing variable should be overwritten
162 * Returns: 0 on success, -1 on error
163 *
164 * This function is only used for wpa_cli action scripts. OS wrapper does not
165 * need to implement this if such functionality is not needed.
166 */
167 int os_setenv(const char *name, const char *value, int overwrite);
168
169 /**
170 * os_unsetenv - Delete environent variable
171 * @name: Name of the variable
172 * Returns: 0 on success, -1 on error
173 *
174 * This function is only used for wpa_cli action scripts. OS wrapper does not
175 * need to implement this if such functionality is not needed.
176 */
177 int os_unsetenv(const char *name);
178
179 /**
180 * os_readfile - Read a file to an allocated memory buffer
181 * @name: Name of the file to read
182 * @len: For returning the length of the allocated buffer
183 * Returns: Pointer to the allocated buffer or %NULL on failure
184 *
185 * This function allocates memory and reads the given file to this buffer. Both
186 * binary and text files can be read with this function. The caller is
187 * responsible for freeing the returned buffer with os_free().
188 */
189 char * os_readfile(const char *name, size_t *len);
190
191 /*
192 * The following functions are wrapper for standard ANSI C or POSIX functions.
193 * By default, they are just defined to use the standard function name and no
194 * os_*.c implementation is needed for them. This avoids extra function calls
195 * by allowing the C pre-processor take care of the function name mapping.
196 *
197 * If the target system uses a C library that does not provide these functions,
198 * build_config.h can be used to define the wrappers to use a different
199 * function name. This can be done on function-by-function basis since the
200 * defines here are only used if build_config.h does not define the os_* name.
201 * If needed, os_*.c file can be used to implement the functions that are not
202 * included in the C library on the target system. Alternatively,
203 * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
204 * these functions need to be implemented in os_*.c file for the target system.
205 */
206
207 #ifndef os_malloc
208 #define os_malloc(s) k_malloc((s))
209 #endif
210 #ifndef os_realloc
211 #define os_realloc(p, s) realloc((p), (s))
212 #endif
213 #ifndef os_zalloc
214 #define os_zalloc(s) k_calloc(1, (s))
215 #endif
216 #ifndef os_calloc
217 #define os_calloc(p, s) calloc((p), (s))
218 #endif
219
220 #ifndef os_free
221 #define os_free(p) k_free((p))
222 #endif
223
224 #ifndef os_bzero
225 #define os_bzero(s, n) memset(s, 0, n)
226 #endif
227
228
229 #ifndef os_strdup
230 #ifdef _MSC_VER
231 #define os_strdup(s) _strdup(s)
232 #else
233 #define os_strdup(s) strdup(s)
234 #endif
235 #endif
236 char * ets_strdup(const char *s);
237
238 #ifndef os_memcpy
239 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
240 #endif
241 #ifndef os_memmove
242 #define os_memmove(d, s, n) memmove((d), (s), (n))
243 #endif
244 #ifndef os_memset
245 #define os_memset(s, c, n) memset(s, c, n)
246 #endif
247 #ifndef os_memcmp
248 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
249 #endif
250 #ifndef os_memcmp_const
251 #define os_memcmp_const(s1, s2, n) memcmp((s1), (s2), (n))
252 #endif
253
254
255 #ifndef os_strlen
256 #define os_strlen(s) strlen(s)
257 #endif
258 #ifndef os_strcasecmp
259 #ifdef _MSC_VER
260 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
261 #else
262 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
263 #endif
264 #endif
265 #ifndef os_strncasecmp
266 #ifdef _MSC_VER
267 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
268 #else
269 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
270 #endif
271 #endif
272 #ifndef os_strchr
273 #define os_strchr(s, c) strchr((s), (c))
274 #endif
275 #ifndef os_strcmp
276 #define os_strcmp(s1, s2) strcmp((s1), (s2))
277 #endif
278 #ifndef os_strncmp
279 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
280 #endif
281 #ifndef os_strncpy
282 #define os_strncpy(d, s, n) strncpy((d), (s), (n))
283 #endif
284 #ifndef os_strrchr
285 #define os_strrchr(s, c) strrchr((s), (c))
286 #endif
287 #ifndef os_strstr
288 #define os_strstr(h, n) strstr((h), (n))
289 #endif
290 #ifndef os_strlcpy
291 #define os_strlcpy(d, s, n) strlcpy((d), (s), (n))
292 #endif
293 #ifndef os_strcat
294 #define os_strcat(d, s) strcat((d), (s))
295 #endif
296
297 #ifndef os_snprintf
298 #ifdef _MSC_VER
299 #define os_snprintf _snprintf
300 #else
301 #define os_snprintf snprintf
302 #endif
303 #endif
304 #ifndef os_sprintf
305 #define os_sprintf sprintf
306 #endif
307
os_snprintf_error(size_t size,int res)308 static inline int os_snprintf_error(size_t size, int res)
309 {
310 return res < 0 || (unsigned int) res >= size;
311 }
312
os_realloc_array(void * ptr,size_t nmemb,size_t size)313 static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
314 {
315 if (size && nmemb > (~(size_t) 0) / size)
316 return NULL;
317 return os_realloc(ptr, nmemb * size);
318 }
319
320 #ifdef USE_MBEDTLS_CRYPTO
321 void forced_memzero(void *ptr, size_t len);
322 #else
323 /* Try to prevent most compilers from optimizing out clearing of memory that
324 * becomes unaccessible after this function is called. This is mostly the case
325 * for clearing local stack variables at the end of a function. This is not
326 * exactly perfect, i.e., someone could come up with a compiler that figures out
327 * the pointer is pointing to memset and then end up optimizing the call out, so
328 * try go a bit further by storing the first octet (now zero) to make this even
329 * a bit more difficult to optimize out. Once memset_s() is available, that
330 * could be used here instead. */
331 static void * (* const volatile memset_func)(void *, int, size_t) = memset;
332 static uint8_t forced_memzero_val;
333
forced_memzero(void * ptr,size_t len)334 static inline void forced_memzero(void *ptr, size_t len)
335 {
336 memset_func(ptr, 0, len);
337 if (len) {
338 forced_memzero_val = ((uint8_t *) ptr)[0];
339 }
340 }
341 #endif
342 #endif /* OS_H */
343