| 1 |
/* |
| 2 |
* OS specific functions |
| 3 |
* Copyright (c) 2005-2009, Jouni Malinen <[email protected]> |
| 4 |
* |
| 5 |
* This software may be distributed under the terms of the BSD license. |
| 6 |
* See README for more details. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef OS_H |
| 10 |
#define OS_H |
| 11 |
|
| 12 |
typedef long os_time_t; |
| 13 |
|
| 14 |
/** |
| 15 |
* os_sleep - Sleep (sec, usec) |
| 16 |
* @sec: Number of seconds to sleep |
| 17 |
* @usec: Number of microseconds to sleep |
| 18 |
*/ |
| 19 |
void os_sleep(os_time_t sec, os_time_t usec); |
| 20 |
|
| 21 |
struct os_time { |
| 22 |
os_time_t sec; |
| 23 |
os_time_t usec; |
| 24 |
}; |
| 25 |
|
| 26 |
struct os_reltime { |
| 27 |
os_time_t sec; |
| 28 |
os_time_t usec; |
| 29 |
}; |
| 30 |
|
| 31 |
/** |
| 32 |
* os_get_time - Get current time (sec, usec) |
| 33 |
* @t: Pointer to buffer for the time |
| 34 |
* Returns: 0 on success, -1 on failure |
| 35 |
*/ |
| 36 |
int os_get_time(struct os_time *t); |
| 37 |
|
| 38 |
/** |
| 39 |
* os_get_reltime - Get relative time (sec, usec) |
| 40 |
* @t: Pointer to buffer for the time |
| 41 |
* Returns: 0 on success, -1 on failure |
| 42 |
*/ |
| 43 |
int os_get_reltime(struct os_reltime *t); |
| 44 |
|
| 45 |
|
| 46 |
/* Helpers for handling struct os_time */ |
| 47 |
|
| 48 |
static inline int os_time_before(struct os_time *a, struct os_time *b) |
| 49 |
{ |
| 50 |
return (a->sec < b->sec) || |
| 51 |
(a->sec == b->sec && a->usec < b->usec); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
static inline void os_time_sub(struct os_time *a, struct os_time *b, |
| 56 |
struct os_time *res) |
| 57 |
{ |
| 58 |
res->sec = a->sec - b->sec; |
| 59 |
res->usec = a->usec - b->usec; |
| 60 |
if (res->usec < 0) { |
| 61 |
res->sec--; |
| 62 |
res->usec += 1000000; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/* Helpers for handling struct os_reltime */ |
| 68 |
|
| 69 |
static inline int os_reltime_before(struct os_reltime *a, |
| 70 |
struct os_reltime *b) |
| 71 |
{ |
| 72 |
return (a->sec < b->sec) || |
| 73 |
(a->sec == b->sec && a->usec < b->usec); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b, |
| 78 |
struct os_reltime *res) |
| 79 |
{ |
| 80 |
res->sec = a->sec - b->sec; |
| 81 |
res->usec = a->usec - b->usec; |
| 82 |
if (res->usec < 0) { |
| 83 |
res->sec--; |
| 84 |
res->usec += 1000000; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
static inline void os_reltime_age(struct os_reltime *start, |
| 90 |
struct os_reltime *age) |
| 91 |
{ |
| 92 |
struct os_reltime now; |
| 93 |
|
| 94 |
os_get_reltime(&now); |
| 95 |
os_reltime_sub(&now, start, age); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
static inline int os_reltime_expired(struct os_reltime *now, |
| 100 |
struct os_reltime *ts, |
| 101 |
os_time_t timeout_secs) |
| 102 |
{ |
| 103 |
struct os_reltime age; |
| 104 |
|
| 105 |
os_reltime_sub(now, ts, &age); |
| 106 |
return (age.sec > timeout_secs) || |
| 107 |
(age.sec == timeout_secs && age.usec > 0); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static inline int os_reltime_initialized(struct os_reltime *t) |
| 112 |
{ |
| 113 |
return t->sec != 0 || t->usec != 0; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* os_mktime - Convert broken-down time into seconds since 1970-01-01 |
| 119 |
* @year: Four digit year |
| 120 |
* @month: Month (1 .. 12) |
| 121 |
* @day: Day of month (1 .. 31) |
| 122 |
* @hour: Hour (0 .. 23) |
| 123 |
* @min: Minute (0 .. 59) |
| 124 |
* @sec: Second (0 .. 60) |
| 125 |
* @t: Buffer for returning calendar time representation (seconds since |
| 126 |
* 1970-01-01 00:00:00) |
| 127 |
* Returns: 0 on success, -1 on failure |
| 128 |
* |
| 129 |
* Note: The result is in seconds from Epoch, i.e., in UTC, not in local time |
| 130 |
* which is used by POSIX mktime(). |
| 131 |
*/ |
| 132 |
int os_mktime(int year, int month, int day, int hour, int min, int sec, |
| 133 |
os_time_t *t); |
| 134 |
|
| 135 |
struct os_tm { |
| 136 |
int sec; /* 0..59 or 60 for leap seconds */ |
| 137 |
int min; /* 0..59 */ |
| 138 |
int hour; /* 0..23 */ |
| 139 |
int day; /* 1..31 */ |
| 140 |
int month; /* 1..12 */ |
| 141 |
int year; /* Four digit year */ |
| 142 |
}; |
| 143 |
|
| 144 |
int os_gmtime(os_time_t t, struct os_tm *tm); |
| 145 |
|
| 146 |
/** |
| 147 |
* os_daemonize - Run in the background (detach from the controlling terminal) |
| 148 |
* @pid_file: File name to write the process ID to or %NULL to skip this |
| 149 |
* Returns: 0 on success, -1 on failure |
| 150 |
*/ |
| 151 |
int os_daemonize(const char *pid_file); |
| 152 |
|
| 153 |
/** |
| 154 |
* os_daemonize_terminate - Stop running in the background (remove pid file) |
| 155 |
* @pid_file: File name to write the process ID to or %NULL to skip this |
| 156 |
*/ |
| 157 |
void os_daemonize_terminate(const char *pid_file); |
| 158 |
|
| 159 |
/** |
| 160 |
* os_get_random - Get cryptographically strong pseudo random data |
| 161 |
* @buf: Buffer for pseudo random data |
| 162 |
* @len: Length of the buffer |
| 163 |
* Returns: 0 on success, -1 on failure |
| 164 |
*/ |
| 165 |
int os_get_random(unsigned char *buf, size_t len); |
| 166 |
|
| 167 |
/** |
| 168 |
* os_random - Get pseudo random value (not necessarily very strong) |
| 169 |
* Returns: Pseudo random value |
| 170 |
*/ |
| 171 |
unsigned long os_random(void); |
| 172 |
|
| 173 |
/** |
| 174 |
* os_rel2abs_path - Get an absolute path for a file |
| 175 |
* @rel_path: Relative path to a file |
| 176 |
* Returns: Absolute path for the file or %NULL on failure |
| 177 |
* |
| 178 |
* This function tries to convert a relative path of a file to an absolute path |
| 179 |
* in order for the file to be found even if current working directory has |
| 180 |
* changed. The returned value is allocated and caller is responsible for |
| 181 |
* freeing it. It is acceptable to just return the same path in an allocated |
| 182 |
* buffer, e.g., return strdup(rel_path). This function is only used to find |
| 183 |
* configuration files when os_daemonize() may have changed the current working |
| 184 |
* directory and relative path would be pointing to a different location. |
| 185 |
*/ |
| 186 |
char * os_rel2abs_path(const char *rel_path); |
| 187 |
|
| 188 |
/** |
| 189 |
* os_program_init - Program initialization (called at start) |
| 190 |
* Returns: 0 on success, -1 on failure |
| 191 |
* |
| 192 |
* This function is called when a programs starts. If there are any OS specific |
| 193 |
* processing that is needed, it can be placed here. It is also acceptable to |
| 194 |
* just return 0 if not special processing is needed. |
| 195 |
*/ |
| 196 |
int os_program_init(void); |
| 197 |
|
| 198 |
/** |
| 199 |
* os_program_deinit - Program deinitialization (called just before exit) |
| 200 |
* |
| 201 |
* This function is called just before a program exists. If there are any OS |
| 202 |
* specific processing, e.g., freeing resourced allocated in os_program_init(), |
| 203 |
* it should be done here. It is also acceptable for this function to do |
| 204 |
* nothing. |
| 205 |
*/ |
| 206 |
void os_program_deinit(void); |
| 207 |
|
| 208 |
/** |
| 209 |
* os_setenv - Set environment variable |
| 210 |
* @name: Name of the variable |
| 211 |
* @value: Value to set to the variable |
| 212 |
* @overwrite: Whether existing variable should be overwritten |
| 213 |
* Returns: 0 on success, -1 on error |
| 214 |
* |
| 215 |
* This function is only used for wpa_cli action scripts. OS wrapper does not |
| 216 |
* need to implement this if such functionality is not needed. |
| 217 |
*/ |
| 218 |
int os_setenv(const char *name, const char *value, int overwrite); |
| 219 |
|
| 220 |
/** |
| 221 |
* os_unsetenv - Delete environent variable |
| 222 |
* @name: Name of the variable |
| 223 |
* Returns: 0 on success, -1 on error |
| 224 |
* |
| 225 |
* This function is only used for wpa_cli action scripts. OS wrapper does not |
| 226 |
* need to implement this if such functionality is not needed. |
| 227 |
*/ |
| 228 |
int os_unsetenv(const char *name); |
| 229 |
|
| 230 |
/** |
| 231 |
* os_readfile - Read a file to an allocated memory buffer |
| 232 |
* @name: Name of the file to read |
| 233 |
* @len: For returning the length of the allocated buffer |
| 234 |
* Returns: Pointer to the allocated buffer or %NULL on failure |
| 235 |
* |
| 236 |
* This function allocates memory and reads the given file to this buffer. Both |
| 237 |
* binary and text files can be read with this function. The caller is |
| 238 |
* responsible for freeing the returned buffer with os_free(). |
| 239 |
*/ |
| 240 |
char * os_readfile(const char *name, size_t *len); |
| 241 |
|
| 242 |
/** |
| 243 |
* os_file_exists - Check whether the specified file exists |
| 244 |
* @fname: Path and name of the file |
| 245 |
* Returns: 1 if the file exists or 0 if not |
| 246 |
*/ |
| 247 |
int os_file_exists(const char *fname); |
| 248 |
|
| 249 |
/** |
| 250 |
* os_fdatasync - Sync a file's (for a given stream) state with storage device |
| 251 |
* @stream: the stream to be flushed |
| 252 |
* Returns: 0 if the operation succeeded or -1 on failure |
| 253 |
*/ |
| 254 |
int os_fdatasync(FILE *stream); |
| 255 |
|
| 256 |
/** |
| 257 |
* os_zalloc - Allocate and zero memory |
| 258 |
* @size: Number of bytes to allocate |
| 259 |
* Returns: Pointer to allocated and zeroed memory or %NULL on failure |
| 260 |
* |
| 261 |
* Caller is responsible for freeing the returned buffer with os_free(). |
| 262 |
*/ |
| 263 |
void * os_zalloc(size_t size); |
| 264 |
|
| 265 |
/** |
| 266 |
* os_calloc - Allocate and zero memory for an array |
| 267 |
* @nmemb: Number of members in the array |
| 268 |
* @size: Number of bytes in each member |
| 269 |
* Returns: Pointer to allocated and zeroed memory or %NULL on failure |
| 270 |
* |
| 271 |
* This function can be used as a wrapper for os_zalloc(nmemb * size) when an |
| 272 |
* allocation is used for an array. The main benefit over os_zalloc() is in |
| 273 |
* having an extra check to catch integer overflows in multiplication. |
| 274 |
* |
| 275 |
* Caller is responsible for freeing the returned buffer with os_free(). |
| 276 |
*/ |
| 277 |
static inline void * os_calloc(size_t nmemb, size_t size) |
| 278 |
{ |
| 279 |
if (size && nmemb > (~(size_t) 0) / size) |
| 280 |
return NULL; |
| 281 |
return os_zalloc(nmemb * size); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
/* |
| 286 |
* The following functions are wrapper for standard ANSI C or POSIX functions. |
| 287 |
* By default, they are just defined to use the standard function name and no |
| 288 |
* os_*.c implementation is needed for them. This avoids extra function calls |
| 289 |
* by allowing the C pre-processor take care of the function name mapping. |
| 290 |
* |
| 291 |
* If the target system uses a C library that does not provide these functions, |
| 292 |
* build_config.h can be used to define the wrappers to use a different |
| 293 |
* function name. This can be done on function-by-function basis since the |
| 294 |
* defines here are only used if build_config.h does not define the os_* name. |
| 295 |
* If needed, os_*.c file can be used to implement the functions that are not |
| 296 |
* included in the C library on the target system. Alternatively, |
| 297 |
* OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case |
| 298 |
* these functions need to be implemented in os_*.c file for the target system. |
| 299 |
*/ |
| 300 |
|
| 301 |
#ifdef OS_NO_C_LIB_DEFINES |
| 302 |
|
| 303 |
/** |
| 304 |
* os_malloc - Allocate dynamic memory |
| 305 |
* @size: Size of the buffer to allocate |
| 306 |
* Returns: Allocated buffer or %NULL on failure |
| 307 |
* |
| 308 |
* Caller is responsible for freeing the returned buffer with os_free(). |
| 309 |
*/ |
| 310 |
void * os_malloc(size_t size); |
| 311 |
|
| 312 |
/** |
| 313 |
* os_realloc - Re-allocate dynamic memory |
| 314 |
* @ptr: Old buffer from os_malloc() or os_realloc() |
| 315 |
* @size: Size of the new buffer |
| 316 |
* Returns: Allocated buffer or %NULL on failure |
| 317 |
* |
| 318 |
* Caller is responsible for freeing the returned buffer with os_free(). |
| 319 |
* If re-allocation fails, %NULL is returned and the original buffer (ptr) is |
| 320 |
* not freed and caller is still responsible for freeing it. |
| 321 |
*/ |
| 322 |
void * os_realloc(void *ptr, size_t size); |
| 323 |
|
| 324 |
/** |
| 325 |
* os_free - Free dynamic memory |
| 326 |
* @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL |
| 327 |
*/ |
| 328 |
void os_free(void *ptr); |
| 329 |
|
| 330 |
/** |
| 331 |
* os_memcpy - Copy memory area |
| 332 |
* @dest: Destination |
| 333 |
* @src: Source |
| 334 |
* @n: Number of bytes to copy |
| 335 |
* Returns: dest |
| 336 |
* |
| 337 |
* The memory areas src and dst must not overlap. os_memmove() can be used with |
| 338 |
* overlapping memory. |
| 339 |
*/ |
| 340 |
void * os_memcpy(void *dest, const void *src, size_t n); |
| 341 |
|
| 342 |
/** |
| 343 |
* os_memmove - Copy memory area |
| 344 |
* @dest: Destination |
| 345 |
* @src: Source |
| 346 |
* @n: Number of bytes to copy |
| 347 |
* Returns: dest |
| 348 |
* |
| 349 |
* The memory areas src and dst may overlap. |
| 350 |
*/ |
| 351 |
void * os_memmove(void *dest, const void *src, size_t n); |
| 352 |
|
| 353 |
/** |
| 354 |
* os_memset - Fill memory with a constant byte |
| 355 |
* @s: Memory area to be filled |
| 356 |
* @c: Constant byte |
| 357 |
* @n: Number of bytes started from s to fill with c |
| 358 |
* Returns: s |
| 359 |
*/ |
| 360 |
void * os_memset(void *s, int c, size_t n); |
| 361 |
|
| 362 |
/** |
| 363 |
* os_memcmp - Compare memory areas |
| 364 |
* @s1: First buffer |
| 365 |
* @s2: Second buffer |
| 366 |
* @n: Maximum numbers of octets to compare |
| 367 |
* Returns: An integer less than, equal to, or greater than zero if s1 is |
| 368 |
* found to be less than, to match, or be greater than s2. Only first n |
| 369 |
* characters will be compared. |
| 370 |
*/ |
| 371 |
int os_memcmp(const void *s1, const void *s2, size_t n); |
| 372 |
|
| 373 |
/** |
| 374 |
* os_strdup - Duplicate a string |
| 375 |
* @s: Source string |
| 376 |
* Returns: Allocated buffer with the string copied into it or %NULL on failure |
| 377 |
* |
| 378 |
* Caller is responsible for freeing the returned buffer with os_free(). |
| 379 |
*/ |
| 380 |
char * os_strdup(const char *s); |
| 381 |
|
| 382 |
/** |
| 383 |
* os_strlen - Calculate the length of a string |
| 384 |
* @s: '\0' terminated string |
| 385 |
* Returns: Number of characters in s (not counting the '\0' terminator) |
| 386 |
*/ |
| 387 |
size_t os_strlen(const char *s); |
| 388 |
|
| 389 |
/** |
| 390 |
* os_strcasecmp - Compare two strings ignoring case |
| 391 |
* @s1: First string |
| 392 |
* @s2: Second string |
| 393 |
* Returns: An integer less than, equal to, or greater than zero if s1 is |
| 394 |
* found to be less than, to match, or be greatred than s2 |
| 395 |
*/ |
| 396 |
int os_strcasecmp(const char *s1, const char *s2); |
| 397 |
|
| 398 |
/** |
| 399 |
* os_strncasecmp - Compare two strings ignoring case |
| 400 |
* @s1: First string |
| 401 |
* @s2: Second string |
| 402 |
* @n: Maximum numbers of characters to compare |
| 403 |
* Returns: An integer less than, equal to, or greater than zero if s1 is |
| 404 |
* found to be less than, to match, or be greater than s2. Only first n |
| 405 |
* characters will be compared. |
| 406 |
*/ |
| 407 |
int os_strncasecmp(const char *s1, const char *s2, size_t n); |
| 408 |
|
| 409 |
/** |
| 410 |
* os_strchr - Locate the first occurrence of a character in string |
| 411 |
* @s: String |
| 412 |
* @c: Character to search for |
| 413 |
* Returns: Pointer to the matched character or %NULL if not found |
| 414 |
*/ |
| 415 |
char * os_strchr(const char *s, int c); |
| 416 |
|
| 417 |
/** |
| 418 |
* os_strrchr - Locate the last occurrence of a character in string |
| 419 |
* @s: String |
| 420 |
* @c: Character to search for |
| 421 |
* Returns: Pointer to the matched character or %NULL if not found |
| 422 |
*/ |
| 423 |
char * os_strrchr(const char *s, int c); |
| 424 |
|
| 425 |
/** |
| 426 |
* os_strcmp - Compare two strings |
| 427 |
* @s1: First string |
| 428 |
* @s2: Second string |
| 429 |
* Returns: An integer less than, equal to, or greater than zero if s1 is |
| 430 |
* found to be less than, to match, or be greatred than s2 |
| 431 |
*/ |
| 432 |
int os_strcmp(const char *s1, const char *s2); |
| 433 |
|
| 434 |
/** |
| 435 |
* os_strncmp - Compare two strings |
| 436 |
* @s1: First string |
| 437 |
* @s2: Second string |
| 438 |
* @n: Maximum numbers of characters to compare |
| 439 |
* Returns: An integer less than, equal to, or greater than zero if s1 is |
| 440 |
* found to be less than, to match, or be greater than s2. Only first n |
| 441 |
* characters will be compared. |
| 442 |
*/ |
| 443 |
int os_strncmp(const char *s1, const char *s2, size_t n); |
| 444 |
|
| 445 |
/** |
| 446 |
* os_strstr - Locate a substring |
| 447 |
* @haystack: String (haystack) to search from |
| 448 |
* @needle: Needle to search from haystack |
| 449 |
* Returns: Pointer to the beginning of the substring or %NULL if not found |
| 450 |
*/ |
| 451 |
char * os_strstr(const char *haystack, const char *needle); |
| 452 |
|
| 453 |
/** |
| 454 |
* os_snprintf - Print to a memory buffer |
| 455 |
* @str: Memory buffer to print into |
| 456 |
* @size: Maximum length of the str buffer |
| 457 |
* @format: printf format |
| 458 |
* Returns: Number of characters printed (not including trailing '\0'). |
| 459 |
* |
| 460 |
* If the output buffer is truncated, number of characters which would have |
| 461 |
* been written is returned. Since some C libraries return -1 in such a case, |
| 462 |
* the caller must be prepared on that value, too, to indicate truncation. |
| 463 |
* |
| 464 |
* Note: Some C library implementations of snprintf() may not guarantee null |
| 465 |
* termination in case the output is truncated. The OS wrapper function of |
| 466 |
* os_snprintf() should provide this guarantee, i.e., to null terminate the |
| 467 |
* output buffer if a C library version of the function is used and if that |
| 468 |
* function does not guarantee null termination. |
| 469 |
* |
| 470 |
* If the target system does not include snprintf(), see, e.g., |
| 471 |
* http://www.ijs.si/software/snprintf/ for an example of a portable |
| 472 |
* implementation of snprintf. |
| 473 |
*/ |
| 474 |
int os_snprintf(char *str, size_t size, const char *format, ...); |
| 475 |
|
| 476 |
#else /* OS_NO_C_LIB_DEFINES */ |
| 477 |
|
| 478 |
#ifdef WPA_TRACE |
| 479 |
void * os_malloc(size_t size); |
| 480 |
void * os_realloc(void *ptr, size_t size); |
| 481 |
void os_free(void *ptr); |
| 482 |
char * os_strdup(const char *s); |
| 483 |
#else /* WPA_TRACE */ |
| 484 |
#ifndef os_malloc |
| 485 |
#define os_malloc(s) malloc((s)) |
| 486 |
#endif |
| 487 |
#ifndef os_realloc |
| 488 |
#define os_realloc(p, s) realloc((p), (s)) |
| 489 |
#endif |
| 490 |
#ifndef os_free |
| 491 |
#define os_free(p) free((p)) |
| 492 |
#endif |
| 493 |
#ifndef os_strdup |
| 494 |
#ifdef _MSC_VER |
| 495 |
#define os_strdup(s) _strdup(s) |
| 496 |
#else |
| 497 |
#define os_strdup(s) strdup(s) |
| 498 |
#endif |
| 499 |
#endif |
| 500 |
#endif /* WPA_TRACE */ |
| 501 |
|
| 502 |
#ifndef os_memcpy |
| 503 |
#define os_memcpy(d, s, n) memcpy((d), (s), (n)) |
| 504 |
#endif |
| 505 |
#ifndef os_memmove |
| 506 |
#define os_memmove(d, s, n) memmove((d), (s), (n)) |
| 507 |
#endif |
| 508 |
#ifndef os_memset |
| 509 |
#define os_memset(s, c, n) memset(s, c, n) |
| 510 |
#endif |
| 511 |
#ifndef os_memcmp |
| 512 |
#define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n)) |
| 513 |
#endif |
| 514 |
|
| 515 |
#ifndef os_strlen |
| 516 |
#define os_strlen(s) strlen(s) |
| 517 |
#endif |
| 518 |
#ifndef os_strcasecmp |
| 519 |
#ifdef _MSC_VER |
| 520 |
#define os_strcasecmp(s1, s2) _stricmp((s1), (s2)) |
| 521 |
#else |
| 522 |
#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2)) |
| 523 |
#endif |
| 524 |
#endif |
| 525 |
#ifndef os_strncasecmp |
| 526 |
#ifdef _MSC_VER |
| 527 |
#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n)) |
| 528 |
#else |
| 529 |
#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n)) |
| 530 |
#endif |
| 531 |
#endif |
| 532 |
#ifndef os_strchr |
| 533 |
#define os_strchr(s, c) strchr((s), (c)) |
| 534 |
#endif |
| 535 |
#ifndef os_strcmp |
| 536 |
#define os_strcmp(s1, s2) strcmp((s1), (s2)) |
| 537 |
#endif |
| 538 |
#ifndef os_strncmp |
| 539 |
#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n)) |
| 540 |
#endif |
| 541 |
#ifndef os_strrchr |
| 542 |
#define os_strrchr(s, c) strrchr((s), (c)) |
| 543 |
#endif |
| 544 |
#ifndef os_strstr |
| 545 |
#define os_strstr(h, n) strstr((h), (n)) |
| 546 |
#endif |
| 547 |
|
| 548 |
#ifndef os_snprintf |
| 549 |
#ifdef _MSC_VER |
| 550 |
#define os_snprintf _snprintf |
| 551 |
#else |
| 552 |
#define os_snprintf snprintf |
| 553 |
#endif |
| 554 |
#endif |
| 555 |
|
| 556 |
#endif /* OS_NO_C_LIB_DEFINES */ |
| 557 |
|
| 558 |
|
| 559 |
static inline int os_snprintf_error(size_t size, int res) |
| 560 |
{ |
| 561 |
return res < 0 || (unsigned int) res >= size; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size) |
| 566 |
{ |
| 567 |
if (size && nmemb > (~(size_t) 0) / size) |
| 568 |
return NULL; |
| 569 |
return os_realloc(ptr, nmemb * size); |
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* os_remove_in_array - Remove a member from an array by index |
| 574 |
* @ptr: Pointer to the array |
| 575 |
* @nmemb: Current member count of the array |
| 576 |
* @size: The size per member of the array |
| 577 |
* @idx: Index of the member to be removed |
| 578 |
*/ |
| 579 |
static inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size, |
| 580 |
size_t idx) |
| 581 |
{ |
| 582 |
if (idx < nmemb - 1) |
| 583 |
os_memmove(((unsigned char *) ptr) + idx * size, |
| 584 |
((unsigned char *) ptr) + (idx + 1) * size, |
| 585 |
(nmemb - idx - 1) * size); |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* os_strlcpy - Copy a string with size bound and NUL-termination |
| 590 |
* @dest: Destination |
| 591 |
* @src: Source |
| 592 |
* @siz: Size of the target buffer |
| 593 |
* Returns: Total length of the target string (length of src) (not including |
| 594 |
* NUL-termination) |
| 595 |
* |
| 596 |
* This function matches in behavior with the strlcpy(3) function in OpenBSD. |
| 597 |
*/ |
| 598 |
size_t os_strlcpy(char *dest, const char *src, size_t siz); |
| 599 |
|
| 600 |
/** |
| 601 |
* os_memcmp_const - Constant time memory comparison |
| 602 |
* @a: First buffer to compare |
| 603 |
* @b: Second buffer to compare |
| 604 |
* @len: Number of octets to compare |
| 605 |
* Returns: 0 if buffers are equal, non-zero if not |
| 606 |
* |
| 607 |
* This function is meant for comparing passwords or hash values where |
| 608 |
* difference in execution time could provide external observer information |
| 609 |
* about the location of the difference in the memory buffers. The return value |
| 610 |
* does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to |
| 611 |
* sort items into a defined order. Unlike os_memcmp(), execution time of |
| 612 |
* os_memcmp_const() does not depend on the contents of the compared memory |
| 613 |
* buffers, but only on the total compared length. |
| 614 |
*/ |
| 615 |
int os_memcmp_const(const void *a, const void *b, size_t len); |
| 616 |
|
| 617 |
|
| 618 |
/** |
| 619 |
* os_memdup - Allocate duplicate of passed memory chunk |
| 620 |
* @src: Source buffer to duplicate |
| 621 |
* @len: Length of source buffer |
| 622 |
* Returns: %NULL if allocation failed, copy of src buffer otherwise |
| 623 |
* |
| 624 |
* This function allocates a memory block like os_malloc() would, and |
| 625 |
* copies the given source buffer into it. |
| 626 |
*/ |
| 627 |
void * os_memdup(const void *src, size_t len); |
| 628 |
|
| 629 |
/** |
| 630 |
* os_exec - Execute an external program |
| 631 |
* @program: Path to the program |
| 632 |
* @arg: Command line argument string |
| 633 |
* @wait_completion: Whether to wait until the program execution completes |
| 634 |
* Returns: 0 on success, -1 on error |
| 635 |
*/ |
| 636 |
int os_exec(const char *program, const char *arg, int wait_completion); |
| 637 |
|
| 638 |
|
| 639 |
#ifdef OS_REJECT_C_LIB_FUNCTIONS |
| 640 |
#define malloc OS_DO_NOT_USE_malloc |
| 641 |
#define realloc OS_DO_NOT_USE_realloc |
| 642 |
#define free OS_DO_NOT_USE_free |
| 643 |
#define memcpy OS_DO_NOT_USE_memcpy |
| 644 |
#define memmove OS_DO_NOT_USE_memmove |
| 645 |
#define memset OS_DO_NOT_USE_memset |
| 646 |
#define memcmp OS_DO_NOT_USE_memcmp |
| 647 |
#undef strdup |
| 648 |
#define strdup OS_DO_NOT_USE_strdup |
| 649 |
#define strlen OS_DO_NOT_USE_strlen |
| 650 |
#define strcasecmp OS_DO_NOT_USE_strcasecmp |
| 651 |
#define strncasecmp OS_DO_NOT_USE_strncasecmp |
| 652 |
#undef strchr |
| 653 |
#define strchr OS_DO_NOT_USE_strchr |
| 654 |
#undef strcmp |
| 655 |
#define strcmp OS_DO_NOT_USE_strcmp |
| 656 |
#undef strncmp |
| 657 |
#define strncmp OS_DO_NOT_USE_strncmp |
| 658 |
#undef strncpy |
| 659 |
#define strncpy OS_DO_NOT_USE_strncpy |
| 660 |
#define strrchr OS_DO_NOT_USE_strrchr |
| 661 |
#define strstr OS_DO_NOT_USE_strstr |
| 662 |
#undef snprintf |
| 663 |
#define snprintf OS_DO_NOT_USE_snprintf |
| 664 |
|
| 665 |
#define strcpy OS_DO_NOT_USE_strcpy |
| 666 |
#endif /* OS_REJECT_C_LIB_FUNCTIONS */ |
| 667 |
|
| 668 |
|
| 669 |
#if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) |
| 670 |
#define TEST_FAIL() testing_test_fail() |
| 671 |
int testing_test_fail(void); |
| 672 |
extern char wpa_trace_fail_func[256]; |
| 673 |
extern unsigned int wpa_trace_fail_after; |
| 674 |
extern char wpa_trace_test_fail_func[256]; |
| 675 |
extern unsigned int wpa_trace_test_fail_after; |
| 676 |
#else |
| 677 |
#define TEST_FAIL() 0 |
| 678 |
#endif |
| 679 |
|
| 680 |
#endif /* OS_H */ |