Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-curproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/libcfs/linux/linux-curproc.c
35  *
36  * Lustre curproc API implementation for Linux kernel
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #include <linux/sched.h>
42 #include <linux/fs_struct.h>
43
44 #include <linux/compat.h>
45 #include <linux/thread_info.h>
46
47 #define DEBUG_SUBSYSTEM S_LNET
48
49 #include <libcfs/libcfs.h>
50
51 /*
52  * Implementation of cfs_curproc API (see portals/include/libcfs/curproc.h)
53  * for Linux kernel.
54  */
55
56 uid_t  cfs_curproc_uid(void)
57 {
58         return current_uid();
59 }
60
61 gid_t  cfs_curproc_gid(void)
62 {
63         return current_gid();
64 }
65
66 uid_t  cfs_curproc_fsuid(void)
67 {
68         return current_fsuid();
69 }
70
71 uid_t  cfs_curproc_euid(void)
72 {
73         return current_euid();
74 }
75
76 uid_t  cfs_curproc_egid(void)
77 {
78         return current_egid();
79 }
80
81 gid_t  cfs_curproc_fsgid(void)
82 {
83         return current_fsgid();
84 }
85
86 pid_t  cfs_curproc_pid(void)
87 {
88         return current->pid;
89 }
90
91 int    cfs_curproc_groups_nr(void)
92 {
93         int nr;
94
95         task_lock(current);
96         nr = current_cred()->group_info->ngroups;
97         task_unlock(current);
98         return nr;
99 }
100
101 void   cfs_curproc_groups_dump(gid_t *array, int size)
102 {
103         task_lock(current);
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);
107 }
108
109
110 int    cfs_curproc_is_in_groups(gid_t gid)
111 {
112         return in_group_p(gid);
113 }
114
115 mode_t cfs_curproc_umask(void)
116 {
117         return current->fs->umask;
118 }
119
120 char  *cfs_curproc_comm(void)
121 {
122         return current->comm;
123 }
124
125 /* Currently all the CFS_CAP_* defines match CAP_* ones. */
126 #define cfs_cap_pack(cap) (cap)
127 #define cfs_cap_unpack(cap) (cap)
128
129 void cfs_cap_raise(cfs_cap_t cap)
130 {
131         struct cred *cred;
132         if ((cred = prepare_creds())) {
133                 cap_raise(cred->cap_effective, cfs_cap_unpack(cap));
134                 commit_creds(cred);
135         }
136 }
137
138 void cfs_cap_lower(cfs_cap_t cap)
139 {
140         struct cred *cred;
141         if ((cred = prepare_creds())) {
142                 cap_lower(cred->cap_effective, cfs_cap_unpack(cap));
143                 commit_creds(cred);
144         }
145 }
146
147 int cfs_cap_raised(cfs_cap_t cap)
148 {
149         return cap_raised(current_cap(), cfs_cap_unpack(cap));
150 }
151
152 void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
153 {
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]);
161 #else
162         #error "need correct _KERNEL_CAPABILITY_VERSION "
163 #endif
164 }
165
166 void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
167 {
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);
174 #else
175         #error "need correct _KERNEL_CAPABILITY_VERSION "
176 #endif
177 }
178
179 cfs_cap_t cfs_curproc_cap_pack(void)
180 {
181         cfs_cap_t cap;
182         cfs_kernel_cap_pack(current_cap(), &cap);
183         return cap;
184 }
185
186 void cfs_curproc_cap_unpack(cfs_cap_t cap)
187 {
188         struct cred *cred;
189         if ((cred = prepare_creds())) {
190                 cfs_kernel_cap_unpack(&cred->cap_effective, cap);
191                 commit_creds(cred);
192         }
193 }
194
195 int cfs_capable(cfs_cap_t cap)
196 {
197         return capable(cfs_cap_unpack(cap));
198 }
199
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)
208 {
209 #ifdef HAVE_IS_COMPAT_TASK
210         return is_compat_task();
211 #else
212         return (BITS_PER_LONG == 32);
213 #endif
214 }
215
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);
235
236 /*
237  * Local variables:
238  * c-indentation-style: "K&R"
239  * c-basic-offset: 8
240  * tab-width: 8
241  * fill-column: 80
242  * scroll-step: 1
243  * End:
244  */