Whamcloud - gitweb
ee381d67a72fd523e65cf8ad2abdc2035fd4ad45
[fs/lustre-release.git] / libcfs / libcfs / user-prim.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/user-prim.c
37  *
38  * Implementations of portable APIs for liblustre
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 /*
44  * liblustre is single-threaded, so most "synchronization" APIs are trivial.
45  */
46
47 #ifndef __KERNEL__
48
49 #include <libcfs/libcfs.h>
50
51 /*
52  * Wait queue. No-op implementation.
53  */
54
55 void cfs_waitq_init(struct cfs_waitq *waitq)
56 {
57         LASSERT(waitq != NULL);
58         (void)waitq;
59 }
60
61 void cfs_waitlink_init(struct cfs_waitlink *link)
62 {
63         LASSERT(link != NULL);
64         (void)link;
65 }
66
67 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
68 {
69         LASSERT(waitq != NULL);
70         LASSERT(link != NULL);
71         (void)waitq;
72         (void)link;
73 }
74
75 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
76 {
77         LASSERT(waitq != NULL);
78         LASSERT(link != NULL);
79         (void)waitq;
80         (void)link;
81 }
82
83 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
84 {
85         LASSERT(waitq != NULL);
86         LASSERT(link != NULL);
87         (void)waitq;
88         (void)link;
89 }
90
91 int cfs_waitq_active(struct cfs_waitq *waitq)
92 {
93         LASSERT(waitq != NULL);
94         (void)waitq;
95         return 0;
96 }
97
98 void cfs_waitq_signal(struct cfs_waitq *waitq)
99 {
100         LASSERT(waitq != NULL);
101         (void)waitq;
102 }
103
104 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
105 {
106         LASSERT(waitq != NULL);
107         (void)waitq;
108 }
109
110 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
111 {
112         LASSERT(waitq != NULL);
113         (void)waitq;
114 }
115
116 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
117 {
118         LASSERT(link != NULL);
119         (void)link;
120 }
121
122 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state, 
123                             int64_t timeout)
124 {
125         LASSERT(link != NULL);
126         (void)link;
127         return 0;
128 }
129
130 void cfs_schedule_timeout(cfs_task_state_t state, int64_t timeout)
131 {
132         cfs_waitlink_t    l;  
133         /* sleep(timeout) here instead? */
134         cfs_waitq_timedwait(&l, state, timeout);
135 }
136
137 void 
138 cfs_pause(cfs_duration_t d)
139 {
140         struct timespec s;
141         
142         cfs_duration_nsec(d, &s);
143         nanosleep(&s, NULL);
144 }
145
146 /*
147  * Timer
148  */
149
150 void cfs_init_timer(cfs_timer_t *t)
151 {
152         CFS_INIT_LIST_HEAD(&t->tl_list);
153 }
154
155 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
156 {
157         CFS_INIT_LIST_HEAD(&l->tl_list);
158         l->function = func;
159         l->data = (ulong_ptr_t)arg;
160         return;
161 }
162
163 int cfs_timer_is_armed(cfs_timer_t *l)
164 {
165         if (cfs_time_before(cfs_time_current(), l->expires))
166                 return 1;
167         else
168                 return 0;
169 }
170
171 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
172 {
173         l->expires = deadline;
174 }
175
176 void cfs_timer_disarm(cfs_timer_t *l)
177 {
178 }
179 cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
180 {
181         return l->expires;
182 }
183
184
185 #ifdef HAVE_LIBPTHREAD
186
187 /*
188  * Threads
189  */
190
191 struct lustre_thread_arg {
192         cfs_thread_t f; 
193         void *arg;
194 };
195 static void *cfs_thread_helper(void *data)
196 {
197         struct lustre_thread_arg *targ = data;
198         cfs_thread_t f  = targ->f;
199         void *arg = targ->arg;
200
201         free(targ);
202         
203         (void)f(arg);
204         return NULL;
205 }
206 int cfs_create_thread(cfs_thread_t func, void *arg)
207 {
208         pthread_t tid;
209         pthread_attr_t tattr;
210         int rc;
211         struct lustre_thread_arg *targ_p = malloc(sizeof(struct lustre_thread_arg));
212
213         if ( targ_p == NULL )
214                 return -ENOMEM;
215         
216         targ_p->f = func;
217         targ_p->arg = arg;
218
219         pthread_attr_init(&tattr); 
220         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
221         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
222         pthread_attr_destroy(&tattr);
223         return -rc;
224 }
225 #endif
226
227 uid_t cfs_curproc_uid(void)
228 {
229         return getuid();
230 }
231
232 int cfs_parse_int_tunable(int *value, char *name)
233 {
234         char    *env = getenv(name);
235         char    *end;
236
237         if (env == NULL)
238                 return 0;
239
240         *value = (int)strtoull(env, &end, 0);
241         if (*end == 0)
242                 return 0;
243
244         CERROR("Can't parse tunable %s=%s\n", name, env);
245         return -EINVAL;
246 }
247
248
249 void cfs_enter_debugger(void)
250 {
251         /*
252          * nothing for now.
253          */
254 }
255
256 void cfs_daemonize(char *str)
257 {
258         return;
259 }
260
261 int cfs_daemonize_ctxt(char *str)
262 {
263         return 0;
264 }
265
266 cfs_sigset_t cfs_block_allsigs(void)
267 {
268         cfs_sigset_t   all;
269         cfs_sigset_t   old;
270         int            rc;
271
272         sigfillset(&all);
273         rc = sigprocmask(SIG_SETMASK, &all, &old);
274         LASSERT(rc == 0);
275
276         return old;
277 }
278
279 cfs_sigset_t cfs_block_sigs(cfs_sigset_t blocks)
280 {
281         cfs_sigset_t   old;
282         int   rc;
283         
284         rc = sigprocmask(SIG_SETMASK, &blocks, &old);
285         LASSERT (rc == 0);
286
287         return old;
288 }
289
290 void cfs_restore_sigs(cfs_sigset_t old)
291 {
292         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
293
294         LASSERT (rc == 0);
295 }
296
297 int cfs_signal_pending(void)
298 {
299         cfs_sigset_t    empty;
300         cfs_sigset_t    set;
301         int  rc;
302
303         rc = sigpending(&set);
304         LASSERT (rc == 0);
305
306         sigemptyset(&empty);
307
308         return !memcmp(&empty, &set, sizeof(set));
309 }
310
311 void cfs_clear_sigpending(void)
312 {
313         return;
314 }
315
316 #ifdef __linux__
317
318 /*
319  * In glibc (NOT in Linux, so check above is not right), implement
320  * stack-back-tracing through backtrace() function.
321  */
322 #include <execinfo.h>
323
324 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
325 {
326         backtrace(trace->frame, ARRAY_SIZE(trace->frame));
327 }
328
329 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
330 {
331         if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
332                 return trace->frame[frame_no];
333         else
334                 return NULL;
335 }
336
337 #else
338
339 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
340 {}
341 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
342 {
343         return NULL;
344 }
345
346 /* __linux__ */
347 #endif
348
349 void lbug_with_loc(const char *file, const char *func, const int line)
350 {
351         /* No libcfs_catastrophe in userspace! */
352         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line, "LBUG\n");
353         abort();
354 }
355
356 /* !__KERNEL__ */
357 #endif
358
359 /*
360  * Local variables:
361  * c-indentation-style: "K&R"
362  * c-basic-offset: 8
363  * tab-width: 8
364  * fill-column: 80
365  * scroll-step: 1
366  * End:
367  */