os_unix.c 10 KB

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