26 static struct dl_list alloc_list;
28 #define ALLOC_MAGIC 0xa84ef1b2
29 #define FREED_MAGIC 0x67fd487a
31 struct os_alloc_trace {
54 res = gettimeofday(&tv,
NULL);
61 int os_mktime(
int year,
int month,
int day,
int hour,
int min,
int sec,
65 time_t t_local, t1, t2;
68 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
69 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
73 memset(&tm, 0,
sizeof(tm));
74 tm.tm_year = year - 1900;
75 tm.tm_mon = month - 1;
81 t_local = mktime(&tm);
84 tm1 = localtime(&t_local);
87 tm1 = gmtime(&t_local);
103 static int os_daemon(
int nochdir,
int noclose)
110 devnull = open(
"/dev/null", O_RDWR);
114 if (dup2(devnull, STDIN_FILENO) < 0) {
119 if (dup2(devnull, STDOUT_FILENO) < 0) {
124 if (dup2(devnull, STDERR_FILENO) < 0) {
132 #define os_daemon daemon
147 FILE *f = fopen(pid_file,
"w");
149 fprintf(f,
"%u\n", getpid());
171 f = fopen(
"/dev/urandom",
"rb");
173 printf(
"Could not open /dev/urandom.\n");
177 rc = fread(buf, 1, len, f);
180 return rc != len ? -1 : 0;
192 char *buf =
NULL, *cwd, *ret;
193 size_t len = 128, cwd_len, rel_len, ret_len;
196 if (rel_path[0] ==
'/')
203 cwd = getcwd(buf, len);
207 if (last_errno != ERANGE)
220 ret_len = cwd_len + 1 + rel_len + 1;
225 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
226 ret[ret_len - 1] =
'\0';
236 dl_list_init(&alloc_list);
245 struct os_alloc_trace *a;
246 unsigned long total = 0;
247 dl_list_for_each(a, &alloc_list,
struct os_alloc_trace, list) {
249 if (a->magic != ALLOC_MAGIC) {
252 a, a->magic, (
unsigned long) a->len);
256 a, (
unsigned long) a->len);
257 wpa_trace_dump(
"memleak", a);
261 (
unsigned long) total);
266 int os_setenv(
const char *name,
const char *value,
int overwrite)
268 return setenv(name, value, overwrite);
274 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__) || \
279 return unsetenv(name);
289 f = fopen(name,
"rb");
293 fseek(f, 0, SEEK_END);
295 fseek(f, 0, SEEK_SET);
303 if (fread(buf, 1, *len, f) != *len) {
318 return calloc(1, size);
330 while (--left != 0) {
331 if ((*dest++ = *s++) ==
'\0')
352 struct os_alloc_trace *a;
353 a = malloc(
sizeof(*a) + size);
356 a->magic = ALLOC_MAGIC;
357 dl_list_add(&alloc_list, &a->list);
366 struct os_alloc_trace *a;
373 a = (
struct os_alloc_trace *) ptr - 1;
374 if (a->magic != ALLOC_MAGIC) {
377 a->magic == FREED_MAGIC ?
" (already freed)" :
"");
378 wpa_trace_show(
"Invalid os_realloc() call");
395 struct os_alloc_trace *a;
399 a = (
struct os_alloc_trace *) ptr - 1;
400 if (a->magic != ALLOC_MAGIC) {
403 a->magic == FREED_MAGIC ?
" (already freed)" :
"");
404 wpa_trace_show(
"Invalid os_free() call");
407 dl_list_del(&a->list);
408 a->magic = FREED_MAGIC;
410 wpa_trace_check_ref(ptr);
void os_sleep(os_time_t sec, os_time_t usec)
int os_get_random(unsigned char *buf, size_t len)
int os_get_time(struct os_time *t)
int os_setenv(const char *name, const char *value, int overwrite)
#define os_memset(s, c, n)
int os_program_init(void)
void os_daemonize_terminate(const char *pid_file)
int os_unsetenv(const char *name)
unsigned long os_random(void)
char * os_rel2abs_path(const char *rel_path)
int os_mktime(int year, int month, int day, int hour, int min, int sec, os_time_t *t)
char * os_readfile(const char *name, size_t *len)
#define os_memcpy(d, s, n)
size_t os_strlcpy(char *dest, const char *src, size_t siz)
void os_program_deinit(void)
void * os_zalloc(size_t size)
int os_daemonize(const char *pid_file)
void wpa_printf(int level, const char *fmt,...) PRINTF_FORMAT(2