Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * OS specific functions for UNIX/POSIX systems |
| 3 | * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi> |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 11 | #include <time.h> |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 12 | #include <sys/wait.h> |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 13 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #ifdef ANDROID |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 15 | #include <grp.h> |
| 16 | #include <pwd.h> |
Nick Kralevich | a562b19 | 2013-02-28 13:55:41 -0800 | [diff] [blame] | 17 | #include <sys/capability.h> |
Elliott Hughes | 0f4fce1 | 2014-07-18 17:45:49 -0700 | [diff] [blame] | 18 | #include <sys/prctl.h> |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 19 | #include <sys/types.h> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 20 | #endif /* ANDROID */ |
| 21 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 22 | #ifdef __MACH__ |
| 23 | #include <CoreServices/CoreServices.h> |
| 24 | #include <mach/mach.h> |
| 25 | #include <mach/mach_time.h> |
| 26 | #endif /* __MACH__ */ |
| 27 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 28 | #include "os.h" |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 29 | #include "common.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 30 | |
| 31 | #ifdef WPA_TRACE |
| 32 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 33 | #include "wpa_debug.h" |
| 34 | #include "trace.h" |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 35 | #include "list.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 36 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 37 | static struct dl_list alloc_list = DL_LIST_HEAD_INIT(alloc_list); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 38 | |
| 39 | #define ALLOC_MAGIC 0xa84ef1b2 |
| 40 | #define FREED_MAGIC 0x67fd487a |
| 41 | |
| 42 | struct os_alloc_trace { |
| 43 | unsigned int magic; |
| 44 | struct dl_list list; |
| 45 | size_t len; |
| 46 | WPA_TRACE_INFO |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 47 | } __attribute__((aligned(16))); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 48 | |
| 49 | #endif /* WPA_TRACE */ |
| 50 | |
| 51 | |
| 52 | void os_sleep(os_time_t sec, os_time_t usec) |
| 53 | { |
| 54 | if (sec) |
| 55 | sleep(sec); |
| 56 | if (usec) |
| 57 | usleep(usec); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | int os_get_time(struct os_time *t) |
| 62 | { |
| 63 | int res; |
| 64 | struct timeval tv; |
| 65 | res = gettimeofday(&tv, NULL); |
| 66 | t->sec = tv.tv_sec; |
| 67 | t->usec = tv.tv_usec; |
| 68 | return res; |
| 69 | } |
| 70 | |
| 71 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 72 | int os_get_reltime(struct os_reltime *t) |
| 73 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 74 | #ifndef __MACH__ |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 75 | #if defined(CLOCK_BOOTTIME) |
| 76 | static clockid_t clock_id = CLOCK_BOOTTIME; |
| 77 | #elif defined(CLOCK_MONOTONIC) |
| 78 | static clockid_t clock_id = CLOCK_MONOTONIC; |
| 79 | #else |
| 80 | static clockid_t clock_id = CLOCK_REALTIME; |
| 81 | #endif |
| 82 | struct timespec ts; |
| 83 | int res; |
| 84 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 85 | if (TEST_FAIL()) |
| 86 | return -1; |
| 87 | |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 88 | while (1) { |
| 89 | res = clock_gettime(clock_id, &ts); |
| 90 | if (res == 0) { |
| 91 | t->sec = ts.tv_sec; |
| 92 | t->usec = ts.tv_nsec / 1000; |
| 93 | return 0; |
| 94 | } |
| 95 | switch (clock_id) { |
| 96 | #ifdef CLOCK_BOOTTIME |
| 97 | case CLOCK_BOOTTIME: |
| 98 | clock_id = CLOCK_MONOTONIC; |
| 99 | break; |
| 100 | #endif |
| 101 | #ifdef CLOCK_MONOTONIC |
| 102 | case CLOCK_MONOTONIC: |
| 103 | clock_id = CLOCK_REALTIME; |
| 104 | break; |
| 105 | #endif |
| 106 | case CLOCK_REALTIME: |
| 107 | return -1; |
| 108 | } |
| 109 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 110 | #else /* __MACH__ */ |
| 111 | uint64_t abstime, nano; |
| 112 | static mach_timebase_info_data_t info = { 0, 0 }; |
| 113 | |
| 114 | if (!info.denom) { |
| 115 | if (mach_timebase_info(&info) != KERN_SUCCESS) |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | abstime = mach_absolute_time(); |
| 120 | nano = (abstime * info.numer) / info.denom; |
| 121 | |
| 122 | t->sec = nano / NSEC_PER_SEC; |
| 123 | t->usec = (nano - (((uint64_t) t->sec) * NSEC_PER_SEC)) / NSEC_PER_USEC; |
| 124 | |
| 125 | return 0; |
| 126 | #endif /* __MACH__ */ |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 130 | int os_mktime(int year, int month, int day, int hour, int min, int sec, |
| 131 | os_time_t *t) |
| 132 | { |
| 133 | struct tm tm, *tm1; |
| 134 | time_t t_local, t1, t2; |
| 135 | os_time_t tz_offset; |
| 136 | |
| 137 | if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 || |
| 138 | hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || |
| 139 | sec > 60) |
| 140 | return -1; |
| 141 | |
| 142 | memset(&tm, 0, sizeof(tm)); |
| 143 | tm.tm_year = year - 1900; |
| 144 | tm.tm_mon = month - 1; |
| 145 | tm.tm_mday = day; |
| 146 | tm.tm_hour = hour; |
| 147 | tm.tm_min = min; |
| 148 | tm.tm_sec = sec; |
| 149 | |
| 150 | t_local = mktime(&tm); |
| 151 | |
| 152 | /* figure out offset to UTC */ |
| 153 | tm1 = localtime(&t_local); |
| 154 | if (tm1) { |
| 155 | t1 = mktime(tm1); |
| 156 | tm1 = gmtime(&t_local); |
| 157 | if (tm1) { |
| 158 | t2 = mktime(tm1); |
| 159 | tz_offset = t2 - t1; |
| 160 | } else |
| 161 | tz_offset = 0; |
| 162 | } else |
| 163 | tz_offset = 0; |
| 164 | |
| 165 | *t = (os_time_t) t_local - tz_offset; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 170 | int os_gmtime(os_time_t t, struct os_tm *tm) |
| 171 | { |
| 172 | struct tm *tm2; |
| 173 | time_t t2 = t; |
| 174 | |
| 175 | tm2 = gmtime(&t2); |
| 176 | if (tm2 == NULL) |
| 177 | return -1; |
| 178 | tm->sec = tm2->tm_sec; |
| 179 | tm->min = tm2->tm_min; |
| 180 | tm->hour = tm2->tm_hour; |
| 181 | tm->day = tm2->tm_mday; |
| 182 | tm->month = tm2->tm_mon + 1; |
| 183 | tm->year = tm2->tm_year + 1900; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 188 | #ifdef __APPLE__ |
| 189 | #include <fcntl.h> |
| 190 | static int os_daemon(int nochdir, int noclose) |
| 191 | { |
| 192 | int devnull; |
| 193 | |
| 194 | if (chdir("/") < 0) |
| 195 | return -1; |
| 196 | |
| 197 | devnull = open("/dev/null", O_RDWR); |
| 198 | if (devnull < 0) |
| 199 | return -1; |
| 200 | |
| 201 | if (dup2(devnull, STDIN_FILENO) < 0) { |
| 202 | close(devnull); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | if (dup2(devnull, STDOUT_FILENO) < 0) { |
| 207 | close(devnull); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (dup2(devnull, STDERR_FILENO) < 0) { |
| 212 | close(devnull); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | #else /* __APPLE__ */ |
| 219 | #define os_daemon daemon |
| 220 | #endif /* __APPLE__ */ |
| 221 | |
| 222 | |
| 223 | int os_daemonize(const char *pid_file) |
| 224 | { |
| 225 | #if defined(__uClinux__) || defined(__sun__) |
| 226 | return -1; |
| 227 | #else /* defined(__uClinux__) || defined(__sun__) */ |
| 228 | if (os_daemon(0, 0)) { |
| 229 | perror("daemon"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | if (pid_file) { |
| 234 | FILE *f = fopen(pid_file, "w"); |
| 235 | if (f) { |
| 236 | fprintf(f, "%u\n", getpid()); |
| 237 | fclose(f); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return -0; |
| 242 | #endif /* defined(__uClinux__) || defined(__sun__) */ |
| 243 | } |
| 244 | |
| 245 | |
| 246 | void os_daemonize_terminate(const char *pid_file) |
| 247 | { |
| 248 | if (pid_file) |
| 249 | unlink(pid_file); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | int os_get_random(unsigned char *buf, size_t len) |
| 254 | { |
| 255 | FILE *f; |
| 256 | size_t rc; |
| 257 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 258 | if (TEST_FAIL()) |
| 259 | return -1; |
| 260 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 261 | f = fopen("/dev/urandom", "rb"); |
| 262 | if (f == NULL) { |
| 263 | printf("Could not open /dev/urandom.\n"); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | rc = fread(buf, 1, len, f); |
| 268 | fclose(f); |
| 269 | |
| 270 | return rc != len ? -1 : 0; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | unsigned long os_random(void) |
| 275 | { |
| 276 | return random(); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | char * os_rel2abs_path(const char *rel_path) |
| 281 | { |
| 282 | char *buf = NULL, *cwd, *ret; |
| 283 | size_t len = 128, cwd_len, rel_len, ret_len; |
| 284 | int last_errno; |
| 285 | |
Dmitry Shmidt | 64f47c5 | 2013-04-16 10:41:54 -0700 | [diff] [blame] | 286 | if (!rel_path) |
| 287 | return NULL; |
| 288 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 289 | if (rel_path[0] == '/') |
| 290 | return os_strdup(rel_path); |
| 291 | |
| 292 | for (;;) { |
| 293 | buf = os_malloc(len); |
| 294 | if (buf == NULL) |
| 295 | return NULL; |
| 296 | cwd = getcwd(buf, len); |
| 297 | if (cwd == NULL) { |
| 298 | last_errno = errno; |
| 299 | os_free(buf); |
| 300 | if (last_errno != ERANGE) |
| 301 | return NULL; |
| 302 | len *= 2; |
| 303 | if (len > 2000) |
| 304 | return NULL; |
| 305 | } else { |
| 306 | buf[len - 1] = '\0'; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | cwd_len = os_strlen(cwd); |
| 312 | rel_len = os_strlen(rel_path); |
| 313 | ret_len = cwd_len + 1 + rel_len + 1; |
| 314 | ret = os_malloc(ret_len); |
| 315 | if (ret) { |
| 316 | os_memcpy(ret, cwd, cwd_len); |
| 317 | ret[cwd_len] = '/'; |
| 318 | os_memcpy(ret + cwd_len + 1, rel_path, rel_len); |
| 319 | ret[ret_len - 1] = '\0'; |
| 320 | } |
| 321 | os_free(buf); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | |
| 326 | int os_program_init(void) |
| 327 | { |
| 328 | #ifdef ANDROID |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 329 | struct __user_cap_header_struct header; |
| 330 | struct __user_cap_data_struct cap; |
| 331 | struct group *grp = getgrnam("wifi"); |
| 332 | gid_t gid_wifi = grp ? grp->gr_gid : 0; |
| 333 | struct passwd *pwd = getpwnam("wifi"); |
| 334 | uid_t uid_wifi = pwd ? pwd->pw_uid : 0; |
| 335 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 336 | /* |
| 337 | * We ignore errors here since errors are normal if we |
| 338 | * are already running as non-root. |
| 339 | */ |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 340 | #ifdef ANDROID_SETGROUPS_OVERRIDE |
| 341 | gid_t groups[] = { ANDROID_SETGROUPS_OVERRIDE }; |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 342 | |
| 343 | if (!gid_wifi || !uid_wifi) return -1; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 344 | #else /* ANDROID_SETGROUPS_OVERRIDE */ |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 345 | gid_t groups[3]; |
| 346 | |
| 347 | if (!gid_wifi || !uid_wifi) return -1; |
| 348 | groups[0] = gid_wifi; |
| 349 | |
| 350 | grp = getgrnam("inet"); |
| 351 | groups[1] = grp ? grp->gr_gid : 0; |
| 352 | if (!groups[1]) return -1; |
| 353 | |
| 354 | grp = getgrnam("keystore"); |
| 355 | groups[2] = grp ? grp->gr_gid : 0; |
| 356 | if (!groups[2]) return -1; |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 357 | #endif /* ANDROID_SETGROUPS_OVERRIDE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 358 | |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 359 | setgroups(ARRAY_SIZE(groups), groups); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 360 | |
| 361 | prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 362 | |
Yifan Hong | e031790 | 2017-07-20 14:15:22 -0700 | [diff] [blame] | 363 | setgid(gid_wifi); |
| 364 | setuid(uid_wifi); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 365 | |
| 366 | header.version = _LINUX_CAPABILITY_VERSION; |
| 367 | header.pid = 0; |
| 368 | cap.effective = cap.permitted = |
| 369 | (1 << CAP_NET_ADMIN) | (1 << CAP_NET_RAW); |
| 370 | cap.inheritable = 0; |
| 371 | capset(&header, &cap); |
| 372 | #endif /* ANDROID */ |
| 373 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | |
| 378 | void os_program_deinit(void) |
| 379 | { |
| 380 | #ifdef WPA_TRACE |
| 381 | struct os_alloc_trace *a; |
| 382 | unsigned long total = 0; |
| 383 | dl_list_for_each(a, &alloc_list, struct os_alloc_trace, list) { |
| 384 | total += a->len; |
| 385 | if (a->magic != ALLOC_MAGIC) { |
| 386 | wpa_printf(MSG_INFO, "MEMLEAK[%p]: invalid magic 0x%x " |
| 387 | "len %lu", |
| 388 | a, a->magic, (unsigned long) a->len); |
| 389 | continue; |
| 390 | } |
| 391 | wpa_printf(MSG_INFO, "MEMLEAK[%p]: len %lu", |
| 392 | a, (unsigned long) a->len); |
| 393 | wpa_trace_dump("memleak", a); |
| 394 | } |
| 395 | if (total) |
| 396 | wpa_printf(MSG_INFO, "MEMLEAK: total %lu bytes", |
| 397 | (unsigned long) total); |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 398 | wpa_trace_deinit(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 399 | #endif /* WPA_TRACE */ |
| 400 | } |
| 401 | |
| 402 | |
| 403 | int os_setenv(const char *name, const char *value, int overwrite) |
| 404 | { |
| 405 | return setenv(name, value, overwrite); |
| 406 | } |
| 407 | |
| 408 | |
| 409 | int os_unsetenv(const char *name) |
| 410 | { |
| 411 | #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \ |
| 412 | defined(__OpenBSD__) |
| 413 | unsetenv(name); |
| 414 | return 0; |
| 415 | #else |
| 416 | return unsetenv(name); |
| 417 | #endif |
| 418 | } |
| 419 | |
| 420 | |
| 421 | char * os_readfile(const char *name, size_t *len) |
| 422 | { |
| 423 | FILE *f; |
| 424 | char *buf; |
| 425 | long pos; |
| 426 | |
| 427 | f = fopen(name, "rb"); |
| 428 | if (f == NULL) |
| 429 | return NULL; |
| 430 | |
| 431 | if (fseek(f, 0, SEEK_END) < 0 || (pos = ftell(f)) < 0) { |
| 432 | fclose(f); |
| 433 | return NULL; |
| 434 | } |
| 435 | *len = pos; |
| 436 | if (fseek(f, 0, SEEK_SET) < 0) { |
| 437 | fclose(f); |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | buf = os_malloc(*len); |
| 442 | if (buf == NULL) { |
| 443 | fclose(f); |
| 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | if (fread(buf, 1, *len, f) != *len) { |
| 448 | fclose(f); |
| 449 | os_free(buf); |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | fclose(f); |
| 454 | |
| 455 | return buf; |
| 456 | } |
| 457 | |
| 458 | |
Dmitry Shmidt | d5dc24e | 2014-03-12 14:22:04 -0700 | [diff] [blame] | 459 | int os_file_exists(const char *fname) |
| 460 | { |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 461 | return access(fname, F_OK) == 0; |
Dmitry Shmidt | d5dc24e | 2014-03-12 14:22:04 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 465 | int os_fdatasync(FILE *stream) |
Mitchell Wills | 447c7ff | 2015-08-24 17:24:30 -0700 | [diff] [blame] | 466 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 467 | if (!fflush(stream)) { |
| 468 | #ifdef __linux__ |
| 469 | return fdatasync(fileno(stream)); |
| 470 | #else /* !__linux__ */ |
| 471 | #ifdef F_FULLFSYNC |
| 472 | /* OS X does not implement fdatasync(). */ |
| 473 | return fcntl(fileno(stream), F_FULLFSYNC); |
| 474 | #else /* F_FULLFSYNC */ |
Mitchell Wills | 447c7ff | 2015-08-24 17:24:30 -0700 | [diff] [blame] | 475 | return fsync(fileno(stream)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 476 | #endif /* F_FULLFSYNC */ |
| 477 | #endif /* __linux__ */ |
| 478 | } |
| 479 | |
Mitchell Wills | 447c7ff | 2015-08-24 17:24:30 -0700 | [diff] [blame] | 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 484 | #ifndef WPA_TRACE |
| 485 | void * os_zalloc(size_t size) |
| 486 | { |
| 487 | return calloc(1, size); |
| 488 | } |
| 489 | #endif /* WPA_TRACE */ |
| 490 | |
| 491 | |
| 492 | size_t os_strlcpy(char *dest, const char *src, size_t siz) |
| 493 | { |
| 494 | const char *s = src; |
| 495 | size_t left = siz; |
| 496 | |
| 497 | if (left) { |
| 498 | /* Copy string up to the maximum size of the dest buffer */ |
| 499 | while (--left != 0) { |
| 500 | if ((*dest++ = *s++) == '\0') |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if (left == 0) { |
| 506 | /* Not enough room for the string; force NUL-termination */ |
| 507 | if (siz != 0) |
| 508 | *dest = '\0'; |
| 509 | while (*s++) |
| 510 | ; /* determine total src string length */ |
| 511 | } |
| 512 | |
| 513 | return s - src - 1; |
| 514 | } |
| 515 | |
| 516 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 517 | int os_memcmp_const(const void *a, const void *b, size_t len) |
| 518 | { |
| 519 | const u8 *aa = a; |
| 520 | const u8 *bb = b; |
| 521 | size_t i; |
| 522 | u8 res; |
| 523 | |
| 524 | for (res = 0, i = 0; i < len; i++) |
| 525 | res |= aa[i] ^ bb[i]; |
| 526 | |
| 527 | return res; |
| 528 | } |
| 529 | |
| 530 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 531 | void * os_memdup(const void *src, size_t len) |
| 532 | { |
| 533 | void *r = os_malloc(len); |
| 534 | |
| 535 | if (r) |
| 536 | os_memcpy(r, src, len); |
| 537 | return r; |
| 538 | } |
| 539 | |
| 540 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 541 | #ifdef WPA_TRACE |
| 542 | |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 543 | #if defined(WPA_TRACE_BFD) && defined(CONFIG_TESTING_OPTIONS) |
| 544 | char wpa_trace_fail_func[256] = { 0 }; |
| 545 | unsigned int wpa_trace_fail_after; |
| 546 | |
| 547 | static int testing_fail_alloc(void) |
| 548 | { |
| 549 | const char *func[WPA_TRACE_LEN]; |
| 550 | size_t i, res, len; |
| 551 | char *pos, *next; |
| 552 | int match; |
| 553 | |
| 554 | if (!wpa_trace_fail_after) |
| 555 | return 0; |
| 556 | |
| 557 | res = wpa_trace_calling_func(func, WPA_TRACE_LEN); |
| 558 | i = 0; |
| 559 | if (i < res && os_strcmp(func[i], __func__) == 0) |
| 560 | i++; |
| 561 | if (i < res && os_strcmp(func[i], "os_malloc") == 0) |
| 562 | i++; |
| 563 | if (i < res && os_strcmp(func[i], "os_zalloc") == 0) |
| 564 | i++; |
| 565 | if (i < res && os_strcmp(func[i], "os_calloc") == 0) |
| 566 | i++; |
| 567 | if (i < res && os_strcmp(func[i], "os_realloc") == 0) |
| 568 | i++; |
| 569 | if (i < res && os_strcmp(func[i], "os_realloc_array") == 0) |
| 570 | i++; |
| 571 | if (i < res && os_strcmp(func[i], "os_strdup") == 0) |
| 572 | i++; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame^] | 573 | if (i < res && os_strcmp(func[i], "os_memdup") == 0) |
| 574 | i++; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 575 | |
| 576 | pos = wpa_trace_fail_func; |
| 577 | |
| 578 | match = 0; |
| 579 | while (i < res) { |
| 580 | int allow_skip = 1; |
| 581 | int maybe = 0; |
| 582 | |
| 583 | if (*pos == '=') { |
| 584 | allow_skip = 0; |
| 585 | pos++; |
| 586 | } else if (*pos == '?') { |
| 587 | maybe = 1; |
| 588 | pos++; |
| 589 | } |
| 590 | next = os_strchr(pos, ';'); |
| 591 | if (next) |
| 592 | len = next - pos; |
| 593 | else |
| 594 | len = os_strlen(pos); |
| 595 | if (os_memcmp(pos, func[i], len) != 0) { |
| 596 | if (maybe && next) { |
| 597 | pos = next + 1; |
| 598 | continue; |
| 599 | } |
| 600 | if (allow_skip) { |
| 601 | i++; |
| 602 | continue; |
| 603 | } |
| 604 | return 0; |
| 605 | } |
| 606 | if (!next) { |
| 607 | match = 1; |
| 608 | break; |
| 609 | } |
| 610 | pos = next + 1; |
| 611 | i++; |
| 612 | } |
| 613 | if (!match) |
| 614 | return 0; |
| 615 | |
| 616 | wpa_trace_fail_after--; |
| 617 | if (wpa_trace_fail_after == 0) { |
| 618 | wpa_printf(MSG_INFO, "TESTING: fail allocation at %s", |
| 619 | wpa_trace_fail_func); |
| 620 | for (i = 0; i < res; i++) |
| 621 | wpa_printf(MSG_INFO, "backtrace[%d] = %s", |
| 622 | (int) i, func[i]); |
| 623 | return 1; |
| 624 | } |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 629 | |
| 630 | char wpa_trace_test_fail_func[256] = { 0 }; |
| 631 | unsigned int wpa_trace_test_fail_after; |
| 632 | |
| 633 | int testing_test_fail(void) |
| 634 | { |
| 635 | const char *func[WPA_TRACE_LEN]; |
| 636 | size_t i, res, len; |
| 637 | char *pos, *next; |
| 638 | int match; |
| 639 | |
| 640 | if (!wpa_trace_test_fail_after) |
| 641 | return 0; |
| 642 | |
| 643 | res = wpa_trace_calling_func(func, WPA_TRACE_LEN); |
| 644 | i = 0; |
| 645 | if (i < res && os_strcmp(func[i], __func__) == 0) |
| 646 | i++; |
| 647 | |
| 648 | pos = wpa_trace_test_fail_func; |
| 649 | |
| 650 | match = 0; |
| 651 | while (i < res) { |
| 652 | int allow_skip = 1; |
| 653 | int maybe = 0; |
| 654 | |
| 655 | if (*pos == '=') { |
| 656 | allow_skip = 0; |
| 657 | pos++; |
| 658 | } else if (*pos == '?') { |
| 659 | maybe = 1; |
| 660 | pos++; |
| 661 | } |
| 662 | next = os_strchr(pos, ';'); |
| 663 | if (next) |
| 664 | len = next - pos; |
| 665 | else |
| 666 | len = os_strlen(pos); |
| 667 | if (os_memcmp(pos, func[i], len) != 0) { |
| 668 | if (maybe && next) { |
| 669 | pos = next + 1; |
| 670 | continue; |
| 671 | } |
| 672 | if (allow_skip) { |
| 673 | i++; |
| 674 | continue; |
| 675 | } |
| 676 | return 0; |
| 677 | } |
| 678 | if (!next) { |
| 679 | match = 1; |
| 680 | break; |
| 681 | } |
| 682 | pos = next + 1; |
| 683 | i++; |
| 684 | } |
| 685 | if (!match) |
| 686 | return 0; |
| 687 | |
| 688 | wpa_trace_test_fail_after--; |
| 689 | if (wpa_trace_test_fail_after == 0) { |
| 690 | wpa_printf(MSG_INFO, "TESTING: fail at %s", |
| 691 | wpa_trace_test_fail_func); |
| 692 | for (i = 0; i < res; i++) |
| 693 | wpa_printf(MSG_INFO, "backtrace[%d] = %s", |
| 694 | (int) i, func[i]); |
| 695 | return 1; |
| 696 | } |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 701 | #else |
| 702 | |
| 703 | static inline int testing_fail_alloc(void) |
| 704 | { |
| 705 | return 0; |
| 706 | } |
| 707 | #endif |
| 708 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 709 | void * os_malloc(size_t size) |
| 710 | { |
| 711 | struct os_alloc_trace *a; |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 712 | |
| 713 | if (testing_fail_alloc()) |
| 714 | return NULL; |
| 715 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 716 | a = malloc(sizeof(*a) + size); |
| 717 | if (a == NULL) |
| 718 | return NULL; |
| 719 | a->magic = ALLOC_MAGIC; |
| 720 | dl_list_add(&alloc_list, &a->list); |
| 721 | a->len = size; |
| 722 | wpa_trace_record(a); |
| 723 | return a + 1; |
| 724 | } |
| 725 | |
| 726 | |
| 727 | void * os_realloc(void *ptr, size_t size) |
| 728 | { |
| 729 | struct os_alloc_trace *a; |
| 730 | size_t copy_len; |
| 731 | void *n; |
| 732 | |
| 733 | if (ptr == NULL) |
| 734 | return os_malloc(size); |
| 735 | |
| 736 | a = (struct os_alloc_trace *) ptr - 1; |
| 737 | if (a->magic != ALLOC_MAGIC) { |
| 738 | wpa_printf(MSG_INFO, "REALLOC[%p]: invalid magic 0x%x%s", |
| 739 | a, a->magic, |
| 740 | a->magic == FREED_MAGIC ? " (already freed)" : ""); |
| 741 | wpa_trace_show("Invalid os_realloc() call"); |
| 742 | abort(); |
| 743 | } |
| 744 | n = os_malloc(size); |
| 745 | if (n == NULL) |
| 746 | return NULL; |
| 747 | copy_len = a->len; |
| 748 | if (copy_len > size) |
| 749 | copy_len = size; |
| 750 | os_memcpy(n, a + 1, copy_len); |
| 751 | os_free(ptr); |
| 752 | return n; |
| 753 | } |
| 754 | |
| 755 | |
| 756 | void os_free(void *ptr) |
| 757 | { |
| 758 | struct os_alloc_trace *a; |
| 759 | |
| 760 | if (ptr == NULL) |
| 761 | return; |
| 762 | a = (struct os_alloc_trace *) ptr - 1; |
| 763 | if (a->magic != ALLOC_MAGIC) { |
| 764 | wpa_printf(MSG_INFO, "FREE[%p]: invalid magic 0x%x%s", |
| 765 | a, a->magic, |
| 766 | a->magic == FREED_MAGIC ? " (already freed)" : ""); |
| 767 | wpa_trace_show("Invalid os_free() call"); |
| 768 | abort(); |
| 769 | } |
| 770 | dl_list_del(&a->list); |
| 771 | a->magic = FREED_MAGIC; |
| 772 | |
| 773 | wpa_trace_check_ref(ptr); |
| 774 | free(a); |
| 775 | } |
| 776 | |
| 777 | |
| 778 | void * os_zalloc(size_t size) |
| 779 | { |
| 780 | void *ptr = os_malloc(size); |
| 781 | if (ptr) |
| 782 | os_memset(ptr, 0, size); |
| 783 | return ptr; |
| 784 | } |
| 785 | |
| 786 | |
| 787 | char * os_strdup(const char *s) |
| 788 | { |
| 789 | size_t len; |
| 790 | char *d; |
| 791 | len = os_strlen(s); |
| 792 | d = os_malloc(len + 1); |
| 793 | if (d == NULL) |
| 794 | return NULL; |
| 795 | os_memcpy(d, s, len); |
| 796 | d[len] = '\0'; |
| 797 | return d; |
| 798 | } |
| 799 | |
| 800 | #endif /* WPA_TRACE */ |
Jouni Malinen | 772e12c | 2014-10-07 10:29:35 -0700 | [diff] [blame] | 801 | |
| 802 | |
| 803 | int os_exec(const char *program, const char *arg, int wait_completion) |
| 804 | { |
| 805 | pid_t pid; |
| 806 | int pid_status; |
| 807 | |
| 808 | pid = fork(); |
| 809 | if (pid < 0) { |
| 810 | perror("fork"); |
| 811 | return -1; |
| 812 | } |
| 813 | |
| 814 | if (pid == 0) { |
| 815 | /* run the external command in the child process */ |
| 816 | const int MAX_ARG = 30; |
| 817 | char *_program, *_arg, *pos; |
| 818 | char *argv[MAX_ARG + 1]; |
| 819 | int i; |
| 820 | |
| 821 | _program = os_strdup(program); |
| 822 | _arg = os_strdup(arg); |
| 823 | |
| 824 | argv[0] = _program; |
| 825 | |
| 826 | i = 1; |
| 827 | pos = _arg; |
| 828 | while (i < MAX_ARG && pos && *pos) { |
| 829 | while (*pos == ' ') |
| 830 | pos++; |
| 831 | if (*pos == '\0') |
| 832 | break; |
| 833 | argv[i++] = pos; |
| 834 | pos = os_strchr(pos, ' '); |
| 835 | if (pos) |
| 836 | *pos++ = '\0'; |
| 837 | } |
| 838 | argv[i] = NULL; |
| 839 | |
| 840 | execv(program, argv); |
| 841 | perror("execv"); |
| 842 | os_free(_program); |
| 843 | os_free(_arg); |
| 844 | exit(0); |
| 845 | return -1; |
| 846 | } |
| 847 | |
| 848 | if (wait_completion) { |
| 849 | /* wait for the child process to complete in the parent */ |
| 850 | waitpid(pid, &pid_status, 0); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |