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 #ifdef EXPORTED_TASKLIST_LOCK
230 extern rwlock_t tasklist_lock __attribute__((weak));
233 afs_osi_TraverseProcTable()
235 #if !defined(LINUX_KEYRING_SUPPORT)
236 struct task_struct *p;
237 #ifdef EXPORTED_TASKLIST_LOCK
239 read_lock(&tasklist_lock);
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
242 #ifdef EXPORTED_TASKLIST_LOCK
248 #ifdef DEFINED_FOR_EACH_PROCESS
249 for_each_process(p) if (p->pid) {
250 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
254 if (p->state & TASK_ZOMBIE)
257 afs_GCPAGs_perproc_func(p);
260 for_each_task(p) if (p->pid) {
261 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
265 if (p->state & TASK_ZOMBIE)
268 afs_GCPAGs_perproc_func(p);
271 #ifdef EXPORTED_TASKLIST_LOCK
273 read_unlock(&tasklist_lock);
275 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
276 #ifdef EXPORTED_TASKLIST_LOCK
285 /* return a pointer (sometimes a static copy ) to the cred for a
287 * subsequent calls may overwrite the previously returned value.
290 #if defined(AFS_SGI65_ENV)
291 const struct AFS_UCRED *
292 afs_osi_proc2cred(AFS_PROC * p)
296 #elif defined(AFS_HPUX_ENV)
297 const struct AFS_UCRED *
298 afs_osi_proc2cred(AFS_PROC * p)
304 * Cannot use afs_warnuser() here, as the code path
305 * eventually wants to grab sched_lock, which is
311 #elif defined(AFS_AIX_ENV)
313 /* GLOBAL DECLARATIONS */
316 * LOCKS: the caller must do
317 * simple_lock(&proc_tbl_lock);
318 * simple_unlock(&proc_tbl_lock);
319 * around calls to this function.
322 const struct AFS_UCRED *
323 afs_osi_proc2cred(AFS_PROC * pproc)
325 struct AFS_UCRED *pcred = 0;
328 * pointer to process user structure valid in *our*
331 * The user structure for a process is stored in the user
332 * address space (as distinct from the kernel address
333 * space), and so to refer to the user structure of a
334 * different process we must employ special measures.
336 * I followed the example used in the AIX getproc() system
337 * call in bos/kernel/proc/getproc.c
339 struct user *xmem_userp;
341 struct xmem dp; /* ptr to xmem descriptor */
342 int xm; /* xmem result */
349 * The process private segment in which the user
350 * area is located may disappear. We need to increment
351 * its use count. Therefore we
352 * - get the proc_tbl_lock to hold the segment.
353 * - get the p_lock to lockout vm_cleardata.
354 * - vm_att to load the segment register (no check)
355 * - xmattach to bump its use count.
356 * - release the p_lock.
357 * - release the proc_tbl_lock.
358 * - do whatever we need.
359 * - xmdetach to decrement the use count.
360 * - vm_det to free the segment register (no check)
365 /* simple_lock(&proc_tbl_lock); */
367 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
369 if (pproc->p_adspace != NULLSEGVAL) {
373 simple_lock(&pproc->p_pvprocp->pv_lock);
375 simple_lock(&pproc->p_lock);
378 if (pproc->p_threadcount &&
380 pproc->p_pvprocp->pv_threadlist) {
382 pproc->p_threadlist) {
386 * arbitrarily pick the first thread in pproc
388 struct thread *pproc_thread =
390 pproc->p_pvprocp->pv_threadlist;
396 * location of 'struct user' in pproc's
399 struct user *pproc_userp = pproc_thread->t_userp;
402 * create a pointer valid in my own address space
405 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
407 dp.aspace_id = XMEM_INVAL;
408 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
412 simple_unlock(&pproc->p_pvprocp->pv_lock);
414 simple_unlock(&pproc->p_lock);
417 /* simple_unlock(&proc_tbl_lock); */
418 if (xm == XMEM_SUCC) {
420 static struct AFS_UCRED cred;
423 * What locking should we use to protect access to the user
424 * area? If needed also change the code in AIX/osi_groups.c.
427 /* copy cred to local address space */
428 cred = *xmem_userp->U_cred;
434 vm_det((void *)xmem_userp);
440 #elif defined(AFS_OSF_ENV)
441 const struct AFS_UCRED *
442 afs_osi_proc2cred(AFS_PROC * pr)
444 struct AFS_UCRED *rv = NULL;
450 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
451 || (pr->p_stat == SSTOP))
456 #elif defined(AFS_DARWIN80_ENV)
457 const struct AFS_UCRED *
458 afs_osi_proc2cred(AFS_PROC * pr)
460 struct AFS_UCRED *rv = NULL;
461 static struct AFS_UCRED cr;
467 pcred = proc_ucred(pr);
469 cr.cr_uid = pcred->cr_uid;
470 cr.cr_ngroups = pcred->cr_ngroups;
471 memcpy(cr.cr_groups, pcred->cr_groups,
472 NGROUPS * sizeof(gid_t));
475 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
476 const struct AFS_UCRED *
477 afs_osi_proc2cred(AFS_PROC * pr)
479 struct AFS_UCRED *rv = NULL;
480 static struct AFS_UCRED cr;
486 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
487 || (pr->p_stat == SSTOP)) {
490 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
491 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
492 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
493 NGROUPS * sizeof(gid_t));
500 #elif defined(AFS_LINUX22_ENV)
501 const struct AFS_UCRED *
502 afs_osi_proc2cred(AFS_PROC * pr)
504 struct AFS_UCRED *rv = NULL;
505 static struct AFS_UCRED cr;
511 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
512 || (pr->state == TASK_UNINTERRUPTIBLE)
513 || (pr->state == TASK_STOPPED)) {
516 #if defined(AFS_LINUX26_ENV)
517 get_group_info(pr->group_info);
518 cr.cr_group_info = pr->group_info;
520 cr.cr_ngroups = pr->ngroups;
521 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
529 const struct AFS_UCRED *
530 afs_osi_proc2cred(AFS_PROC * pr)
532 struct AFS_UCRED *rv = NULL;
543 #endif /* AFS_GCPAGS */