1: /*
2: * Copyright (c) 1982, 1986 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: *
6: * @(#)kern_acct.c 7.1 (Berkeley) 6/5/86
7: */
8:
9: #include "param.h"
10: #include "systm.h"
11: #include "dir.h"
12: #include "user.h"
13: #include "inode.h"
14: #include "fs.h"
15: #include "kernel.h"
16: #include "acct.h"
17: #include "uio.h"
18:
19: /*
20: * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY.
21: */
22: struct inode *acctp;
23: struct inode *savacctp;
24:
25: /*
26: * Perform process accounting functions.
27: */
28: sysacct()
29: {
30: register struct inode *ip;
31: register struct a {
32: char *fname;
33: } *uap = (struct a *)u.u_ap;
34: register struct nameidata *ndp = &u.u_nd;
35:
36: if (suser()) {
37: if (savacctp) {
38: acctp = savacctp;
39: savacctp = NULL;
40: }
41: if (uap->fname==NULL) {
42: if (ip = acctp) {
43: irele(ip);
44: acctp = NULL;
45: }
46: return;
47: }
48: ndp->ni_nameiop = LOOKUP | FOLLOW;
49: ndp->ni_segflg = UIO_USERSPACE;
50: ndp->ni_dirp = uap->fname;
51: ip = namei(ndp);
52: if (ip == NULL)
53: return;
54: if ((ip->i_mode&IFMT) != IFREG) {
55: u.u_error = EACCES;
56: iput(ip);
57: return;
58: }
59: if (ip->i_fs->fs_ronly) {
60: u.u_error = EROFS;
61: iput(ip);
62: return;
63: }
64: if (acctp && (acctp->i_number != ip->i_number ||
65: acctp->i_dev != ip->i_dev))
66: irele(acctp);
67: acctp = ip;
68: iunlock(ip);
69: }
70: }
71:
72: int acctsuspend = 2; /* stop accounting when < 2% free space left */
73: int acctresume = 4; /* resume when free space risen to > 4% */
74:
75: struct acct acctbuf;
76: /*
77: * On exit, write a record on the accounting file.
78: */
79: acct()
80: {
81: register int i;
82: register struct inode *ip;
83: register struct fs *fs;
84: register struct rusage *ru;
85: off_t siz;
86: struct timeval t;
87: register struct acct *ap = &acctbuf;
88:
89: if (savacctp) {
90: fs = savacctp->i_fs;
91: if (freespace(fs, fs->fs_minfree + acctresume) > 0) {
92: acctp = savacctp;
93: savacctp = NULL;
94: printf("Accounting resumed\n");
95: }
96: }
97: if ((ip = acctp) == NULL)
98: return;
99: fs = acctp->i_fs;
100: if (freespace(fs, fs->fs_minfree + acctsuspend) <= 0) {
101: savacctp = acctp;
102: acctp = NULL;
103: printf("Accounting suspended\n");
104: return;
105: }
106: ilock(ip);
107: for (i = 0; i < sizeof (ap->ac_comm); i++)
108: ap->ac_comm[i] = u.u_comm[i];
109: ru = &u.u_ru;
110: ap->ac_utime = compress(ru->ru_utime.tv_sec, ru->ru_utime.tv_usec);
111: ap->ac_stime = compress(ru->ru_stime.tv_sec, ru->ru_stime.tv_usec);
112: t = time;
113: timevalsub(&t, &u.u_start);
114: ap->ac_etime = compress(t.tv_sec, t.tv_usec);
115: ap->ac_btime = u.u_start.tv_sec;
116: ap->ac_uid = u.u_ruid;
117: ap->ac_gid = u.u_rgid;
118: t = ru->ru_stime;
119: timevaladd(&t, &ru->ru_utime);
120: if (i = t.tv_sec * hz + t.tv_usec / tick)
121: ap->ac_mem = (ru->ru_ixrss+ru->ru_idrss+ru->ru_isrss) / i;
122: else
123: ap->ac_mem = 0;
124: ap->ac_mem >>= CLSIZELOG2;
125: ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0);
126: if (u.u_ttyp)
127: ap->ac_tty = u.u_ttyd;
128: else
129: ap->ac_tty = NODEV;
130: ap->ac_flag = u.u_acflag;
131: siz = ip->i_size;
132: u.u_error = 0; /* XXX */
133: u.u_error =
134: rdwri(UIO_WRITE, ip, (caddr_t)ap, sizeof (acctbuf), siz,
135: 1, (int *)0);
136: if (u.u_error)
137: itrunc(ip, (u_long)siz);
138: iunlock(ip);
139: }
140:
141: /*
142: * Produce a pseudo-floating point representation
143: * with 3 bits base-8 exponent, 13 bits fraction.
144: */
145: compress(t, ut)
146: register long t;
147: long ut;
148: {
149: register exp = 0, round = 0;
150:
151: t = t * AHZ; /* compiler will convert only this format to a shift */
152: if (ut)
153: t += ut / (1000000 / AHZ);
154: while (t >= 8192) {
155: exp++;
156: round = t&04;
157: t >>= 3;
158: }
159: if (round) {
160: t++;
161: if (t >= 8192) {
162: t >>= 3;
163: exp++;
164: }
165: }
166: return ((exp<<13) + t);
167: }
Defined functions
acct
defined in line
79; used 1 times
Defined variables
acctp
defined in line
22; used 13 times
Defined struct's
a
defined in line
31; used 2 times