Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / 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  * Lustre curproc API implementation for XNU kernel
5  *
6  * Copyright (C) 2004 Cluster File Systems, Inc.
7  * Author: Nikita Danilov <nikita@clusterfs.com>
8  *
9  * This file is part of Lustre, http://www.lustre.org.
10  *
11  * Lustre is free software; you can redistribute it and/or modify it under the
12  * terms of version 2 of the GNU General Public License as published by the
13  * Free Software Foundation. Lustre is distributed in the hope that it will be
14  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
16  * Public License for more details. You should have received a copy of the GNU
17  * General Public License along with Lustre; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #define DEBUG_SUBSYSTEM S_LNET
22
23 #include <libcfs/libcfs.h>
24 #include <libcfs/kp30.h>
25
26 /*
27  * Implementation of cfs_curproc API (see lnet/include/libcfs/curproc.h)
28  * for XNU kernel.
29  */
30
31 static inline struct ucred *curproc_ucred(void)
32 {
33 #ifdef __DARWIN8__
34         return proc_ucred(current_proc());
35 #else
36         return current_proc()->p_cred->pc_ucred;
37 #endif
38 }
39
40 uid_t  cfs_curproc_uid(void)
41 {
42         return curproc_ucred()->cr_uid;
43 }
44
45 gid_t  cfs_curproc_gid(void)
46 {
47         LASSERT(curproc_ucred()->cr_ngroups > 0);
48         return curproc_ucred()->cr_groups[0];
49 }
50
51 uid_t  cfs_curproc_fsuid(void)
52 {
53 #ifdef __DARWIN8__
54         return curproc_ucred()->cr_ruid;
55 #else
56         return current_proc()->p_cred->p_ruid;
57 #endif
58 }
59
60 gid_t  cfs_curproc_fsgid(void)
61 {
62 #ifdef __DARWIN8__
63         return curproc_ucred()->cr_rgid;
64 #else
65         return current_proc()->p_cred->p_rgid;
66 #endif
67 }
68
69 pid_t  cfs_curproc_pid(void)
70 {
71 #ifdef __DARWIN8__
72         /* no pid for each thread, return address of thread struct */
73         return (pid_t)current_thread();
74 #else
75         return current_proc()->p_pid;
76 #endif
77 }
78
79 int    cfs_curproc_groups_nr(void)
80 {
81         LASSERT(curproc_ucred()->cr_ngroups > 0);
82         return curproc_ucred()->cr_ngroups - 1;
83 }
84
85 int    cfs_curproc_is_in_groups(gid_t gid)
86 {
87         int i;
88         struct ucred *cr;
89
90         cr = curproc_ucred();
91         LASSERT(cr != NULL);
92
93         for (i = 0; i < cr->cr_ngroups; ++ i) {
94                 if (cr->cr_groups[i] == gid)
95                         return 1;
96         }
97         return 0;
98 }
99
100 void   cfs_curproc_groups_dump(gid_t *array, int size)
101 {
102         struct ucred *cr;
103
104         cr = curproc_ucred();
105         LASSERT(cr != NULL);
106         CLASSERT(sizeof array[0] == sizeof (__u32));
107
108         size = min_t(int, size, cr->cr_ngroups);
109         memcpy(array, &cr->cr_groups[1], size * sizeof(gid_t));
110 }
111
112 mode_t cfs_curproc_umask(void)
113 {
114 #ifdef __DARWIN8__
115         /*
116          * XXX Liang:
117          *
118          * fd_cmask is not available in kexts, so we just assume 
119          * verything is permited.
120          */
121         return -1;
122 #else
123         return current_proc()->p_fd->fd_cmask;
124 #endif
125 }
126
127 char  *cfs_curproc_comm(void)
128 {
129 #ifdef __DARWIN8__
130         /*
131          * Writing to proc->p_comm is not permited in Darwin8,
132          * because proc_selfname() only return a copy of proc->p_comm,
133          * so this function is not really working while user try to 
134          * change comm of current process.
135          */
136         static char     pcomm[MAXCOMLEN+1];
137
138         proc_selfname(pcomm, MAXCOMLEN+1);
139         return pcomm;
140 #else
141         return current_proc()->p_comm;
142 #endif
143 }
144
145 cfs_kernel_cap_t cfs_curproc_cap_get(void)
146 {
147         return -1;
148 }
149
150 void cfs_curproc_cap_set(cfs_kernel_cap_t cap)
151 {
152         return;
153 }
154
155
156 /*
157  * Local variables:
158  * c-indentation-style: "K&R"
159  * c-basic-offset: 8
160  * tab-width: 8
161  * fill-column: 80
162  * scroll-step: 1
163  * End:
164  */