Commit | Line | Data |
---|---|---|
7e7051a8 GR |
1 | /* |
2 | * Copyright (c) 1994 Christopher G. Demetriou | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. All advertising materials mentioning features or use of this software | |
14 | * must display the following acknowledgement: | |
15 | * This product includes software developed by Christopher G. Demetriou. | |
16 | * 4. The name of the author may not be used to endorse or promote products | |
17 | * derived from this software without specific prior written permission | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | */ | |
30 | ||
31 | #ifndef LINT | |
32 | static char rcsid[] = "$Id: usrdb.c,v 1.1 1994/03/24 18:42:01 cgd Exp $"; | |
33 | #endif | |
34 | ||
35 | #include <sys/types.h> | |
36 | #include <sys/acct.h> | |
37 | #include <err.h> | |
38 | #include <errno.h> | |
39 | #include <fcntl.h> | |
40 | #include <pwd.h> | |
41 | #include "extern.h" | |
42 | #include "pathnames.h" | |
43 | ||
44 | static int uid_compare __P((const DBT *, const DBT *)); | |
45 | ||
46 | static DB *usracct_db; | |
47 | ||
48 | int | |
49 | usracct_init() | |
50 | { | |
51 | DB *saved_usracct_db; | |
52 | BTREEINFO bti; | |
53 | int error; | |
54 | ||
55 | bzero(&bti, sizeof bti); | |
56 | bti.compare = uid_compare; | |
57 | ||
58 | usracct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti); | |
59 | if (usracct_db == NULL) | |
60 | return (-1); | |
61 | ||
62 | error = 0; | |
63 | if (!iflag) { | |
64 | DBT key, data; | |
65 | int serr, nerr; | |
66 | ||
67 | saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE, | |
68 | &bti); | |
69 | if (saved_usracct_db == NULL) { | |
70 | error = (errno == ENOENT) ? 0 : -1; | |
71 | if (error) | |
72 | warn("retrieving user accounting summary"); | |
73 | goto out; | |
74 | } | |
75 | ||
76 | serr = DB_SEQ(saved_usracct_db, &key, &data, R_FIRST); | |
77 | if (serr < 0) { | |
78 | warn("retrieving user accounting summary"); | |
79 | error = -1; | |
80 | goto closeout; | |
81 | } | |
82 | while (serr == 0) { | |
83 | nerr = DB_PUT(usracct_db, &key, &data, 0); | |
84 | if (nerr < 0) { | |
85 | warn("initializing user accounting stats"); | |
86 | error = -1; | |
87 | break; | |
88 | } | |
89 | ||
90 | serr = DB_SEQ(saved_usracct_db, &key, &data, R_NEXT); | |
91 | if (serr < 0) { | |
92 | warn("retrieving user accounting summary"); | |
93 | error = -1; | |
94 | break; | |
95 | } | |
96 | } | |
97 | ||
98 | closeout: | |
99 | if (DB_CLOSE(saved_usracct_db) < 0) { | |
100 | warn("closing user accounting summary"); | |
101 | error = -1; | |
102 | } | |
103 | } | |
104 | ||
105 | out: | |
106 | if (error != 0) | |
107 | usracct_destroy(); | |
108 | return (error); | |
109 | } | |
110 | ||
111 | void | |
112 | usracct_destroy() | |
113 | { | |
114 | if (DB_CLOSE(usracct_db) < 0) | |
115 | warn("destroying user accounting stats"); | |
116 | } | |
117 | ||
118 | int | |
119 | usracct_add(ci) | |
120 | const struct cmdinfo *ci; | |
121 | { | |
122 | DBT key, data; | |
123 | struct userinfo newui; | |
124 | u_long uid; | |
125 | int rv; | |
126 | ||
127 | uid = ci->ci_uid; | |
128 | key.data = &uid; | |
129 | key.size = sizeof uid; | |
130 | ||
131 | rv = DB_GET(usracct_db, &key, &data, 0); | |
132 | if (rv < 0) { | |
133 | warn("get key %d from user accounting stats", uid); | |
134 | return (-1); | |
135 | } else if (rv == 0) { /* it's there; copy whole thing */ | |
136 | /* add the old data to the new data */ | |
137 | bcopy(data.data, &newui, data.size); | |
138 | if (newui.ui_uid != uid) { | |
139 | warnx("key %d != expected record number %d", | |
140 | newui.ui_uid, uid); | |
141 | warnx("inconsistent user accounting stats"); | |
142 | return (-1); | |
143 | } | |
144 | } else { /* it's not there; zero it and copy the key */ | |
145 | bzero(&newui, sizeof newui); | |
146 | newui.ui_uid = ci->ci_uid; | |
147 | } | |
148 | ||
149 | newui.ui_calls += ci->ci_calls; | |
150 | newui.ui_utime += ci->ci_utime; | |
151 | newui.ui_stime += ci->ci_stime; | |
152 | newui.ui_mem += ci->ci_mem; | |
153 | newui.ui_io += ci->ci_io; | |
154 | ||
155 | data.data = &newui; | |
156 | data.size = sizeof newui; | |
157 | rv = DB_PUT(usracct_db, &key, &data, 0); | |
158 | if (rv < 0) { | |
159 | warn("add key %d to user accounting stats", uid); | |
160 | return (-1); | |
161 | } else if (rv != 0) { | |
162 | warnx("DB_PUT returned 1"); | |
163 | return (-1); | |
164 | } | |
165 | ||
166 | return (0); | |
167 | } | |
168 | ||
169 | int | |
170 | usracct_update() | |
171 | { | |
172 | DB *saved_usracct_db; | |
173 | DBT key, data; | |
174 | BTREEINFO bti; | |
175 | u_long uid; | |
176 | int error, serr, nerr; | |
177 | ||
178 | bzero(&bti, sizeof bti); | |
179 | bti.compare = uid_compare; | |
180 | ||
181 | saved_usracct_db = dbopen(_PATH_USRACCT, O_RDWR|O_CREAT|O_TRUNC, 0644, | |
182 | DB_BTREE, &bti); | |
183 | if (saved_usracct_db == NULL) { | |
184 | warn("creating user accounting summary"); | |
185 | return (-1); | |
186 | } | |
187 | ||
188 | error = 0; | |
189 | ||
190 | serr = DB_SEQ(usracct_db, &key, &data, R_FIRST); | |
191 | if (serr < 0) { | |
192 | warn("retrieving user accounting stats"); | |
193 | error = -1; | |
194 | } | |
195 | while (serr == 0) { | |
196 | nerr = DB_PUT(saved_usracct_db, &key, &data, 0); | |
197 | if (nerr < 0) { | |
198 | warn("saving user accounting summary"); | |
199 | error = -1; | |
200 | break; | |
201 | } | |
202 | ||
203 | serr = DB_SEQ(usracct_db, &key, &data, R_NEXT); | |
204 | if (serr < 0) { | |
205 | warn("retrieving user accounting stats"); | |
206 | error = -1; | |
207 | break; | |
208 | } | |
209 | } | |
210 | ||
211 | if (DB_SYNC(saved_usracct_db, 0) < 0) { | |
212 | warn("syncing process accounting summary"); | |
213 | error = -1; | |
214 | } | |
215 | out: | |
216 | if (DB_CLOSE(saved_usracct_db) < 0) { | |
217 | warn("closing process accounting summary"); | |
218 | error = -1; | |
219 | } | |
220 | return error; | |
221 | } | |
222 | ||
223 | void | |
224 | usracct_print() | |
225 | { | |
226 | DBT key, data; | |
227 | struct userinfo *ui; | |
228 | struct passwd *pwd; | |
229 | double t; | |
230 | int rv; | |
231 | ||
232 | rv = DB_SEQ(usracct_db, &key, &data, R_FIRST); | |
233 | if (rv < 0) | |
234 | warn("retrieving user accounting stats"); | |
235 | ||
236 | while (rv == 0) { | |
237 | ui = (struct userinfo *) data.data; | |
238 | ||
239 | pwd = getpwuid(ui->ui_uid); | |
240 | printf("%-8s %9qu ", | |
241 | pwd->pw_name, ui->ui_calls); | |
242 | /*user_from_uid(ui->ui_uid, 0), ui->ui_calls);*/ | |
243 | ||
244 | t = (double) (ui->ui_utime + ui->ui_stime) / | |
245 | (double) AHZ; | |
246 | if (t < 0.0001) /* kill divide by zero */ | |
247 | t = 0.0001; | |
248 | ||
249 | printf("%12.2lf%s ", t / 60.0, "cpu"); | |
250 | ||
251 | /* ui->ui_calls is always != 0 */ | |
252 | if (dflag) | |
253 | printf("%12qu%s", ui->ui_io / ui->ui_calls, "avio"); | |
254 | else | |
255 | printf("%12qu%s", ui->ui_io, "tio"); | |
256 | ||
257 | /* t is always >= 0.0001; see above */ | |
258 | if (kflag) | |
259 | printf("%12qu%s", ui->ui_mem / t, "k"); | |
260 | else | |
261 | printf("%12qu%s", ui->ui_mem, "k*sec"); | |
262 | ||
263 | printf("\n"); | |
264 | ||
265 | rv = DB_SEQ(usracct_db, &key, &data, R_NEXT); | |
266 | if (rv < 0) | |
267 | warn("retrieving user accounting stats"); | |
268 | } | |
269 | } | |
270 | ||
271 | static int | |
272 | uid_compare(k1, k2) | |
273 | const DBT *k1, *k2; | |
274 | { | |
275 | u_long d1, d2; | |
276 | ||
277 | bcopy(k1->data, &d1, sizeof d1); | |
278 | bcopy(k2->data, &d2, sizeof d2); | |
279 | ||
280 | if (d1 < d2) | |
281 | return -1; | |
282 | else if (d1 == d2) | |
283 | return 0; | |
284 | else | |
285 | return 1; | |
286 | } |