2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include "afs/param.h"
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 */
18 #include <sys/adspace.h> /* for vm_att(), vm_det() */
23 /* afs_osi_TraverseProcTable() - Walk through the systems process
24 * table, calling afs_GCPAGs_perproc_func() for each process.
27 #if defined(AFS_SUN5_ENV)
29 afs_osi_TraverseProcTable(void)
32 for (prp = practive; prp != NULL; prp = prp->p_next) {
33 afs_GCPAGs_perproc_func(prp);
38 #if defined(AFS_HPUX_ENV)
41 * NOTE: h/proc_private.h gives the process table locking rules
42 * It indicates that access to p_cred must be protected by
44 * mp_mtproc_unlock(p);
46 * The code in sys/pm_prot.c uses pcred_lock() to protect access to
47 * the process creds, and uses mp_mtproc_lock() only for audit-related
48 * changes. To be safe, we use both.
52 afs_osi_TraverseProcTable(void)
57 MP_SPINLOCK(activeproc_lock);
58 MP_SPINLOCK(sched_lock);
62 * Instead of iterating through all of proc[], traverse only
63 * the list of active processes. As an example of this,
64 * see foreach_process() in sys/vm_sched.c.
66 * We hold the locks for the entire scan in order to get a
67 * consistent view of the current set of creds.
70 for (p = proc; endchain == 0; p = &proc[p->p_fandx]) {
71 if (p->p_fandx == 0) {
79 afs_GCPAGs_perproc_func(p);
84 MP_SPINUNLOCK(sched_lock);
85 MP_SPINUNLOCK(activeproc_lock);
89 #if defined(AFS_SGI_ENV)
92 /* TODO: Fix this later. */
94 SGI_ProcScanFunc(void *p, void *arg, int mode)
98 #else /* AFS_SGI65_ENV */
100 SGI_ProcScanFunc(proc_t * p, void *arg, int mode)
102 afs_int32(*perproc_func) (afs_proc_t *) = arg;
104 /* we pass in the function pointer for arg,
105 * mode ==0 for startup call, ==1 for each valid proc,
106 * and ==2 for terminate call.
109 code = perproc_func(p);
113 #endif /* AFS_SGI65_ENV */
116 afs_osi_TraverseProcTable(void)
118 procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
120 #endif /* AFS_SGI_ENV */
122 #if defined(AFS_AIX_ENV)
124 #define max_proc v.ve_proc
127 afs_osi_TraverseProcTable(void)
133 * For binary compatibility, on AIX we need to be careful to use the
134 * proper size of a struct proc, even if it is different from what
135 * we were compiled with.
137 if (!afs_gcpags_procsize)
140 #ifndef AFS_AIX51_ENV
141 simple_lock(&proc_tbl_lock);
143 for (p = (afs_proc_t *)v.vb_proc, i = 0; p < max_proc;
144 p = (afs_proc_t *)((char *)p + afs_gcpags_procsize), i++) {
147 if (p->p_pvprocp->pv_stat == SNONE)
149 if (p->p_pvprocp->pv_stat == SIDL)
151 if (p->p_pvprocp->pv_stat == SEXIT)
154 if (p->p_stat == SNONE)
156 if (p->p_stat == SIDL)
158 if (p->p_stat == SEXIT)
164 if (PROCMASK(p->p_pid) != i) {
165 afs_gcpags = AFS_GCPAGS_EPIDCHECK;
171 if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
172 afs_gcpags = AFS_GCPAGS_ENICECHECK;
176 afs_GCPAGs_perproc_func(p);
178 #ifndef AFS_AIX51_ENV
179 simple_unlock(&proc_tbl_lock);
184 #if (defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)) || defined(AFS_FBSD_ENV)
186 afs_osi_TraverseProcTable(void)
189 LIST_FOREACH(p, &allproc, p_list) {
190 if (p->p_stat == SIDL)
192 if (p->p_stat == SZOMB)
194 if (p->p_flag & P_SYSTEM)
196 afs_GCPAGs_perproc_func(p);
201 #if defined(AFS_LINUX22_ENV)
202 #ifdef EXPORTED_TASKLIST_LOCK
203 extern rwlock_t tasklist_lock __attribute__((weak));
206 afs_osi_TraverseProcTable()
208 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_HAS_CRED) || defined(EXPORTED_RCU_READ_LOCK))
209 struct task_struct *p;
211 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
213 read_lock(&tasklist_lock);
214 #endif /* EXPORTED_TASKLIST_LOCK */
215 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
216 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
218 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
220 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
222 #ifdef DEFINED_FOR_EACH_PROCESS
223 for_each_process(p) if (p->pid) {
224 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
228 if (p->state & TASK_ZOMBIE)
231 afs_GCPAGs_perproc_func(p);
234 for_each_task(p) if (p->pid) {
235 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
239 if (p->state & TASK_ZOMBIE)
242 afs_GCPAGs_perproc_func(p);
245 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
247 read_unlock(&tasklist_lock);
248 #endif /* EXPORTED_TASKLIST_LOCK */
249 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
250 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
252 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
254 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
259 /* return a pointer (sometimes a static copy ) to the cred for a
261 * subsequent calls may overwrite the previously returned value.
264 #if defined(AFS_SGI65_ENV)
266 afs_osi_proc2cred(afs_proc_t * p)
270 #elif defined(AFS_HPUX_ENV)
272 afs_osi_proc2cred(afs_proc_t * p)
278 * Cannot use afs_warnuser() here, as the code path
279 * eventually wants to grab sched_lock, which is
285 #elif defined(AFS_AIX_ENV)
287 /* GLOBAL DECLARATIONS */
290 * LOCKS: the caller must do
291 * simple_lock(&proc_tbl_lock);
292 * simple_unlock(&proc_tbl_lock);
293 * around calls to this function.
297 afs_osi_proc2cred(afs_proc_t * pproc)
299 afs_ucred_t *pcred = 0;
302 * pointer to process user structure valid in *our*
305 * The user structure for a process is stored in the user
306 * address space (as distinct from the kernel address
307 * space), and so to refer to the user structure of a
308 * different process we must employ special measures.
310 * I followed the example used in the AIX getproc() system
311 * call in bos/kernel/proc/getproc.c
313 struct user *xmem_userp;
315 struct xmem dp; /* ptr to xmem descriptor */
316 int xm; /* xmem result */
323 * The process private segment in which the user
324 * area is located may disappear. We need to increment
325 * its use count. Therefore we
326 * - get the proc_tbl_lock to hold the segment.
327 * - get the p_lock to lockout vm_cleardata.
328 * - vm_att to load the segment register (no check)
329 * - xmattach to bump its use count.
330 * - release the p_lock.
331 * - release the proc_tbl_lock.
332 * - do whatever we need.
333 * - xmdetach to decrement the use count.
334 * - vm_det to free the segment register (no check)
339 /* simple_lock(&proc_tbl_lock); */
341 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
343 if (pproc->p_adspace != NULLSEGVAL) {
347 simple_lock(&pproc->p_pvprocp->pv_lock);
349 simple_lock(&pproc->p_lock);
352 if (pproc->p_threadcount &&
354 pproc->p_pvprocp->pv_threadlist) {
356 pproc->p_threadlist) {
360 * arbitrarily pick the first thread in pproc
362 struct thread *pproc_thread =
364 pproc->p_pvprocp->pv_threadlist;
370 * location of 'struct user' in pproc's
373 struct user *pproc_userp = pproc_thread->t_userp;
376 * create a pointer valid in my own address space
379 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
381 dp.aspace_id = XMEM_INVAL;
382 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
386 simple_unlock(&pproc->p_pvprocp->pv_lock);
388 simple_unlock(&pproc->p_lock);
391 /* simple_unlock(&proc_tbl_lock); */
392 if (xm == XMEM_SUCC) {
394 static afs_ucred_t cred;
397 * What locking should we use to protect access to the user
398 * area? If needed also change the code in AIX/osi_groups.c.
401 /* copy cred to local address space */
402 cred = *xmem_userp->U_cred;
408 vm_det((void *)xmem_userp);
414 #elif defined(AFS_DARWIN80_ENV)
416 afs_osi_proc2cred(afs_proc_t * pr)
418 afs_ucred_t *rv = NULL;
419 static afs_ucred_t cr;
425 pcred = proc_ucred(pr);
427 cr.cr_uid = pcred->cr_uid;
428 cr.cr_ngroups = pcred->cr_ngroups;
429 memcpy(cr.cr_groups, pcred->cr_groups,
430 NGROUPS * sizeof(gid_t));
433 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
435 afs_osi_proc2cred(afs_proc_t * pr)
437 afs_ucred_t *rv = NULL;
438 static afs_ucred_t cr;
444 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
445 || (pr->p_stat == SSTOP)) {
448 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
449 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
450 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
451 NGROUPS * sizeof(gid_t));
458 #elif defined(AFS_LINUX22_ENV)
459 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_HAS_CRED) || defined(EXPORTED_RCU_READ_LOCK))
461 afs_osi_proc2cred(afs_proc_t * pr)
463 afs_ucred_t *rv = NULL;
464 static afs_ucred_t cr;
470 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
471 || (pr->state == TASK_UNINTERRUPTIBLE)
472 || (pr->state == TASK_STOPPED)) {
474 cr.cr_uid = task_uid(pr);
475 #if defined(AFS_LINUX26_ENV)
476 #if defined(STRUCT_TASK_HAS_CRED)
477 get_group_info(pr->cred->group_info);
478 cr.cr_group_info = pr->cred->group_info;
480 get_group_info(pr->group_info);
481 cr.cr_group_info = pr->group_info;
484 cr.cr_ngroups = pr->ngroups;
485 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
495 afs_osi_proc2cred(afs_proc_t * pr)
497 afs_ucred_t *rv = NULL;
508 #endif /* AFS_GCPAGS */