Whamcloud - gitweb
dfc4957a55912df02a39b6a42b253b73bc380eb7
[fs/lustre-release.git] / libcfs / libcfs / darwin / darwin-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/darwin/darwin-curproc.c
35  *
36  * Lustre curproc API implementation for XNU kernel
37  *
38  * Author: Nikita Danilov <nikita@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <libcfs/libcfs.h>
44
45 /*
46  * Implementation of cfs_curproc API (see lnet/include/libcfs/curproc.h)
47  * for XNU kernel.
48  */
49
50 static inline struct ucred *curproc_ucred(void)
51 {
52 #ifdef __DARWIN8__
53         return proc_ucred(current_proc());
54 #else
55         return current_proc()->p_cred->pc_ucred;
56 #endif
57 }
58
59 uid_t  cfs_curproc_uid(void)
60 {
61         return curproc_ucred()->cr_uid;
62 }
63
64 gid_t  cfs_curproc_gid(void)
65 {
66         LASSERT(curproc_ucred()->cr_ngroups > 0);
67         return curproc_ucred()->cr_groups[0];
68 }
69
70 uid_t  cfs_curproc_fsuid(void)
71 {
72 #ifdef __DARWIN8__
73         return curproc_ucred()->cr_ruid;
74 #else
75         return current_proc()->p_cred->p_ruid;
76 #endif
77 }
78
79 gid_t  cfs_curproc_fsgid(void)
80 {
81 #ifdef __DARWIN8__
82         return curproc_ucred()->cr_rgid;
83 #else
84         return current_proc()->p_cred->p_rgid;
85 #endif
86 }
87
88 pid_t  cfs_curproc_pid(void)
89 {
90 #ifdef __DARWIN8__
91         /* no pid for each thread, return address of thread struct */
92         return (pid_t)current_thread();
93 #else
94         return current_proc()->p_pid;
95 #endif
96 }
97
98 int    cfs_curproc_groups_nr(void)
99 {
100         LASSERT(curproc_ucred()->cr_ngroups > 0);
101         return curproc_ucred()->cr_ngroups - 1;
102 }
103
104 int    cfs_curproc_is_in_groups(gid_t gid)
105 {
106         int i;
107         struct ucred *cr;
108
109         cr = curproc_ucred();
110         LASSERT(cr != NULL);
111
112         for (i = 0; i < cr->cr_ngroups; ++ i) {
113                 if (cr->cr_groups[i] == gid)
114                         return 1;
115         }
116         return 0;
117 }
118
119 void   cfs_curproc_groups_dump(gid_t *array, int size)
120 {
121         struct ucred *cr;
122
123         cr = curproc_ucred();
124         LASSERT(cr != NULL);
125         CLASSERT(sizeof array[0] == sizeof (__u32));
126
127         size = min_t(int, size, cr->cr_ngroups);
128         memcpy(array, &cr->cr_groups[1], size * sizeof(gid_t));
129 }
130
131 mode_t cfs_curproc_umask(void)
132 {
133 #ifdef __DARWIN8__
134         /*
135          * XXX Liang:
136          *
137          * fd_cmask is not available in kexts, so we just assume 
138          * verything is permited.
139          */
140         return -1;
141 #else
142         return current_proc()->p_fd->fd_cmask;
143 #endif
144 }
145
146 char  *cfs_curproc_comm(void)
147 {
148 #ifdef __DARWIN8__
149         /*
150          * Writing to proc->p_comm is not permited in Darwin8,
151          * because proc_selfname() only return a copy of proc->p_comm,
152          * so this function is not really working while user try to 
153          * change comm of current process.
154          */
155         static char     pcomm[MAXCOMLEN+1];
156
157         proc_selfname(pcomm, MAXCOMLEN+1);
158         return pcomm;
159 #else
160         return current_proc()->p_comm;
161 #endif
162 }
163
164 void cfs_cap_raise(cfs_cap_t cap) {}
165 void cfs_cap_lower(cfs_cap_t cap) {}
166
167 int cfs_cap_raised(cfs_cap_t cap)
168 {
169         return 1;
170 }
171
172 cfs_cap_t cfs_curproc_cap_pack(void) {
173         return -1;
174 }
175
176 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
177 }
178
179 int cfs_capable(cfs_cap_t cap)
180 {
181         return cap == CFS_CAP_SYS_BOOT ? is_suser(): is_suser1();
182 }
183
184 /*
185  * Local variables:
186  * c-indentation-style: "K&R"
187  * c-basic-offset: 8
188  * tab-width: 8
189  * fill-column: 80
190  * scroll-step: 1
191  * End:
192  */