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