Whamcloud - gitweb
LU-694 ptlrpc: Job Stats
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-curproc.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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/libcfs/linux/linux-curproc.c
35  *
36  * Lustre curproc API implementation for Linux kernel
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #include <linux/sched.h>
42 #include <linux/fs_struct.h>
43
44 #include <linux/compat.h>
45 #include <linux/thread_info.h>
46
47 #define DEBUG_SUBSYSTEM S_LNET
48
49 #include <libcfs/libcfs.h>
50
51 /*
52  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
53  * for Linux kernel.
54  */
55
56 uid_t  cfs_curproc_uid(void)
57 {
58         return current_uid();
59 }
60
61 gid_t  cfs_curproc_gid(void)
62 {
63         return current_gid();
64 }
65
66 uid_t  cfs_curproc_fsuid(void)
67 {
68         return current_fsuid();
69 }
70
71 uid_t  cfs_curproc_euid(void)
72 {
73         return current_euid();
74 }
75
76 uid_t  cfs_curproc_egid(void)
77 {
78         return current_egid();
79 }
80
81 gid_t  cfs_curproc_fsgid(void)
82 {
83         return current_fsgid();
84 }
85
86 pid_t  cfs_curproc_pid(void)
87 {
88         return current->pid;
89 }
90
91 int    cfs_curproc_groups_nr(void)
92 {
93         int nr;
94
95         task_lock(current);
96         nr = current_cred()->group_info->ngroups;
97         task_unlock(current);
98         return nr;
99 }
100
101 void   cfs_curproc_groups_dump(gid_t *array, int size)
102 {
103         task_lock(current);
104         size = min_t(int, size, current_cred()->group_info->ngroups);
105         memcpy(array, current_cred()->group_info->blocks[0], size * sizeof(__u32));
106         task_unlock(current);
107 }
108
109
110 int    cfs_curproc_is_in_groups(gid_t gid)
111 {
112         return in_group_p(gid);
113 }
114
115 mode_t cfs_curproc_umask(void)
116 {
117         return current->fs->umask;
118 }
119
120 char  *cfs_curproc_comm(void)
121 {
122         return current->comm;
123 }
124
125 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
126 #define cfs_cap_pack(cap) (cap)
127 #define cfs_cap_unpack(cap) (cap)
128
129 void cfs_cap_raise(cfs_cap_t cap)
130 {
131         struct cred *cred;
132         if ((cred = prepare_creds())) {
133                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
134                 commit_creds(cred);
135         }
136 }
137
138 void cfs_cap_lower(cfs_cap_t cap)
139 {
140         struct cred *cred;
141         if ((cred = prepare_creds())) {
142                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
143                 commit_creds(cred);
144         }
145 }
146
147 int cfs_cap_raised(cfs_cap_t cap)
148 {
149         return cap_raised(current_cap(), cfs_cap_unpack(cap));
150 }
151
152 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
153 {
154 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
155         *cap = cfs_cap_pack(kcap);
156 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
157         *cap = cfs_cap_pack(kcap[0]);
158 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
159         /* XXX lost high byte */
160         *cap = cfs_cap_pack(kcap.cap[0]);
161 #else
162         #error "need correct _KERNEL_CAPABILITY_VERSION "
163 #endif
164 }
165
166 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
167 {
168 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
169         *kcap = cfs_cap_unpack(cap);
170 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
171         (*kcap)[0] = cfs_cap_unpack(cap);
172 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
173         kcap->cap[0] = cfs_cap_unpack(cap);
174 #else
175         #error "need correct _KERNEL_CAPABILITY_VERSION "
176 #endif
177 }
178
179 cfs_cap_t cfs_curproc_cap_pack(void)
180 {
181         cfs_cap_t cap;
182         cfs_kernel_cap_pack(current_cap(), &cap);
183         return cap;
184 }
185
186 void cfs_curproc_cap_unpack(cfs_cap_t cap)
187 {
188         struct cred *cred;
189         if ((cred = prepare_creds())) {
190                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
191                 commit_creds(cred);
192         }
193 }
194
195 int cfs_capable(cfs_cap_t cap)
196 {
197         return capable(cfs_cap_unpack(cap));
198 }
199
200 /* Check if task is running in 32-bit API mode, for the purpose of
201  * userspace binary interfaces.  On 32-bit Linux this is (unfortunately)
202  * always true, even if the application is using LARGEFILE64 and 64-bit
203  * APIs, because Linux provides no way for the filesystem to know if it
204  * is called via 32-bit or 64-bit APIs.  Other clients may vary.  On
205  * 64-bit systems, this will only be true if the binary is calling a
206  * 32-bit system call. */
207 int cfs_curproc_is_32bit(void)
208 {
209 #ifdef HAVE_IS_COMPAT_TASK
210         return is_compat_task();
211 #else
212         return (BITS_PER_LONG == 32);
213 #endif
214 }
215
216 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
217                                  void *buf, int len, int write)
218 {
219 #ifdef HAVE_ACCESS_PROCESS_VM
220         return access_process_vm(tsk, addr, buf, len, write);
221 #else
222         /* Just copied from kernel for the kernels which doesn't
223          * have access_process_vm() exported */
224         struct mm_struct *mm;
225         struct vm_area_struct *vma;
226         struct page *page;
227         void *old_buf = buf;
228
229         mm = get_task_mm(tsk);
230         if (!mm)
231                 return 0;
232
233         down_read(&mm->mmap_sem);
234         /* ignore errors, just check how much was sucessfully transfered */
235         while (len) {
236                 int bytes, ret, offset;
237                 void *maddr;
238
239                 ret = get_user_pages(tsk, mm, addr, 1,
240                                      write, 1, &page, &vma);
241                 if (ret <= 0)
242                         break;
243
244                 bytes = len;
245                 offset = addr & (PAGE_SIZE-1);
246                 if (bytes > PAGE_SIZE-offset)
247                         bytes = PAGE_SIZE-offset;
248
249                 maddr = kmap(page);
250                 if (write) {
251                         copy_to_user_page(vma, page, addr,
252                                           maddr + offset, buf, bytes);
253                         set_page_dirty_lock(page);
254                 } else {
255                         copy_from_user_page(vma, page, addr,
256                                             buf, maddr + offset, bytes);
257                 }
258                 kunmap(page);
259                 page_cache_release(page);
260                 len -= bytes;
261                 buf += bytes;
262                 addr += bytes;
263         }
264         up_read(&mm->mmap_sem);
265         mmput(mm);
266
267         return buf - old_buf;
268 #endif /* HAVE_ACCESS_PROCESS_VM */
269 }
270
271 /* Read the environment variable of current process specified by @key. */
272 int cfs_get_environ(const char *key, char *value, int *val_len)
273 {
274         struct mm_struct *mm;
275         char *buffer, *tmp_buf = NULL;
276         int buf_len = CFS_PAGE_SIZE;
277         unsigned long addr;
278         int ret;
279         ENTRY;
280
281         buffer = (char *)cfs_alloc(buf_len, CFS_ALLOC_USER);
282         if (!buffer)
283                 RETURN(-ENOMEM);
284
285         mm = get_task_mm(current);
286         if (!mm) {
287                 cfs_free((void *)buffer);
288                 RETURN(-EINVAL);
289         }
290
291         addr = mm->env_start;
292         ret = -ENOENT;
293
294         while (addr < mm->env_end) {
295                 int this_len, retval, scan_len;
296                 char *env_start, *env_end;
297
298                 memset(buffer, 0, buf_len);
299
300                 this_len = min((int)(mm->env_end - addr), buf_len);
301                 retval = cfs_access_process_vm(current, addr, buffer,
302                                                this_len, 0);
303                 if (retval != this_len)
304                         break;
305
306                 addr += retval;
307
308                 /* Parse the buffer to find out the specified key/value pair.
309                  * The "key=value" entries are separated by '\0'. */
310                 env_start = buffer;
311                 scan_len = this_len;
312                 while (scan_len) {
313                         char *entry;
314                         int entry_len;
315
316                         env_end = (char *)memscan(env_start, '\0', scan_len);
317                         LASSERT(env_end >= env_start &&
318                                 env_end <= env_start + scan_len);
319
320                         /* The last entry of this buffer cross the buffer
321                          * boundary, reread it in next cycle. */
322                         if (unlikely(env_end - env_start == scan_len)) {
323                                 /* This entry is too large to fit in buffer */
324                                 if (unlikely(scan_len == this_len)) {
325                                         CERROR("Too long env variable.\n");
326                                         ret = -EINVAL;
327                                         goto out;
328                                 }
329                                 addr -= scan_len;
330                                 break;
331                         }
332
333                         entry = env_start;
334                         entry_len = env_end - env_start;
335
336                         /* Key length + length of '=' */
337                         if (entry_len > strlen(key) + 1 &&
338                             !memcmp(entry, key, strlen(key))) {
339                                 entry += (strlen(key) + 1);
340                                 entry_len -= (strlen(key) + 1);
341                                 /* The 'value' buffer passed in is too small.*/
342                                 if (entry_len >= *val_len) {
343                                         CERROR("Buffer is too small. "
344                                                "entry_len=%d buffer_len=%d\n",
345                                                entry_len, *val_len);
346                                         ret = -EOVERFLOW;
347                                 } else {
348                                         memcpy(value, entry, entry_len);
349                                         *val_len = entry_len;
350                                         ret = 0;
351                                 }
352                                 goto out;
353                         }
354
355                         scan_len -= (env_end - env_start + 1);
356                         env_start = env_end + 1;
357                 }
358         }
359 out:
360         mmput(mm);
361         cfs_free((void *)buffer);
362         if (tmp_buf)
363                 cfs_free((void *)tmp_buf);
364         RETURN(ret);
365 }
366 EXPORT_SYMBOL(cfs_get_environ);
367
368 EXPORT_SYMBOL(cfs_curproc_uid);
369 EXPORT_SYMBOL(cfs_curproc_pid);
370 EXPORT_SYMBOL(cfs_curproc_euid);
371 EXPORT_SYMBOL(cfs_curproc_gid);
372 EXPORT_SYMBOL(cfs_curproc_egid);
373 EXPORT_SYMBOL(cfs_curproc_fsuid);
374 EXPORT_SYMBOL(cfs_curproc_fsgid);
375 EXPORT_SYMBOL(cfs_curproc_umask);
376 EXPORT_SYMBOL(cfs_curproc_comm);
377 EXPORT_SYMBOL(cfs_curproc_groups_nr);
378 EXPORT_SYMBOL(cfs_curproc_groups_dump);
379 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
380 EXPORT_SYMBOL(cfs_cap_raise);
381 EXPORT_SYMBOL(cfs_cap_lower);
382 EXPORT_SYMBOL(cfs_cap_raised);
383 EXPORT_SYMBOL(cfs_curproc_cap_pack);
384 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
385 EXPORT_SYMBOL(cfs_capable);
386 EXPORT_SYMBOL(cfs_curproc_is_32bit);
387
388 /*
389  * Local variables:
390  * c-indentation-style: "K&R"
391  * c-basic-offset: 8
392  * tab-width: 8
393  * fill-column: 80
394  * scroll-step: 1
395  * End:
396  */