Whamcloud - gitweb
9f2b7b38df054d6101022ffc580e30d19e204166
[fs/lustre-release.git] / libcfs / libcfs / user-prim.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 <string.h>
50 #include <libcfs/libcfs.h>
51
52 /*
53  * Wait queue. No-op implementation.
54  */
55
56 void cfs_waitq_init(struct cfs_waitq *waitq)
57 {
58         LASSERT(waitq != NULL);
59         (void)waitq;
60 }
61
62 void cfs_waitlink_init(struct cfs_waitlink *link)
63 {
64         LASSERT(link != NULL);
65         (void)link;
66 }
67
68 void cfs_waitq_add(struct cfs_waitq *waitq, struct cfs_waitlink *link)
69 {
70         LASSERT(waitq != NULL);
71         LASSERT(link != NULL);
72         (void)waitq;
73         (void)link;
74 }
75
76 void cfs_waitq_add_exclusive(struct cfs_waitq *waitq, struct cfs_waitlink *link)
77 {
78         LASSERT(waitq != NULL);
79         LASSERT(link != NULL);
80         (void)waitq;
81         (void)link;
82 }
83
84 void cfs_waitq_add_exclusive_head(struct cfs_waitq *waitq, struct cfs_waitlink *link)
85 {
86         cfs_waitq_add_exclusive(waitq, link);
87 }
88
89 void cfs_waitq_del(struct cfs_waitq *waitq, struct cfs_waitlink *link)
90 {
91         LASSERT(waitq != NULL);
92         LASSERT(link != NULL);
93         (void)waitq;
94         (void)link;
95 }
96
97 int cfs_waitq_active(struct cfs_waitq *waitq)
98 {
99         LASSERT(waitq != NULL);
100         (void)waitq;
101         return 0;
102 }
103
104 void cfs_waitq_signal(struct cfs_waitq *waitq)
105 {
106         LASSERT(waitq != NULL);
107         (void)waitq;
108 }
109
110 void cfs_waitq_signal_nr(struct cfs_waitq *waitq, int nr)
111 {
112         LASSERT(waitq != NULL);
113         (void)waitq;
114 }
115
116 void cfs_waitq_broadcast(struct cfs_waitq *waitq)
117 {
118         LASSERT(waitq != NULL);
119         (void)waitq;
120 }
121
122 void cfs_waitq_wait(struct cfs_waitlink *link, cfs_task_state_t state)
123 {
124         LASSERT(link != NULL);
125         (void)link;
126
127         /* well, wait for something to happen */
128         call_wait_handler(0);
129 }
130
131 int64_t cfs_waitq_timedwait(struct cfs_waitlink *link, cfs_task_state_t state,
132                             int64_t timeout)
133 {
134         LASSERT(link != NULL);
135         (void)link;
136         call_wait_handler(timeout);
137         return 0;
138 }
139
140 void cfs_schedule_timeout_and_set_state(cfs_task_state_t state, int64_t timeout)
141 {
142         cfs_waitlink_t    l;
143         /* sleep(timeout) here instead? */
144         cfs_waitq_timedwait(&l, state, timeout);
145 }
146
147 void
148 cfs_pause(cfs_duration_t d)
149 {
150         struct timespec s;
151
152         cfs_duration_nsec(d, &s);
153         nanosleep(&s, NULL);
154 }
155
156 int cfs_need_resched(void)
157 {
158         return 0;
159 }
160
161 void cfs_cond_resched(void)
162 {
163 }
164
165 /*
166  * Timer
167  */
168
169 void cfs_init_timer(cfs_timer_t *t)
170 {
171         CFS_INIT_LIST_HEAD(&t->tl_list);
172 }
173
174 void cfs_timer_init(cfs_timer_t *l, cfs_timer_func_t *func, void *arg)
175 {
176         CFS_INIT_LIST_HEAD(&l->tl_list);
177         l->function = func;
178         l->data = (ulong_ptr_t)arg;
179         return;
180 }
181
182 int cfs_timer_is_armed(cfs_timer_t *l)
183 {
184         if (cfs_time_before(cfs_time_current(), l->expires))
185                 return 1;
186         else
187                 return 0;
188 }
189
190 void cfs_timer_arm(cfs_timer_t *l, cfs_time_t deadline)
191 {
192         l->expires = deadline;
193 }
194
195 void cfs_timer_disarm(cfs_timer_t *l)
196 {
197 }
198 cfs_time_t cfs_timer_deadline(cfs_timer_t *l)
199 {
200         return l->expires;
201 }
202
203
204 #ifdef HAVE_LIBPTHREAD
205
206 /*
207  * Threads
208  */
209
210 struct lustre_thread_arg {
211         cfs_thread_t f;
212         void *arg;
213 };
214 static void *cfs_thread_helper(void *data)
215 {
216         struct lustre_thread_arg *targ = data;
217         cfs_thread_t f  = targ->f;
218         void *arg = targ->arg;
219
220         free(targ);
221
222         (void)f(arg);
223         return NULL;
224 }
225
226 void *kthread_run(cfs_thread_t func, void *arg, const char namefmt[], ...)
227 {
228         pthread_t tid;
229         pthread_attr_t tattr;
230         int rc;
231         struct lustre_thread_arg *targ_p =
232                                 malloc(sizeof(struct lustre_thread_arg));
233
234         if (targ_p == NULL)
235                 return ERR_PTR(-ENOMEM);
236
237         targ_p->f = func;
238         targ_p->arg = arg;
239
240         pthread_attr_init(&tattr);
241         pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
242         rc = pthread_create(&tid, &tattr, cfs_thread_helper, targ_p);
243         pthread_attr_destroy(&tattr);
244         return ERR_PTR(rc);
245 }
246 #endif
247
248 uid_t cfs_curproc_uid(void)
249 {
250         return getuid();
251 }
252
253 gid_t cfs_curproc_gid(void)
254 {
255         return getgid();
256 }
257
258 uid_t cfs_curproc_fsuid(void)
259 {
260         return getuid();
261 }
262
263 gid_t cfs_curproc_fsgid(void)
264 {
265         return getgid();
266 }
267
268 #ifndef HAVE_STRLCPY /* not in glibc for RHEL 5.x, remove when obsolete */
269 size_t strlcpy(char *tgt, const char *src, size_t tgt_len)
270 {
271         int src_len = strlen(src);
272
273         strncpy(tgt, src, tgt_len - 1);
274         tgt[tgt_len - 1] = '\0';
275
276         return src_len + 1;
277 }
278 #endif
279
280 #ifndef HAVE_STRLCAT /* not in glibc for RHEL 5.x, remove when obsolete */
281 size_t strlcat(char *tgt, const char *src, size_t size)
282 {
283         size_t tgt_len = strlen(tgt);
284
285         if (size > tgt_len) {
286                 strncat(tgt, src, size - tgt_len - 1);
287                 tgt[size - 1] = '\0';
288         }
289
290         return tgt_len + strlen(src);
291 }
292 #endif
293
294 /* Read the environment variable of current process specified by @key. */
295 int cfs_get_environ(const char *key, char *value, int *val_len)
296 {
297         char *entry;
298         int len;
299
300         entry = getenv(key);
301         if (entry == NULL)
302                 return -ENOENT;
303
304         len = strlcpy(value, entry, *val_len);
305         if (len >= *val_len)
306                 return -EOVERFLOW;
307
308         return 0;
309 }
310
311 void cfs_enter_debugger(void)
312 {
313         /*
314          * nothing for now.
315          */
316 }
317
318 int unshare_fs_struct()
319 {
320         return 0;
321 }
322
323 cfs_sigset_t cfs_block_allsigs(void)
324 {
325         cfs_sigset_t   all;
326         cfs_sigset_t   old;
327         int            rc;
328
329         sigfillset(&all);
330         rc = sigprocmask(SIG_BLOCK, &all, &old);
331         LASSERT(rc == 0);
332
333         return old;
334 }
335
336 cfs_sigset_t cfs_block_sigs(unsigned long sigs)
337 {
338         cfs_sigset_t   old;
339         cfs_sigset_t   blocks = { { sigs } }; /* kludge */
340         int   rc;
341
342         rc = sigprocmask(SIG_BLOCK, &blocks, &old);
343         LASSERT (rc == 0);
344
345         return old;
346 }
347
348 /* Block all signals except for the @sigs. It's only used in
349  * Linux kernel, just a dummy here. */
350 cfs_sigset_t cfs_block_sigsinv(unsigned long sigs)
351 {
352         cfs_sigset_t old;
353         int rc;
354
355         /* Return old blocked sigs */
356         rc = sigprocmask(SIG_SETMASK, NULL, &old);
357         LASSERT(rc == 0);
358
359         return old;
360 }
361
362 void cfs_restore_sigs(cfs_sigset_t old)
363 {
364         int   rc = sigprocmask(SIG_SETMASK, &old, NULL);
365
366         LASSERT (rc == 0);
367 }
368
369 int cfs_signal_pending(void)
370 {
371         cfs_sigset_t    empty;
372         cfs_sigset_t    set;
373         int  rc;
374
375         rc = sigpending(&set);
376         LASSERT (rc == 0);
377
378         sigemptyset(&empty);
379
380         return !memcmp(&empty, &set, sizeof(set));
381 }
382
383 void cfs_clear_sigpending(void)
384 {
385         return;
386 }
387
388 #ifdef __linux__
389
390 /*
391  * In glibc (NOT in Linux, so check above is not right), implement
392  * stack-back-tracing through backtrace() function.
393  */
394 #include <execinfo.h>
395
396 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
397 {
398         backtrace(trace->frame, ARRAY_SIZE(trace->frame));
399 }
400
401 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
402 {
403         if (0 <= frame_no && frame_no < ARRAY_SIZE(trace->frame))
404                 return trace->frame[frame_no];
405         else
406                 return NULL;
407 }
408
409 #else
410
411 void cfs_stack_trace_fill(struct cfs_stack_trace *trace)
412 {}
413 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no)
414 {
415         return NULL;
416 }
417
418 /* __linux__ */
419 #endif
420
421 void lbug_with_loc(struct libcfs_debug_msg_data *msgdata)
422 {
423         /* No libcfs_catastrophe in userspace! */
424         libcfs_debug_msg(msgdata, "LBUG\n");
425         abort();
426 }
427
428 /* !__KERNEL__ */
429 #endif
430
431 /*
432  * Local variables:
433  * c-indentation-style: "K&R"
434  * c-basic-offset: 8
435  * tab-width: 8
436  * fill-column: 80
437  * scroll-step: 1
438  * End:
439  */