* Copyright (c) 1994 Christopher G. Demetriou
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
static char rcsid
[] = "$Id: usrdb.c,v 1.1 1994/03/24 18:42:01 cgd Exp $";
static int uid_compare
__P((const DBT
*, const DBT
*));
bti
.compare
= uid_compare
;
usracct_db
= dbopen(NULL
, O_RDWR
, 0, DB_BTREE
, &bti
);
saved_usracct_db
= dbopen(_PATH_USRACCT
, O_RDONLY
, 0, DB_BTREE
,
if (saved_usracct_db
== NULL
) {
error
= (errno
== ENOENT
) ? 0 : -1;
warn("retrieving user accounting summary");
serr
= DB_SEQ(saved_usracct_db
, &key
, &data
, R_FIRST
);
warn("retrieving user accounting summary");
nerr
= DB_PUT(usracct_db
, &key
, &data
, 0);
warn("initializing user accounting stats");
serr
= DB_SEQ(saved_usracct_db
, &key
, &data
, R_NEXT
);
warn("retrieving user accounting summary");
if (DB_CLOSE(saved_usracct_db
) < 0) {
warn("closing user accounting summary");
if (DB_CLOSE(usracct_db
) < 0)
warn("destroying user accounting stats");
const struct cmdinfo
*ci
;
rv
= DB_GET(usracct_db
, &key
, &data
, 0);
warn("get key %d from user accounting stats", uid
);
} else if (rv
== 0) { /* it's there; copy whole thing */
/* add the old data to the new data */
bcopy(data
.data
, &newui
, data
.size
);
if (newui
.ui_uid
!= uid
) {
warnx("key %d != expected record number %d",
warnx("inconsistent user accounting stats");
} else { /* it's not there; zero it and copy the key */
bzero(&newui
, sizeof newui
);
newui
.ui_uid
= ci
->ci_uid
;
newui
.ui_calls
+= ci
->ci_calls
;
newui
.ui_utime
+= ci
->ci_utime
;
newui
.ui_stime
+= ci
->ci_stime
;
newui
.ui_mem
+= ci
->ci_mem
;
newui
.ui_io
+= ci
->ci_io
;
data
.size
= sizeof newui
;
rv
= DB_PUT(usracct_db
, &key
, &data
, 0);
warn("add key %d to user accounting stats", uid
);
warnx("DB_PUT returned 1");
bti
.compare
= uid_compare
;
saved_usracct_db
= dbopen(_PATH_USRACCT
, O_RDWR
|O_CREAT
|O_TRUNC
, 0644,
if (saved_usracct_db
== NULL
) {
warn("creating user accounting summary");
serr
= DB_SEQ(usracct_db
, &key
, &data
, R_FIRST
);
warn("retrieving user accounting stats");
nerr
= DB_PUT(saved_usracct_db
, &key
, &data
, 0);
warn("saving user accounting summary");
serr
= DB_SEQ(usracct_db
, &key
, &data
, R_NEXT
);
warn("retrieving user accounting stats");
if (DB_SYNC(saved_usracct_db
, 0) < 0) {
warn("syncing process accounting summary");
if (DB_CLOSE(saved_usracct_db
) < 0) {
warn("closing process accounting summary");
rv
= DB_SEQ(usracct_db
, &key
, &data
, R_FIRST
);
warn("retrieving user accounting stats");
ui
= (struct userinfo
*) data
.data
;
pwd
= getpwuid(ui
->ui_uid
);
pwd
->pw_name
, ui
->ui_calls
);
/*user_from_uid(ui->ui_uid, 0), ui->ui_calls);*/
t
= (double) (ui
->ui_utime
+ ui
->ui_stime
) /
if (t
< 0.0001) /* kill divide by zero */
printf("%12.2lf%s ", t
/ 60.0, "cpu");
/* ui->ui_calls is always != 0 */
printf("%12qu%s", ui
->ui_io
/ ui
->ui_calls
, "avio");
printf("%12qu%s", ui
->ui_io
, "tio");
/* t is always >= 0.0001; see above */
printf("%12qu%s", ui
->ui_mem
/ t
, "k");
printf("%12qu%s", ui
->ui_mem
, "k*sec");
rv
= DB_SEQ(usracct_db
, &key
, &data
, R_NEXT
);
warn("retrieving user accounting stats");
bcopy(k1
->data
, &d1
, sizeof d1
);
bcopy(k2
->data
, &d2
, sizeof d2
);