keyring-gcpags-20060927
[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 void
230 afs_osi_TraverseProcTable()
231 {
232 #if !defined(LINUX_KEYRING_SUPPORT)
233     extern rwlock_t tasklist_lock __attribute__((weak));
234     struct task_struct *p;
235  
236     if (&tasklist_lock)
237        read_lock(&tasklist_lock);
238 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
239     else
240         rcu_read_lock();
241 #endif
242
243 #ifdef DEFINED_FOR_EACH_PROCESS
244     for_each_process(p) if (p->pid) {
245 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
246         if (p->exit_state)
247             continue;
248 #else
249         if (p->state & TASK_ZOMBIE)
250             continue;
251 #endif
252         afs_GCPAGs_perproc_func(p);
253     }
254 #else
255     for_each_task(p) if (p->pid) {
256 #ifdef STRUCT_TASK_STRUCT_HAS_EXIT_STATE
257         if (p->exit_state)
258             continue;
259 #else
260         if (p->state & TASK_ZOMBIE)
261             continue;
262 #endif
263         afs_GCPAGs_perproc_func(p);
264     }
265 #endif
266     if (&tasklist_lock)
267        read_unlock(&tasklist_lock);
268 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
269     else
270         rcu_read_unlock();
271 #endif
272 #endif
273 }
274 #endif
275
276 /* return a pointer (sometimes a static copy ) to the cred for a
277  * given AFS_PROC.
278  * subsequent calls may overwrite the previously returned value.
279  */
280
281 #if defined(AFS_SGI65_ENV)
282 const struct AFS_UCRED *
283 afs_osi_proc2cred(AFS_PROC * p)
284 {
285     return NULL;
286 }
287 #elif defined(AFS_HPUX_ENV)
288 const struct AFS_UCRED *
289 afs_osi_proc2cred(AFS_PROC * p)
290 {
291     if (!p)
292         return;
293
294     /*
295      * Cannot use afs_warnuser() here, as the code path
296      * eventually wants to grab sched_lock, which is
297      * already held here
298      */
299
300     return p_cred(p);
301 }
302 #elif defined(AFS_AIX_ENV)
303
304 /* GLOBAL DECLARATIONS */
305
306 /*
307  * LOCKS: the caller must do
308  *  simple_lock(&proc_tbl_lock);
309  *  simple_unlock(&proc_tbl_lock);
310  * around calls to this function.
311  */
312
313 const struct AFS_UCRED *
314 afs_osi_proc2cred(AFS_PROC * pproc)
315 {
316     struct AFS_UCRED *pcred = 0;
317
318     /*
319      * pointer to process user structure valid in *our*
320      * address space
321      *
322      * The user structure for a process is stored in the user
323      * address space (as distinct from the kernel address
324      * space), and so to refer to the user structure of a
325      * different process we must employ special measures.
326      *
327      * I followed the example used in the AIX getproc() system
328      * call in bos/kernel/proc/getproc.c
329      */
330     struct user *xmem_userp;
331
332     struct xmem dp;             /* ptr to xmem descriptor */
333     int xm;                     /* xmem result */
334
335     if (!pproc) {
336         return pcred;
337     }
338
339     /*
340      * The process private segment in which the user
341      * area is located may disappear. We need to increment
342      * its use count. Therefore we
343      *    - get the proc_tbl_lock to hold the segment.
344      *    - get the p_lock to lockout vm_cleardata.
345      *    - vm_att to load the segment register (no check)
346      *    - xmattach to bump its use count.
347      *    - release the p_lock.
348      *    - release the proc_tbl_lock.
349      *    - do whatever we need.
350      *    - xmdetach to decrement the use count.
351      *    - vm_det to free the segment register (no check)
352      */
353
354     xmem_userp = NULL;
355     xm = XMEM_FAIL;
356     /* simple_lock(&proc_tbl_lock); */
357 #ifdef __64BIT__
358     if (pproc->p_adspace != vm_handle(NULLSEGID, (int32long64_t) 0)) {
359 #else
360     if (pproc->p_adspace != NULLSEGVAL) {
361 #endif
362
363 #ifdef AFS_AIX51_ENV
364         simple_lock(&pproc->p_pvprocp->pv_lock);
365 #else
366         simple_lock(&pproc->p_lock);
367 #endif
368
369         if (pproc->p_threadcount &&
370 #ifdef AFS_AIX51_ENV
371             pproc->p_pvprocp->pv_threadlist) {
372 #else
373             pproc->p_threadlist) {
374 #endif
375
376             /*
377              * arbitrarily pick the first thread in pproc
378              */
379             struct thread *pproc_thread =
380 #ifdef AFS_AIX51_ENV
381                 pproc->p_pvprocp->pv_threadlist;
382 #else
383                 pproc->p_threadlist;
384 #endif
385
386             /*
387              * location of 'struct user' in pproc's
388              * address space
389              */
390             struct user *pproc_userp = pproc_thread->t_userp;
391
392             /*
393              * create a pointer valid in my own address space
394              */
395
396             xmem_userp = (struct user *)vm_att(pproc->p_adspace, pproc_userp);
397
398             dp.aspace_id = XMEM_INVAL;
399             xm = xmattach(xmem_userp, sizeof(*xmem_userp), &dp, SYS_ADSPACE);
400         }
401
402 #ifdef AFS_AIX51_ENV
403         simple_unlock(&pproc->p_pvprocp->pv_lock);
404 #else
405         simple_unlock(&pproc->p_lock);
406 #endif
407     }
408     /* simple_unlock(&proc_tbl_lock); */
409     if (xm == XMEM_SUCC) {
410
411         static struct AFS_UCRED cred;
412
413         /*
414          * What locking should we use to protect access to the user
415          * area?  If needed also change the code in AIX/osi_groups.c.
416          */
417
418         /* copy cred to local address space */
419         cred = *xmem_userp->U_cred;
420         pcred = &cred;
421
422         xmdetach(&dp);
423     }
424     if (xmem_userp) {
425         vm_det((void *)xmem_userp);
426     }
427
428     return pcred;
429 }
430
431 #elif defined(AFS_OSF_ENV)
432 const struct AFS_UCRED *
433 afs_osi_proc2cred(AFS_PROC * pr)
434 {
435     struct AFS_UCRED *rv = NULL;
436
437     if (pr == NULL) {
438         return NULL;
439     }
440
441     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
442         || (pr->p_stat == SSTOP))
443         rv = pr->p_rcred;
444
445     return rv;
446 }
447 #elif defined(AFS_DARWIN80_ENV) 
448 const struct AFS_UCRED *
449 afs_osi_proc2cred(AFS_PROC * pr)
450 {
451     struct AFS_UCRED *rv = NULL;
452     static struct AFS_UCRED cr;
453     struct ucred *pcred;
454
455     if (pr == NULL) {
456         return NULL;
457     }
458     pcred = proc_ucred(pr);
459     cr.cr_ref = 1;
460     cr.cr_uid = pcred->cr_uid;
461     cr.cr_ngroups = pcred->cr_ngroups;
462     memcpy(cr.cr_groups, pcred->cr_groups,
463            NGROUPS * sizeof(gid_t));
464     return &cr;
465 }
466 #elif defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
467 const struct AFS_UCRED *
468 afs_osi_proc2cred(AFS_PROC * pr)
469 {
470     struct AFS_UCRED *rv = NULL;
471     static struct AFS_UCRED cr;
472
473     if (pr == NULL) {
474         return NULL;
475     }
476
477     if ((pr->p_stat == SSLEEP) || (pr->p_stat == SRUN)
478         || (pr->p_stat == SSTOP)) {
479         pcred_readlock(pr);
480         cr.cr_ref = 1;
481         cr.cr_uid = pr->p_cred->pc_ucred->cr_uid;
482         cr.cr_ngroups = pr->p_cred->pc_ucred->cr_ngroups;
483         memcpy(cr.cr_groups, pr->p_cred->pc_ucred->cr_groups,
484                NGROUPS * sizeof(gid_t));
485         pcred_unlock(pr);
486         rv = &cr;
487     }
488
489     return rv;
490 }
491 #elif defined(AFS_LINUX22_ENV)
492 const struct AFS_UCRED *
493 afs_osi_proc2cred(AFS_PROC * pr)
494 {
495     struct AFS_UCRED *rv = NULL;
496     static struct AFS_UCRED cr;
497
498     if (pr == NULL) {
499         return NULL;
500     }
501
502     if ((pr->state == TASK_RUNNING) || (pr->state == TASK_INTERRUPTIBLE)
503         || (pr->state == TASK_UNINTERRUPTIBLE)
504         || (pr->state == TASK_STOPPED)) {
505         cr.cr_ref = 1;
506         cr.cr_uid = pr->uid;
507 #if defined(AFS_LINUX26_ENV)
508         get_group_info(pr->group_info);
509         cr.cr_group_info = pr->group_info;
510 #else
511         cr.cr_ngroups = pr->ngroups;
512         memcpy(cr.cr_groups, pr->groups, NGROUPS * sizeof(gid_t));
513 #endif
514         rv = &cr;
515     }
516
517     return rv;
518 }
519 #else
520 const struct AFS_UCRED *
521 afs_osi_proc2cred(AFS_PROC * pr)
522 {
523     struct AFS_UCRED *rv = NULL;
524
525     if (pr == NULL) {
526         return NULL;
527     }
528     rv = pr->p_cred;
529
530     return rv;
531 }
532 #endif
533
534 #endif /* AFS_GCPAGS */