Whamcloud - gitweb
385bbefb8cf15ac36c399d3a91d6799a0afbf6bb
[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 #define DEBUG_SUBSYSTEM S_LNET
47
48 #include <libcfs/libcfs.h>
49
50 /*
51  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
52  * for Linux kernel.
53  */
54
55 uid_t  cfs_curproc_uid(void)
56 {
57         return current_uid();
58 }
59
60 gid_t  cfs_curproc_gid(void)
61 {
62         return current_gid();
63 }
64
65 uid_t  cfs_curproc_fsuid(void)
66 {
67         return current_fsuid();
68 }
69
70 uid_t  cfs_curproc_euid(void)
71 {
72         return current_egid();
73 }
74
75 uid_t  cfs_curproc_egid(void)
76 {
77         return current_egid();
78 }
79
80 gid_t  cfs_curproc_fsgid(void)
81 {
82         return current_fsgid();
83 }
84
85 pid_t  cfs_curproc_pid(void)
86 {
87         return current->pid;
88 }
89
90 int    cfs_curproc_groups_nr(void)
91 {
92         int nr;
93
94 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
95         task_lock(current);
96         nr = current_cred()->group_info->ngroups;
97         task_unlock(current);
98 #else
99         nr = current->ngroups;
100 #endif
101         return nr;
102 }
103
104 void   cfs_curproc_groups_dump(gid_t *array, int size)
105 {
106 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
107         task_lock(current);
108         size = min_t(int, size, current_cred()->group_info->ngroups);
109         memcpy(array, current_cred()->group_info->blocks[0], size * sizeof(__u32));
110         task_unlock(current);
111 #else
112         LASSERT(size <= NGROUPS);
113         size = min_t(int, size, current->ngroups);
114         memcpy(array, current->groups, size * sizeof(__u32));
115 #endif
116 }
117
118
119 int    cfs_curproc_is_in_groups(gid_t gid)
120 {
121         return in_group_p(gid);
122 }
123
124 mode_t cfs_curproc_umask(void)
125 {
126         return current->fs->umask;
127 }
128
129 char  *cfs_curproc_comm(void)
130 {
131         return current->comm;
132 }
133
134 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
135 #define cfs_cap_pack(cap) (cap)
136 #define cfs_cap_unpack(cap) (cap)
137
138 void cfs_cap_raise(cfs_cap_t cap)
139 {
140         struct cred *cred;
141         if ((cred = prepare_creds())) {
142                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
143                 commit_creds(cred);
144         }
145 }
146
147 void cfs_cap_lower(cfs_cap_t cap)
148 {
149         struct cred *cred;
150         if ((cred = prepare_creds())) {
151                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
152                 commit_creds(cred);
153         }
154 }
155
156 int cfs_cap_raised(cfs_cap_t cap)
157 {
158         return cap_raised(current_cap(), cfs_cap_unpack(cap));
159 }
160
161 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
162 {
163 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
164         *cap = cfs_cap_pack(kcap);
165 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
166         *cap = cfs_cap_pack(kcap[0]);
167 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
168         /* XXX lost high byte */
169         *cap = cfs_cap_pack(kcap.cap[0]);
170 #else
171         #error "need correct _KERNEL_CAPABILITY_VERSION "
172 #endif
173 }
174
175 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
176 {
177 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
178         *kcap = cfs_cap_unpack(cap);
179 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
180         (*kcap)[0] = cfs_cap_unpack(cap);
181 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
182         kcap->cap[0] = cfs_cap_unpack(cap);
183 #else
184         #error "need correct _KERNEL_CAPABILITY_VERSION "
185 #endif
186 }
187
188 cfs_cap_t cfs_curproc_cap_pack(void)
189 {
190         cfs_cap_t cap;
191         cfs_kernel_cap_pack(current_cap(), &cap);
192         return cap;
193 }
194
195 void cfs_curproc_cap_unpack(cfs_cap_t cap)
196 {
197         struct cred *cred;
198         if ((cred = prepare_creds())) {
199                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
200                 commit_creds(cred);
201         }
202 }
203
204 int cfs_capable(cfs_cap_t cap)
205 {
206         return capable(cfs_cap_unpack(cap));
207 }
208
209 EXPORT_SYMBOL(cfs_curproc_uid);
210 EXPORT_SYMBOL(cfs_curproc_pid);
211 EXPORT_SYMBOL(cfs_curproc_euid);
212 EXPORT_SYMBOL(cfs_curproc_gid);
213 EXPORT_SYMBOL(cfs_curproc_egid);
214 EXPORT_SYMBOL(cfs_curproc_fsuid);
215 EXPORT_SYMBOL(cfs_curproc_fsgid);
216 EXPORT_SYMBOL(cfs_curproc_umask);
217 EXPORT_SYMBOL(cfs_curproc_comm);
218 EXPORT_SYMBOL(cfs_curproc_groups_nr);
219 EXPORT_SYMBOL(cfs_curproc_groups_dump);
220 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
221 EXPORT_SYMBOL(cfs_cap_raise);
222 EXPORT_SYMBOL(cfs_cap_lower);
223 EXPORT_SYMBOL(cfs_cap_raised);
224 EXPORT_SYMBOL(cfs_curproc_cap_pack);
225 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
226 EXPORT_SYMBOL(cfs_capable);
227
228 /*
229  * Local variables:
230  * c-indentation-style: "K&R"
231  * c-basic-offset: 8
232  * tab-width: 8
233  * fill-column: 80
234  * scroll-step: 1
235  * End:
236  */