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;
238 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
240 read_lock(&tasklist_lock);
241 #endif /* EXPORTED_TASKLIST_LOCK */
242 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
243 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
245 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
247 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
249 #ifdef DEFINED_FOR_EACH_PROCESS
250 for_each_process(p) if (p->pid) {
251 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
255 if (p->state & TASK_ZOMBIE)
258 afs_GCPAGs_perproc_func(p);
261 for_each_task(p) if (p->pid) {
262 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
266 if (p->state & TASK_ZOMBIE)
269 afs_GCPAGs_perproc_func(p);
272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
274 read_unlock(&tasklist_lock);
275 #endif /* EXPORTED_TASKLIST_LOCK */
276 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) && defined(EXPORTED_TASKLIST_LOCK)
279 #endif /* EXPORTED_TASKLIST_LOCK && LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) */
281 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16) */
286 /* return a pointer (sometimes a static copy ) to the cred for a
288 * subsequent calls may overwrite the previously returned value.
291 #if defined(AFS_SGI65_ENV)
292 const struct AFS_UCRED *
293 afs_osi_proc2cred(AFS_PROC * p)
297 #elif defined(AFS_HPUX_ENV)
298 const struct AFS_UCRED *
299 afs_osi_proc2cred(AFS_PROC * p)
305 * Cannot use afs_warnuser() here, as the code path
306 * eventually wants to grab sched_lock, which is
312 #elif defined(AFS_AIX_ENV)
314 /* GLOBAL DECLARATIONS */
317 * LOCKS: the caller must do
318 * simple_lock(&proc_tbl_lock);
319 * simple_unlock(&proc_tbl_lock);
320 * around calls to this function.
323 const struct AFS_UCRED *
324 afs_osi_proc2cred(AFS_PROC * pproc)
326 struct AFS_UCRED *pcred = 0;
329 * pointer to process user structure valid in *our*
332 * The user structure for a process is stored in the user
333 * address space (as distinct from the kernel address
334 * space), and so to refer to the user structure of a
335 * different process we must employ special measures.
337 * I followed the example used in the AIX getproc() system
338 * call in bos/kernel/proc/getproc.c
340 struct user *xmem_userp;
342 struct xmem dp; /* ptr to xmem descriptor */
343 int xm; /* xmem result */
350 * The process private segment in which the user
351 * area is located may disappear. We need to increment
352 * its use count. Therefore we
353 * - get the proc_tbl_lock to hold the segment.
354 * - get the p_lock to lockout vm_cleardata.
355 * - vm_att to load the segment register (no check)
356 * - xmattach to bump its use count.
357 * - release the p_lock.
358 * - release the proc_tbl_lock.
359 * - do whatever we need.
360 * - xmdetach to decrement the use count.
361 * - vm_det to free the segment register (no check)
366 /* simple_lock(&proc_tbl_lock); */
368 if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
370 if (pproc->p_adspace != NULLSEGVAL) {
374 simple_lock(&pproc->p_pvprocp->pv_lock);
376 simple_lock(&pproc->p_lock);
379 if (pproc->p_threadcount &&
381 pproc->p_pvprocp->pv_threadlist) {
383 pproc->p_threadlist) {
387 * arbitrarily pick the first thread in pproc
389 struct thread *pproc_thread =
391 pproc->p_pvprocp->pv_threadlist;
397 * location of 'struct user' in pproc's
400 struct user *pproc_userp = pproc_thread->t_userp;
403 * create a pointer valid in my own address space
406 xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
408 dp.aspace_id = XMEM_INVAL;
409 xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
413 simple_unlock(&pproc->p_pvprocp->pv_lock);
415 simple_unlock(&pproc->p_lock);
418 /* simple_unlock(&proc_tbl_lock); */
419 if (xm == XMEM_SUCC) {
421 static struct AFS_UCRED cred;
424 * What locking should we use to protect access to the user
425 * area? If needed also change the code in AIX/osi_groups.c.
428 /* copy cred to local address space */
429 cred = *xmem_userp->U_cred;
435 vm_det((void *)xmem_userp);
441 #elif defined(AFS_OSF_ENV)
442 const struct AFS_UCRED *
443 afs_osi_proc2cred(AFS_PROC * pr)
445 struct AFS_UCRED *rv = NULL;
451 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
452 || (pr->p_stat == SSTOP))
457 #elif defined(AFS_DARWIN80_ENV)
458 const struct AFS_UCRED *
459 afs_osi_proc2cred(AFS_PROC * pr)
461 struct AFS_UCRED *rv = NULL;
462 static struct AFS_UCRED cr;
468 pcred = proc_ucred(pr);
470 cr.cr_uid = pcred->cr_uid;
471 cr.cr_ngroups = pcred->cr_ngroups;
472 memcpy(cr.cr_groups, pcred->cr_groups,
473 NGROUPS * sizeof(gid_t));
476 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
477 const struct AFS_UCRED *
478 afs_osi_proc2cred(AFS_PROC * pr)
480 struct AFS_UCRED *rv = NULL;
481 static struct AFS_UCRED cr;
487 if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
488 || (pr->p_stat == SSTOP)) {
491 cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
492 cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
493 memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
494 NGROUPS * sizeof(gid_t));
501 #elif defined(AFS_LINUX22_ENV)
502 const struct AFS_UCRED *
503 afs_osi_proc2cred(AFS_PROC * pr)
505 struct AFS_UCRED *rv = NULL;
506 static struct AFS_UCRED cr;
512 if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
513 || (pr->state == TASK_UNINTERRUPTIBLE)
514 || (pr->state == TASK_STOPPED)) {
516 cr.cr_uid = task_uid(pr);
517 #if defined(AFS_LINUX26_ENV)
518 #if defined(STRUCT_TASK_HAS_CRED)
519 get_group_info(pr->cred->group_info);
520 cr.cr_group_info = pr->cred->group_info;
522 get_group_info(pr->group_info);
523 cr.cr_group_info = pr->group_info;
526 cr.cr_ngroups = pr->ngroups;
527 memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
535 const struct AFS_UCRED *
536 afs_osi_proc2cred(AFS_PROC * pr)
538 struct AFS_UCRED *rv = NULL;
549 #endif /* AFS_GCPAGS */