missing error return
[unix-history] / usr / src / sys / kern / kern_acct.c
CommitLineData
da7c5cc6
KM
1/*
2 * Copyright (c) 1982 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
01b0e233 6 * @(#)kern_acct.c 6.9 (Berkeley) %G%
da7c5cc6 7 */
06138e31 8
94368568
JB
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"
06138e31
SL
18
19/*
20 * SHOULD REPLACE THIS WITH A DRIVER THAT CAN BE READ TO SIMPLIFY.
21 */
22struct inode *acctp;
23struct inode *savacctp;
24
25/*
26 * Perform process accounting functions.
27 */
28sysacct()
29{
30 register struct inode *ip;
31 register struct a {
32 char *fname;
33 } *uap = (struct a *)u.u_ap;
715baff1 34 register struct nameidata *ndp = &u.u_nd;
06138e31
SL
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 }
715baff1
KM
48 ndp->ni_nameiop = LOOKUP | FOLLOW;
49 ndp->ni_segflg = UIO_USERSPACE;
50 ndp->ni_dirp = uap->fname;
51 ip = namei(ndp);
01b0e233 52 if (ip == NULL)
06138e31 53 return;
01b0e233 54 if ((ip->i_mode&IFMT) != IFREG) {
06138e31
SL
55 u.u_error = EACCES;
56 iput(ip);
57 return;
58 }
dbba2129
KM
59 if (ip->i_fs->fs_ronly) {
60 u.u_error = EROFS;
61 iput(ip);
62 return;
63 }
06138e31
SL
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
72int acctsuspend = 2; /* stop accounting when < 2% free space left */
73int acctresume = 4; /* resume when free space risen to > 4% */
74
75struct acct acctbuf;
76/*
77 * On exit, write a record on the accounting file.
78 */
79acct()
80{
81 register int i;
82 register struct inode *ip;
83 register struct fs *fs;
032146e9 84 register struct rusage *ru;
06138e31 85 off_t siz;
032146e9 86 struct timeval t;
06138e31
SL
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];
032146e9
SL
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;
06138e31
SL
116 ap->ac_uid = u.u_ruid;
117 ap->ac_gid = u.u_rgid;
032146e9
SL
118 t = ru->ru_stime;
119 timevaladd(&t, &ru->ru_utime);
350f5463 120 if (i = t.tv_sec * hz + t.tv_usec / tick)
032146e9
SL
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;
01b0e233 125 ap->ac_io = compress(ru->ru_inblock + ru->ru_oublock, (long)0);
06138e31
SL
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 */
032146e9 145compress(t, ut)
06138e31 146 register long t;
032146e9 147 long ut;
06138e31
SL
148{
149 register exp = 0, round = 0;
150
5d10e647 151 t = t * AHZ; /* compiler will convert only this format to a shift */
032146e9 152 if (ut)
5d10e647 153 t += ut / (1000000 / AHZ);
06138e31
SL
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}