source: rtems-libbsd/freebsd/contrib/wpa/src/utils/os_unix.c @ 0aa7e63

55-freebsd-126-freebsd-12
Last change on this file since 0aa7e63 was 8f2267b, checked in by Sichen Zhao <1473996754@…>, on 10/12/17 at 12:16:07

Port wpa supplicant to RTEMS.

Add wpa_supplicant lib support and shell command support in RTEMS.

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