xref: /original-bsd/sys/kern/kern_resource.c (revision fbed46ce)
1 /*	kern_resource.c	4.6	82/02/27	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/acct.h"
6 #include "../h/dir.h"
7 #include "../h/user.h"
8 #include "../h/inode.h"
9 #include "../h/proc.h"
10 #include "../h/seg.h"
11 
12 struct	inode *acctp;
13 
14 /*
15  * Perform process accounting functions.
16  */
17 sysacct()
18 {
19 	register struct inode *ip;
20 	register struct a {
21 		char	*fname;
22 	} *uap;
23 
24 	uap = (struct a *)u.u_ap;
25 	if (suser()) {
26 		if (uap->fname==NULL) {
27 			if (ip = acctp) {
28 				ilock(ip);
29 				iput(ip);
30 				acctp = NULL;
31 			}
32 			return;
33 		}
34 		if (acctp) {
35 			u.u_error = EBUSY;
36 			return;
37 		}
38 		ip = namei(uchar, 0, 1);
39 		if(ip == NULL)
40 			return;
41 		if((ip->i_mode & IFMT) != IFREG) {
42 			u.u_error = EACCES;
43 			iput(ip);
44 			return;
45 		}
46 		acctp = ip;
47 		irele(ip);
48 	}
49 }
50 
51 struct	acct acctbuf;
52 /*
53  * On exit, write a record on the accounting file.
54  */
55 acct()
56 {
57 	register i;
58 	register struct inode *ip;
59 	off_t siz;
60 	register struct acct *ap = &acctbuf;
61 
62 	if ((ip=acctp)==NULL)
63 		return;
64 	ilock(ip);
65 	for (i=0; i<sizeof(ap->ac_comm); i++)
66 		ap->ac_comm[i] = u.u_comm[i];
67 	ap->ac_utime = compress((long)u.u_vm.vm_utime);
68 	ap->ac_stime = compress((long)u.u_vm.vm_stime);
69 	ap->ac_etime = compress((long)(time - u.u_start));
70 	ap->ac_btime = u.u_start;
71 	ap->ac_uid = u.u_ruid;
72 	ap->ac_gid = u.u_rgid;
73 	ap->ac_mem = 0;
74 	if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
75 		ap->ac_mem = (u.u_vm.vm_ixrss + u.u_vm.vm_idsrss) / i;
76 	ap->ac_io = compress((long)(u.u_vm.vm_inblk + u.u_vm.vm_oublk));
77 	ap->ac_tty = u.u_ttyd;
78 	ap->ac_flag = u.u_acflag;
79 	siz = ip->i_size;
80 	u.u_offset = siz;
81 	u.u_base = (caddr_t)ap;
82 	u.u_count = sizeof(acctbuf);
83 	u.u_segflg = 1;
84 	u.u_error = 0;
85 	writei(ip);
86 	if(u.u_error)
87 		ip->i_size = siz;
88 	irele(ip);
89 }
90 
91 /*
92  * Produce a pseudo-floating point representation
93  * with 3 bits base-8 exponent, 13 bits fraction.
94  */
95 compress(t)
96 register long t;
97 {
98 	register exp = 0, round = 0;
99 
100 	while (t >= 8192) {
101 		exp++;
102 		round = t&04;
103 		t >>= 3;
104 	}
105 	if (round) {
106 		t++;
107 		if (t >= 8192) {
108 			t >>= 3;
109 			exp++;
110 		}
111 	}
112 	return((exp<<13) + t);
113 }
114