xref: /csrg-svn/sys/kern/kern_resource.c (revision 4816)
1*4816Swnj /*	kern_resource.c	4.5	81/11/08	*/
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*4816Swnj 				ilock(ip);
293109Swnj 				iput(ip);
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;
47*4816Swnj 		irele(ip);
487Sbill 	}
497Sbill }
507Sbill 
513098Swnj 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;
603098Swnj 	register struct acct *ap = &acctbuf;
617Sbill 
627Sbill 	if ((ip=acctp)==NULL)
637Sbill 		return;
64*4816Swnj 	ilock(ip);
653098Swnj 	for (i=0; i<sizeof(ap->ac_comm); i++)
663098Swnj 		ap->ac_comm[i] = u.u_comm[i];
673098Swnj 	ap->ac_utime = compress((long)u.u_vm.vm_utime);
683098Swnj 	ap->ac_stime = compress((long)u.u_vm.vm_stime);
693098Swnj 	ap->ac_etime = compress((long)(time - u.u_start));
703098Swnj 	ap->ac_btime = u.u_start;
713098Swnj 	ap->ac_uid = u.u_ruid;
723098Swnj 	ap->ac_gid = u.u_rgid;
733098Swnj 	ap->ac_mem = 0;
7489Sbill 	if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
753098Swnj 		ap->ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
763098Swnj 	ap->ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
773098Swnj 	ap->ac_tty = u.u_ttyd;
783098Swnj 	ap->ac_flag = u.u_acflag;
797Sbill 	siz = ip->i_size;
807Sbill 	u.u_offset = siz;
813098Swnj 	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;
88*4816Swnj 	irele(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 }
114