os_unix.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * OS specific functions for UNIX/POSIX systems
  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. #include "includes.h"
  9. #include <time.h>
  10. #ifdef ANDROID
  11. #include <sys/capability.h>
  12. #include <sys/prctl.h>
  13. #include <private/android_filesystem_config.h>
  14. #endif /* ANDROID */
  15. #include "os.h"
  16. #include "common.h"
  17. #ifdef WPA_TRACE
  18. #include "wpa_debug.h"
  19. #include "trace.h"
  20. #include "list.h"
  21. static struct dl_list alloc_list;
  22. #define ALLOC_MAGIC 0xa84ef1b2
  23. #define FREED_MAGIC 0x67fd487a
  24. struct os_alloc_trace {
  25. unsigned int magic;
  26. struct dl_list list;
  27. size_t len;
  28. WPA_TRACE_INFO
  29. };
  30. #endif /* WPA_TRACE */
  31. void os_sleep(os_time_t sec, os_time_t usec)
  32. {
  33. if (sec)
  34. sleep(sec);
  35. if (usec)
  36. usleep(usec);
  37. }
  38. int os_get_time(struct os_time *t)
  39. {
  40. int res;
  41. struct timeval tv;
  42. res = gettimeofday(&tv, NULL);
  43. t->sec = tv.tv_sec;
  44. t->usec = tv.tv_usec;
  45. return res;
  46. }
  47. int os_get_reltime(struct os_reltime *t)
  48. {
  49. #if defined(CLOCK_BOOTTIME)
  50. static clockid_t clock_id = CLOCK_BOOTTIME;
  51. #elif defined(CLOCK_MONOTONIC)
  52. static clockid_t clock_id = CLOCK_MONOTONIC;
  53. #else
  54. static clockid_t clock_id = CLOCK_REALTIME;
  55. #endif
  56. struct timespec ts;
  57. int res;
  58. while (1) {
  59. res = clock_gettime(clock_id, &ts);
  60. if (res == 0) {
  61. t->sec = ts.tv_sec;
  62. t->usec = ts.tv_nsec / 1000;
  63. return 0;
  64. }
  65. switch (clock_id) {
  66. #ifdef CLOCK_BOOTTIME
  67. case CLOCK_BOOTTIME:
  68. clock_id = CLOCK_MONOTONIC;
  69. break;
  70. #endif
  71. #ifdef CLOCK_MONOTONIC
  72. case CLOCK_MONOTONIC:
  73. clock_id = CLOCK_REALTIME;
  74. break;
  75. #endif
  76. case CLOCK_REALTIME:
  77. return -1;
  78. }
  79. }
  80. }
  81. int os_mktime(int year, int month, int day, int hour, int min, int sec,
  82. os_time_t *t)
  83. {
  84. struct tm tm, *tm1;
  85. time_t t_local, t1, t2;
  86. os_time_t tz_offset;
  87. if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
  88. hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
  89. sec > 60)
  90. return -1;
  91. memset(&tm, 0, sizeof(tm));
  92. tm.tm_year = year - 1900;
  93. tm.tm_mon = month - 1;
  94. tm.tm_mday = day;
  95. tm.tm_hour = hour;
  96. tm.tm_min = min;
  97. tm.tm_sec = sec;
  98. t_local = mktime(&tm);
  99. /* figure out offset to UTC */
  100. tm1 = localtime(&t_local);
  101. if (tm1) {
  102. t1 = mktime(tm1);
  103. tm1 = gmtime(&t_local);
  104. if (tm1) {
  105. t2 = mktime(tm1);
  106. tz_offset = t2 - t1;
  107. } else
  108. tz_offset = 0;
  109. } else
  110. tz_offset = 0;
  111. *t = (os_time_t) t_local - tz_offset;
  112. return 0;
  113. }
  114. int os_gmtime(os_time_t t, struct os_tm *tm)
  115. {
  116. struct tm *tm2;
  117. time_t t2 = t;
  118. tm2 = gmtime(&t2);
  119. if (tm2 == NULL)
  120. return -1;
  121. tm->sec = tm2->tm_sec;
  122. tm->min = tm2->tm_min;
  123. tm->hour = tm2->tm_hour;
  124. tm->day = tm2->tm_mday;
  125. tm->month = tm2->tm_mon + 1;
  126. tm->year = tm2->tm_year + 1900;
  127. return 0;
  128. }
  129. #ifdef __APPLE__
  130. #include <fcntl.h>
  131. static int os_daemon(int nochdir, int noclose)
  132. {
  133. int devnull;
  134. if (chdir("/") < 0)
  135. return -1;
  136. devnull = open("/dev/null", O_RDWR);
  137. if (devnull < 0)
  138. return -1;
  139. if (dup2(devnull, STDIN_FILENO) < 0) {
  140. close(devnull);
  141. return -1;
  142. }
  143. if (dup2(devnull, STDOUT_FILENO) < 0) {
  144. close(devnull);
  145. return -1;
  146. }
  147. if (dup2(devnull, STDERR_FILENO) < 0) {
  148. close(devnull);
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. #else /* __APPLE__ */
  154. #define os_daemon daemon
  155. #endif /* __APPLE__ */
  156. int os_daemonize(const char *pid_file)
  157. {
  158. #if defined(__uClinux__) || defined(__sun__)
  159. return -1;
  160. #else /* defined(__uClinux__) || defined(__sun__) */
  161. if (os_daemon(0, 0)) {
  162. perror("daemon");
  163. return -1;
  164. }
  165. if (pid_file) {
  166. FILE *f = fopen(pid_file, "w");
  167. if (f) {
  168. fprintf(f, "%u\n", getpid());
  169. fclose(f);
  170. }
  171. }
  172. return -0;
  173. #endif /* defined(__uClinux__) || defined(__sun__) */
  174. }
  175. void os_daemonize_terminate(const char *pid_file)
  176. {
  177. if (pid_file)
  178. unlink(pid_file);
  179. }
  180. int os_get_random(unsigned char *buf, size_t len)
  181. {
  182. FILE *f;
  183. size_t rc;
  184. f = fopen("/dev/urandom", "rb");
  185. if (f == NULL) {
  186. printf("Could not open /dev/urandom.\n");
  187. return -1;
  188. }
  189. rc = fread(buf, 1, len, f);
  190. fclose(f);
  191. return rc != len ? -1 : 0;
  192. }
  193. unsigned long os_random(void)
  194. {
  195. return random();
  196. }
  197. char * os_rel2abs_path(const char *rel_path)
  198. {
  199. char *buf = NULL, *cwd, *ret;
  200. size_t len = 128, cwd_len, rel_len, ret_len;
  201. int last_errno;
  202. if (!rel_path)
  203. return NULL;
  204. if (rel_path[0] == '/')
  205. return os_strdup(rel_path);
  206. for (;;) {
  207. buf = os_malloc(len);
  208. if (buf == NULL)
  209. return NULL;
  210. cwd = getcwd(buf, len);
  211. if (cwd == NULL) {
  212. last_errno = errno;
  213. os_free(buf);
  214. if (last_errno != ERANGE)
  215. return NULL;
  216. len *= 2;
  217. if (len > 2000)
  218. return NULL;
  219. } else {
  220. buf[len - 1] = '\0';
  221. break;
  222. }
  223. }
  224. cwd_len = os_strlen(cwd);
  225. rel_len = os_strlen(rel_path);
  226. ret_len = cwd_len + 1 + rel_len + 1;
  227. ret = os_malloc(ret_len);
  228. if (ret) {
  229. os_memcpy(ret, cwd, cwd_len);
  230. ret[cwd_len] = '/';
  231. os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
  232. ret[ret_len - 1] = '\0';
  233. }
  234. os_free(buf);
  235. return ret;
  236. }
  237. int os_program_init(void)
  238. {
  239. #ifdef ANDROID
  240. /*
  241. * We ignore errors here since errors are normal if we
  242. * are already running as non-root.
  243. */
  244. #ifdef ANDROID_SETGROUPS_OVERRIDE
  245. gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE };
  246. #else /* ANDROID_SETGROUPS_OVERRIDE */
  247. gid_t groups[] = { AID_INET, AID_WIFI, AID_KEYSTORE };
  248. #endif /* ANDROID_SETGROUPS_OVERRIDE */
  249. struct __user_cap_header_struct header;
  250. struct __user_cap_data_struct cap;
  251. setgroups(ARRAY_SIZE(groups), groups);
  252. prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
  253. setgid(AID_WIFI);
  254. setuid(AID_WIFI);
  255. header.version = _LINUX_CAPABILITY_VERSION;
  256. header.pid = 0;
  257. cap.effective = cap.permitted =
  258. (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW);
  259. cap.inheritable = 0;
  260. capset(&header, &cap);
  261. #endif /* ANDROID */
  262. #ifdef WPA_TRACE
  263. dl_list_init(&alloc_list);
  264. #endif /* WPA_TRACE */
  265. return 0;
  266. }
  267. void os_program_deinit(void)
  268. {
  269. #ifdef WPA_TRACE
  270. struct os_alloc_trace *a;
  271. unsigned long total = 0;
  272. dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) {
  273. total += a->len;
  274. if (a->magic != ALLOC_MAGIC) {
  275. wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x "
  276. "len %lu",
  277. a, a->magic, (unsigned long) a->len);
  278. continue;
  279. }
  280. wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu",
  281. a, (unsigned long) a->len);
  282. wpa_trace_dump("memleak", a);
  283. }
  284. if (total)
  285. wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes",
  286. (unsigned long) total);
  287. #endif /* WPA_TRACE */
  288. }
  289. int os_setenv(const char *name, const char *value, int overwrite)
  290. {
  291. return setenv(name, value, overwrite);
  292. }
  293. int os_unsetenv(const char *name)
  294. {
  295. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
  296. defined(__OpenBSD__)
  297. unsetenv(name);
  298. return 0;
  299. #else
  300. return unsetenv(name);
  301. #endif
  302. }
  303. char * os_readfile(const char *name, size_t *len)
  304. {
  305. FILE *f;
  306. char *buf;
  307. long pos;
  308. f = fopen(name, "rb");
  309. if (f == NULL)
  310. return NULL;
  311. if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) {
  312. fclose(f);
  313. return NULL;
  314. }
  315. *len = pos;
  316. if (fseek(f, 0, SEEK_SET) < 0) {
  317. fclose(f);
  318. return NULL;
  319. }
  320. buf = os_malloc(*len);
  321. if (buf == NULL) {
  322. fclose(f);
  323. return NULL;
  324. }
  325. if (fread(buf, 1, *len, f) != *len) {
  326. fclose(f);
  327. os_free(buf);
  328. return NULL;
  329. }
  330. fclose(f);
  331. return buf;
  332. }
  333. int os_file_exists(const char *fname)
  334. {
  335. FILE *f = fopen(fname, "rb");
  336. if (f == NULL)
  337. return 0;
  338. fclose(f);
  339. return 1;
  340. }
  341. #ifndef WPA_TRACE
  342. void * os_zalloc(size_t size)
  343. {
  344. return calloc(1, size);
  345. }
  346. #endif /* WPA_TRACE */
  347. size_t os_strlcpy(char *dest, const char *src, size_t siz)
  348. {
  349. const char *s = src;
  350. size_t left = siz;
  351. if (left) {
  352. /* Copy string up to the maximum size of the dest buffer */
  353. while (--left != 0) {
  354. if ((*dest++ = *s++) == '\0')
  355. break;
  356. }
  357. }
  358. if (left == 0) {
  359. /* Not enough room for the string; force NUL-termination */
  360. if (siz != 0)
  361. *dest = '\0';
  362. while (*s++)
  363. ; /* determine total src string length */
  364. }
  365. return s - src - 1;
  366. }
  367. int os_memcmp_const(const void *a, const void *b, size_t len)
  368. {
  369. const u8 *aa = a;
  370. const u8 *bb = b;
  371. size_t i;
  372. u8 res;
  373. for (res = 0, i = 0; i < len; i++)
  374. res |= aa[i] ^ bb[i];
  375. return res;
  376. }
  377. #ifdef WPA_TRACE
  378. void * os_malloc(size_t size)
  379. {
  380. struct os_alloc_trace *a;
  381. a = malloc(sizeof(*a) + size);
  382. if (a == NULL)
  383. return NULL;
  384. a->magic = ALLOC_MAGIC;
  385. dl_list_add(&alloc_list, &a->list);
  386. a->len = size;
  387. wpa_trace_record(a);
  388. return a + 1;
  389. }
  390. void * os_realloc(void *ptr, size_t size)
  391. {
  392. struct os_alloc_trace *a;
  393. size_t copy_len;
  394. void *n;
  395. if (ptr == NULL)
  396. return os_malloc(size);
  397. a = (struct os_alloc_trace *) ptr - 1;
  398. if (a->magic != ALLOC_MAGIC) {
  399. wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s",
  400. a, a->magic,
  401. a->magic == FREED_MAGIC ? " (already freed)" : "");
  402. wpa_trace_show("Invalid os_realloc() call");
  403. abort();
  404. }
  405. n = os_malloc(size);
  406. if (n == NULL)
  407. return NULL;
  408. copy_len = a->len;
  409. if (copy_len > size)
  410. copy_len = size;
  411. os_memcpy(n, a + 1, copy_len);
  412. os_free(ptr);
  413. return n;
  414. }
  415. void os_free(void *ptr)
  416. {
  417. struct os_alloc_trace *a;
  418. if (ptr == NULL)
  419. return;
  420. a = (struct os_alloc_trace *) ptr - 1;
  421. if (a->magic != ALLOC_MAGIC) {
  422. wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s",
  423. a, a->magic,
  424. a->magic == FREED_MAGIC ? " (already freed)" : "");
  425. wpa_trace_show("Invalid os_free() call");
  426. abort();
  427. }
  428. dl_list_del(&a->list);
  429. a->magic = FREED_MAGIC;
  430. wpa_trace_check_ref(ptr);
  431. free(a);
  432. }
  433. void * os_zalloc(size_t size)
  434. {
  435. void *ptr = os_malloc(size);
  436. if (ptr)
  437. os_memset(ptr, 0, size);
  438. return ptr;
  439. }
  440. char * os_strdup(const char *s)
  441. {
  442. size_t len;
  443. char *d;
  444. len = os_strlen(s);
  445. d = os_malloc(len + 1);
  446. if (d == NULL)
  447. return NULL;
  448. os_memcpy(d, s, len);
  449. d[len] = '\0';
  450. return d;
  451. }
  452. #endif /* WPA_TRACE */