xref: /csrg-svn/sys/kern/kern_sysctl.c (revision 40813)
139963Smarc /*
239963Smarc  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
339963Smarc  * All rights reserved.
439963Smarc  *
539963Smarc  * Redistribution and use in source and binary forms are permitted
639963Smarc  * provided that the above copyright notice and this paragraph are
739963Smarc  * duplicated in all such forms and that any documentation,
839963Smarc  * advertising materials, and other materials related to such
939963Smarc  * distribution and use acknowledge that the software was developed
1039963Smarc  * by the University of California, Berkeley.  The name of the
1139963Smarc  * University may not be used to endorse or promote products derived
1239963Smarc  * from this software without specific prior written permission.
1339963Smarc  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1439963Smarc  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1539963Smarc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1639963Smarc  *
17*40813Smarc  *	@(#)kern_sysctl.c	7.6 (Berkeley) 04/05/90
1839963Smarc  */
1939963Smarc 
2039965Smckusick #include "param.h"
2139965Smckusick #include "user.h"
2239965Smckusick #include "proc.h"
2340206Smarc #include "text.h"
2439963Smarc #include "kinfo.h"
2539963Smarc #include "vm.h"
2639963Smarc #include "ioctl.h"
2739963Smarc #include "tty.h"
2839963Smarc #include "buf.h"
2939963Smarc 
3040068Smarc 
3140068Smarc #define snderr(e) { error = (e); goto release;}
3240068Smarc extern int kinfo_doproc(), kinfo_rtable();
3340068Smarc struct kinfo_lock kinfo_lock;
3440068Smarc 
3540206Smarc getkerninfo()
3639963Smarc {
3739963Smarc 	register struct a {
3839963Smarc 		int	op;
3939963Smarc 		char	*where;
4039963Smarc 		int	*size;
4139963Smarc 		int	arg;
4239963Smarc 	} *uap = (struct a *)u.u_ap;
4340206Smarc 
4440068Smarc 	int	bufsize,	/* max size of users buffer */
4540206Smarc 		needed,	locked, (*server)(), error = 0;
4639963Smarc 
4740206Smarc 	if (error = copyin((caddr_t)uap->size,
4840206Smarc 				(caddr_t)&bufsize, sizeof (bufsize)))
49*40813Smarc 		goto done;
5040068Smarc 
5139963Smarc 	switch (ki_type(uap->op)) {
5239963Smarc 
5339963Smarc 	case KINFO_PROC:
5440068Smarc 		server = kinfo_doproc;
5539963Smarc 		break;
5639963Smarc 
5740068Smarc 	case KINFO_RT:
5840068Smarc 		server = kinfo_rtable;
5940068Smarc 		break;
6040068Smarc 
6139963Smarc 	default:
6240206Smarc 		error = EINVAL;
63*40813Smarc 		goto done;
6439963Smarc 	}
65*40813Smarc 	if (uap->where == NULL || uap->size == NULL) {
66*40813Smarc 		error = (*server)(uap->op, NULL, NULL, uap->arg, &needed);
67*40813Smarc 		goto done;
68*40813Smarc 	}
6940206Smarc 	while (kinfo_lock.kl_lock) {
7040206Smarc 		kinfo_lock.kl_want++;
7140206Smarc 		sleep(&kinfo_lock, PRIBIO+1);
7240206Smarc 		kinfo_lock.kl_want--;
7340206Smarc 		kinfo_lock.kl_locked++;
7440206Smarc 	}
7540206Smarc 	kinfo_lock.kl_lock++;
7640206Smarc 
77*40813Smarc 	if (!useracc(uap->where, bufsize, B_WRITE))
7840068Smarc 		snderr(EFAULT);
7940068Smarc 	/*
8040068Smarc 	 * lock down target pages - NEED DEADLOCK AVOIDANCE
8140068Smarc 	 */
82*40813Smarc 	if (bufsize > ((int)ptob(freemem) - (20 * 1024))) 	/* XXX */
8340068Smarc 		snderr(ENOMEM);
84*40813Smarc 	vslock(uap->where, bufsize);
85*40813Smarc 	locked = bufsize;
86*40813Smarc 	error = (*server)(uap->op, uap->where, &bufsize, uap->arg, &needed);
8740068Smarc 	vsunlock(uap->where, locked, B_WRITE);
88*40813Smarc 	if (error == 0)
89*40813Smarc 		error = copyout((caddr_t)&bufsize,
90*40813Smarc 				(caddr_t)uap->size, sizeof (bufsize));
9140068Smarc release:
9240068Smarc 	kinfo_lock.kl_lock--;
9340068Smarc 	if (kinfo_lock.kl_want)
9440068Smarc 		wakeup(&kinfo_lock);
95*40813Smarc done:
9640068Smarc 	if (error)
9740068Smarc 		u.u_error = error;
9840068Smarc 	else
9940068Smarc 		u.u_r.r_val1 = needed;
10039963Smarc }
10139963Smarc 
10239963Smarc /*
10339963Smarc  * try over estimating by 5 procs
10439963Smarc  */
10539963Smarc #define KINFO_PROCSLOP	(5 * sizeof (struct kinfo_proc))
10639963Smarc 
10740206Smarc kinfo_doproc(op, where, acopysize, arg, aneeded)
10839963Smarc 	char *where;
10939963Smarc 	int *acopysize, *aneeded;
11039963Smarc {
11139963Smarc 	register struct proc *p;
11239963Smarc 	register caddr_t dp = (caddr_t)where;
11339963Smarc 	register needed = 0;
11439963Smarc 	int buflen;
11539963Smarc 	int doingzomb;
11640067Smarc 	struct eproc eproc;
11739963Smarc 	struct tty *tp;
11839963Smarc 	int error = 0;
11939963Smarc 
12039963Smarc 	if (where != NULL)
12139963Smarc 		buflen = *acopysize;
12239963Smarc 
12339963Smarc 	p = allproc;
12439963Smarc 	doingzomb = 0;
12539963Smarc again:
12639963Smarc 	for (; p != NULL; p = p->p_nxt) {
12739963Smarc 		/*
12839963Smarc 		 * TODO - make more efficient (see notes below).
12939963Smarc 		 * do by session.
13039963Smarc 		 */
13139963Smarc 		switch (ki_op(op)) {
13239963Smarc 
13339963Smarc 		case KINFO_PROC_PID:
13439963Smarc 			/* could do this with just a lookup */
13539963Smarc 			if (p->p_pid != (pid_t)arg)
13639963Smarc 				continue;
13739963Smarc 			break;
13839963Smarc 
13939963Smarc 		case KINFO_PROC_PGRP:
14039963Smarc 			/* could do this by traversing pgrp */
14139963Smarc 			if (p->p_pgrp->pg_id != (pid_t)arg)
14239963Smarc 				continue;
14339963Smarc 			break;
14439963Smarc 
14539963Smarc 		case KINFO_PROC_TTY:
14639963Smarc 			if ((p->p_flag&SCTTY) == 0 ||
14739963Smarc 			    p->p_session->s_ttyp == NULL ||
14839963Smarc 			    p->p_session->s_ttyp->t_dev != (dev_t)arg)
14939963Smarc 				continue;
15039963Smarc 			break;
15139963Smarc 
15239963Smarc 		case KINFO_PROC_UID:
15339963Smarc 			if (p->p_uid != (uid_t)arg)
15439963Smarc 				continue;
15539963Smarc 			break;
15639963Smarc 
15739963Smarc 		case KINFO_PROC_RUID:
15839963Smarc 			if (p->p_ruid != (uid_t)arg)
15939963Smarc 				continue;
16039963Smarc 			break;
16139963Smarc 		}
16239963Smarc 		if (where != NULL && buflen >= sizeof (struct kinfo_proc)) {
16340206Smarc 			register struct text *txt;
16440206Smarc 
16539963Smarc 			if (error = copyout((caddr_t)p, dp,
16639963Smarc 			    sizeof (struct proc)))
16739963Smarc 				return (error);
16839963Smarc 			dp += sizeof (struct proc);
169*40813Smarc 			/*
170*40813Smarc 			 *	XXX NEED ALLIGNMENT
171*40813Smarc 			 */
17240206Smarc 			eproc.e_paddr = p;
17340206Smarc 			eproc.e_sess = p->p_pgrp->pg_session;
17440206Smarc 			eproc.e_pgid = p->p_pgrp->pg_id;
17540206Smarc 			eproc.e_jobc = p->p_pgrp->pg_jobc;
17639963Smarc 			tp = p->p_pgrp->pg_session->s_ttyp;
17739963Smarc 			if ((p->p_flag&SCTTY) && tp != NULL) {
17840206Smarc 				eproc.e_tdev = tp->t_dev;
17940206Smarc 				eproc.e_tpgid = tp->t_pgrp ?
18039963Smarc 					tp->t_pgrp->pg_id : -1;
18140206Smarc 				eproc.e_tsess = tp->t_session;
18239963Smarc 			} else
18340206Smarc 				eproc.e_tdev = NODEV;
18440206Smarc 			if (p->p_wmesg)
18540206Smarc 				strncpy(eproc.e_wmesg, p->p_wmesg, WMESGLEN);
18640206Smarc 			if (txt = p->p_textp) {
18740206Smarc 				eproc.e_xsize = txt->x_size;
18840206Smarc 				eproc.e_xrssize = txt->x_rssize;
18940206Smarc 				eproc.e_xccount = txt->x_ccount;
19040206Smarc 				eproc.e_xswrss = txt->x_swrss;
19140206Smarc 			} else {
19240206Smarc 				eproc.e_xsize = eproc.e_xrssize =
19340206Smarc 				  eproc.e_xccount =  eproc.e_xswrss = 0;
19440206Smarc 			}
19540067Smarc 			if (error = copyout((caddr_t)&eproc, dp,
19640067Smarc 			    sizeof (eproc)))
19739963Smarc 				return (error);
19840067Smarc 			dp += sizeof (eproc);
19939963Smarc 			buflen -= sizeof (struct kinfo_proc);
20039963Smarc 		}
20139963Smarc 		needed += sizeof (struct kinfo_proc);
20239963Smarc 	}
20339963Smarc 	if (doingzomb == 0) {
20439963Smarc 		p = zombproc;
20539963Smarc 		doingzomb++;
20639963Smarc 		goto again;
20739963Smarc 	}
20839963Smarc 	if (where != NULL)
20939963Smarc 		*acopysize = dp - where;
21040068Smarc 	else
21140068Smarc 		needed += KINFO_PROCSLOP;
21239963Smarc 	*aneeded = needed;
21339963Smarc 
21439963Smarc 	return (0);
21539963Smarc }
216