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"
16 #include "afs/sysincludes.h" /* Standard vendor system headers */
17 #include "afsincludes.h" /* Afs-based standard headers */
18 #include "afs/afs_stats.h" /* afs statistics */
20 #include <sys/adspace.h> /* for vm_att(), vm_det() */
25 /* afs_osi_TraverseProcTable() - Walk through the systems process
26 * table, calling afs_GCPAGs_perproc_func() for each process.
29 #if defined(AFS_SUN5_ENV)
31 afs_osi_TraverseProcTable(void)
34 for (prp = practive; prp != NULL; prp = prp->p_next) {
35 afs_GCPAGs_perproc_func(prp);
40 #if defined(AFS_HPUX_ENV)
43 * NOTE: h/proc_private.h gives the process table locking rules
44 * It indicates that access to p_cred must be protected by
46 * mp_mtproc_unlock(p);
48 * The code in sys/pm_prot.c uses pcred_lock() to protect access to
49 * the process creds, and uses mp_mtproc_lock() only for audit-related
50 * changes. To be safe, we use both.
54 afs_osi_TraverseProcTable(void)
59 MP_SPINLOCK(activeproc_lock);
60 MP_SPINLOCK(sched_lock);
64 * Instead of iterating through all of proc[], traverse only
65 * the list of active processes. As an example of this,
66 * see foreach_process() in sys/vm_sched.c.
68 * We hold the locks for the entire scan in order to get a
69 * consistent view of the current set of creds.
72 for (p = proc; endchain == 0; p = &proc[p->p_fandx]) {
73 if (p->p_fandx == 0) {
81 afs_GCPAGs_perproc_func(p);
86 MP_SPINUNLOCK(sched_lock);
87 MP_SPINUNLOCK(activeproc_lock);
91 #if defined(AFS_SGI_ENV)
94 /* TODO: Fix this later. */
96 SGI_ProcScanFunc(void *p, void *arg, int mode)
100 #else /* AFS_SGI65_ENV */
102 SGI_ProcScanFunc(proc_t * p, void *arg, int mode)
104 afs_int32(*perproc_func) (struct proc *) = arg;
106 /* we pass in the function pointer for arg,
107 * mode ==0 for startup call, ==1 for each valid proc,
108 * and ==2 for terminate call.
111 code = perproc_func(p);
115 #endif /* AFS_SGI65_ENV */
118 afs_osi_TraverseProcTable(void)
120 procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
122 #endif /* AFS_SGI_ENV */
124 #if defined(AFS_AIX_ENV)
126 #define max_proc v.ve_proc
129 afs_osi_TraverseProcTable(void)
135 * For binary compatibility, on AIX we need to be careful to use the
136 * proper size of a struct proc, even if it is different from what
137 * we were compiled with.
139 if (!afs_gcpags_procsize)
142 #ifndef AFS_AIX51_ENV
143 simple_lock(&proc_tbl_lock);
145 for (p = (struct proc *)v.vb_proc, i = 0; p < max_proc;
146 p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
149 if (p->p_pvprocp->pv_stat == SNONE)
151 if (p->p_pvprocp->pv_stat == SIDL)
153 if (p->p_pvprocp->pv_stat == SEXIT)
156 if (p->p_stat == SNONE)
158 if (p->p_stat == SIDL)
160 if (p->p_stat == SEXIT)
166 if (PROCMASK(p->p_pid) != i) {
167 afs_gcpags = AFS_GCPAGS_EPIDCHECK;
173 if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
174 afs_gcpags = AFS_GCPAGS_ENICECHECK;
178 afs_GCPAGs_perproc_func(p);
180 #ifndef AFS_AIX51_ENV
181 simple_unlock(&proc_tbl_lock);
186 #if defined(AFS_OSF_ENV)
189 extern struct pid_entry *pidtab;
194 afs_osi_TraverseProcTable(void)
196 struct pid_entry *pe;
198 #define pidNPID (pidtab + npid)
203 for (pe = pidtab; pe < pidNPID; ++pe) {
204 if (pe->pe_proc != PROC_NULL)
205 afs_GCPAGs_perproc_func(pe->pe_proc);
211 #if (defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)) || defined(AFS_FBSD_ENV)
213 afs_osi_TraverseProcTable(void)
216 LIST_FOREACH(p, &allproc, p_list) {
217 if (p->p_stat == SIDL)
219 if (p->p_stat == SZOMB)
221 if (p->p_flag & P_SYSTEM)
223 afs_GCPAGs_perproc_func(p);
228 #if defined(AFS_LINUX22_ENV)
230 afs_osi_TraverseProcTable()
232 #if !defined(LINUX_KEYRING_SUPPORT)
233 extern rwlock_t tasklist_lock __attribute__((weak));
234 struct task_struct *p;
237 read_lock(&tasklist_lock);
238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
243 #ifdef DEFINED_FOR_EACH_PROCESS
244 for_each_process(p) if (p->pid) {
245 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
249 if (p->state & TASK_ZOMBIE)
252 afs_GCPAGs_perproc_func(p);
255 for_each_task(p) if (p->pid) {
256 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
260 if (p->state & TASK_ZOMBIE)
263 afs_GCPAGs_perproc_func(p);
267 read_unlock(&tasklist_lock);
268 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
276 /* return a pointer (sometimes a static copy ) to the cred for a
278 * subsequent calls may overwrite the previously returned value.
281 #if defined(AFS_SGI65_ENV)
282 const struct AFS_UCRED *
283 afs_osi_proc2cred(AFS_PROC * p)
287 #elif defined(AFS_HPUX_ENV)
288 const struct AFS_UCRED *
289 afs_osi_proc2cred(AFS_PROC * p)
295 * Cannot use afs_warnuser() here, as the code path
296 * eventually wants to grab sched_lock, which is
302 #elif defined(AFS_AIX_ENV)
304 /* GLOBAL DECLARATIONS */
307 * LOCKS: the caller must do
308 * simple_lock(&proc_tbl_lock);
309 * simple_unlock(&proc_tbl_lock);
310 * around calls to this function.
313 const struct AFS_UCRED *
314 afs_osi_proc2cred(AFS_PROC * pproc)
316 struct AFS_UCRED *pcred = 0;
319 * pointer to process user structure valid in *our*
322 * The user structure for a process is stored in the user
323 * address space (as distinct from the kernel address
324 * space), and so to refer to the user structure of a
325 * different process we must employ special measures.
327 * I followed the example used in the AIX getproc() system
328 * call in bos/kernel/proc/getproc.c
330 struct user *xmem_userp;
332 struct xmem dp; /* ptr to xmem descriptor */
333 int xm; /* xmem result */
340 * The process private segment in which the user
341 * area is located may disappear. We need to increment
342 * its use count. Therefore we
343 * - get the proc_tbl_lock to hold the segment.
344 * - get the p_lock to lockout vm_cleardata.
345 * - vm_att to load the segment register (no check)
346 * - xmattach to bump its use count.
347 * - release the p_lock.
348 * - release the proc_tbl_lock.
349 * - do whatever we need.
350 * - xmdetach to decrement the use count.
351 * - vm_det to free the segment register (no check)
356 /* simple_lock(&proc_tbl_lock); */
358 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
360 if (pproc->p_adspace != NULLSEGVAL) {
364 simple_lock(&pproc->p_pvprocp->pv_lock);
366 simple_lock(&pproc->p_lock);
369 if (pproc->p_threadcount &&
371 pproc->p_pvprocp->pv_threadlist) {
373 pproc->p_threadlist) {
377 * arbitrarily pick the first thread in pproc
379 struct thread *pproc_thread =
381 pproc->p_pvprocp->pv_threadlist;
387 * location of 'struct user' in pproc's
390 struct user *pproc_userp = pproc_thread->t_userp;
393 * create a pointer valid in my own address space
396 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
398 dp.aspace_id = XMEM_INVAL;
399 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
403 simple_unlock(&pproc->p_pvprocp->pv_lock);
405 simple_unlock(&pproc->p_lock);
408 /* simple_unlock(&proc_tbl_lock); */
409 if (xm == XMEM_SUCC) {
411 static struct AFS_UCRED cred;
414 * What locking should we use to protect access to the user
415 * area? If needed also change the code in AIX/osi_groups.c.
418 /* copy cred to local address space */
419 cred = *xmem_userp->U_cred;
425 vm_det((void *)xmem_userp);
431 #elif defined(AFS_OSF_ENV)
432 const struct AFS_UCRED *
433 afs_osi_proc2cred(AFS_PROC * pr)
435 struct AFS_UCRED *rv = NULL;
441 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
442 || (pr->p_stat == SSTOP))
447 #elif defined(AFS_DARWIN80_ENV)
448 const struct AFS_UCRED *
449 afs_osi_proc2cred(AFS_PROC * pr)
451 struct AFS_UCRED *rv = NULL;
452 static struct AFS_UCRED cr;
458 pcred = proc_ucred(pr);
460 cr.cr_uid = pcred->cr_uid;
461 cr.cr_ngroups = pcred->cr_ngroups;
462 memcpy(cr.cr_groups, pcred->cr_groups,
463 NGROUPS * sizeof(gid_t));
466 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
467 const struct AFS_UCRED *
468 afs_osi_proc2cred(AFS_PROC * pr)
470 struct AFS_UCRED *rv = NULL;
471 static struct AFS_UCRED cr;
477 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
478 || (pr->p_stat == SSTOP)) {
481 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
482 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
483 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
484 NGROUPS * sizeof(gid_t));
491 #elif defined(AFS_LINUX22_ENV)
492 const struct AFS_UCRED *
493 afs_osi_proc2cred(AFS_PROC * pr)
495 struct AFS_UCRED *rv = NULL;
496 static struct AFS_UCRED cr;
502 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
503 || (pr->state == TASK_UNINTERRUPTIBLE)
504 || (pr->state == TASK_STOPPED)) {
507 #if defined(AFS_LINUX26_ENV)
508 get_group_info(pr->group_info);
509 cr.cr_group_info = pr->group_info;
511 cr.cr_ngroups = pr->ngroups;
512 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
520 const struct AFS_UCRED *
521 afs_osi_proc2cred(AFS_PROC * pr)
523 struct AFS_UCRED *rv = NULL;
534 #endif /* AFS_GCPAGS */