Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / utils / os_internal.c
index 5260e23..ed6eb3c 100644 (file)
@@ -2,14 +2,8 @@
  * wpa_supplicant/hostapd / Internal implementation of OS specific functions
  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
+ * This software may be distributed under the terms of the BSD license.
+ * See README for more details.
  *
  * This file is an example of operating system specific  wrapper functions.
  * This version implements many of the functions internally, so it can be used
  */
 
 #include "includes.h"
+#include <time.h>
+#include <sys/wait.h>
 
 #undef OS_REJECT_C_LIB_FUNCTIONS
-#include "os.h"
+#include "common.h"
 
 void os_sleep(os_time_t sec, os_time_t usec)
 {
@@ -47,6 +43,17 @@ int os_get_time(struct os_time *t)
 }
 
 
+int os_get_reltime(struct os_reltime *t)
+{
+       int res;
+       struct timeval tv;
+       res = gettimeofday(&tv, NULL);
+       t->sec = tv.tv_sec;
+       t->usec = tv.tv_usec;
+       return res;
+}
+
+
 int os_mktime(int year, int month, int day, int hour, int min, int sec,
              os_time_t *t)
 {
@@ -70,10 +77,28 @@ int os_mktime(int year, int month, int day, int hour, int min, int sec,
 }
 
 
+int os_gmtime(os_time_t t, struct os_tm *tm)
+{
+       struct tm *tm2;
+       time_t t2 = t;
+
+       tm2 = gmtime(&t2);
+       if (tm2 == NULL)
+               return -1;
+       tm->sec = tm2->tm_sec;
+       tm->min = tm2->tm_min;
+       tm->hour = tm2->tm_hour;
+       tm->day = tm2->tm_mday;
+       tm->month = tm2->tm_mon + 1;
+       tm->year = tm2->tm_year + 1900;
+       return 0;
+}
+
+
 int os_daemonize(const char *pid_file)
 {
        if (daemon(0, 0)) {
-               perror("daemon");
+               wpa_printf(MSG_ERROR, "daemon: %s", strerror(errno));
                return -1;
        }
 
@@ -144,8 +169,8 @@ char * os_rel2abs_path(const char *rel_path)
                }
        }
 
-       cwd_len = strlen(cwd);
-       rel_len = strlen(rel_path);
+       cwd_len = os_strlen(cwd);
+       rel_len = os_strlen(rel_path);
        ret_len = cwd_len + 1 + rel_len + 1;
        ret = os_malloc(ret_len);
        if (ret) {
@@ -218,6 +243,12 @@ char * os_readfile(const char *name, size_t *len)
 }
 
 
+int os_fdatasync(FILE *stream)
+{
+       return 0;
+}
+
+
 void * os_zalloc(size_t size)
 {
        void *n = os_malloc(size);
@@ -440,6 +471,20 @@ size_t os_strlcpy(char *dest, const char *src, size_t siz)
 }
 
 
+int os_memcmp_const(const void *a, const void *b, size_t len)
+{
+       const u8 *aa = a;
+       const u8 *bb = b;
+       size_t i;
+       u8 res;
+
+       for (res = 0, i = 0; i < len; i++)
+               res |= aa[i] ^ bb[i];
+
+       return res;
+}
+
+
 char * os_strstr(const char *haystack, const char *needle)
 {
        size_t len = os_strlen(needle);
@@ -469,3 +514,57 @@ int os_snprintf(char *str, size_t size, const char *format, ...)
                str[size - 1] = '\0';
        return ret;
 }
+
+
+int os_exec(const char *program, const char *arg, int wait_completion)
+{
+       pid_t pid;
+       int pid_status;
+
+       pid = fork();
+       if (pid < 0) {
+               wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
+               return -1;
+       }
+
+       if (pid == 0) {
+               /* run the external command in the child process */
+               const int MAX_ARG = 30;
+               char *_program, *_arg, *pos;
+               char *argv[MAX_ARG + 1];
+               int i;
+
+               _program = os_strdup(program);
+               _arg = os_strdup(arg);
+
+               argv[0] = _program;
+
+               i = 1;
+               pos = _arg;
+               while (i < MAX_ARG && pos && *pos) {
+                       while (*pos == ' ')
+                               pos++;
+                       if (*pos == '\0')
+                               break;
+                       argv[i++] = pos;
+                       pos = os_strchr(pos, ' ');
+                       if (pos)
+                               *pos++ = '\0';
+               }
+               argv[i] = NULL;
+
+               execv(program, argv);
+               wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
+               os_free(_program);
+               os_free(_arg);
+               exit(0);
+               return -1;
+       }
+
+       if (wait_completion) {
+               /* wait for the child process to complete in the parent */
+               waitpid(pid, &pid_status, 0);
+       }
+
+       return 0;
+}