Whamcloud - gitweb
4bfcca3792ac808dc82d6a4ad8af067d2627feca
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-curproc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/linux/linux-curproc.c
37  *
38  * Lustre curproc API implementation for Linux kernel
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  */
42
43 #include <linux/sched.h>
44 #include <linux/fs_struct.h>
45
46 #include <linux/compat.h>
47 #include <linux/thread_info.h>
48
49 #define DEBUG_SUBSYSTEM S_LNET
50
51 #include <libcfs/libcfs.h>
52
53 /*
54  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
55  * for Linux kernel.
56  */
57
58 uid_t  cfs_curproc_uid(void)
59 {
60         return current_uid();
61 }
62
63 gid_t  cfs_curproc_gid(void)
64 {
65         return current_gid();
66 }
67
68 uid_t  cfs_curproc_fsuid(void)
69 {
70         return current_fsuid();
71 }
72
73 uid_t  cfs_curproc_euid(void)
74 {
75         return current_euid();
76 }
77
78 uid_t  cfs_curproc_egid(void)
79 {
80         return current_egid();
81 }
82
83 gid_t  cfs_curproc_fsgid(void)
84 {
85         return current_fsgid();
86 }
87
88 pid_t  cfs_curproc_pid(void)
89 {
90         return current->pid;
91 }
92
93 int    cfs_curproc_groups_nr(void)
94 {
95         int nr;
96
97 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
98         task_lock(current);
99         nr = current_cred()->group_info->ngroups;
100         task_unlock(current);
101 #else
102         nr = current->ngroups;
103 #endif
104         return nr;
105 }
106
107 void   cfs_curproc_groups_dump(gid_t *array, int size)
108 {
109 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
110         task_lock(current);
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);
114 #else
115         LASSERT(size <= NGROUPS);
116         size = min_t(int, size, current->ngroups);
117         memcpy(array, current->groups, size * sizeof(__u32));
118 #endif
119 }
120
121
122 int    cfs_curproc_is_in_groups(gid_t gid)
123 {
124         return in_group_p(gid);
125 }
126
127 mode_t cfs_curproc_umask(void)
128 {
129         return current->fs->umask;
130 }
131
132 char  *cfs_curproc_comm(void)
133 {
134         return current->comm;
135 }
136
137 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
138 #define cfs_cap_pack(cap) (cap)
139 #define cfs_cap_unpack(cap) (cap)
140
141 void cfs_cap_raise(cfs_cap_t cap)
142 {
143         struct cred *cred;
144         if ((cred = prepare_creds())) {
145                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
146                 commit_creds(cred);
147         }
148 }
149
150 void cfs_cap_lower(cfs_cap_t cap)
151 {
152         struct cred *cred;
153         if ((cred = prepare_creds())) {
154                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
155                 commit_creds(cred);
156         }
157 }
158
159 int cfs_cap_raised(cfs_cap_t cap)
160 {
161         return cap_raised(current_cap(), cfs_cap_unpack(cap));
162 }
163
164 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
165 {
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]);
173 #else
174         #error "need correct _KERNEL_CAPABILITY_VERSION "
175 #endif
176 }
177
178 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
179 {
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);
186 #else
187         #error "need correct _KERNEL_CAPABILITY_VERSION "
188 #endif
189 }
190
191 cfs_cap_t cfs_curproc_cap_pack(void)
192 {
193         cfs_cap_t cap;
194         cfs_kernel_cap_pack(current_cap(), &cap);
195         return cap;
196 }
197
198 void cfs_curproc_cap_unpack(cfs_cap_t cap)
199 {
200         struct cred *cred;
201         if ((cred = prepare_creds())) {
202                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
203                 commit_creds(cred);
204         }
205 }
206
207 int cfs_capable(cfs_cap_t cap)
208 {
209         return capable(cfs_cap_unpack(cap));
210 }
211
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)
220 {
221 #ifdef HAVE_IS_COMPAT_TASK
222         return is_compat_task();
223 #else
224         return (BITS_PER_LONG == 32);
225 #endif
226 }
227
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);
247
248 /*
249  * Local variables:
250  * c-indentation-style: "K&R"
251  * c-basic-offset: 8
252  * tab-width: 8
253  * fill-column: 80
254  * scroll-step: 1
255  * End:
256  */