xref: /csrg-svn/sys/kern/kern_resource.c (revision 7141)
1*7141Smckusick /*	kern_resource.c	4.8	82/06/10	*/
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 
123098Swnj struct	inode *acctp;
133098Swnj 
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) {
273109Swnj 			if (ip = acctp) {
28*7141Smckusick 				irele(ip);
297Sbill 				acctp = NULL;
307Sbill 			}
317Sbill 			return;
327Sbill 		}
337Sbill 		if (acctp) {
347Sbill 			u.u_error = EBUSY;
357Sbill 			return;
367Sbill 		}
375990Swnj 		ip = namei(uchar, 0, 1);
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;
467121Smckusick 		iunlock(ip);
477Sbill 	}
487Sbill }
497Sbill 
503098Swnj struct	acct acctbuf;
517Sbill /*
527Sbill  * On exit, write a record on the accounting file.
537Sbill  */
547Sbill acct()
557Sbill {
567Sbill 	register i;
577Sbill 	register struct inode *ip;
587Sbill 	off_t siz;
593098Swnj 	register struct acct *ap = &acctbuf;
607Sbill 
617Sbill 	if ((ip=acctp)==NULL)
627Sbill 		return;
634816Swnj 	ilock(ip);
643098Swnj 	for (i=0; i<sizeof(ap->ac_comm); i++)
653098Swnj 		ap->ac_comm[i] = u.u_comm[i];
663098Swnj 	ap->ac_utime = compress((long)u.u_vm.vm_utime);
673098Swnj 	ap->ac_stime = compress((long)u.u_vm.vm_stime);
683098Swnj 	ap->ac_etime = compress((long)(time - u.u_start));
693098Swnj 	ap->ac_btime = u.u_start;
703098Swnj 	ap->ac_uid = u.u_ruid;
713098Swnj 	ap->ac_gid = u.u_rgid;
723098Swnj 	ap->ac_mem = 0;
7389Sbill 	if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
743098Swnj 		ap->ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
753098Swnj 	ap->ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
763098Swnj 	ap->ac_tty = u.u_ttyd;
773098Swnj 	ap->ac_flag = u.u_acflag;
787Sbill 	siz = ip->i_size;
797Sbill 	u.u_offset = siz;
803098Swnj 	u.u_base = (caddr_t)ap;
817Sbill 	u.u_count = sizeof(acctbuf);
827Sbill 	u.u_segflg = 1;
837Sbill 	u.u_error = 0;
847Sbill 	writei(ip);
857Sbill 	if(u.u_error)
867Sbill 		ip->i_size = siz;
877121Smckusick 	iunlock(ip);
887Sbill }
897Sbill 
907Sbill /*
917Sbill  * Produce a pseudo-floating point representation
927Sbill  * with 3 bits base-8 exponent, 13 bits fraction.
937Sbill  */
947Sbill compress(t)
951784Sbill register long t;
967Sbill {
977Sbill 	register exp = 0, round = 0;
987Sbill 
997Sbill 	while (t >= 8192) {
1007Sbill 		exp++;
1017Sbill 		round = t&04;
1027Sbill 		t >>= 3;
1037Sbill 	}
1047Sbill 	if (round) {
1057Sbill 		t++;
1067Sbill 		if (t >= 8192) {
1077Sbill 			t >>= 3;
1087Sbill 			exp++;
1097Sbill 		}
1107Sbill 	}
1117Sbill 	return((exp<<13) + t);
1127Sbill }
113