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 *) = 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 *)v.vb_proc, i = 0; p < max_proc;
144 p = (AFS_PROC *)((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_OSF_ENV)
187 extern struct pid_entry *pidtab;
192 afs_osi_TraverseProcTable(void)
194 struct pid_entry *pe;
196 #define pidNPID (pidtab + npid)
201 for (pe = pidtab; pe < pidNPID; ++pe) {
202 if (pe->pe_proc != PROC_NULL)
203 afs_GCPAGs_perproc_func(pe->pe_proc);
209 #if (defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)) || defined(AFS_FBSD_ENV)
211 afs_osi_TraverseProcTable(void)
214 LIST_FOREACH(p, &allproc, p_list) {
215 if (p->p_stat == SIDL)
217 if (p->p_stat == SZOMB)
219 if (p->p_flag & P_SYSTEM)
221 afs_GCPAGs_perproc_func(p);
226 #if defined(AFS_LINUX22_ENV)
227 #ifdef EXPORTED_TASKLIST_LOCK
228 extern rwlock_t tasklist_lock __attribute__((weak));
231 afs_osi_TraverseProcTable()
233 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_HAS_CRED) || defined(EXPORTED_RCU_READ_LOCK))
234 struct task_struct *p;
236 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
238 read_lock(&tasklist_lock);
239 #endif /* EXPORTED_TASKLIST_LOCK */
240 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
241 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
243 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
245 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
247 #ifdef DEFINED_FOR_EACH_PROCESS
248 for_each_process(p) if (p->pid) {
249 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
253 if (p->state & TASK_ZOMBIE)
256 afs_GCPAGs_perproc_func(p);
259 for_each_task(p) if (p->pid) {
260 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
264 if (p->state & TASK_ZOMBIE)
267 afs_GCPAGs_perproc_func(p);
270 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
272 read_unlock(&tasklist_lock);
273 #endif /* EXPORTED_TASKLIST_LOCK */
274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
275 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
277 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
279 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
284 /* return a pointer (sometimes a static copy ) to the cred for a
286 * subsequent calls may overwrite the previously returned value.
289 #if defined(AFS_SGI65_ENV)
291 afs_osi_proc2cred(AFS_PROC * p)
295 #elif defined(AFS_HPUX_ENV)
297 afs_osi_proc2cred(AFS_PROC * p)
303 * Cannot use afs_warnuser() here, as the code path
304 * eventually wants to grab sched_lock, which is
310 #elif defined(AFS_AIX_ENV)
312 /* GLOBAL DECLARATIONS */
315 * LOCKS: the caller must do
316 * simple_lock(&proc_tbl_lock);
317 * simple_unlock(&proc_tbl_lock);
318 * around calls to this function.
322 afs_osi_proc2cred(AFS_PROC * pproc)
324 AFS_UCRED *pcred = 0;
327 * pointer to process user structure valid in *our*
330 * The user structure for a process is stored in the user
331 * address space (as distinct from the kernel address
332 * space), and so to refer to the user structure of a
333 * different process we must employ special measures.
335 * I followed the example used in the AIX getproc() system
336 * call in bos/kernel/proc/getproc.c
338 struct user *xmem_userp;
340 struct xmem dp; /* ptr to xmem descriptor */
341 int xm; /* xmem result */
348 * The process private segment in which the user
349 * area is located may disappear. We need to increment
350 * its use count. Therefore we
351 * - get the proc_tbl_lock to hold the segment.
352 * - get the p_lock to lockout vm_cleardata.
353 * - vm_att to load the segment register (no check)
354 * - xmattach to bump its use count.
355 * - release the p_lock.
356 * - release the proc_tbl_lock.
357 * - do whatever we need.
358 * - xmdetach to decrement the use count.
359 * - vm_det to free the segment register (no check)
364 /* simple_lock(&proc_tbl_lock); */
366 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
368 if (pproc->p_adspace != NULLSEGVAL) {
372 simple_lock(&pproc->p_pvprocp->pv_lock);
374 simple_lock(&pproc->p_lock);
377 if (pproc->p_threadcount &&
379 pproc->p_pvprocp->pv_threadlist) {
381 pproc->p_threadlist) {
385 * arbitrarily pick the first thread in pproc
387 struct thread *pproc_thread =
389 pproc->p_pvprocp->pv_threadlist;
395 * location of 'struct user' in pproc's
398 struct user *pproc_userp = pproc_thread->t_userp;
401 * create a pointer valid in my own address space
404 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
406 dp.aspace_id = XMEM_INVAL;
407 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
411 simple_unlock(&pproc->p_pvprocp->pv_lock);
413 simple_unlock(&pproc->p_lock);
416 /* simple_unlock(&proc_tbl_lock); */
417 if (xm == XMEM_SUCC) {
419 static AFS_UCRED cred;
422 * What locking should we use to protect access to the user
423 * area? If needed also change the code in AIX/osi_groups.c.
426 /* copy cred to local address space */
427 cred = *xmem_userp->U_cred;
433 vm_det((void *)xmem_userp);
439 #elif defined(AFS_OSF_ENV)
441 afs_osi_proc2cred(AFS_PROC * pr)
443 AFS_UCRED *rv = NULL;
449 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
450 || (pr->p_stat == SSTOP))
455 #elif defined(AFS_DARWIN80_ENV)
457 afs_osi_proc2cred(AFS_PROC * pr)
459 AFS_UCRED *rv = NULL;
466 pcred = proc_ucred(pr);
468 cr.cr_uid = pcred->cr_uid;
469 cr.cr_ngroups = pcred->cr_ngroups;
470 memcpy(cr.cr_groups, pcred->cr_groups,
471 NGROUPS * sizeof(gid_t));
474 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
476 afs_osi_proc2cred(AFS_PROC * pr)
478 AFS_UCRED *rv = NULL;
485 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
486 || (pr->p_stat == SSTOP)) {
489 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
490 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
491 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
492 NGROUPS * sizeof(gid_t));
499 #elif defined(AFS_LINUX22_ENV)
500 #if !defined(LINUX_KEYRING_SUPPORT) && (!defined(STRUCT_TASK_HAS_CRED) || defined(EXPORTED_RCU_READ_LOCK))
502 afs_osi_proc2cred(AFS_PROC * pr)
504 AFS_UCRED *rv = NULL;
511 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
512 || (pr->state == TASK_UNINTERRUPTIBLE)
513 || (pr->state == TASK_STOPPED)) {
515 cr.cr_uid = task_uid(pr);
516 #if defined(AFS_LINUX26_ENV)
517 #if defined(STRUCT_TASK_HAS_CRED)
518 get_group_info(pr->cred->group_info);
519 cr.cr_group_info = pr->cred->group_info;
521 get_group_info(pr->group_info);
522 cr.cr_group_info = pr->group_info;
525 cr.cr_ngroups = pr->ngroups;
526 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
536 afs_osi_proc2cred(AFS_PROC * pr)
538 AFS_UCRED *rv = NULL;
549 #endif /* AFS_GCPAGS */