]> git.ipfire.org Git - people/ms/strongswan.git/blobdiff - src/libstrongswan/utils/utils.c
atomics: Move atomics/recounting support to separate files
[people/ms/strongswan.git] / src / libstrongswan / utils / utils.c
index 81eb2acec72ecdd0a49eb5859152980d9eac120a..8a52d04c595fffacf2f02ada70b277bee337643f 100644 (file)
  */
 
 #define _GNU_SOURCE /* for memrchr */
+#ifdef WIN32
+/* for GetTickCount64, Windows 7 */
+# define _WIN32_WINNT 0x0601
+#endif
+
+#include "utils.h"
+
 #include <sys/stat.h>
 #include <string.h>
 #include <stdio.h>
 #include <limits.h>
 #include <dirent.h>
 #include <time.h>
-#include <pthread.h>
-
-#include "utils.h"
+#ifndef WIN32
+# include <signal.h>
+#endif
 
-#include "collections/enumerator.h"
-#include "utils/debug.h"
-#include "utils/chunk.h"
+#include <library.h>
+#include <utils/debug.h>
+#include <utils/chunk.h>
+#include <collections/enumerator.h>
+#include <threading/mutex.h>
+#include <threading/condvar.h>
 
 ENUM(status_names, SUCCESS, NEED_MORE,
        "SUCCESS",
@@ -48,23 +58,47 @@ ENUM(status_names, SUCCESS, NEED_MORE,
 );
 
 /**
- * See header
+ * Described in header.
  */
-void utils_init()
+void* malloc_align(size_t size, u_int8_t align)
 {
-#ifdef WIN32
-       windows_init();
-#endif /* WIN32 */
+       u_int8_t pad;
+       void *ptr;
+
+       if (align == 0)
+       {
+               align = 1;
+       }
+       ptr = malloc(align + sizeof(pad) + size);
+       if (!ptr)
+       {
+               return NULL;
+       }
+       /* store padding length just before data, down to the allocation boundary
+        * to do some verification during free_align() */
+       pad = align - ((uintptr_t)ptr % align);
+       memset(ptr, pad, pad);
+       return ptr + pad;
 }
 
 /**
- * See header
+ * Described in header.
  */
-void utils_deinit()
+void free_align(void *ptr)
 {
-#ifdef WIN32
-       windows_deinit();
-#endif /* WIN32 */
+       u_int8_t pad, *pos;
+
+       pos = ptr - 1;
+       /* verify padding to check any corruption */
+       for (pad = *pos; (void*)pos >= ptr - pad; pos--)
+       {
+               if (*pos != pad)
+               {
+                       DBG1(DBG_LIB, "!!!! invalid free_align() !!!!");
+                       return;
+               }
+       }
+       free(ptr - pad);
 }
 
 /**
@@ -118,6 +152,25 @@ void memwipe_noinline(void *ptr, size_t n)
        memwipe_inline(ptr, n);
 }
 
+/**
+ * Described in header.
+ */
+bool memeq_const(const void *x, const void *y, size_t len)
+{
+       const u_char *a, *b;
+       u_int bad = 0;
+       size_t i;
+
+       a = (const u_char*)x;
+       b = (const u_char*)y;
+
+       for (i = 0; i < len; i++)
+       {
+               bad |= a[i] != b[i];
+       }
+       return !bad;
+}
+
 /**
  * Described in header.
  */
@@ -236,6 +289,84 @@ char* strreplace(const char *str, const char *search, const char *replace)
        return res;
 }
 
+#ifdef WIN32
+
+/**
+ * Flag to indicate signaled wait_sigint()
+ */
+static bool sigint_signaled = FALSE;
+
+/**
+ * Condvar to wait in wait_sigint()
+ */
+static condvar_t *sigint_cond;
+
+/**
+ * Mutex to check signaling()
+ */
+static mutex_t *sigint_mutex;
+
+/**
+ * Control handler to catch ^C
+ */
+static BOOL WINAPI handler(DWORD dwCtrlType)
+{
+       switch (dwCtrlType)
+       {
+               case CTRL_C_EVENT:
+               case CTRL_BREAK_EVENT:
+               case CTRL_CLOSE_EVENT:
+                       sigint_mutex->lock(sigint_mutex);
+                       sigint_signaled = TRUE;
+                       sigint_cond->signal(sigint_cond);
+                       sigint_mutex->unlock(sigint_mutex);
+                       return TRUE;
+               default:
+                       return FALSE;
+       }
+}
+
+/**
+ * Windows variant
+ */
+void wait_sigint()
+{
+       SetConsoleCtrlHandler(handler, TRUE);
+
+       sigint_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
+       sigint_cond = condvar_create(CONDVAR_TYPE_DEFAULT);
+
+       sigint_mutex->lock(sigint_mutex);
+       while (!sigint_signaled)
+       {
+               sigint_cond->wait(sigint_cond, sigint_mutex);
+       }
+       sigint_mutex->unlock(sigint_mutex);
+
+       sigint_mutex->destroy(sigint_mutex);
+       sigint_cond->destroy(sigint_cond);
+}
+
+#else /* !WIN32 */
+
+/**
+ * Unix variant
+ */
+void wait_sigint()
+{
+       sigset_t set;
+       int sig;
+
+       sigemptyset(&set);
+       sigaddset(&set, SIGINT);
+       sigaddset(&set, SIGTERM);
+
+       sigprocmask(SIG_BLOCK, &set, NULL);
+       sigwait(&set, &sig);
+}
+
+#endif
+
 /**
  * Described in header.
  */
@@ -243,21 +374,30 @@ char* path_dirname(const char *path)
 {
        char *pos;
 
-       pos = path ? strrchr(path, '/') : NULL;
+       pos = path ? strrchr(path, DIRECTORY_SEPARATOR[0]) : NULL;
 
        if (pos && !pos[1])
        {       /* if path ends with slashes we have to look beyond them */
-               while (pos > path && *pos == '/')
+               while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* skip trailing slashes */
                        pos--;
                }
-               pos = memrchr(path, '/', pos - path + 1);
+               pos = memrchr(path, DIRECTORY_SEPARATOR[0], pos - path + 1);
        }
        if (!pos)
        {
+#ifdef WIN32
+               if (path && strlen(path))
+               {
+                       if ((isalpha(path[0]) && path[1] == ':'))
+                       {       /* if just a drive letter given, return that as dirname */
+                               return chunk_clone(chunk_from_chars(path[0], ':', 0)).ptr;
+                       }
+               }
+#endif
                return strdup(".");
        }
-       while (pos > path && *pos == '/')
+       while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
        {       /* skip superfluous slashes */
                pos--;
        }
@@ -275,24 +415,51 @@ char* path_basename(const char *path)
        {
                return strdup(".");
        }
-       pos = strrchr(path, '/');
+       pos = strrchr(path, DIRECTORY_SEPARATOR[0]);
        if (pos && !pos[1])
        {       /* if path ends with slashes we have to look beyond them */
-               while (pos > path && *pos == '/')
+               while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* skip trailing slashes */
                        pos--;
                }
-               if (pos == path && *pos == '/')
+               if (pos == path && *pos == DIRECTORY_SEPARATOR[0])
                {       /* contains only slashes */
-                       return strdup("/");
+                       return strdup(DIRECTORY_SEPARATOR);
                }
                trail = pos + 1;
-               pos = memrchr(path, '/', trail - path);
+               pos = memrchr(path, DIRECTORY_SEPARATOR[0], trail - path);
        }
        pos = pos ? pos + 1 : (char*)path;
        return trail ? strndup(pos, trail - pos) : strdup(pos);
 }
 
+/**
+ * Described in header.
+ */
+bool path_absolute(const char *path)
+{
+       if (!path)
+       {
+               return FALSE;
+       }
+#ifdef WIN32
+       if (strpfx(path, "\\\\"))
+       {       /* UNC */
+               return TRUE;
+       }
+       if (strlen(path) && isalpha(path[0]) && path[1] == ':')
+       {       /* drive letter */
+               return TRUE;
+       }
+#else /* !WIN32 */
+       if (path[0] == DIRECTORY_SEPARATOR[0])
+       {
+               return TRUE;
+       }
+#endif
+       return FALSE;
+}
+
 /**
  * Described in header.
  */
@@ -462,6 +629,19 @@ void closefrom(int lowfd)
  */
 time_t time_monotonic(timeval_t *tv)
 {
+#ifdef WIN32
+       ULONGLONG ms;
+       time_t s;
+
+       ms = GetTickCount64();
+       s = ms / 1000;
+       if (tv)
+       {
+               tv->tv_sec = s;
+               tv->tv_usec = (ms - (s * 1000)) * 1000;
+       }
+       return s;
+#else /* !WIN32 */
 #if defined(HAVE_CLOCK_GETTIME) && \
        (defined(HAVE_CONDATTR_CLOCK_MONOTONIC) || \
         defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC))
@@ -493,6 +673,7 @@ time_t time_monotonic(timeval_t *tv)
                return -1;
        }
        return tv->tv_sec;
+#endif /* !WIN32 */
 }
 
 /**
@@ -542,76 +723,6 @@ void nop()
 {
 }
 
-#if !defined(HAVE_GCC_ATOMIC_OPERATIONS) && !defined(HAVE_GCC_SYNC_OPERATIONS)
-
-/**
- * We use a single mutex for all refcount variables.
- */
-static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-/**
- * Increase refcount
- */
-refcount_t ref_get(refcount_t *ref)
-{
-       refcount_t current;
-
-       pthread_mutex_lock(&ref_mutex);
-       current = ++(*ref);
-       pthread_mutex_unlock(&ref_mutex);
-       return current;
-}
-
-/**
- * Decrease refcount
- */
-bool ref_put(refcount_t *ref)
-{
-       bool more_refs;
-
-       pthread_mutex_lock(&ref_mutex);
-       more_refs = --(*ref) > 0;
-       pthread_mutex_unlock(&ref_mutex);
-       return !more_refs;
-}
-
-/**
- * Current refcount
- */
-refcount_t ref_cur(refcount_t *ref)
-{
-       refcount_t current;
-
-       pthread_mutex_lock(&ref_mutex);
-       current = *ref;
-       pthread_mutex_unlock(&ref_mutex);
-       return current;
-}
-
-/**
- * Single mutex for all compare and swap operations.
- */
-static pthread_mutex_t cas_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-/**
- * Compare and swap if equal to old value
- */
-#define _cas_impl(name, type) \
-bool cas_##name(type *ptr, type oldval, type newval) \
-{ \
-       bool swapped; \
-       pthread_mutex_lock(&cas_mutex); \
-       if ((swapped = (*ptr == oldval))) { *ptr = newval; } \
-       pthread_mutex_unlock(&cas_mutex); \
-       return swapped; \
-}
-
-_cas_impl(bool, bool)
-_cas_impl(ptr, void*)
-
-#endif /* !HAVE_GCC_ATOMIC_OPERATIONS && !HAVE_GCC_SYNC_OPERATIONS */
-
-
 #ifdef HAVE_FMEMOPEN_FALLBACK
 
 static int fmemread(chunk_t *cookie, char *buf, int size)
@@ -656,6 +767,30 @@ FILE *fmemopen(void *buf, size_t size, const char *mode)
 
 #endif /* FMEMOPEN fallback*/
 
+/**
+ * See header
+ */
+void utils_init()
+{
+#ifdef WIN32
+       windows_init();
+#endif
+       atomics_init();
+       strerror_init();
+}
+
+/**
+ * See header
+ */
+void utils_deinit()
+{
+#ifdef WIN32
+       windows_deinit();
+#endif
+       atomics_deinit();
+       strerror_deinit();
+}
+
 /**
  * Described in header.
  */