xref: /csrg-svn/sys/kern/kern_resource.c (revision 3098)
1*3098Swnj /*	kern_resource.c	4.3	03/09/81	*/
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 
12*3098Swnj struct	inode *acctp;
13*3098Swnj 
147Sbill /*
157Sbill  * Perform process accounting functions.
167Sbill  */
177Sbill sysacct()
187Sbill {
197Sbill 	register struct inode *ip;
207Sbill 	register struct a {
217Sbill 		char	*fname;
227Sbill 	} *uap;
237Sbill 
247Sbill 	uap = (struct a *)u.u_ap;
257Sbill 	if (suser()) {
267Sbill 		if (uap->fname==NULL) {
277Sbill 			if (acctp) {
287Sbill 				plock(acctp);
297Sbill 				iput(acctp);
307Sbill 				acctp = NULL;
317Sbill 			}
327Sbill 			return;
337Sbill 		}
347Sbill 		if (acctp) {
357Sbill 			u.u_error = EBUSY;
367Sbill 			return;
377Sbill 		}
387Sbill 		ip = namei(uchar, 0);
397Sbill 		if(ip == NULL)
407Sbill 			return;
417Sbill 		if((ip->i_mode & IFMT) != IFREG) {
427Sbill 			u.u_error = EACCES;
437Sbill 			iput(ip);
447Sbill 			return;
457Sbill 		}
467Sbill 		acctp = ip;
477Sbill 		prele(ip);
487Sbill 	}
497Sbill }
507Sbill 
51*3098Swnj struct	acct acctbuf;
527Sbill /*
537Sbill  * On exit, write a record on the accounting file.
547Sbill  */
557Sbill acct()
567Sbill {
577Sbill 	register i;
587Sbill 	register struct inode *ip;
597Sbill 	off_t siz;
60*3098Swnj 	register struct acct *ap = &acctbuf;
617Sbill 
627Sbill 	if ((ip=acctp)==NULL)
637Sbill 		return;
647Sbill 	plock(ip);
65*3098Swnj 	for (i=0; i<sizeof(ap->ac_comm); i++)
66*3098Swnj 		ap->ac_comm[i] = u.u_comm[i];
67*3098Swnj 	ap->ac_utime = compress((long)u.u_vm.vm_utime);
68*3098Swnj 	ap->ac_stime = compress((long)u.u_vm.vm_stime);
69*3098Swnj 	ap->ac_etime = compress((long)(time - u.u_start));
70*3098Swnj 	ap->ac_btime = u.u_start;
71*3098Swnj 	ap->ac_uid = u.u_ruid;
72*3098Swnj 	ap->ac_gid = u.u_rgid;
73*3098Swnj 	ap->ac_mem = 0;
7489Sbill 	if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
75*3098Swnj 		ap->ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
76*3098Swnj 	ap->ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
77*3098Swnj 	ap->ac_tty = u.u_ttyd;
78*3098Swnj 	ap->ac_flag = u.u_acflag;
797Sbill 	siz = ip->i_size;
807Sbill 	u.u_offset = siz;
81*3098Swnj 	u.u_base = (caddr_t)ap;
827Sbill 	u.u_count = sizeof(acctbuf);
837Sbill 	u.u_segflg = 1;
847Sbill 	u.u_error = 0;
857Sbill 	writei(ip);
867Sbill 	if(u.u_error)
877Sbill 		ip->i_size = siz;
887Sbill 	prele(ip);
897Sbill }
907Sbill 
917Sbill /*
927Sbill  * Produce a pseudo-floating point representation
937Sbill  * with 3 bits base-8 exponent, 13 bits fraction.
947Sbill  */
957Sbill compress(t)
961784Sbill register long t;
977Sbill {
987Sbill 	register exp = 0, round = 0;
997Sbill 
1007Sbill 	while (t >= 8192) {
1017Sbill 		exp++;
1027Sbill 		round = t&04;
1037Sbill 		t >>= 3;
1047Sbill 	}
1057Sbill 	if (round) {
1067Sbill 		t++;
1077Sbill 		if (t >= 8192) {
1087Sbill 			t >>= 3;
1097Sbill 			exp++;
1107Sbill 		}
1117Sbill 	}
1127Sbill 	return((exp<<13) + t);
1137Sbill }
1147Sbill 
1157Sbill /*
1167Sbill  * lock user into core as much
1177Sbill  * as possible. swapping may still
1187Sbill  * occur if core grows.
1197Sbill  */
1207Sbill syslock()
1217Sbill {
1227Sbill 	register struct proc *p;
1237Sbill 	register struct a {
1247Sbill 		int	flag;
1257Sbill 	} *uap;
1267Sbill 
1277Sbill 	uap = (struct a *)u.u_ap;
1287Sbill 	if(suser()) {
1297Sbill 		p = u.u_procp;
1307Sbill 		p->p_flag &= ~SULOCK;
1317Sbill 		if(uap->flag)
1327Sbill 			p->p_flag |= SULOCK;
1337Sbill 	}
1347Sbill }
135