]> git.saurik.com Git - apple/system_cmds.git/blame - sa.tproj/usrdb.c
system_cmds-735.20.1.tar.gz
[apple/system_cmds.git] / sa.tproj / usrdb.c
CommitLineData
2fc1e207
A
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
ef8ad44b
A
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: src/usr.sbin/sa/usrdb.c,v 1.16 2007/05/22 06:51:38 dds Exp $");
2fc1e207
A
33
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 <pwd.h>
41#include <stdint.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include "extern.h"
46#include "pathnames.h"
47
ef8ad44b 48static int uid_compare(const DBT *, const DBT *);
2fc1e207
A
49
50static DB *usracct_db;
51
ef8ad44b
A
52/* Legacy format in AHZV1 units. */
53struct userinfov1 {
54 uid_t ui_uid; /* user id; for consistency */
55 u_quad_t ui_calls; /* number of invocations */
56 u_quad_t ui_utime; /* user time */
57 u_quad_t ui_stime; /* system time */
58 u_quad_t ui_mem; /* memory use */
59 u_quad_t ui_io; /* number of disk i/o ops */
60};
61
62/*
63 * Convert a v1 data record into the current version.
64 * Return 0 if OK, -1 on error, setting errno.
65 */
66static int
67v1_to_v2(DBT *key, DBT *data)
68{
69 struct userinfov1 uiv1;
70 static struct userinfo uiv2;
71 static uid_t uid;
72
73 if (key->size != sizeof(u_long) || data->size != sizeof(uiv1)) {
74 errno = EFTYPE;
75 return (-1);
76 }
77
78 /* Convert key. */
79 key->size = sizeof(uid_t);
80 uid = (uid_t)*(u_long *)(key->data);
81 key->data = &uid;
82
83 /* Convert data. */
84 memcpy(&uiv1, data->data, data->size);
85 memset(&uiv2, 0, sizeof(uiv2));
86 uiv2.ui_uid = uiv1.ui_uid;
87 uiv2.ui_calls = uiv1.ui_calls;
88 uiv2.ui_utime = ((double)uiv1.ui_utime / AHZV1) * 1000000;
89 uiv2.ui_stime = ((double)uiv1.ui_stime / AHZV1) * 1000000;
90 uiv2.ui_mem = uiv1.ui_mem;
91 uiv2.ui_io = uiv1.ui_io;
92 data->size = sizeof(uiv2);
93 data->data = &uiv2;
94
95 return (0);
96}
97
98/* Copy usrdb_file to in-memory usracct_db. */
2fc1e207 99int
cf37c299 100usracct_init(void)
2fc1e207 101{
2fc1e207 102 BTREEINFO bti;
2fc1e207
A
103
104 bzero(&bti, sizeof bti);
105 bti.compare = uid_compare;
106
ef8ad44b
A
107 return (db_copy_in(&usracct_db, usrdb_file, "user accounting",
108 &bti, v1_to_v2));
2fc1e207
A
109}
110
111void
cf37c299 112usracct_destroy(void)
2fc1e207 113{
ef8ad44b 114 db_destroy(usracct_db, "user accounting");
2fc1e207
A
115}
116
117int
ef8ad44b 118usracct_add(const struct cmdinfo *ci)
2fc1e207
A
119{
120 DBT key, data;
121 struct userinfo newui;
ef8ad44b 122 uid_t uid;
2fc1e207
A
123 int rv;
124
125 uid = ci->ci_uid;
126 key.data = &uid;
127 key.size = sizeof uid;
128
129 rv = DB_GET(usracct_db, &key, &data, 0);
130 if (rv < 0) {
ef8ad44b 131 warn("get key %u from user accounting stats", uid);
2fc1e207
A
132 return (-1);
133 } else if (rv == 0) { /* it's there; copy whole thing */
134 /* add the old data to the new data */
135 bcopy(data.data, &newui, data.size);
136 if (newui.ui_uid != uid) {
ef8ad44b 137 warnx("key %u != expected record number %u",
2fc1e207
A
138 newui.ui_uid, uid);
139 warnx("inconsistent user accounting stats");
140 return (-1);
141 }
142 } else { /* it's not there; zero it and copy the key */
143 bzero(&newui, sizeof newui);
144 newui.ui_uid = ci->ci_uid;
145 }
146
147 newui.ui_calls += ci->ci_calls;
148 newui.ui_utime += ci->ci_utime;
149 newui.ui_stime += ci->ci_stime;
150 newui.ui_mem += ci->ci_mem;
151 newui.ui_io += ci->ci_io;
152
153 data.data = &newui;
154 data.size = sizeof newui;
155 rv = DB_PUT(usracct_db, &key, &data, 0);
156 if (rv < 0) {
ef8ad44b 157 warn("add key %u to user accounting stats", uid);
2fc1e207
A
158 return (-1);
159 } else if (rv != 0) {
160 warnx("DB_PUT returned 1");
161 return (-1);
162 }
163
164 return (0);
165}
166
ef8ad44b 167/* Copy in-memory usracct_db to usrdb_file. */
2fc1e207 168int
cf37c299 169usracct_update(void)
2fc1e207 170{
2fc1e207 171 BTREEINFO bti;
2fc1e207
A
172
173 bzero(&bti, sizeof bti);
174 bti.compare = uid_compare;
175
ef8ad44b
A
176 return (db_copy_out(usracct_db, usrdb_file, "user accounting",
177 &bti));
2fc1e207
A
178}
179
180void
cf37c299 181usracct_print(void)
2fc1e207
A
182{
183 DBT key, data;
184 struct userinfo uistore, *ui = &uistore;
185 double t;
186 int rv;
187
188 rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
189 if (rv < 0)
190 warn("retrieving user accounting stats");
191
192 while (rv == 0) {
193 memcpy(ui, data.data, sizeof(struct userinfo));
194
195 printf("%-*s %9ju ", MAXLOGNAME - 1,
196 user_from_uid(ui->ui_uid, 0), (uintmax_t)ui->ui_calls);
197
ef8ad44b 198#ifdef __APPLE__
2fc1e207
A
199 t = (double) (ui->ui_utime + ui->ui_stime) /
200 (double) AHZ;
201 if (t < 0.0001) /* kill divide by zero */
202 t = 0.0001;
203
204 printf("%12.2f%s ", t / 60.0, "cpu");
205
206 /* ui->ui_calls is always != 0 */
207 if (dflag)
208 printf("%12ju%s",
209 (uintmax_t)(ui->ui_io / ui->ui_calls), "avio");
210 else
211 printf("%12ju%s", (uintmax_t)ui->ui_io, "tio");
212
213 /* t is always >= 0.0001; see above */
214 if (kflag)
215 printf("%12.0f%s", ui->ui_mem / t, "k");
216 else
217 printf("%12ju%s", (uintmax_t)ui->ui_mem, "k*sec");
ef8ad44b
A
218#else
219 t = (ui->ui_utime + ui->ui_stime) / 1000000;
220 if (t < 0.000001) /* kill divide by zero */
221 t = 0.000001;
222
223 printf("%12.2f%s ", t / 60.0, "cpu");
224
225 /* ui->ui_calls is always != 0 */
226 if (dflag)
227 printf("%12.0f%s",
228 ui->ui_io / ui->ui_calls, "avio");
229 else
230 printf("%12.0f%s", ui->ui_io, "tio");
231
232 /* t is always >= 0.000001; see above. */
233 if (kflag)
234 printf("%12.0f%s", ui->ui_mem / t, "k");
235 else
236 printf("%12.0f%s", ui->ui_mem, "k*sec");
237#endif
2fc1e207
A
238
239 printf("\n");
240
241 rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
242 if (rv < 0)
243 warn("retrieving user accounting stats");
244 }
245}
246
247static int
ef8ad44b 248uid_compare(const DBT *k1, const DBT *k2)
2fc1e207 249{
ef8ad44b 250 uid_t d1, d2;
2fc1e207
A
251
252 bcopy(k1->data, &d1, sizeof d1);
253 bcopy(k2->data, &d2, sizeof d2);
254
255 if (d1 < d2)
256 return -1;
257 else if (d1 == d2)
258 return 0;
259 else
260 return 1;
261}