Whamcloud - gitweb
Branch b1_8
[fs/lustre-release.git] / lnet / 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  * lnet/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
45 #define DEBUG_SUBSYSTEM S_LNET
46
47 #include <libcfs/libcfs.h>
48 #include <libcfs/kp30.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 gid_t  cfs_curproc_fsgid(void)
71 {
72         return current->fsgid;
73 }
74
75 pid_t  cfs_curproc_pid(void)
76 {
77         return current->pid;
78 }
79
80 int    cfs_curproc_groups_nr(void)
81 {
82         int nr;
83
84 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
85         task_lock(current);
86         nr = current->group_info->ngroups;
87         task_unlock(current);
88 #else
89         nr = current->ngroups;
90 #endif
91         return nr;
92 }
93
94 void   cfs_curproc_groups_dump(gid_t *array, int size)
95 {
96 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,4)
97         task_lock(current);
98         size = min_t(int, size, current->group_info->ngroups);
99         memcpy(array, current->group_info->blocks[0], size * sizeof(__u32));
100         task_unlock(current);
101 #else
102         LASSERT(size <= NGROUPS);
103         size = min_t(int, size, current->ngroups);
104         memcpy(array, current->groups, size * sizeof(__u32));
105 #endif
106 }
107
108
109 int    cfs_curproc_is_in_groups(gid_t gid)
110 {
111         return in_group_p(gid);
112 }
113
114 mode_t cfs_curproc_umask(void)
115 {
116         return current->fs->umask;
117 }
118
119 char  *cfs_curproc_comm(void)
120 {
121         return current->comm;
122 }
123
124 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
125 #define cfs_cap_pack(cap) (cap)
126 #define cfs_cap_unpack(cap) (cap)
127
128 void cfs_cap_raise(cfs_cap_t cap)
129 {
130         cap_raise(cfs_current()->cap_effective, cfs_cap_unpack(cap));
131 }
132
133 void cfs_cap_lower(cfs_cap_t cap)
134 {
135         cap_lower(cfs_current()->cap_effective, cfs_cap_unpack(cap));
136 }
137
138 int cfs_cap_raised(cfs_cap_t cap)
139 {
140         return cap_raised(cfs_current()->cap_effective, cfs_cap_unpack(cap));
141 }
142
143 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
144 {
145 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
146         *cap = cfs_cap_pack(kcap);
147 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
148         *cap = cfs_cap_pack(kcap[0]);
149 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
150         /* XXX lost high byte */
151         *cap = cfs_cap_pack(kcap.cap[0]);
152 #else
153         #error "need correct _KERNEL_CAPABILITY_VERSION "
154 #endif
155 }
156
157 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
158 {
159 #if defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x19980330
160         *kcap = cfs_cap_unpack(cap);
161 #elif defined (_LINUX_CAPABILITY_VERSION) && _LINUX_CAPABILITY_VERSION == 0x20071026
162         (*kcap)[0] = cfs_cap_unpack(cap);
163 #elif defined(_KERNEL_CAPABILITY_VERSION) && _KERNEL_CAPABILITY_VERSION == 0x20080522
164         kcap->cap[0] = cfs_cap_unpack(cap);
165 #else
166         #error "need correct _KERNEL_CAPABILITY_VERSION "
167 #endif
168 }
169
170 cfs_cap_t cfs_curproc_cap_pack(void)
171 {
172         cfs_cap_t cap;
173         cfs_kernel_cap_pack(current->cap_effective, &cap);
174         return cap;
175 }
176
177 void cfs_curproc_cap_unpack(cfs_cap_t cap)
178 {
179         cfs_kernel_cap_unpack(&current->cap_effective, cap);
180 }
181
182 int cfs_capable(cfs_cap_t cap)
183 {
184         return capable(cfs_cap_unpack(cap));
185 }
186
187 EXPORT_SYMBOL(cfs_curproc_uid);
188 EXPORT_SYMBOL(cfs_curproc_pid);
189 EXPORT_SYMBOL(cfs_curproc_gid);
190 EXPORT_SYMBOL(cfs_curproc_fsuid);
191 EXPORT_SYMBOL(cfs_curproc_fsgid);
192 EXPORT_SYMBOL(cfs_curproc_umask);
193 EXPORT_SYMBOL(cfs_curproc_comm);
194 EXPORT_SYMBOL(cfs_curproc_groups_nr);
195 EXPORT_SYMBOL(cfs_curproc_groups_dump);
196 EXPORT_SYMBOL(cfs_curproc_is_in_groups);
197 EXPORT_SYMBOL(cfs_cap_raise);
198 EXPORT_SYMBOL(cfs_cap_lower);
199 EXPORT_SYMBOL(cfs_cap_raised);
200 EXPORT_SYMBOL(cfs_kernel_cap_pack);
201 EXPORT_SYMBOL(cfs_kernel_cap_unpack);
202 EXPORT_SYMBOL(cfs_curproc_cap_pack);
203 EXPORT_SYMBOL(cfs_curproc_cap_unpack);
204 EXPORT_SYMBOL(cfs_capable);
205
206 /*
207  * Local variables:
208  * c-indentation-style: "K&R"
209  * c-basic-offset: 8
210  * tab-width: 8
211  * fill-column: 80
212  * scroll-step: 1
213  * End:
214  */