Whamcloud - gitweb
LU-4476 kernel: support process namespace containers
[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  current_uid(void)
60 {
61         return curproc_ucred()->cr_uid;
62 }
63
64 gid_t  current_gid(void)
65 {
66         LASSERT(curproc_ucred()->cr_ngroups > 0);
67         return curproc_ucred()->cr_groups[0];
68 }
69
70 uid_t  current_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  current_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  current_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    in_group_p(gid_t gid)
99 {
100         int i;
101         struct ucred *cr;
102
103         cr = curproc_ucred();
104         LASSERT(cr != NULL);
105
106         for (i = 0; i < cr->cr_ngroups; ++ i) {
107                 if (cr->cr_groups[i] == gid)
108                         return 1;
109         }
110         return 0;
111 }
112
113 mode_t current_umask(void)
114 {
115 #ifdef __DARWIN8__
116         /*
117          * XXX Liang:
118          *
119          * fd_cmask is not available in kexts, so we just assume 
120          * verything is permited.
121          */
122         return -1;
123 #else
124         return current_proc()->p_fd->fd_cmask;
125 #endif
126 }
127
128 char  *current_comm(void)
129 {
130 #ifdef __DARWIN8__
131         /*
132          * Writing to proc->p_comm is not permited in Darwin8,
133          * because proc_selfname() only return a copy of proc->p_comm,
134          * so this function is not really working while user try to 
135          * change comm of current process.
136          */
137         static char     pcomm[MAXCOMLEN+1];
138
139         proc_selfname(pcomm, MAXCOMLEN+1);
140         return pcomm;
141 #else
142         return current_proc()->p_comm;
143 #endif
144 }
145
146 struct user_namespace init_user_ns __read_mostly;
147 EXPORT_SYMBOL(init_user_ns);
148
149 void cfs_cap_raise(cfs_cap_t cap) {}
150 void cfs_cap_lower(cfs_cap_t cap) {}
151
152 int cfs_cap_raised(cfs_cap_t cap)
153 {
154         return 1;
155 }
156
157 cfs_cap_t cfs_curproc_cap_pack(void) {
158         return -1;
159 }
160
161 void cfs_curproc_cap_unpack(cfs_cap_t cap) {
162 }
163
164 int cfs_capable(cfs_cap_t cap)
165 {
166         return cap == CFS_CAP_SYS_BOOT ? is_suser(): is_suser1();
167 }
168
169 /*
170  * Local variables:
171  * c-indentation-style: "K&R"
172  * c-basic-offset: 8
173  * tab-width: 8
174  * fill-column: 80
175  * scroll-step: 1
176  * End:
177  */