]>
git.ipfire.org Git - thirdparty/hostap.git/blob - src/utils/os_internal.c
2 * wpa_supplicant/hostapd / Internal implementation of OS specific functions
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
8 * This file is an example of operating system specific wrapper functions.
9 * This version implements many of the functions internally, so it can be used
10 * to fill in missing functions from the target system C libraries.
12 * Some of the functions are using standard C library calls in order to keep
13 * this file in working condition to allow the functions to be tested on a
14 * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
15 * this file to work correctly. Note that these implementations are only
16 * examples and are not optimized for speed.
23 #undef OS_REJECT_C_LIB_FUNCTIONS
26 void os_sleep(os_time_t sec
, os_time_t usec
)
28 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L)
29 const struct timespec req
= { sec
, usec
* 1000 };
31 nanosleep(&req
, NULL
);
41 int os_get_time(struct os_time
*t
)
45 res
= gettimeofday(&tv
, NULL
);
52 int os_get_reltime(struct os_reltime
*t
)
56 res
= gettimeofday(&tv
, NULL
);
63 int os_mktime(int year
, int month
, int day
, int hour
, int min
, int sec
,
68 if (year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
69 hour
< 0 || hour
> 23 || min
< 0 || min
> 59 || sec
< 0 ||
73 os_memset(&tm
, 0, sizeof(tm
));
74 tm
.tm_year
= year
- 1900;
75 tm
.tm_mon
= month
- 1;
81 *t
= (os_time_t
) mktime(&tm
);
86 int os_gmtime(os_time_t t
, struct os_tm
*tm
)
94 tm
->sec
= tm2
->tm_sec
;
95 tm
->min
= tm2
->tm_min
;
96 tm
->hour
= tm2
->tm_hour
;
97 tm
->day
= tm2
->tm_mday
;
98 tm
->month
= tm2
->tm_mon
+ 1;
99 tm
->year
= tm2
->tm_year
+ 1900;
104 int os_daemonize(const char *pid_file
)
107 wpa_printf(MSG_ERROR
, "daemon: %s", strerror(errno
));
112 FILE *f
= fopen(pid_file
, "w");
114 fprintf(f
, "%u\n", getpid());
123 void os_daemonize_terminate(const char *pid_file
)
130 int os_get_random(unsigned char *buf
, size_t len
)
135 f
= fopen("/dev/urandom", "rb");
137 printf("Could not open /dev/urandom.\n");
141 rc
= fread(buf
, 1, len
, f
);
144 return rc
!= len
? -1 : 0;
148 unsigned long os_random(void)
154 char * os_rel2abs_path(const char *rel_path
)
156 char *buf
= NULL
, *cwd
, *ret
;
157 size_t len
= 128, cwd_len
, rel_len
, ret_len
;
159 if (rel_path
[0] == '/')
160 return os_strdup(rel_path
);
163 buf
= os_malloc(len
);
166 cwd
= getcwd(buf
, len
);
169 if (errno
!= ERANGE
) {
178 cwd_len
= os_strlen(cwd
);
179 rel_len
= os_strlen(rel_path
);
180 ret_len
= cwd_len
+ 1 + rel_len
+ 1;
181 ret
= os_malloc(ret_len
);
183 os_memcpy(ret
, cwd
, cwd_len
);
185 os_memcpy(ret
+ cwd_len
+ 1, rel_path
, rel_len
);
186 ret
[ret_len
- 1] = '\0';
193 int os_program_init(void)
199 void os_program_deinit(void)
204 int os_setenv(const char *name
, const char *value
, int overwrite
)
206 return setenv(name
, value
, overwrite
);
210 int os_unsetenv(const char *name
)
212 #if defined(__FreeBSD__) || defined(__NetBSD__)
216 return unsetenv(name
);
221 char * os_readfile(const char *name
, size_t *len
)
226 f
= fopen(name
, "rb");
230 fseek(f
, 0, SEEK_END
);
232 fseek(f
, 0, SEEK_SET
);
234 buf
= os_malloc(*len
);
240 if (fread(buf
, 1, *len
, f
) != *len
) {
252 int os_fdatasync(FILE *stream
)
258 void * os_zalloc(size_t size
)
260 void *n
= os_malloc(size
);
262 os_memset(n
, 0, size
);
267 void * os_malloc(size_t size
)
273 void * os_realloc(void *ptr
, size_t size
)
275 return realloc(ptr
, size
);
279 void os_free(void *ptr
)
285 void * os_memcpy(void *dest
, const void *src
, size_t n
)
295 void * os_memmove(void *dest
, const void *src
, size_t n
)
298 os_memcpy(dest
, src
, n
);
300 /* overlapping areas */
301 char *d
= (char *) dest
+ n
;
302 const char *s
= (const char *) src
+ n
;
310 void * os_memset(void *s
, int c
, size_t n
)
319 int os_memcmp(const void *s1
, const void *s2
, size_t n
)
321 const unsigned char *p1
= s1
, *p2
= s2
;
338 char * os_strdup(const char *s
)
345 res
= os_malloc(len
+ 1);
347 os_memcpy(res
, s
, len
+ 1);
352 size_t os_strlen(const char *s
)
361 int os_strcasecmp(const char *s1
, const char *s2
)
364 * Ignoring case is not required for main functionality, so just use
365 * the case sensitive version of the function.
367 return os_strcmp(s1
, s2
);
371 int os_strncasecmp(const char *s1
, const char *s2
, size_t n
)
374 * Ignoring case is not required for main functionality, so just use
375 * the case sensitive version of the function.
377 return os_strncmp(s1
, s2
, n
);
381 char * os_strchr(const char *s
, int c
)
392 char * os_strrchr(const char *s
, int c
)
407 int os_strcmp(const char *s1
, const char *s2
)
420 int os_strncmp(const char *s1
, const char *s2
, size_t n
)
439 size_t os_strlcpy(char *dest
, const char *src
, size_t siz
)
445 /* Copy string up to the maximum size of the dest buffer */
446 while (--left
!= 0) {
447 if ((*dest
++ = *s
++) == '\0')
453 /* Not enough room for the string; force NUL-termination */
457 ; /* determine total src string length */
464 int os_memcmp_const(const void *a
, const void *b
, size_t len
)
471 for (res
= 0, i
= 0; i
< len
; i
++)
472 res
|= aa
[i
] ^ bb
[i
];
478 char * os_strstr(const char *haystack
, const char *needle
)
480 size_t len
= os_strlen(needle
);
482 if (os_strncmp(haystack
, needle
, len
) == 0)
483 return (char *) haystack
;
491 int os_snprintf(char *str
, size_t size
, const char *format
, ...)
496 /* See http://www.ijs.si/software/snprintf/ for portable
497 * implementation of snprintf.
500 va_start(ap
, format
);
501 ret
= vsnprintf(str
, size
, format
, ap
);
504 str
[size
- 1] = '\0';
509 int os_exec(const char *program
, const char *arg
, int wait_completion
)
516 wpa_printf(MSG_ERROR
, "fork: %s", strerror(errno
));
521 /* run the external command in the child process */
522 const int MAX_ARG
= 30;
523 char *_program
, *_arg
, *pos
;
524 char *argv
[MAX_ARG
+ 1];
527 _program
= os_strdup(program
);
528 _arg
= os_strdup(arg
);
534 while (i
< MAX_ARG
&& pos
&& *pos
) {
540 pos
= os_strchr(pos
, ' ');
546 execv(program
, argv
);
547 wpa_printf(MSG_ERROR
, "execv: %s", strerror(errno
));
554 if (wait_completion
) {
555 /* wait for the child process to complete in the parent */
556 waitpid(pid
, &pid_status
, 0);