]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/sa/usrdb.c
rtsold: Fix bugs reported by Coverity
[FreeBSD/FreeBSD.git] / usr.sbin / sa / usrdb.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994 Christopher G. Demetriou
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/acct.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <pwd.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include "extern.h"
48 #include "pathnames.h"
49
50 static int uid_compare(const DBT *, const DBT *);
51
52 static DB       *usracct_db;
53
54 /* Legacy format in AHZV1 units. */
55 struct userinfov1 {
56         uid_t           ui_uid;                 /* user id; for consistency */
57         u_quad_t        ui_calls;               /* number of invocations */
58         u_quad_t        ui_utime;               /* user time */
59         u_quad_t        ui_stime;               /* system time */
60         u_quad_t        ui_mem;                 /* memory use */
61         u_quad_t        ui_io;                  /* number of disk i/o ops */
62 };
63
64 /*
65  * Convert a v1 data record into the current version.
66  * Return 0 if OK, -1 on error, setting errno.
67  */
68 static int
69 v1_to_v2(DBT *key, DBT *data)
70 {
71         struct userinfov1 uiv1;
72         static struct userinfo uiv2;
73         static uid_t uid;
74
75         if (key->size != sizeof(u_long) || data->size != sizeof(uiv1)) {
76                 errno = EFTYPE;
77                 return (-1);
78         }
79
80         /* Convert key. */
81         key->size = sizeof(uid_t);
82         uid = (uid_t)*(u_long *)(key->data);
83         key->data = &uid;
84
85         /* Convert data. */
86         memcpy(&uiv1, data->data, data->size);
87         memset(&uiv2, 0, sizeof(uiv2));
88         uiv2.ui_uid = uiv1.ui_uid;
89         uiv2.ui_calls = uiv1.ui_calls;
90         uiv2.ui_utime = ((double)uiv1.ui_utime / AHZV1) * 1000000;
91         uiv2.ui_stime = ((double)uiv1.ui_stime / AHZV1) * 1000000;
92         uiv2.ui_mem = uiv1.ui_mem;
93         uiv2.ui_io = uiv1.ui_io;
94         data->size = sizeof(uiv2);
95         data->data = &uiv2;
96
97         return (0);
98 }
99
100 /* Copy usrdb_file to in-memory usracct_db. */
101 int
102 usracct_init(void)
103 {
104         BTREEINFO bti;
105
106         bzero(&bti, sizeof bti);
107         bti.compare = uid_compare;
108
109         return (db_copy_in(&usracct_db, usrdb_file, "user accounting",
110             &bti, v1_to_v2));
111 }
112
113 void
114 usracct_destroy(void)
115 {
116         db_destroy(usracct_db, "user accounting");
117 }
118
119 int
120 usracct_add(const struct cmdinfo *ci)
121 {
122         DBT key, data;
123         struct userinfo newui;
124         uid_t 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 %u 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 %u != expected record number %u",
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 %u 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 /* Copy in-memory usracct_db to usrdb_file. */
170 int
171 usracct_update(void)
172 {
173         BTREEINFO bti;
174
175         bzero(&bti, sizeof bti);
176         bti.compare = uid_compare;
177
178         return (db_copy_out(usracct_db, usrdb_file, "user accounting",
179             &bti));
180 }
181
182 void
183 usracct_print(void)
184 {
185         DBT key, data;
186         struct userinfo uistore, *ui = &uistore;
187         double t;
188         int rv;
189
190         rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
191         if (rv < 0)
192                 warn("retrieving user accounting stats");
193
194         while (rv == 0) {
195                 memcpy(ui, data.data, sizeof(struct userinfo));
196
197                 printf("%-*s %9ju ", MAXLOGNAME - 1,
198                     user_from_uid(ui->ui_uid, 0), (uintmax_t)ui->ui_calls);
199
200                 t = (ui->ui_utime + ui->ui_stime) / 1000000;
201                 if (t < 0.000001)               /* kill divide by zero */
202                         t = 0.000001;
203
204                 printf("%12.2f%s ", t / 60.0, "cpu");
205
206                 /* ui->ui_calls is always != 0 */
207                 if (dflag)
208                         printf("%12.0f%s",
209                             ui->ui_io / ui->ui_calls, "avio");
210                 else
211                         printf("%12.0f%s", ui->ui_io, "tio");
212
213                 /* t is always >= 0.000001; see above. */
214                 if (kflag)
215                         printf("%12.0f%s", ui->ui_mem / t, "k");
216                 else
217                         printf("%12.0f%s", ui->ui_mem, "k*sec");
218
219                 printf("\n");
220
221                 rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
222                 if (rv < 0)
223                         warn("retrieving user accounting stats");
224         }
225 }
226
227 static int
228 uid_compare(const DBT *k1, const DBT *k2)
229 {
230         uid_t d1, d2;
231
232         bcopy(k1->data, &d1, sizeof d1);
233         bcopy(k2->data, &d2, sizeof d2);
234
235         if (d1 < d2)
236                 return -1;
237         else if (d1 == d2)
238                 return 0;
239         else
240                 return 1;
241 }