4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/linux/linux-curproc.c
38 * Lustre curproc API implementation for Linux kernel
40 * Author: Nikita Danilov <nikita@clusterfs.com>
43 #include <linux/sched.h>
44 #include <linux/fs_struct.h>
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
49 #define DEBUG_SUBSYSTEM S_LNET
51 #include <libcfs/libcfs.h>
54 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
58 uid_t cfs_curproc_uid(void)
63 gid_t cfs_curproc_gid(void)
68 uid_t cfs_curproc_fsuid(void)
70 return current_fsuid();
73 uid_t cfs_curproc_euid(void)
75 return current_euid();
78 uid_t cfs_curproc_egid(void)
80 return current_egid();
83 gid_t cfs_curproc_fsgid(void)
85 return current_fsgid();
88 pid_t cfs_curproc_pid(void)
93 int cfs_curproc_groups_nr(void)
98 nr = current_cred()->group_info->ngroups;
103 void cfs_curproc_groups_dump(gid_t *array, int size)
106 size = min_t(int, size, current_cred()->group_info->ngroups);
107 memcpy(array, current_cred()->group_info->blocks[0], size * sizeof(__u32));
108 task_unlock(current);
112 int cfs_curproc_is_in_groups(gid_t gid)
114 return in_group_p(gid);
117 mode_t cfs_curproc_umask(void)
119 return current->fs->umask;
122 char *cfs_curproc_comm(void)
124 return current->comm;
127 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
128 #define cfs_cap_pack(cap) (cap)
129 #define cfs_cap_unpack(cap) (cap)
131 void cfs_cap_raise(cfs_cap_t cap)
134 if ((cred = prepare_creds())) {
135 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
140 void cfs_cap_lower(cfs_cap_t cap)
143 if ((cred = prepare_creds())) {
144 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
149 int cfs_cap_raised(cfs_cap_t cap)
151 return cap_raised(current_cap(), cfs_cap_unpack(cap));
154 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
156 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
157 *cap = cfs_cap_pack(kcap);
158 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
159 *cap = cfs_cap_pack(kcap[0]);
160 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
161 /* XXX lost high byte */
162 *cap = cfs_cap_pack(kcap.cap[0]);
164 #error "need correct _KERNEL_CAPABILITY_VERSION "
168 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
170 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
171 *kcap = cfs_cap_unpack(cap);
172 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
173 (*kcap)[0] = cfs_cap_unpack(cap);
174 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
175 kcap->cap[0] = cfs_cap_unpack(cap);
177 #error "need correct _KERNEL_CAPABILITY_VERSION "
181 cfs_cap_t cfs_curproc_cap_pack(void)
184 cfs_kernel_cap_pack(current_cap(), &cap);
188 void cfs_curproc_cap_unpack(cfs_cap_t cap)
191 if ((cred = prepare_creds())) {
192 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
197 int cfs_capable(cfs_cap_t cap)
199 return capable(cfs_cap_unpack(cap));
202 /* Check if task is running in 32-bit API mode, for the purpose of
203 * userspace binary interfaces. On 32-bit Linux this is (unfortunately)
204 * always true, even if the application is using LARGEFILE64 and 64-bit
205 * APIs, because Linux provides no way for the filesystem to know if it
206 * is called via 32-bit or 64-bit APIs. Other clients may vary. On
207 * 64-bit systems, this will only be true if the binary is calling a
208 * 32-bit system call. */
209 int cfs_curproc_is_32bit(void)
211 #ifdef HAVE_IS_COMPAT_TASK
212 return is_compat_task();
214 return (BITS_PER_LONG == 32);
218 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
219 void *buf, int len, int write)
221 #ifdef HAVE_ACCESS_PROCESS_VM
222 return access_process_vm(tsk, addr, buf, len, write);
224 /* Just copied from kernel for the kernels which doesn't
225 * have access_process_vm() exported */
226 struct mm_struct *mm;
227 struct vm_area_struct *vma;
231 mm = get_task_mm(tsk);
235 down_read(&mm->mmap_sem);
236 /* ignore errors, just check how much was sucessfully transfered */
238 int bytes, rc, offset;
241 rc = get_user_pages(tsk, mm, addr, 1,
242 write, 1, &page, &vma);
247 offset = addr & (PAGE_SIZE-1);
248 if (bytes > PAGE_SIZE-offset)
249 bytes = PAGE_SIZE-offset;
253 copy_to_user_page(vma, page, addr,
254 maddr + offset, buf, bytes);
255 set_page_dirty_lock(page);
257 copy_from_user_page(vma, page, addr,
258 buf, maddr + offset, bytes);
261 page_cache_release(page);
266 up_read(&mm->mmap_sem);
269 return buf - old_buf;
270 #endif /* HAVE_ACCESS_PROCESS_VM */
273 /* Read the environment variable of current process specified by @key. */
274 int cfs_get_environ(const char *key, char *value, int *val_len)
276 struct mm_struct *mm;
277 char *buffer, *tmp_buf = NULL;
278 int buf_len = CFS_PAGE_SIZE;
279 int key_len = strlen(key);
284 buffer = cfs_alloc(buf_len, CFS_ALLOC_USER);
288 mm = get_task_mm(current);
294 /* Avoid deadlocks on mmap_sem if called from sys_mmap_pgoff(),
295 * which is already holding mmap_sem for writes. If some other
296 * thread gets the write lock in the meantime, this thread will
297 * block, but at least it won't deadlock on itself. LU-1735 */
298 if (down_read_trylock(&mm->mmap_sem) == 0)
300 up_read(&mm->mmap_sem);
302 addr = mm->env_start;
303 while (addr < mm->env_end) {
304 int this_len, retval, scan_len;
305 char *env_start, *env_end;
307 memset(buffer, 0, buf_len);
309 this_len = min_t(int, mm->env_end - addr, buf_len);
310 retval = cfs_access_process_vm(current, addr, buffer,
312 if (retval != this_len)
317 /* Parse the buffer to find out the specified key/value pair.
318 * The "key=value" entries are separated by '\0'. */
325 env_end = memscan(env_start, '\0', scan_len);
326 LASSERT(env_end >= env_start &&
327 env_end <= env_start + scan_len);
329 /* The last entry of this buffer cross the buffer
330 * boundary, reread it in next cycle. */
331 if (unlikely(env_end - env_start == scan_len)) {
332 /* This entry is too large to fit in buffer */
333 if (unlikely(scan_len == this_len)) {
334 CERROR("Too long env variable.\n");
335 GOTO(out, rc = -EINVAL);
342 entry_len = env_end - env_start;
344 /* Key length + length of '=' */
345 if (entry_len > key_len + 1 &&
346 !memcmp(entry, key, key_len)) {
347 entry += key_len + 1;
348 entry_len -= key_len + 1;
349 /* The 'value' buffer passed in is too small.*/
350 if (entry_len >= *val_len)
351 GOTO(out, rc = -EOVERFLOW);
353 memcpy(value, entry, entry_len);
354 *val_len = entry_len;
358 scan_len -= (env_end - env_start + 1);
359 env_start = env_end + 1;
362 GOTO(out, rc = -ENOENT);
366 cfs_free((void *)buffer);
368 cfs_free((void *)tmp_buf);
371 EXPORT_SYMBOL(cfs_get_environ);
373 EXPORT_SYMBOL(cfs_curproc_uid);
374 EXPORT_SYMBOL(cfs_curproc_pid);
375 EXPORT_SYMBOL(cfs_curproc_euid);
376 EXPORT_SYMBOL(cfs_curproc_gid);
377 EXPORT_SYMBOL(cfs_curproc_egid);
378 EXPORT_SYMBOL(cfs_curproc_fsuid);
379 EXPORT_SYMBOL(cfs_curproc_fsgid);
380 EXPORT_SYMBOL(cfs_curproc_umask);
381 EXPORT_SYMBOL(cfs_curproc_comm);
382 EXPORT_SYMBOL(cfs_curproc_groups_nr);
383 EXPORT_SYMBOL(cfs_curproc_groups_dump);
384 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
385 EXPORT_SYMBOL(cfs_cap_raise);
386 EXPORT_SYMBOL(cfs_cap_lower);
387 EXPORT_SYMBOL(cfs_cap_raised);
388 EXPORT_SYMBOL(cfs_curproc_cap_pack);
389 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
390 EXPORT_SYMBOL(cfs_capable);
391 EXPORT_SYMBOL(cfs_curproc_is_32bit);
395 * c-indentation-style: "K&R"