os.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * OS specific functions
  3. * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #ifndef OS_H
  9. #define OS_H
  10. typedef long os_time_t;
  11. /**
  12. * os_sleep - Sleep (sec, usec)
  13. * @sec: Number of seconds to sleep
  14. * @usec: Number of microseconds to sleep
  15. */
  16. void os_sleep(os_time_t sec, os_time_t usec);
  17. struct os_time {
  18. os_time_t sec;
  19. os_time_t usec;
  20. };
  21. /**
  22. * os_get_time - Get current time (sec, usec)
  23. * @t: Pointer to buffer for the time
  24. * Returns: 0 on success, -1 on failure
  25. */
  26. int os_get_time(struct os_time *t);
  27. /* Helper macros for handling struct os_time */
  28. #define os_time_before(a, b) \
  29. ((a)->sec < (b)->sec || \
  30. ((a)->sec == (b)->sec && (a)->usec < (b)->usec))
  31. #define os_time_sub(a, b, res) do { \
  32. (res)->sec = (a)->sec - (b)->sec; \
  33. (res)->usec = (a)->usec - (b)->usec; \
  34. if ((res)->usec < 0) { \
  35. (res)->sec--; \
  36. (res)->usec += 1000000; \
  37. } \
  38. } while (0)
  39. /**
  40. * os_mktime - Convert broken-down time into seconds since 1970-01-01
  41. * @year: Four digit year
  42. * @month: Month (1 .. 12)
  43. * @day: Day of month (1 .. 31)
  44. * @hour: Hour (0 .. 23)
  45. * @min: Minute (0 .. 59)
  46. * @sec: Second (0 .. 60)
  47. * @t: Buffer for returning calendar time representation (seconds since
  48. * 1970-01-01 00:00:00)
  49. * Returns: 0 on success, -1 on failure
  50. *
  51. * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
  52. * which is used by POSIX mktime().
  53. */
  54. int os_mktime(int year, int month, int day, int hour, int min, int sec,
  55. os_time_t *t);
  56. struct os_tm {
  57. int sec; /* 0..59 or 60 for leap seconds */
  58. int min; /* 0..59 */
  59. int hour; /* 0..23 */
  60. int day; /* 1..31 */
  61. int month; /* 1..12 */
  62. int year; /* Four digit year */
  63. };
  64. int os_gmtime(os_time_t t, struct os_tm *tm);
  65. /**
  66. * os_daemonize - Run in the background (detach from the controlling terminal)
  67. * @pid_file: File name to write the process ID to or %NULL to skip this
  68. * Returns: 0 on success, -1 on failure
  69. */
  70. int os_daemonize(const char *pid_file);
  71. /**
  72. * os_daemonize_terminate - Stop running in the background (remove pid file)
  73. * @pid_file: File name to write the process ID to or %NULL to skip this
  74. */
  75. void os_daemonize_terminate(const char *pid_file);
  76. /**
  77. * os_get_random - Get cryptographically strong pseudo random data
  78. * @buf: Buffer for pseudo random data
  79. * @len: Length of the buffer
  80. * Returns: 0 on success, -1 on failure
  81. */
  82. int os_get_random(unsigned char *buf, size_t len);
  83. /**
  84. * os_random - Get pseudo random value (not necessarily very strong)
  85. * Returns: Pseudo random value
  86. */
  87. unsigned long os_random(void);
  88. /**
  89. * os_rel2abs_path - Get an absolute path for a file
  90. * @rel_path: Relative path to a file
  91. * Returns: Absolute path for the file or %NULL on failure
  92. *
  93. * This function tries to convert a relative path of a file to an absolute path
  94. * in order for the file to be found even if current working directory has
  95. * changed. The returned value is allocated and caller is responsible for
  96. * freeing it. It is acceptable to just return the same path in an allocated
  97. * buffer, e.g., return strdup(rel_path). This function is only used to find
  98. * configuration files when os_daemonize() may have changed the current working
  99. * directory and relative path would be pointing to a different location.
  100. */
  101. char * os_rel2abs_path(const char *rel_path);
  102. /**
  103. * os_program_init - Program initialization (called at start)
  104. * Returns: 0 on success, -1 on failure
  105. *
  106. * This function is called when a programs starts. If there are any OS specific
  107. * processing that is needed, it can be placed here. It is also acceptable to
  108. * just return 0 if not special processing is needed.
  109. */
  110. int os_program_init(void);
  111. /**
  112. * os_program_deinit - Program deinitialization (called just before exit)
  113. *
  114. * This function is called just before a program exists. If there are any OS
  115. * specific processing, e.g., freeing resourced allocated in os_program_init(),
  116. * it should be done here. It is also acceptable for this function to do
  117. * nothing.
  118. */
  119. void os_program_deinit(void);
  120. /**
  121. * os_setenv - Set environment variable
  122. * @name: Name of the variable
  123. * @value: Value to set to the variable
  124. * @overwrite: Whether existing variable should be overwritten
  125. * Returns: 0 on success, -1 on error
  126. *
  127. * This function is only used for wpa_cli action scripts. OS wrapper does not
  128. * need to implement this if such functionality is not needed.
  129. */
  130. int os_setenv(const char *name, const char *value, int overwrite);
  131. /**
  132. * os_unsetenv - Delete environent variable
  133. * @name: Name of the variable
  134. * Returns: 0 on success, -1 on error
  135. *
  136. * This function is only used for wpa_cli action scripts. OS wrapper does not
  137. * need to implement this if such functionality is not needed.
  138. */
  139. int os_unsetenv(const char *name);
  140. /**
  141. * os_readfile - Read a file to an allocated memory buffer
  142. * @name: Name of the file to read
  143. * @len: For returning the length of the allocated buffer
  144. * Returns: Pointer to the allocated buffer or %NULL on failure
  145. *
  146. * This function allocates memory and reads the given file to this buffer. Both
  147. * binary and text files can be read with this function. The caller is
  148. * responsible for freeing the returned buffer with os_free().
  149. */
  150. char * os_readfile(const char *name, size_t *len);
  151. /**
  152. * os_zalloc - Allocate and zero memory
  153. * @size: Number of bytes to allocate
  154. * Returns: Pointer to allocated and zeroed memory or %NULL on failure
  155. *
  156. * Caller is responsible for freeing the returned buffer with os_free().
  157. */
  158. void * os_zalloc(size_t size);
  159. /**
  160. * os_calloc - Allocate and zero memory for an array
  161. * @nmemb: Number of members in the array
  162. * @size: Number of bytes in each member
  163. * Returns: Pointer to allocated and zeroed memory or %NULL on failure
  164. *
  165. * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
  166. * allocation is used for an array. The main benefit over os_zalloc() is in
  167. * having an extra check to catch integer overflows in multiplication.
  168. *
  169. * Caller is responsible for freeing the returned buffer with os_free().
  170. */
  171. static inline void * os_calloc(size_t nmemb, size_t size)
  172. {
  173. if (size && nmemb > (~(size_t) 0) / size)
  174. return NULL;
  175. return os_zalloc(nmemb * size);
  176. }
  177. /*
  178. * The following functions are wrapper for standard ANSI C or POSIX functions.
  179. * By default, they are just defined to use the standard function name and no
  180. * os_*.c implementation is needed for them. This avoids extra function calls
  181. * by allowing the C pre-processor take care of the function name mapping.
  182. *
  183. * If the target system uses a C library that does not provide these functions,
  184. * build_config.h can be used to define the wrappers to use a different
  185. * function name. This can be done on function-by-function basis since the
  186. * defines here are only used if build_config.h does not define the os_* name.
  187. * If needed, os_*.c file can be used to implement the functions that are not
  188. * included in the C library on the target system. Alternatively,
  189. * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
  190. * these functions need to be implemented in os_*.c file for the target system.
  191. */
  192. #ifdef OS_NO_C_LIB_DEFINES
  193. /**
  194. * os_malloc - Allocate dynamic memory
  195. * @size: Size of the buffer to allocate
  196. * Returns: Allocated buffer or %NULL on failure
  197. *
  198. * Caller is responsible for freeing the returned buffer with os_free().
  199. */
  200. void * os_malloc(size_t size);
  201. /**
  202. * os_realloc - Re-allocate dynamic memory
  203. * @ptr: Old buffer from os_malloc() or os_realloc()
  204. * @size: Size of the new buffer
  205. * Returns: Allocated buffer or %NULL on failure
  206. *
  207. * Caller is responsible for freeing the returned buffer with os_free().
  208. * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
  209. * not freed and caller is still responsible for freeing it.
  210. */
  211. void * os_realloc(void *ptr, size_t size);
  212. /**
  213. * os_free - Free dynamic memory
  214. * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
  215. */
  216. void os_free(void *ptr);
  217. /**
  218. * os_memcpy - Copy memory area
  219. * @dest: Destination
  220. * @src: Source
  221. * @n: Number of bytes to copy
  222. * Returns: dest
  223. *
  224. * The memory areas src and dst must not overlap. os_memmove() can be used with
  225. * overlapping memory.
  226. */
  227. void * os_memcpy(void *dest, const void *src, size_t n);
  228. /**
  229. * os_memmove - Copy memory area
  230. * @dest: Destination
  231. * @src: Source
  232. * @n: Number of bytes to copy
  233. * Returns: dest
  234. *
  235. * The memory areas src and dst may overlap.
  236. */
  237. void * os_memmove(void *dest, const void *src, size_t n);
  238. /**
  239. * os_memset - Fill memory with a constant byte
  240. * @s: Memory area to be filled
  241. * @c: Constant byte
  242. * @n: Number of bytes started from s to fill with c
  243. * Returns: s
  244. */
  245. void * os_memset(void *s, int c, size_t n);
  246. /**
  247. * os_memcmp - Compare memory areas
  248. * @s1: First buffer
  249. * @s2: Second buffer
  250. * @n: Maximum numbers of octets to compare
  251. * Returns: An integer less than, equal to, or greater than zero if s1 is
  252. * found to be less than, to match, or be greater than s2. Only first n
  253. * characters will be compared.
  254. */
  255. int os_memcmp(const void *s1, const void *s2, size_t n);
  256. /**
  257. * os_strdup - Duplicate a string
  258. * @s: Source string
  259. * Returns: Allocated buffer with the string copied into it or %NULL on failure
  260. *
  261. * Caller is responsible for freeing the returned buffer with os_free().
  262. */
  263. char * os_strdup(const char *s);
  264. /**
  265. * os_strlen - Calculate the length of a string
  266. * @s: '\0' terminated string
  267. * Returns: Number of characters in s (not counting the '\0' terminator)
  268. */
  269. size_t os_strlen(const char *s);
  270. /**
  271. * os_strcasecmp - Compare two strings ignoring case
  272. * @s1: First string
  273. * @s2: Second string
  274. * Returns: An integer less than, equal to, or greater than zero if s1 is
  275. * found to be less than, to match, or be greatred than s2
  276. */
  277. int os_strcasecmp(const char *s1, const char *s2);
  278. /**
  279. * os_strncasecmp - Compare two strings ignoring case
  280. * @s1: First string
  281. * @s2: Second string
  282. * @n: Maximum numbers of characters to compare
  283. * Returns: An integer less than, equal to, or greater than zero if s1 is
  284. * found to be less than, to match, or be greater than s2. Only first n
  285. * characters will be compared.
  286. */
  287. int os_strncasecmp(const char *s1, const char *s2, size_t n);
  288. /**
  289. * os_strchr - Locate the first occurrence of a character in string
  290. * @s: String
  291. * @c: Character to search for
  292. * Returns: Pointer to the matched character or %NULL if not found
  293. */
  294. char * os_strchr(const char *s, int c);
  295. /**
  296. * os_strrchr - Locate the last occurrence of a character in string
  297. * @s: String
  298. * @c: Character to search for
  299. * Returns: Pointer to the matched character or %NULL if not found
  300. */
  301. char * os_strrchr(const char *s, int c);
  302. /**
  303. * os_strcmp - Compare two strings
  304. * @s1: First string
  305. * @s2: Second string
  306. * Returns: An integer less than, equal to, or greater than zero if s1 is
  307. * found to be less than, to match, or be greatred than s2
  308. */
  309. int os_strcmp(const char *s1, const char *s2);
  310. /**
  311. * os_strncmp - Compare two strings
  312. * @s1: First string
  313. * @s2: Second string
  314. * @n: Maximum numbers of characters to compare
  315. * Returns: An integer less than, equal to, or greater than zero if s1 is
  316. * found to be less than, to match, or be greater than s2. Only first n
  317. * characters will be compared.
  318. */
  319. int os_strncmp(const char *s1, const char *s2, size_t n);
  320. /**
  321. * os_strncpy - Copy a string
  322. * @dest: Destination
  323. * @src: Source
  324. * @n: Maximum number of characters to copy
  325. * Returns: dest
  326. */
  327. char * os_strncpy(char *dest, const char *src, size_t n);
  328. /**
  329. * os_strstr - Locate a substring
  330. * @haystack: String (haystack) to search from
  331. * @needle: Needle to search from haystack
  332. * Returns: Pointer to the beginning of the substring or %NULL if not found
  333. */
  334. char * os_strstr(const char *haystack, const char *needle);
  335. /**
  336. * os_snprintf - Print to a memory buffer
  337. * @str: Memory buffer to print into
  338. * @size: Maximum length of the str buffer
  339. * @format: printf format
  340. * Returns: Number of characters printed (not including trailing '\0').
  341. *
  342. * If the output buffer is truncated, number of characters which would have
  343. * been written is returned. Since some C libraries return -1 in such a case,
  344. * the caller must be prepared on that value, too, to indicate truncation.
  345. *
  346. * Note: Some C library implementations of snprintf() may not guarantee null
  347. * termination in case the output is truncated. The OS wrapper function of
  348. * os_snprintf() should provide this guarantee, i.e., to null terminate the
  349. * output buffer if a C library version of the function is used and if that
  350. * function does not guarantee null termination.
  351. *
  352. * If the target system does not include snprintf(), see, e.g.,
  353. * http://www.ijs.si/software/snprintf/ for an example of a portable
  354. * implementation of snprintf.
  355. */
  356. int os_snprintf(char *str, size_t size, const char *format, ...);
  357. #else /* OS_NO_C_LIB_DEFINES */
  358. #ifdef WPA_TRACE
  359. void * os_malloc(size_t size);
  360. void * os_realloc(void *ptr, size_t size);
  361. void os_free(void *ptr);
  362. char * os_strdup(const char *s);
  363. #else /* WPA_TRACE */
  364. #ifndef os_malloc
  365. #define os_malloc(s) malloc((s))
  366. #endif
  367. #ifndef os_realloc
  368. #define os_realloc(p, s) realloc((p), (s))
  369. #endif
  370. #ifndef os_free
  371. #define os_free(p) free((p))
  372. #endif
  373. #ifndef os_strdup
  374. #ifdef _MSC_VER
  375. #define os_strdup(s) _strdup(s)
  376. #else
  377. #define os_strdup(s) strdup(s)
  378. #endif
  379. #endif
  380. #endif /* WPA_TRACE */
  381. #ifndef os_memcpy
  382. #define os_memcpy(d, s, n) memcpy((d), (s), (n))
  383. #endif
  384. #ifndef os_memmove
  385. #define os_memmove(d, s, n) memmove((d), (s), (n))
  386. #endif
  387. #ifndef os_memset
  388. #define os_memset(s, c, n) memset(s, c, n)
  389. #endif
  390. #ifndef os_memcmp
  391. #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
  392. #endif
  393. #ifndef os_strlen
  394. #define os_strlen(s) strlen(s)
  395. #endif
  396. #ifndef os_strcasecmp
  397. #ifdef _MSC_VER
  398. #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
  399. #else
  400. #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
  401. #endif
  402. #endif
  403. #ifndef os_strncasecmp
  404. #ifdef _MSC_VER
  405. #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
  406. #else
  407. #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
  408. #endif
  409. #endif
  410. #ifndef os_strchr
  411. #define os_strchr(s, c) strchr((s), (c))
  412. #endif
  413. #ifndef os_strcmp
  414. #define os_strcmp(s1, s2) strcmp((s1), (s2))
  415. #endif
  416. #ifndef os_strncmp
  417. #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
  418. #endif
  419. #ifndef os_strncpy
  420. #define os_strncpy(d, s, n) strncpy((d), (s), (n))
  421. #endif
  422. #ifndef os_strrchr
  423. #define os_strrchr(s, c) strrchr((s), (c))
  424. #endif
  425. #ifndef os_strstr
  426. #define os_strstr(h, n) strstr((h), (n))
  427. #endif
  428. #ifndef os_snprintf
  429. #ifdef _MSC_VER
  430. #define os_snprintf _snprintf
  431. #else
  432. #define os_snprintf snprintf
  433. #endif
  434. #endif
  435. #endif /* OS_NO_C_LIB_DEFINES */
  436. static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
  437. {
  438. if (size && nmemb > (~(size_t) 0) / size)
  439. return NULL;
  440. return os_realloc(ptr, nmemb * size);
  441. }
  442. /**
  443. * os_strlcpy - Copy a string with size bound and NUL-termination
  444. * @dest: Destination
  445. * @src: Source
  446. * @siz: Size of the target buffer
  447. * Returns: Total length of the target string (length of src) (not including
  448. * NUL-termination)
  449. *
  450. * This function matches in behavior with the strlcpy(3) function in OpenBSD.
  451. */
  452. size_t os_strlcpy(char *dest, const char *src, size_t siz);
  453. #ifdef OS_REJECT_C_LIB_FUNCTIONS
  454. #define malloc OS_DO_NOT_USE_malloc
  455. #define realloc OS_DO_NOT_USE_realloc
  456. #define free OS_DO_NOT_USE_free
  457. #define memcpy OS_DO_NOT_USE_memcpy
  458. #define memmove OS_DO_NOT_USE_memmove
  459. #define memset OS_DO_NOT_USE_memset
  460. #define memcmp OS_DO_NOT_USE_memcmp
  461. #undef strdup
  462. #define strdup OS_DO_NOT_USE_strdup
  463. #define strlen OS_DO_NOT_USE_strlen
  464. #define strcasecmp OS_DO_NOT_USE_strcasecmp
  465. #define strncasecmp OS_DO_NOT_USE_strncasecmp
  466. #undef strchr
  467. #define strchr OS_DO_NOT_USE_strchr
  468. #undef strcmp
  469. #define strcmp OS_DO_NOT_USE_strcmp
  470. #undef strncmp
  471. #define strncmp OS_DO_NOT_USE_strncmp
  472. #undef strncpy
  473. #define strncpy OS_DO_NOT_USE_strncpy
  474. #define strrchr OS_DO_NOT_USE_strrchr
  475. #define strstr OS_DO_NOT_USE_strstr
  476. #undef snprintf
  477. #define snprintf OS_DO_NOT_USE_snprintf
  478. #define strcpy OS_DO_NOT_USE_strcpy
  479. #endif /* OS_REJECT_C_LIB_FUNCTIONS */
  480. #endif /* OS_H */