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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * libcfs/libcfs/linux/linux-curproc.c
36 * Lustre curproc API implementation for Linux kernel
38 * Author: Nikita Danilov <nikita@clusterfs.com>
41 #include <linux/sched.h>
42 #include <linux/fs_struct.h>
44 #include <linux/compat.h>
45 #include <linux/thread_info.h>
47 #define DEBUG_SUBSYSTEM S_LNET
49 #include <libcfs/libcfs.h>
52 * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
56 uid_t cfs_curproc_uid(void)
61 gid_t cfs_curproc_gid(void)
66 uid_t cfs_curproc_fsuid(void)
68 return current_fsuid();
71 uid_t cfs_curproc_euid(void)
73 return current_euid();
76 uid_t cfs_curproc_egid(void)
78 return current_egid();
81 gid_t cfs_curproc_fsgid(void)
83 return current_fsgid();
86 pid_t cfs_curproc_pid(void)
91 int cfs_curproc_groups_nr(void)
96 nr = current_cred()->group_info->ngroups;
101 void cfs_curproc_groups_dump(gid_t *array, int size)
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);
110 int cfs_curproc_is_in_groups(gid_t gid)
112 return in_group_p(gid);
115 mode_t cfs_curproc_umask(void)
117 return current->fs->umask;
120 char *cfs_curproc_comm(void)
122 return current->comm;
125 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
126 #define cfs_cap_pack(cap) (cap)
127 #define cfs_cap_unpack(cap) (cap)
129 void cfs_cap_raise(cfs_cap_t cap)
132 if ((cred = prepare_creds())) {
133 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
138 void cfs_cap_lower(cfs_cap_t cap)
141 if ((cred = prepare_creds())) {
142 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
147 int cfs_cap_raised(cfs_cap_t cap)
149 return cap_raised(current_cap(), cfs_cap_unpack(cap));
152 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
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]);
162 #error "need correct _KERNEL_CAPABILITY_VERSION "
166 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
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);
175 #error "need correct _KERNEL_CAPABILITY_VERSION "
179 cfs_cap_t cfs_curproc_cap_pack(void)
182 cfs_kernel_cap_pack(current_cap(), &cap);
186 void cfs_curproc_cap_unpack(cfs_cap_t cap)
189 if ((cred = prepare_creds())) {
190 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
195 int cfs_capable(cfs_cap_t cap)
197 return capable(cfs_cap_unpack(cap));
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)
209 #ifdef HAVE_IS_COMPAT_TASK
210 return is_compat_task();
212 return (BITS_PER_LONG == 32);
216 static int cfs_access_process_vm(struct task_struct *tsk, unsigned long addr,
217 void *buf, int len, int write)
219 #ifdef HAVE_ACCESS_PROCESS_VM
220 return access_process_vm(tsk, addr, buf, len, write);
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;
229 mm = get_task_mm(tsk);
233 down_read(&mm->mmap_sem);
234 /* ignore errors, just check how much was sucessfully transfered */
236 int bytes, ret, offset;
239 ret = get_user_pages(tsk, mm, addr, 1,
240 write, 1, &page, &vma);
245 offset = addr & (PAGE_SIZE-1);
246 if (bytes > PAGE_SIZE-offset)
247 bytes = PAGE_SIZE-offset;
251 copy_to_user_page(vma, page, addr,
252 maddr + offset, buf, bytes);
253 set_page_dirty_lock(page);
255 copy_from_user_page(vma, page, addr,
256 buf, maddr + offset, bytes);
259 page_cache_release(page);
264 up_read(&mm->mmap_sem);
267 return buf - old_buf;
268 #endif /* HAVE_ACCESS_PROCESS_VM */
271 /* Read the environment variable of current process specified by @key. */
272 int cfs_get_environ(const char *key, char *value, int *val_len)
274 struct mm_struct *mm;
275 char *buffer, *tmp_buf = NULL;
276 int buf_len = CFS_PAGE_SIZE;
281 buffer = (char *)cfs_alloc(buf_len, CFS_ALLOC_USER);
285 mm = get_task_mm(current);
287 cfs_free((void *)buffer);
291 addr = mm->env_start;
294 while (addr < mm->env_end) {
295 int this_len, retval, scan_len;
296 char *env_start, *env_end;
298 memset(buffer, 0, buf_len);
300 this_len = min((int)(mm->env_end - addr), buf_len);
301 retval = cfs_access_process_vm(current, addr, buffer,
303 if (retval != this_len)
308 /* Parse the buffer to find out the specified key/value pair.
309 * The "key=value" entries are separated by '\0'. */
316 env_end = (char *)memscan(env_start, '\0', scan_len);
317 LASSERT(env_end >= env_start &&
318 env_end <= env_start + scan_len);
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");
334 entry_len = env_end - env_start;
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);
348 memcpy(value, entry, entry_len);
349 *val_len = entry_len;
355 scan_len -= (env_end - env_start + 1);
356 env_start = env_end + 1;
361 cfs_free((void *)buffer);
363 cfs_free((void *)tmp_buf);
366 EXPORT_SYMBOL(cfs_get_environ);
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);
390 * c-indentation-style: "K&R"