xref: /csrg-svn/sys/kern/kern_resource.c (revision 1784)
1*1784Sbill /*	kern_resource.c	4.2	11/09/80	*/
27Sbill 
37Sbill #include "../h/param.h"
47Sbill #include "../h/systm.h"
57Sbill #include "../h/acct.h"
67Sbill #include "../h/dir.h"
77Sbill #include "../h/user.h"
87Sbill #include "../h/inode.h"
97Sbill #include "../h/proc.h"
107Sbill #include "../h/seg.h"
117Sbill 
127Sbill /*
137Sbill  * Perform process accounting functions.
147Sbill  */
157Sbill 
167Sbill sysacct()
177Sbill {
187Sbill 	register struct inode *ip;
197Sbill 	register struct a {
207Sbill 		char	*fname;
217Sbill 	} *uap;
227Sbill 
237Sbill 	uap = (struct a *)u.u_ap;
247Sbill 	if (suser()) {
257Sbill 		if (uap->fname==NULL) {
267Sbill 			if (acctp) {
277Sbill 				plock(acctp);
287Sbill 				iput(acctp);
297Sbill 				acctp = NULL;
307Sbill 			}
317Sbill 			return;
327Sbill 		}
337Sbill 		if (acctp) {
347Sbill 			u.u_error = EBUSY;
357Sbill 			return;
367Sbill 		}
377Sbill 		ip = namei(uchar, 0);
387Sbill 		if(ip == NULL)
397Sbill 			return;
407Sbill 		if((ip->i_mode & IFMT) != IFREG) {
417Sbill 			u.u_error = EACCES;
427Sbill 			iput(ip);
437Sbill 			return;
447Sbill 		}
457Sbill 		acctp = ip;
467Sbill 		prele(ip);
477Sbill 	}
487Sbill }
497Sbill 
507Sbill /*
517Sbill  * On exit, write a record on the accounting file.
527Sbill  */
537Sbill acct()
547Sbill {
557Sbill 	register i;
567Sbill 	register struct inode *ip;
577Sbill 	off_t siz;
587Sbill 
597Sbill 	if ((ip=acctp)==NULL)
607Sbill 		return;
617Sbill 	plock(ip);
627Sbill 	for (i=0; i<sizeof(acctbuf.ac_comm); i++)
637Sbill 		acctbuf.ac_comm[i] = u.u_comm[i];
64*1784Sbill 	acctbuf.ac_utime = compress((long)u.u_vm.vm_utime);
65*1784Sbill 	acctbuf.ac_stime = compress((long)u.u_vm.vm_stime);
66*1784Sbill 	acctbuf.ac_etime = compress((long)(time - u.u_start));
677Sbill 	acctbuf.ac_btime = u.u_start;
687Sbill 	acctbuf.ac_uid = u.u_ruid;
697Sbill 	acctbuf.ac_gid = u.u_rgid;
707Sbill 	acctbuf.ac_mem = 0;
7189Sbill 	if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
7289Sbill 		acctbuf.ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
73*1784Sbill 	acctbuf.ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
747Sbill 	acctbuf.ac_tty = u.u_ttyd;
757Sbill 	acctbuf.ac_flag = u.u_acflag;
767Sbill 	siz = ip->i_size;
777Sbill 	u.u_offset = siz;
787Sbill 	u.u_base = (caddr_t)&acctbuf;
797Sbill 	u.u_count = sizeof(acctbuf);
807Sbill 	u.u_segflg = 1;
817Sbill 	u.u_error = 0;
827Sbill 	writei(ip);
837Sbill 	if(u.u_error)
847Sbill 		ip->i_size = siz;
857Sbill 	prele(ip);
867Sbill }
877Sbill 
887Sbill /*
897Sbill  * Produce a pseudo-floating point representation
907Sbill  * with 3 bits base-8 exponent, 13 bits fraction.
917Sbill  */
927Sbill compress(t)
93*1784Sbill register long t;
947Sbill {
957Sbill 	register exp = 0, round = 0;
967Sbill 
977Sbill 	while (t >= 8192) {
987Sbill 		exp++;
997Sbill 		round = t&04;
1007Sbill 		t >>= 3;
1017Sbill 	}
1027Sbill 	if (round) {
1037Sbill 		t++;
1047Sbill 		if (t >= 8192) {
1057Sbill 			t >>= 3;
1067Sbill 			exp++;
1077Sbill 		}
1087Sbill 	}
1097Sbill 	return((exp<<13) + t);
1107Sbill }
1117Sbill 
1127Sbill /*
1137Sbill  * lock user into core as much
1147Sbill  * as possible. swapping may still
1157Sbill  * occur if core grows.
1167Sbill  */
1177Sbill syslock()
1187Sbill {
1197Sbill 	register struct proc *p;
1207Sbill 	register struct a {
1217Sbill 		int	flag;
1227Sbill 	} *uap;
1237Sbill 
1247Sbill 	uap = (struct a *)u.u_ap;
1257Sbill 	if(suser()) {
1267Sbill 		p = u.u_procp;
1277Sbill 		p->p_flag &= ~SULOCK;
1287Sbill 		if(uap->flag)
1297Sbill 			p->p_flag |= SULOCK;
1307Sbill 	}
1317Sbill }
132