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