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 EXPORT_SYMBOL(cfs_curproc_uid);
217 EXPORT_SYMBOL(cfs_curproc_pid);
218 EXPORT_SYMBOL(cfs_curproc_euid);
219 EXPORT_SYMBOL(cfs_curproc_gid);
220 EXPORT_SYMBOL(cfs_curproc_egid);
221 EXPORT_SYMBOL(cfs_curproc_fsuid);
222 EXPORT_SYMBOL(cfs_curproc_fsgid);
223 EXPORT_SYMBOL(cfs_curproc_umask);
224 EXPORT_SYMBOL(cfs_curproc_comm);
225 EXPORT_SYMBOL(cfs_curproc_groups_nr);
226 EXPORT_SYMBOL(cfs_curproc_groups_dump);
227 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
228 EXPORT_SYMBOL(cfs_cap_raise);
229 EXPORT_SYMBOL(cfs_cap_lower);
230 EXPORT_SYMBOL(cfs_cap_raised);
231 EXPORT_SYMBOL(cfs_curproc_cap_pack);
232 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
233 EXPORT_SYMBOL(cfs_capable);
234 EXPORT_SYMBOL(cfs_curproc_is_32bit);
238 * c-indentation-style: "K&R"