f5d240163874b43acad1f016e8bcee7dafbd96b7
[openafs.git] / src / afs / afs_osi_gcpags.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
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
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
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 */
19 #ifdef AFS_AIX_ENV
20 #include <sys/adspace.h>        /* for vm_att(), vm_det() */
21 #endif
22
23 #if AFS_GCPAGS
24
25 /* afs_osi_TraverseProcTable() - Walk through the systems process
26  * table, calling afs_GCPAGs_perproc_func() for each process.
27  */
28
29 #if defined(AFS_SUN5_ENV)
30 void
31 afs_osi_TraverseProcTable(void)
32 {
33     struct proc *prp;
34     for (prp = practive; prp != NULL; prp = prp->p_next) {
35         afs_GCPAGs_perproc_func(prp);
36     }
37 }
38 #endif
39
40 #if defined(AFS_HPUX_ENV)
41
42 /*
43  * NOTE: h/proc_private.h gives the process table locking rules
44  * It indicates that access to p_cred must be protected by
45  * mp_mtproc_lock(p);
46  * mp_mtproc_unlock(p);
47  *
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.
51  */
52
53 void
54 afs_osi_TraverseProcTable(void)
55 {
56     register proc_t *p;
57     int endchain = 0;
58
59     MP_SPINLOCK(activeproc_lock);
60     MP_SPINLOCK(sched_lock);
61     pcred_lock();
62
63     /*
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.
67      *
68      * We hold the locks for the entire scan in order to get a
69      * consistent view of the current set of creds.
70      */
71
72     for (p = proc; endchain == 0; p = &proc[p->p_fandx]) {
73         if (p->p_fandx == 0) {
74             endchain = 1;
75         }
76
77         if (system_proc(p))
78             continue;
79
80         mp_mtproc_lock(p);
81         afs_GCPAGs_perproc_func(p);
82         mp_mtproc_unlock(p);
83     }
84
85     pcred_unlock();
86     MP_SPINUNLOCK(sched_lock);
87     MP_SPINUNLOCK(activeproc_lock);
88 }
89 #endif
90
91 #if defined(AFS_SGI_ENV)
92
93 #ifdef AFS_SGI65_ENV
94 /* TODO: Fix this later. */
95 static int
96 SGI_ProcScanFunc(void *p, void *arg, int mode)
97 {
98     return 0;
99 }
100 #else /* AFS_SGI65_ENV */
101 static int
102 SGI_ProcScanFunc(proc_t * p, void *arg, int mode)
103 {
104     afs_int32(*perproc_func) (struct proc *) = arg;
105     int code = 0;
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.
109      */
110     if (mode == 1) {
111         code = perproc_func(p);
112     }
113     return code;
114 }
115 #endif /* AFS_SGI65_ENV */
116
117 void
118 afs_osi_TraverseProcTable(void)
119 {
120     procscan(SGI_ProcScanFunc, afs_GCPAGs_perproc_func);
121 }
122 #endif /* AFS_SGI_ENV */
123
124 #if defined(AFS_AIX_ENV)
125 #ifdef AFS_AIX51_ENV
126 #define max_proc v.ve_proc
127 #endif
128 void
129 afs_osi_TraverseProcTable(void)
130 {
131     struct proc *p;
132     int i;
133
134     /*
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.
138      */
139     if (!afs_gcpags_procsize)
140         return;
141
142 #ifndef AFS_AIX51_ENV
143     simple_lock(&proc_tbl_lock);
144 #endif
145     for (p = (struct proc *)v.vb_proc, i = 0; p < max_proc;
146          p = (struct proc *)((char *)p + afs_gcpags_procsize), i++) {
147
148 #ifdef AFS_AIX51_ENV
149         if (p->p_pvprocp->pv_stat == SNONE)
150             continue;
151         if (p->p_pvprocp->pv_stat == SIDL)
152             continue;
153         if (p->p_pvprocp->pv_stat == SEXIT)
154             continue;
155 #else
156         if (p->p_stat == SNONE)
157             continue;
158         if (p->p_stat == SIDL)
159             continue;
160         if (p->p_stat == SEXIT)
161             continue;
162 #endif
163
164         /* sanity check */
165
166         if (PROCMASK(p->p_pid) != i) {
167             afs_gcpags = AFS_GCPAGS_EPIDCHECK;
168             break;
169         }
170
171         /* sanity check */
172
173         if ((p->p_nice < P_NICE_MIN) || (P_NICE_MAX < p->p_nice)) {
174             afs_gcpags = AFS_GCPAGS_ENICECHECK;
175             break;
176         }
177
178         afs_GCPAGs_perproc_func(p);
179     }
180 #ifndef AFS_AIX51_ENV
181     simple_unlock(&proc_tbl_lock);
182 #endif
183 }
184 #endif
185
186 #if defined(AFS_OSF_ENV)
187
188 #ifdef AFS_DUX50_ENV
189 extern struct pid_entry *pidtab;
190 extern int npid; 
191 #endif
192
193 void
194 afs_osi_TraverseProcTable(void)
195 {
196     struct pid_entry *pe;
197 #ifdef AFS_DUX50_ENV
198 #define pidNPID (pidtab + npid)
199 #define PID_LOCK()
200 #define PID_UNLOCK()
201 #endif
202     PID_LOCK();
203     for (pe = pidtab; pe < pidNPID; ++pe) {
204         if (pe->pe_proc != PROC_NULL)
205             afs_GCPAGs_perproc_func(pe->pe_proc);
206     }
207     PID_UNLOCK();
208 }
209 #endif
210
211 #if (defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)) || defined(AFS_FBSD_ENV)
212 void
213 afs_osi_TraverseProcTable(void)
214 {
215     struct proc *p;
216     LIST_FOREACH(p, &allproc, p_list) {
217         if (p->p_stat == SIDL)
218             continue;
219         if (p->p_stat == SZOMB)
220             continue;
221         if (p->p_flag & P_SYSTEM)
222             continue;
223         afs_GCPAGs_perproc_func(p);
224     }
225 }
226 #endif
227
228 #if defined(AFS_LINUX22_ENV)
229 #ifdef EXPORTED_TASKLIST_LOCK
230 extern rwlock_t tasklist_lock __attribute__((weak));
231 #endif
232 void
233 afs_osi_TraverseProcTable()
234 {
235 #if !defined(LINUX_KEYRING_SUPPORT)
236     struct task_struct *p;
237 #ifdef EXPORTED_TASKLIST_LOCK
238     if (&tasklist_lock)
239        read_lock(&tasklist_lock);
240 #endif
241 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
242 #ifdef EXPORTED_TASKLIST_LOCK
243     else
244 #endif
245         rcu_read_lock();
246 #endif
247
248 #ifdef DEFINED_FOR_EACH_PROCESS
249     for_each_process(p) if (p->pid) {
250 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
251         if (p->exit_state)
252             continue;
253 #else
254         if (p->state & TASK_ZOMBIE)
255             continue;
256 #endif
257         afs_GCPAGs_perproc_func(p);
258     }
259 #else
260     for_each_task(p) if (p->pid) {
261 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
262         if (p->exit_state)
263             continue;
264 #else
265         if (p->state & TASK_ZOMBIE)
266             continue;
267 #endif
268         afs_GCPAGs_perproc_func(p);
269     }
270 #endif
271 #ifdef EXPORTED_TASKLIST_LOCK
272     if (&tasklist_lock)
273        read_unlock(&tasklist_lock);
274 #endif
275 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
276 #ifdef EXPORTED_TASKLIST_LOCK
277     else
278 #endif
279         rcu_read_unlock();
280 #endif
281 #endif
282 }
283 #endif
284
285 /* return a pointer (sometimes a static copy ) to the cred for a
286  * given AFS_PROC.
287  * subsequent calls may overwrite the previously returned value.
288  */
289
290 #if defined(AFS_SGI65_ENV)
291 const struct AFS_UCRED *
292 afs_osi_proc2cred(AFS_PROC * p)
293 {
294     return NULL;
295 }
296 #elif defined(AFS_HPUX_ENV)
297 const struct AFS_UCRED *
298 afs_osi_proc2cred(AFS_PROC * p)
299 {
300     if (!p)
301         return;
302
303     /*
304      * Cannot use afs_warnuser() here, as the code path
305      * eventually wants to grab sched_lock, which is
306      * already held here
307      */
308
309     return p_cred(p);
310 }
311 #elif defined(AFS_AIX_ENV)
312
313 /* GLOBAL DECLARATIONS */
314
315 /*
316  * LOCKS: the caller must do
317  *  simple_lock(&proc_tbl_lock);
318  *  simple_unlock(&proc_tbl_lock);
319  * around calls to this function.
320  */
321
322 const struct AFS_UCRED *
323 afs_osi_proc2cred(AFS_PROC * pproc)
324 {
325     struct AFS_UCRED *pcred = 0;
326
327     /*
328      * pointer to process user structure valid in *our*
329      * address space
330      *
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.
335      *
336      * I followed the example used in the AIX getproc() system
337      * call in bos/kernel/proc/getproc.c
338      */
339     struct user *xmem_userp;
340
341     struct xmem dp;             /* ptr to xmem descriptor */
342     int xm;                     /* xmem result */
343
344     if (!pproc) {
345         return pcred;
346     }
347
348     /*
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)
361      */
362
363     xmem_userp = NULL;
364     xm = XMEM_FAIL;
365     /* simple_lock(&proc_tbl_lock); */
366 #ifdef __64BIT__
367     if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
368 #else
369     if (pproc->p_adspace != NULLSEGVAL) {
370 #endif
371
372 #ifdef AFS_AIX51_ENV
373         simple_lock(&pproc->p_pvprocp->pv_lock);
374 #else
375         simple_lock(&pproc->p_lock);
376 #endif
377
378         if (pproc->p_threadcount &&
379 #ifdef AFS_AIX51_ENV
380             pproc->p_pvprocp->pv_threadlist) {
381 #else
382             pproc->p_threadlist) {
383 #endif
384
385             /*
386              * arbitrarily pick the first thread in pproc
387              */
388             struct thread *pproc_thread =
389 #ifdef AFS_AIX51_ENV
390                 pproc->p_pvprocp->pv_threadlist;
391 #else
392                 pproc->p_threadlist;
393 #endif
394
395             /*
396              * location of 'struct user' in pproc's
397              * address space
398              */
399             struct user *pproc_userp = pproc_thread->t_userp;
400
401             /*
402              * create a pointer valid in my own address space
403              */
404
405             xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
406
407             dp.aspace_id = XMEM_INVAL;
408             xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
409         }
410
411 #ifdef AFS_AIX51_ENV
412         simple_unlock(&pproc->p_pvprocp->pv_lock);
413 #else
414         simple_unlock(&pproc->p_lock);
415 #endif
416     }
417     /* simple_unlock(&proc_tbl_lock); */
418     if (xm == XMEM_SUCC) {
419
420         static struct AFS_UCRED cred;
421
422         /*
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.
425          */
426
427         /* copy cred to local address space */
428         cred = *xmem_userp->U_cred;
429         pcred = &cred;
430
431         xmdetach(&dp);
432     }
433     if (xmem_userp) {
434         vm_det((void *)xmem_userp);
435     }
436
437     return pcred;
438 }
439
440 #elif defined(AFS_OSF_ENV)
441 const struct AFS_UCRED *
442 afs_osi_proc2cred(AFS_PROC * pr)
443 {
444     struct AFS_UCRED *rv = NULL;
445
446     if (pr == NULL) {
447         return NULL;
448     }
449
450     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
451         || (pr->p_stat == SSTOP))
452         rv = pr->p_rcred;
453
454     return rv;
455 }
456 #elif defined(AFS_DARWIN80_ENV) 
457 const struct AFS_UCRED *
458 afs_osi_proc2cred(AFS_PROC * pr)
459 {
460     struct AFS_UCRED *rv = NULL;
461     static struct AFS_UCRED cr;
462     struct ucred *pcred;
463
464     if (pr == NULL) {
465         return NULL;
466     }
467     pcred = proc_ucred(pr);
468     cr.cr_ref = 1;
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));
473     return &cr;
474 }
475 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
476 const struct AFS_UCRED *
477 afs_osi_proc2cred(AFS_PROC * pr)
478 {
479     struct AFS_UCRED *rv = NULL;
480     static struct AFS_UCRED cr;
481
482     if (pr == NULL) {
483         return NULL;
484     }
485
486     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
487         || (pr->p_stat == SSTOP)) {
488         pcred_readlock(pr);
489         cr.cr_ref = 1;
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));
494         pcred_unlock(pr);
495         rv = &cr;
496     }
497
498     return rv;
499 }
500 #elif defined(AFS_LINUX22_ENV)
501 const struct AFS_UCRED *
502 afs_osi_proc2cred(AFS_PROC * pr)
503 {
504     struct AFS_UCRED *rv = NULL;
505     static struct AFS_UCRED cr;
506
507     if (pr == NULL) {
508         return NULL;
509     }
510
511     if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
512         || (pr->state == TASK_UNINTERRUPTIBLE)
513         || (pr->state == TASK_STOPPED)) {
514         cr.cr_ref = 1;
515         cr.cr_uid = pr->uid;
516 #if defined(AFS_LINUX26_ENV)
517         get_group_info(pr->group_info);
518         cr.cr_group_info = pr->group_info;
519 #else
520         cr.cr_ngroups = pr->ngroups;
521         memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
522 #endif
523         rv = &cr;
524     }
525
526     return rv;
527 }
528 #else
529 const struct AFS_UCRED *
530 afs_osi_proc2cred(AFS_PROC * pr)
531 {
532     struct AFS_UCRED *rv = NULL;
533
534     if (pr == NULL) {
535         return NULL;
536     }
537     rv = pr->p_cred;
538
539     return rv;
540 }
541 #endif
542
543 #endif /* AFS_GCPAGS */