summaryrefslogtreecommitdiff
path: root/lib/util
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util')
-rw-r--r--lib/util/smb_threads.c105
-rw-r--r--lib/util/smb_threads.h120
-rw-r--r--lib/util/smb_threads_internal.h52
-rw-r--r--lib/util/util.h38
-rw-r--r--lib/util/util_strlist.c141
5 files changed, 450 insertions, 6 deletions
diff --git a/lib/util/smb_threads.c b/lib/util/smb_threads.c
new file mode 100644
index 0000000000..84dec4d874
--- /dev/null
+++ b/lib/util/smb_threads.c
@@ -0,0 +1,105 @@
+/*
+ Unix SMB/CIFS implementation.
+ SMB client library implementation (thread interface functions).
+ Copyright (C) Jeremy Allison, 2009.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+ * This code is based in the ideas in openssl
+ * but somewhat simpler and expended to include
+ * thread local storage.
+ */
+
+#include "includes.h"
+
+#define NUM_GLOBAL_LOCKS 1
+
+/*********************************************************
+ Functions to vector the locking primitives used internally
+ by libsmbclient.
+*********************************************************/
+
+const struct smb_thread_functions *global_tfp;
+
+/*********************************************************
+ Dynamic lock array.
+*********************************************************/
+
+void **global_lock_array;
+
+/*********************************************************
+ Function to set the locking primitives used by libsmbclient.
+*********************************************************/
+
+int smb_thread_set_functions(const struct smb_thread_functions *tf)
+{
+ int i;
+
+ global_tfp = tf;
+
+ /* Here we initialize any static locks we're using. */
+ global_lock_array = (void **)SMB_MALLOC_ARRAY(void *, NUM_GLOBAL_LOCKS);
+ if (global_lock_array == NULL) {
+ return ENOMEM;
+ }
+
+ for (i = 0; i < NUM_GLOBAL_LOCKS; i++) {
+ char *name = NULL;
+ if (asprintf(&name, "global_lock_%d", i) == -1) {
+ SAFE_FREE(global_lock_array);
+ return ENOMEM;
+ }
+ global_tfp->create_mutex(name,
+ &global_lock_array[i],
+ __location__);
+ SAFE_FREE(name);
+ }
+
+ return 0;
+}
+
+#if 0
+/* Test. - pthread implementations. */
+#include <pthread.h>
+
+#ifdef malloc
+#undef malloc
+#endif
+
+SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf);
+
+/* Test function. */
+int test_threads(void)
+{
+ int ret;
+ void *plock = NULL;
+
+ smb_thread_set_functions(&tf);
+
+ if ((ret = SMB_THREAD_CREATE_MUTEX("test", plock)) != 0) {
+ printf("Create lock error: %d\n", ret);
+ }
+ if ((ret = SMB_THREAD_LOCK(plock, SMB_THREAD_LOCK)) != 0) {
+ printf("lock error: %d\n", ret);
+ }
+ if ((SMB_THREAD_LOCK(plock, SMB_THREAD_UNLOCK)) != 0) {
+ printf("unlock error: %d\n", ret);
+ }
+ SMB_THREAD_DESTROY_MUTEX(plock);
+
+ return 0;
+}
+#endif
diff --git a/lib/util/smb_threads.h b/lib/util/smb_threads.h
new file mode 100644
index 0000000000..2ca163be9a
--- /dev/null
+++ b/lib/util/smb_threads.h
@@ -0,0 +1,120 @@
+/*
+ Unix SMB/CIFS implementation.
+ SMB thread interface functions.
+ Copyright (C) Jeremy Allison, 2009.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _smb_threads_h_
+#define _smb_threads_h_
+
+enum smb_thread_lock_type {
+ SMB_THREAD_LOCK = 1,
+ SMB_THREAD_UNLOCK
+};
+
+struct smb_thread_functions {
+ /* Mutex and tls functions. */
+ int (*create_mutex)(const char *lockname,
+ void **pplock,
+ const char *location);
+ void (*destroy_mutex)(void *plock,
+ const char *location);
+ int (*lock_mutex)(void *plock, enum smb_thread_lock_type lock_type,
+ const char *location);
+
+ /* Thread local storage. */
+ int (*create_tls)(const char *keyname,
+ void **ppkey,
+ const char *location);
+ void (*destroy_tls)(void *pkey,
+ const char *location);
+ int (*set_tls)(void *pkey, const void *pval, const char *location);
+ void *(*get_tls)(void *pkey, const char *location);
+};
+
+extern const struct smb_thread_functions *global_tfp;
+
+/* Define the pthread version of the functions. */
+
+#define SMB_THREADS_DEF_PTHREAD_IMPLEMENTATION(tf) \
+ \
+static int smb_create_mutex_pthread(const char *lockname, void **pplock, const char *location) \
+{ \
+ pthread_mutex_t *pmut = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t)); \
+ if (!pmut) { \
+ return ENOMEM; \
+ } \
+ pthread_mutex_init(pmut, NULL); \
+ *pplock = (void *)pmut; \
+ return 0; \
+} \
+ \
+static void smb_destroy_mutex_pthread(void *plock, const char *location) \
+{ \
+ pthread_mutex_destroy((pthread_mutex_t *)plock); \
+ free(plock); \
+} \
+ \
+static int smb_lock_pthread(void *plock, enum smb_thread_lock_type lock_type, const char *location) \
+{ \
+ if (lock_type == SMB_THREAD_UNLOCK) { \
+ return pthread_mutex_unlock((pthread_mutex_t *)plock); \
+ } else { \
+ return pthread_mutex_lock((pthread_mutex_t *)plock); \
+ } \
+} \
+ \
+static int smb_create_tls_pthread(const char *keyname, void **ppkey, const char *location) \
+{ \
+ int ret; \
+ pthread_key_t *pkey = (pthread_key_t *)malloc(sizeof(pthread_key_t)); \
+ if (!pkey) { \
+ return ENOMEM; \
+ } \
+ ret = pthread_key_create(pkey, NULL); \
+ if (ret) { \
+ return ret; \
+ } \
+ *ppkey = (void *)pkey; \
+ return 0; \
+} \
+ \
+static void smb_destroy_tls_pthread(void *pkey, const char *location) \
+{ \
+ pthread_key_delete(*(pthread_key_t *)pkey); \
+ free(pkey); \
+} \
+ \
+static int smb_set_tls_pthread(void *pkey, const void *pval, const char *location) \
+{ \
+ return pthread_setspecific(*(pthread_key_t *)pkey, pval); \
+} \
+ \
+static void *smb_get_tls_pthread(void *pkey, const char *location) \
+{ \
+ return pthread_getspecific(*(pthread_key_t *)pkey); \
+} \
+ \
+static const struct smb_thread_functions (tf) = { \
+ smb_create_mutex_pthread, \
+ smb_destroy_mutex_pthread, \
+ smb_lock_pthread, \
+ smb_create_tls_pthread, \
+ smb_destroy_tls_pthread, \
+ smb_set_tls_pthread, \
+ smb_get_tls_pthread }
+
+#endif
diff --git a/lib/util/smb_threads_internal.h b/lib/util/smb_threads_internal.h
new file mode 100644
index 0000000000..3208bc27e1
--- /dev/null
+++ b/lib/util/smb_threads_internal.h
@@ -0,0 +1,52 @@
+/*
+ SMB/CIFS implementation.
+ SMB thread interface internal macros.
+ Copyright (C) Jeremy Allison, 2009.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _smb_threads_internal_h_
+#define _smb_threads_internal_h_
+
+#define SMB_THREAD_CREATE_MUTEX(name, lockvar) \
+ (global_tfp ? global_tfp->create_mutex((name), &(lockvar), __location__) : 0)
+
+#define SMB_THREAD_DESTROY_MUTEX(plock) \
+ do { \
+ if (global_tfp) { \
+ global_tfp->destroy_mutex(plock, __location__); \
+ }; \
+ } while (0)
+
+#define SMB_THREAD_LOCK(plock, type) \
+ (global_tfp ? global_tfp->lock_mutex((plock), (type), __location__) : 0)
+
+#define SMB_THREAD_CREATE_TLS(keyname, key) \
+ (global_tfp ? global_tfp->create_tls((keyname), &(key), __location__) : 0)
+
+#define SMB_THREAD_DESTROY_TLS(key) \
+ do { \
+ if (global_tfp) { \
+ global_tfp->destroy_tls(key); \
+ }; \
+ } while (0)
+
+#define SMB_THREAD_SET_TLS(key, val) \
+ (global_tfp ? global_tfp->set_tls((key),(val),__location__) : 0)
+
+#define SMB_THREAD_GET_TLS(key) \
+ (global_tfp ? global_tfp->get_tls((key), __location__) : NULL)
+
+#endif
diff --git a/lib/util/util.h b/lib/util/util.h
index defef127d9..81c7edfbdf 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -422,7 +422,7 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char
/**
return the number of elements in a string list
*/
-_PUBLIC_ size_t str_list_length(const char * const *list);
+_PUBLIC_ size_t str_list_length(const char **list);
/**
copy a string list
@@ -437,7 +437,7 @@ _PUBLIC_ bool str_list_equal(const char **list1, const char **list2);
/**
add an entry to a string list
*/
-_PUBLIC_ const char **str_list_add(const char **list, const char *s);
+_PUBLIC_ char **str_list_add(char **list, const char *s);
/**
remove an entry from a string list
@@ -453,6 +453,40 @@ _PUBLIC_ bool str_list_check(const char **list, const char *s);
return true if a string is in a list, case insensitively
*/
_PUBLIC_ bool str_list_check_ci(const char **list, const char *s);
+/**
+ append one list to another - expanding list1
+*/
+_PUBLIC_ char **str_list_append(char **list1, const char **list2);
+
+/**
+ remove duplicate elements from a list
+*/
+_PUBLIC_ char **str_list_unique(char **list);
+
+/*
+ very useful when debugging complex list related code
+ */
+_PUBLIC_ void str_list_show(const char **list);
+
+
+/**
+ append one list to another - expanding list1
+ this assumes the elements of list2 are const pointers, so we can re-use them
+*/
+_PUBLIC_ char **str_list_append_const(char **list1, const char **list2);
+
+/**
+ add an entry to a string list
+ this assumes s will not change
+*/
+_PUBLIC_ char **str_list_add_const(char **list, const char *s);
+
+/**
+ copy a string list
+ this assumes list will not change
+*/
+_PUBLIC_ char **str_list_copy_const(TALLOC_CTX *mem_ctx, const char **list);
+
/* The following definitions come from lib/util/util_file.c */
diff --git a/lib/util/util_strlist.c b/lib/util/util_strlist.c
index b069a11e38..bd7bd00888 100644
--- a/lib/util/util_strlist.c
+++ b/lib/util/util_strlist.c
@@ -187,7 +187,7 @@ _PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char
/**
return the number of elements in a string list
*/
-_PUBLIC_ size_t str_list_length(const char * const*list)
+_PUBLIC_ size_t str_list_length(const char **list)
{
size_t ret;
for (ret=0;list && list[ret];ret++) /* noop */ ;
@@ -247,12 +247,12 @@ _PUBLIC_ bool str_list_equal(const char **list1, const char **list2)
/**
add an entry to a string list
*/
-_PUBLIC_ const char **str_list_add(const char **list, const char *s)
+_PUBLIC_ char **str_list_add(char **list, const char *s)
{
size_t len = str_list_length(list);
- const char **ret;
+ char **ret;
- ret = talloc_realloc(NULL, list, const char *, len+2);
+ ret = talloc_realloc(NULL, list, char *, len+2);
if (ret == NULL) return NULL;
ret[len] = talloc_strdup(ret, s);
@@ -308,3 +308,136 @@ _PUBLIC_ bool str_list_check_ci(const char **list, const char *s)
}
+/**
+ append one list to another - expanding list1
+*/
+_PUBLIC_ char **str_list_append(char **list1, const char **list2)
+{
+ size_t len1 = str_list_length(list1);
+ size_t len2 = str_list_length(list2);
+ char **ret;
+ int i;
+
+ ret = talloc_realloc(NULL, list1, char *, len1+len2+1);
+ if (ret == NULL) return NULL;
+
+ for (i=len1;i<len1+len2;i++) {
+ ret[i] = talloc_strdup(ret, list2[i-len1]);
+ if (ret[i] == NULL) {
+ return NULL;
+ }
+ }
+ ret[i] = NULL;
+
+ return ret;
+}
+
+static int list_cmp(const char **el1, const char **el2)
+{
+ return strcmp(*el1, *el2);
+}
+
+/*
+ return a list that only contains the unique elements of a list,
+ removing any duplicates
+ */
+_PUBLIC_ char **str_list_unique(char **list)
+{
+ size_t len = str_list_length(list);
+ char **list2;
+ int i, j;
+ if (len < 2) {
+ return list;
+ }
+ list2 = (char **)talloc_memdup(list, list, sizeof(list[0])*(len+1));
+ qsort(list2, len, sizeof(list2[0]), QSORT_CAST list_cmp);
+ list[0] = list2[0];
+ for (i=j=1;i<len;i++) {
+ if (strcmp(list2[i], list[j-1]) != 0) {
+ list[j] = list2[i];
+ j++;
+ }
+ }
+ list[j] = NULL;
+ list = talloc_realloc(NULL, list, char *, j);
+ talloc_free(list2);
+ return list;
+}
+
+/*
+ very useful when debugging complex list related code
+ */
+_PUBLIC_ void str_list_show(const char **list)
+{
+ int i;
+ DEBUG(0,("{ "));
+ for (i=0;list && list[i];i++) {
+ DEBUG(0,("\"%s\", ", list[i]));
+ }
+ DEBUG(0,("}\n"));
+}
+
+
+
+/**
+ append one list to another - expanding list1
+ this assumes the elements of list2 are const pointers, so we can re-use them
+*/
+_PUBLIC_ char **str_list_append_const(char **list1, const char **list2)
+{
+ size_t len1 = str_list_length(list1);
+ size_t len2 = str_list_length(list2);
+ char **ret;
+ int i;
+
+ ret = talloc_realloc(NULL, list1, char *, len1+len2+1);
+ if (ret == NULL) return NULL;
+
+ for (i=len1;i<len1+len2;i++) {
+ ret[i] = list2[i-len1];
+ }
+ ret[i] = NULL;
+
+ return ret;
+}
+
+/**
+ add an entry to a string list
+ this assumes s will not change
+*/
+_PUBLIC_ char **str_list_add_const(char **list, const char *s)
+{
+ size_t len = str_list_length(list);
+ char **ret;
+
+ ret = talloc_realloc(NULL, list, char *, len+2);
+ if (ret == NULL) return NULL;
+
+ ret[len] = s;
+ ret[len+1] = NULL;
+
+ return ret;
+}
+
+/**
+ copy a string list
+ this assumes list will not change
+*/
+_PUBLIC_ char **str_list_copy_const(TALLOC_CTX *mem_ctx, const char **list)
+{
+ int i;
+ char **ret;
+
+ if (list == NULL)
+ return NULL;
+
+ ret = talloc_array(mem_ctx, char *, str_list_length(list)+1);
+ if (ret == NULL)
+ return NULL;
+
+ for (i=0;list && list[i];i++) {
+ ret[i] = list[i];
+ }
+ ret[i] = NULL;
+ return ret;
+}