Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / afs / AIX / osi_gcpags.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h" /* Standard vendor system headers */
15 #include "afsincludes.h" /* Afs-based standard headers */
16 #include "afs/afs_stats.h" /* afs statistics */
17 #include <sys/adspace.h> /* for vm_att(), vm_det() */
18
19 #if AFS_GCPAGS
20
21 /* afs_osi_TraverseProcTable() - Walk through the systems process
22 * table, calling afs_GCPAGs_perproc_func() for each process.
23 */
24
25 #ifdef AFS_AIX51_ENV
26 #define max_proc v.ve_proc
27 #endif
28 void
29 afs_osi_TraverseProcTable(void)
30 {
31 afs_proc_t *p;
32 int i;
33
34 /*
35 * For binary compatibility, on AIX we need to be careful to use the
36 * proper size of a struct proc, even if it is different from what
37 * we were compiled with.
38 */
39 if (!afs_gcpags_procsize)
40 return;
41
42 #ifndef AFS_AIX51_ENV
43 simple_lock(&proc_tbl_lock);
44 #endif
45 for (p = (afs_proc_t *)v.vb_proc, i = 0; p < max_proc;
46 p = (afs_proc_t *)((char *)p + afs_gcpags_procsize), i++) {
47
48 #ifdef AFS_AIX51_ENV
49 if (p->p_pvprocp->pv_stat == SNONE)
50 continue;
51 if (p->p_pvprocp->pv_stat == SIDL)
52 continue;
53 if (p->p_pvprocp->pv_stat == SEXIT)
54 continue;
55 #else
56 if (p->p_stat == SNONE)
57 continue;
58 if (p->p_stat == SIDL)
59 continue;
60 if (p->p_stat == SEXIT)
61 continue;
62 #endif
63
64 /* sanity check */
65
66 if (PROCMASK(p->p_pid) != i) {
67 afs_gcpags = AFS_GCPAGS_EPIDCHECK;
68 break;
69 }
70
71 /* sanity check */
72
73 if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
74 afs_gcpags = AFS_GCPAGS_ENICECHECK;
75 break;
76 }
77
78 afs_GCPAGs_perproc_func(p);
79 }
80 #ifndef AFS_AIX51_ENV
81 simple_unlock(&proc_tbl_lock);
82 #endif
83 }
84
85 /* return a pointer (sometimes a static copy ) to the cred for a
86 * given afs_proc_t.
87 * subsequent calls may overwrite the previously returned value.
88 */
89
90 /* GLOBAL DECLARATIONS */
91
92 /*
93 * LOCKS: the caller must do
94 * simple_lock(&proc_tbl_lock);
95 * simple_unlock(&proc_tbl_lock);
96 * around calls to this function.
97 */
98
99 const afs_ucred_t *
100 afs_osi_proc2cred(afs_proc_t * pproc)
101 {
102 afs_ucred_t *pcred = 0;
103
104 /*
105 * pointer to process user structure valid in *our*
106 * address space
107 *
108 * The user structure for a process is stored in the user
109 * address space (as distinct from the kernel address
110 * space), and so to refer to the user structure of a
111 * different process we must employ special measures.
112 *
113 * I followed the example used in the AIX getproc() system
114 * call in bos/kernel/proc/getproc.c
115 */
116 struct user *xmem_userp;
117
118 struct xmem dp; /* ptr to xmem descriptor */
119 int xm; /* xmem result */
120
121 if (!pproc) {
122 return pcred;
123 }
124
125 /*
126 * The process private segment in which the user
127 * area is located may disappear. We need to increment
128 * its use count. Therefore we
129 * - get the proc_tbl_lock to hold the segment.
130 * - get the p_lock to lockout vm_cleardata.
131 * - vm_att to load the segment register (no check)
132 * - xmattach to bump its use count.
133 * - release the p_lock.
134 * - release the proc_tbl_lock.
135 * - do whatever we need.
136 * - xmdetach to decrement the use count.
137 * - vm_det to free the segment register (no check)
138 */
139
140 xmem_userp = NULL;
141 xm = XMEM_FAIL;
142 /* simple_lock(&proc_tbl_lock); */
143 #ifdef __64BIT__
144 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
145 #else
146 if (pproc->p_adspace != NULLSEGVAL) {
147 #endif
148
149 #ifdef AFS_AIX51_ENV
150 simple_lock(&pproc->p_pvprocp->pv_lock);
151 #else
152 simple_lock(&pproc->p_lock);
153 #endif
154
155 if (pproc->p_threadcount &&
156 #ifdef AFS_AIX51_ENV
157 pproc->p_pvprocp->pv_threadlist) {
158 #else
159 pproc->p_threadlist) {
160 #endif
161
162 /*
163 * arbitrarily pick the first thread in pproc
164 */
165 struct thread *pproc_thread =
166 #ifdef AFS_AIX51_ENV
167 pproc->p_pvprocp->pv_threadlist;
168 #else
169 pproc->p_threadlist;
170 #endif
171
172 /*
173 * location of 'struct user' in pproc's
174 * address space
175 */
176 struct user *pproc_userp = pproc_thread->t_userp;
177
178 /*
179 * create a pointer valid in my own address space
180 */
181
182 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
183
184 dp.aspace_id = XMEM_INVAL;
185 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
186 }
187
188 #ifdef AFS_AIX51_ENV
189 simple_unlock(&pproc->p_pvprocp->pv_lock);
190 #else
191 simple_unlock(&pproc->p_lock);
192 #endif
193 }
194 /* simple_unlock(&proc_tbl_lock); */
195 if (xm == XMEM_SUCC) {
196
197 static afs_ucred_t cred;
198
199 /*
200 * What locking should we use to protect access to the user
201 * area? If needed also change the code in AIX/osi_groups.c.
202 */
203
204 /* copy cred to local address space */
205 cred = *xmem_userp->U_cred;
206 pcred = &cred;
207
208 xmdetach(&dp);
209 }
210 if (xmem_userp) {
211 vm_det((void *)xmem_userp);
212 }
213
214 return pcred;
215 }
216
217
218 #endif /* AFS_GCPAGS */