1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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)
97 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
99 nr = current_cred()->group_info->ngroups;
100 task_unlock(current);
102 nr = current->ngroups;
107 void cfs_curproc_groups_dump(gid_t *array, int size)
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
111 size = min_t(int, size, current_cred()->group_info->ngroups);
112 memcpy(array, current_cred()->group_info->blocks[0], size * sizeof(__u32));
113 task_unlock(current);
115 LASSERT(size <= NGROUPS);
116 size = min_t(int, size, current->ngroups);
117 memcpy(array, current->groups, size * sizeof(__u32));
122 int cfs_curproc_is_in_groups(gid_t gid)
124 return in_group_p(gid);
127 mode_t cfs_curproc_umask(void)
129 return current->fs->umask;
132 char *cfs_curproc_comm(void)
134 return current->comm;
137 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
138 #define cfs_cap_pack(cap) (cap)
139 #define cfs_cap_unpack(cap) (cap)
141 void cfs_cap_raise(cfs_cap_t cap)
144 if ((cred = prepare_creds())) {
145 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
150 void cfs_cap_lower(cfs_cap_t cap)
153 if ((cred = prepare_creds())) {
154 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
159 int cfs_cap_raised(cfs_cap_t cap)
161 return cap_raised(current_cap(), cfs_cap_unpack(cap));
164 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
166 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
167 *cap = cfs_cap_pack(kcap);
168 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
169 *cap = cfs_cap_pack(kcap[0]);
170 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
171 /* XXX lost high byte */
172 *cap = cfs_cap_pack(kcap.cap[0]);
174 #error "need correct _KERNEL_CAPABILITY_VERSION "
178 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
180 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
181 *kcap = cfs_cap_unpack(cap);
182 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
183 (*kcap)[0] = cfs_cap_unpack(cap);
184 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
185 kcap->cap[0] = cfs_cap_unpack(cap);
187 #error "need correct _KERNEL_CAPABILITY_VERSION "
191 cfs_cap_t cfs_curproc_cap_pack(void)
194 cfs_kernel_cap_pack(current_cap(), &cap);
198 void cfs_curproc_cap_unpack(cfs_cap_t cap)
201 if ((cred = prepare_creds())) {
202 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
207 int cfs_capable(cfs_cap_t cap)
209 return capable(cfs_cap_unpack(cap));
212 /* Check if task is running in 32-bit API mode, for the purpose of
213 * userspace binary interfaces. On 32-bit Linux this is (unfortunately)
214 * always true, even if the application is using LARGEFILE64 and 64-bit
215 * APIs, because Linux provides no way for the filesystem to know if it
216 * is called via 32-bit or 64-bit APIs. Other clients may vary. On
217 * 64-bit systems, this will only be true if the binary is calling a
218 * 32-bit system call. */
219 int cfs_curproc_is_32bit(void)
221 #ifdef HAVE_IS_COMPAT_TASK
222 return is_compat_task();
224 return (BITS_PER_LONG == 32);
228 EXPORT_SYMBOL(cfs_curproc_uid);
229 EXPORT_SYMBOL(cfs_curproc_pid);
230 EXPORT_SYMBOL(cfs_curproc_euid);
231 EXPORT_SYMBOL(cfs_curproc_gid);
232 EXPORT_SYMBOL(cfs_curproc_egid);
233 EXPORT_SYMBOL(cfs_curproc_fsuid);
234 EXPORT_SYMBOL(cfs_curproc_fsgid);
235 EXPORT_SYMBOL(cfs_curproc_umask);
236 EXPORT_SYMBOL(cfs_curproc_comm);
237 EXPORT_SYMBOL(cfs_curproc_groups_nr);
238 EXPORT_SYMBOL(cfs_curproc_groups_dump);
239 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
240 EXPORT_SYMBOL(cfs_cap_raise);
241 EXPORT_SYMBOL(cfs_cap_lower);
242 EXPORT_SYMBOL(cfs_cap_raised);
243 EXPORT_SYMBOL(cfs_curproc_cap_pack);
244 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
245 EXPORT_SYMBOL(cfs_capable);
246 EXPORT_SYMBOL(cfs_curproc_is_32bit);
250 * c-indentation-style: "K&R"