spl5 not splimp
[unix-history] / usr / src / sys / kern / kern_resource.c
CommitLineData
92c0c09d 1/* kern_resource.c 4.5 81/11/08 */
1b64633a
BJ
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
a0eab615
BJ
12struct inode *acctp;
13
1b64633a
BJ
14/*
15 * Perform process accounting functions.
16 */
1b64633a
BJ
17sysacct()
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) {
964bcfb1 27 if (ip = acctp) {
92c0c09d 28 ilock(ip);
964bcfb1 29 iput(ip);
1b64633a
BJ
30 acctp = NULL;
31 }
32 return;
33 }
34 if (acctp) {
35 u.u_error = EBUSY;
36 return;
37 }
38 ip = namei(uchar, 0);
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;
92c0c09d 47 irele(ip);
1b64633a
BJ
48 }
49}
50
a0eab615 51struct acct acctbuf;
1b64633a
BJ
52/*
53 * On exit, write a record on the accounting file.
54 */
55acct()
56{
57 register i;
58 register struct inode *ip;
59 off_t siz;
a0eab615 60 register struct acct *ap = &acctbuf;
1b64633a
BJ
61
62 if ((ip=acctp)==NULL)
63 return;
92c0c09d 64 ilock(ip);
a0eab615
BJ
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;
79c03a01 74 if (i = u.u_vm.vm_utime + u.u_vm.vm_stime)
a0eab615
BJ
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;
1b64633a
BJ
79 siz = ip->i_size;
80 u.u_offset = siz;
a0eab615 81 u.u_base = (caddr_t)ap;
1b64633a
BJ
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;
92c0c09d 88 irele(ip);
1b64633a
BJ
89}
90
91/*
92 * Produce a pseudo-floating point representation
93 * with 3 bits base-8 exponent, 13 bits fraction.
94 */
95compress(t)
49c84d3f 96register long t;
1b64633a
BJ
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}