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