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)
229 extern rwlock_t tasklist_lock __attribute__((weak));
231 afs_osi_TraverseProcTable()
233 struct task_struct *p;
235 read_lock(&tasklist_lock);
236 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
241 #ifdef DEFINED_FOR_EACH_PROCESS
242 for_each_process(p) if (p->pid) {
243 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
247 if (p->state & TASK_ZOMBIE)
250 afs_GCPAGs_perproc_func(p);
253 for_each_task(p) if (p->pid) {
254 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
258 if (p->state & TASK_ZOMBIE)
261 afs_GCPAGs_perproc_func(p);
265 read_unlock(&tasklist_lock);
266 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
273 /* return a pointer (sometimes a static copy ) to the cred for a
275 * subsequent calls may overwrite the previously returned value.
278 #if defined(AFS_SGI65_ENV)
279 const struct AFS_UCRED *
280 afs_osi_proc2cred(AFS_PROC * p)
284 #elif defined(AFS_HPUX_ENV)
285 const struct AFS_UCRED *
286 afs_osi_proc2cred(AFS_PROC * p)
292 * Cannot use afs_warnuser() here, as the code path
293 * eventually wants to grab sched_lock, which is
299 #elif defined(AFS_AIX_ENV)
301 /* GLOBAL DECLARATIONS */
304 * LOCKS: the caller must do
305 * simple_lock(&proc_tbl_lock);
306 * simple_unlock(&proc_tbl_lock);
307 * around calls to this function.
310 const struct AFS_UCRED *
311 afs_osi_proc2cred(AFS_PROC * pproc)
313 struct AFS_UCRED *pcred = 0;
316 * pointer to process user structure valid in *our*
319 * The user structure for a process is stored in the user
320 * address space (as distinct from the kernel address
321 * space), and so to refer to the user structure of a
322 * different process we must employ special measures.
324 * I followed the example used in the AIX getproc() system
325 * call in bos/kernel/proc/getproc.c
327 struct user *xmem_userp;
329 struct xmem dp; /* ptr to xmem descriptor */
330 int xm; /* xmem result */
337 * The process private segment in which the user
338 * area is located may disappear. We need to increment
339 * its use count. Therefore we
340 * - get the proc_tbl_lock to hold the segment.
341 * - get the p_lock to lockout vm_cleardata.
342 * - vm_att to load the segment register (no check)
343 * - xmattach to bump its use count.
344 * - release the p_lock.
345 * - release the proc_tbl_lock.
346 * - do whatever we need.
347 * - xmdetach to decrement the use count.
348 * - vm_det to free the segment register (no check)
353 /* simple_lock(&proc_tbl_lock); */
355 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
357 if (pproc->p_adspace != NULLSEGVAL) {
361 simple_lock(&pproc->p_pvprocp->pv_lock);
363 simple_lock(&pproc->p_lock);
366 if (pproc->p_threadcount &&
368 pproc->p_pvprocp->pv_threadlist) {
370 pproc->p_threadlist) {
374 * arbitrarily pick the first thread in pproc
376 struct thread *pproc_thread =
378 pproc->p_pvprocp->pv_threadlist;
384 * location of 'struct user' in pproc's
387 struct user *pproc_userp = pproc_thread->t_userp;
390 * create a pointer valid in my own address space
393 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
395 dp.aspace_id = XMEM_INVAL;
396 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
400 simple_unlock(&pproc->p_pvprocp->pv_lock);
402 simple_unlock(&pproc->p_lock);
405 /* simple_unlock(&proc_tbl_lock); */
406 if (xm == XMEM_SUCC) {
408 static struct AFS_UCRED cred;
411 * What locking should we use to protect access to the user
412 * area? If needed also change the code in AIX/osi_groups.c.
415 /* copy cred to local address space */
416 cred = *xmem_userp->U_cred;
422 vm_det((void *)xmem_userp);
428 #elif defined(AFS_OSF_ENV)
429 const struct AFS_UCRED *
430 afs_osi_proc2cred(AFS_PROC * pr)
432 struct AFS_UCRED *rv = NULL;
438 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
439 || (pr->p_stat == SSTOP))
444 #elif defined(AFS_DARWIN80_ENV)
445 const struct AFS_UCRED *
446 afs_osi_proc2cred(AFS_PROC * pr)
448 struct AFS_UCRED *rv = NULL;
449 static struct AFS_UCRED cr;
455 pcred = proc_ucred(pr);
457 cr.cr_uid = pcred->cr_uid;
458 cr.cr_ngroups = pcred->cr_ngroups;
459 memcpy(cr.cr_groups, pcred->cr_groups,
460 NGROUPS * sizeof(gid_t));
463 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
464 const struct AFS_UCRED *
465 afs_osi_proc2cred(AFS_PROC * pr)
467 struct AFS_UCRED *rv = NULL;
468 static struct AFS_UCRED cr;
474 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
475 || (pr->p_stat == SSTOP)) {
478 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
479 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
480 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
481 NGROUPS * sizeof(gid_t));
488 #elif defined(AFS_LINUX22_ENV)
489 const struct AFS_UCRED *
490 afs_osi_proc2cred(AFS_PROC * pr)
492 struct AFS_UCRED *rv = NULL;
493 static struct AFS_UCRED cr;
499 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
500 || (pr->state == TASK_UNINTERRUPTIBLE)
501 || (pr->state == TASK_STOPPED)) {
504 #if defined(AFS_LINUX26_ENV)
505 get_group_info(pr->group_info);
506 cr.cr_group_info = pr->group_info;
508 cr.cr_ngroups = pr->ngroups;
509 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
517 const struct AFS_UCRED *
518 afs_osi_proc2cred(AFS_PROC * pr)
520 struct AFS_UCRED *rv = NULL;
531 #endif /* AFS_GCPAGS */