Whamcloud - gitweb
LU-1855 build: fix 'out-of-bounds access' errors
[fs/lustre-release.git] / libcfs / libcfs / user-prim.c
index 07678e0..0cc1e3d 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -46,6 +46,7 @@
 
 #ifndef __KERNEL__
 
+#include <string.h>
 #include <libcfs/libcfs.h>
 
 /*
@@ -80,6 +81,11 @@ void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
         (void)link;
 }
 
+void cfs_waitq_add_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
+{
+        cfs_waitq_add_exclusive(waitq, link);
+}
+
 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
 {
         LASSERT(waitq != NULL);
@@ -117,32 +123,45 @@ void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
 {
         LASSERT(link != NULL);
         (void)link;
+
+        /* well, wait for something to happen */
+        cfs_call_wait_handler(0);
 }
 
-int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state, 
+int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
                             int64_t timeout)
 {
         LASSERT(link != NULL);
         (void)link;
+        cfs_call_wait_handler(timeout);
         return 0;
 }
 
-void cfs_schedule_timeout(cfs_task_state_t state, int64_t timeout)
+void cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
 {
-        cfs_waitlink_t    l;  
+        cfs_waitlink_t    l;
         /* sleep(timeout) here instead? */
         cfs_waitq_timedwait(&l, state, timeout);
 }
 
-void 
+void
 cfs_pause(cfs_duration_t d)
 {
         struct timespec s;
-        
+
         cfs_duration_nsec(d, &s);
         nanosleep(&s, NULL);
 }
 
+int cfs_need_resched(void)
+{
+        return 0;
+}
+
+void cfs_cond_resched(void)
+{
+}
+
 /*
  * Timer
  */
@@ -156,22 +175,13 @@ void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
 {
         CFS_INIT_LIST_HEAD(&l->tl_list);
         l->function = func;
-        l->data = (unsigned long)arg;
+        l->data = (ulong_ptr_t)arg;
         return;
 }
 
-#define cfs_jiffies                             \
-({                                              \
-        unsigned long _ret = 0;                 \
-        struct timeval tv;                      \
-        if (gettimeofday(&tv, NULL) == 0)       \
-                _ret = tv.tv_sec;               \
-        _ret;                                   \
-})
-
 int cfs_timer_is_armed(cfs_timer_t *l)
 {
-        if (cfs_time_before(cfs_jiffies, l->expires))
+        if (cfs_time_before(cfs_time_current(), l->expires))
                 return 1;
         else
                 return 0;
@@ -185,8 +195,7 @@ void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
 void cfs_timer_disarm(cfs_timer_t *l)
 {
 }
-
-long cfs_timer_deadline(cfs_timer_t *l)
+cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
 {
         return l->expires;
 }
@@ -199,7 +208,7 @@ long cfs_timer_deadline(cfs_timer_t *l)
  */
 
 struct lustre_thread_arg {
-        cfs_thread_t f; 
+        cfs_thread_t f;
         void *arg;
 };
 static void *cfs_thread_helper(void *data)
@@ -209,11 +218,12 @@ static void *cfs_thread_helper(void *data)
         void *arg = targ->arg;
 
         free(targ);
-        
+
         (void)f(arg);
         return NULL;
 }
-int cfs_create_thread(cfs_thread_t func, void *arg)
+
+int cfs_create_thread(cfs_thread_t func, void *arg, unsigned long flags)
 {
         pthread_t tid;
         pthread_attr_t tattr;
@@ -222,11 +232,11 @@ int cfs_create_thread(cfs_thread_t func, void *arg)
 
         if ( targ_p == NULL )
                 return -ENOMEM;
-        
+
         targ_p->f = func;
         targ_p->arg = arg;
 
-        pthread_attr_init(&tattr); 
+        pthread_attr_init(&tattr);
         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
         pthread_attr_destroy(&tattr);
@@ -239,22 +249,63 @@ uid_t cfs_curproc_uid(void)
         return getuid();
 }
 
-int cfs_parse_int_tunable(int *value, char *name)
+gid_t cfs_curproc_gid(void)
 {
-        char    *env = getenv(name);
-        char    *end;
+        return getgid();
+}
 
-        if (env == NULL)
-                return 0;
+uid_t cfs_curproc_fsuid(void)
+{
+        return getuid();
+}
 
-        *value = strtoull(env, &end, 0);
-        if (*end == 0)
-                return 0;
+gid_t cfs_curproc_fsgid(void)
+{
+        return getgid();
+}
+
+#ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
+size_t strlcpy(char *tgt, const char *src, size_t tgt_len)
+{
+       int src_len = strlen(src);
+
+       strncpy(tgt, src, tgt_len - 1);
+       tgt[tgt_len - 1] = '\0';
+
+       return src_len + 1;
+}
+#endif
 
-        CERROR("Can't parse tunable %s=%s\n", name, env);
-        return -EINVAL;
+#ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
+size_t strlcat(char *tgt, const char *src, size_t size)
+{
+       size_t tgt_len = strlen(tgt);
+
+       if (size > tgt_len) {
+               strncat(tgt, src, size - tgt_len - 1);
+               tgt[size - 1] = '\0';
+       }
+
+       return tgt_len + strlen(src);
 }
+#endif
+
+/* Read the environment variable of current process specified by @key. */
+int cfs_get_environ(const char *key, char *value, int *val_len)
+{
+       char *entry;
+       int len;
+
+       entry = getenv(key);
+       if (entry == NULL)
+               return -ENOENT;
 
+       len = strlcpy(value, entry, *val_len);
+       if (len >= *val_len)
+               return -EOVERFLOW;
+
+       return 0;
+}
 
 void cfs_enter_debugger(void)
 {
@@ -275,24 +326,39 @@ int cfs_daemonize_ctxt(char *str)
 
 cfs_sigset_t cfs_block_allsigs(void)
 {
-        cfs_sigset_t   all;
-        cfs_sigset_t   old;
-        int            rc;
+       cfs_sigset_t   all;
+       cfs_sigset_t   old;
+       int            rc;
 
-        sigfillset(&all);
-        rc = sigprocmask(SIG_SETMASK, &all, &old);
-        LASSERT(rc == 0);
+       sigfillset(&all);
+       rc = sigprocmask(SIG_BLOCK, &all, &old);
+       LASSERT(rc == 0);
 
-        return old;
+       return old;
 }
 
-cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
+cfs_sigset_t cfs_block_sigs(unsigned long sigs)
 {
-        cfs_sigset_t   old;
-        int   rc;
-        
-        rc = sigprocmask(SIG_SETMASK, &blocks, &old);
-        LASSERT (rc == 0);
+       cfs_sigset_t   old;
+       cfs_sigset_t   blocks = { { sigs } }; /* kludge */
+       int   rc;
+
+       rc = sigprocmask(SIG_BLOCK, &blocks, &old);
+       LASSERT (rc == 0);
+
+       return old;
+}
+
+/* Block all signals except for the @sigs. It's only used in
+ * Linux kernel, just a dummy here. */
+cfs_sigset_t cfs_block_sigsinv(unsigned long sigs)
+{
+        cfs_sigset_t old;
+        int rc;
+
+        /* Return old blocked sigs */
+        rc = sigprocmask(SIG_SETMASK, NULL, &old);
+        LASSERT(rc == 0);
 
         return old;
 }
@@ -356,10 +422,10 @@ void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
 /* __linux__ */
 #endif
 
-void lbug_with_loc(const char *file, const char *func, const int line)
+void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
 {
         /* No libcfs_catastrophe in userspace! */
-        libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
+        libcfs_debug_msg(msgdata, "LBUG\n");
         abort();
 }