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