lex(1): Make WARNS cover libl too.
[dragonfly.git] / usr.sbin / sa / usrdb.c
blob2caea87a0cf7673f9b3da0272d877777bfea10ac
1 /*
2 * Copyright (c) 1994 Christopher G. Demetriou
3 * All rights reserved.
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
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.
30 * $FreeBSD: src/usr.sbin/sa/usrdb.c,v 1.8.2.2 2000/10/28 02:28:20 gallatin Exp $
31 * $DragonFly: src/usr.sbin/sa/usrdb.c,v 1.4 2005/12/05 02:40:28 swildner Exp $
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/acct.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "extern.h"
44 #include "pathnames.h"
46 static int uid_compare(const DBT *, const DBT *);
48 static DB *usracct_db;
50 int
51 usracct_init(void)
53 DB *saved_usracct_db;
54 BTREEINFO bti;
55 int error;
57 bzero(&bti, sizeof bti);
58 bti.compare = uid_compare;
60 usracct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
61 if (usracct_db == NULL)
62 return (-1);
64 error = 0;
65 if (!iflag) {
66 DBT key, data;
67 int serr, nerr;
69 saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE,
70 &bti);
71 if (saved_usracct_db == NULL) {
72 error = (errno == ENOENT) ? 0 : -1;
73 if (error)
74 warn("retrieving user accounting summary");
75 goto out;
78 serr = DB_SEQ(saved_usracct_db, &key, &data, R_FIRST);
79 if (serr < 0) {
80 warn("retrieving user accounting summary");
81 error = -1;
82 goto closeout;
84 while (serr == 0) {
85 nerr = DB_PUT(usracct_db, &key, &data, 0);
86 if (nerr < 0) {
87 warn("initializing user accounting stats");
88 error = -1;
89 break;
92 serr = DB_SEQ(saved_usracct_db, &key, &data, R_NEXT);
93 if (serr < 0) {
94 warn("retrieving user accounting summary");
95 error = -1;
96 break;
100 closeout:
101 if (DB_CLOSE(saved_usracct_db) < 0) {
102 warn("closing user accounting summary");
103 error = -1;
107 out:
108 if (error != 0)
109 usracct_destroy();
110 return (error);
113 void
114 usracct_destroy(void)
116 if (DB_CLOSE(usracct_db) < 0)
117 warn("destroying user accounting stats");
121 usracct_add(const struct cmdinfo *ci)
123 DBT key, data;
124 struct userinfo newui;
125 u_long uid;
126 int rv;
128 uid = ci->ci_uid;
129 key.data = &uid;
130 key.size = sizeof uid;
132 rv = DB_GET(usracct_db, &key, &data, 0);
133 if (rv < 0) {
134 warn("get key %lu from user accounting stats", uid);
135 return (-1);
136 } else if (rv == 0) { /* it's there; copy whole thing */
137 /* add the old data to the new data */
138 bcopy(data.data, &newui, data.size);
139 if (newui.ui_uid != uid) {
140 warnx("key %lu != expected record number %lu",
141 newui.ui_uid, uid);
142 warnx("inconsistent user accounting stats");
143 return (-1);
145 } else { /* it's not there; zero it and copy the key */
146 bzero(&newui, sizeof newui);
147 newui.ui_uid = ci->ci_uid;
150 newui.ui_calls += ci->ci_calls;
151 newui.ui_utime += ci->ci_utime;
152 newui.ui_stime += ci->ci_stime;
153 newui.ui_mem += ci->ci_mem;
154 newui.ui_io += ci->ci_io;
156 data.data = &newui;
157 data.size = sizeof newui;
158 rv = DB_PUT(usracct_db, &key, &data, 0);
159 if (rv < 0) {
160 warn("add key %lu to user accounting stats", uid);
161 return (-1);
162 } else if (rv != 0) {
163 warnx("DB_PUT returned 1");
164 return (-1);
167 return (0);
171 usracct_update(void)
173 DB *saved_usracct_db;
174 DBT key, data;
175 BTREEINFO bti;
176 int error, serr, nerr;
178 bzero(&bti, sizeof bti);
179 bti.compare = uid_compare;
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);
188 error = 0;
190 serr = DB_SEQ(usracct_db, &key, &data, R_FIRST);
191 if (serr < 0) {
192 warn("retrieving user accounting stats");
193 error = -1;
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;
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;
211 if (DB_SYNC(saved_usracct_db, 0) < 0) {
212 warn("syncing process accounting summary");
213 error = -1;
215 if (DB_CLOSE(saved_usracct_db) < 0) {
216 warn("closing process accounting summary");
217 error = -1;
219 return error;
222 void
223 usracct_print(void)
225 DBT key, data;
226 struct userinfo uistore, *ui = &uistore;
227 double t;
228 int rv;
230 rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
231 if (rv < 0)
232 warn("retrieving user accounting stats");
234 while (rv == 0) {
235 memcpy(ui, data.data, sizeof(struct userinfo));
237 printf("%-*s %9qu ", MAXLOGNAME - 1,
238 user_from_uid(ui->ui_uid, 0), ui->ui_calls);
240 t = (double) (ui->ui_utime + ui->ui_stime) /
241 (double) AHZ;
242 if (t < 0.0001) /* kill divide by zero */
243 t = 0.0001;
245 printf("%12.2f%s ", t / 60.0, "cpu");
247 /* ui->ui_calls is always != 0 */
248 if (dflag)
249 printf("%12qu%s", ui->ui_io / ui->ui_calls, "avio");
250 else
251 printf("%12qu%s", ui->ui_io, "tio");
253 /* t is always >= 0.0001; see above */
254 if (kflag)
255 printf("%12.0f%s", ui->ui_mem / t, "k");
256 else
257 printf("%12qu%s", ui->ui_mem, "k*sec");
259 printf("\n");
261 rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
262 if (rv < 0)
263 warn("retrieving user accounting stats");
267 static int
268 uid_compare(const DBT *k1, const DBT *k2)
270 u_long d1, d2;
272 bcopy(k1->data, &d1, sizeof d1);
273 bcopy(k2->data, &d2, sizeof d2);
275 if (d1 < d2)
276 return -1;
277 else if (d1 == d2)
278 return 0;
279 else
280 return 1;