os.h 15 KB

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