1994-09-26 21:22:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1994 Christopher G. Demetriou
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2003-05-03 21:06:42 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-09-26 21:22:57 +00:00
|
|
|
|
2000-05-30 13:27:33 +00:00
|
|
|
#include <sys/param.h>
|
1994-09-26 21:22:57 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/acct.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2002-02-19 00:05:59 +00:00
|
|
|
#include <pwd.h>
|
2002-07-15 16:05:15 +00:00
|
|
|
#include <stdint.h>
|
1996-01-22 09:02:08 +00:00
|
|
|
#include <stdio.h>
|
1997-10-15 06:41:19 +00:00
|
|
|
#include <stdlib.h>
|
1996-01-22 09:02:08 +00:00
|
|
|
#include <string.h>
|
1994-09-26 21:22:57 +00:00
|
|
|
#include "extern.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
|
2005-02-10 12:43:16 +00:00
|
|
|
static int uid_compare(const DBT *, const DBT *);
|
1994-09-26 21:22:57 +00:00
|
|
|
|
|
|
|
static DB *usracct_db;
|
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
/* Legacy format in AHZV1 units. */
|
|
|
|
struct userinfov1 {
|
|
|
|
uid_t ui_uid; /* user id; for consistency */
|
|
|
|
u_quad_t ui_calls; /* number of invocations */
|
|
|
|
u_quad_t ui_utime; /* user time */
|
|
|
|
u_quad_t ui_stime; /* system time */
|
|
|
|
u_quad_t ui_mem; /* memory use */
|
|
|
|
u_quad_t ui_io; /* number of disk i/o ops */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a v1 data record into the current version.
|
|
|
|
* Return 0 if OK, -1 on error, setting errno.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
v1_to_v2(DBT *key, DBT *data)
|
|
|
|
{
|
|
|
|
struct userinfov1 uiv1;
|
|
|
|
static struct userinfo uiv2;
|
|
|
|
static uid_t uid;
|
|
|
|
|
|
|
|
if (key->size != sizeof(u_long) || data->size != sizeof(uiv1)) {
|
|
|
|
errno = EFTYPE;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert key. */
|
|
|
|
key->size = sizeof(uid_t);
|
|
|
|
uid = (uid_t)*(u_long *)(key->data);
|
|
|
|
key->data = &uid;
|
|
|
|
|
|
|
|
/* Convert data. */
|
|
|
|
memcpy(&uiv1, data->data, data->size);
|
|
|
|
memset(&uiv2, 0, sizeof(uiv2));
|
|
|
|
uiv2.ui_uid = uiv1.ui_uid;
|
|
|
|
uiv2.ui_calls = uiv1.ui_calls;
|
|
|
|
uiv2.ui_utime = ((double)uiv1.ui_utime / AHZV1) * 1000000;
|
|
|
|
uiv2.ui_stime = ((double)uiv1.ui_stime / AHZV1) * 1000000;
|
|
|
|
uiv2.ui_mem = uiv1.ui_mem;
|
|
|
|
uiv2.ui_io = uiv1.ui_io;
|
|
|
|
data->size = sizeof(uiv2);
|
|
|
|
data->data = &uiv2;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy usrdb_file to in-memory usracct_db. */
|
1994-09-26 21:22:57 +00:00
|
|
|
int
|
2009-12-29 22:53:27 +00:00
|
|
|
usracct_init(void)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
|
|
|
BTREEINFO bti;
|
|
|
|
|
|
|
|
bzero(&bti, sizeof bti);
|
|
|
|
bti.compare = uid_compare;
|
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
return (db_copy_in(&usracct_db, usrdb_file, "user accounting",
|
|
|
|
&bti, v1_to_v2));
|
1994-09-26 21:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-29 22:53:27 +00:00
|
|
|
usracct_destroy(void)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
2007-05-22 06:51:38 +00:00
|
|
|
db_destroy(usracct_db, "user accounting");
|
1994-09-26 21:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-02-10 12:43:16 +00:00
|
|
|
usracct_add(const struct cmdinfo *ci)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
|
|
|
DBT key, data;
|
|
|
|
struct userinfo newui;
|
2007-05-22 06:51:38 +00:00
|
|
|
uid_t uid;
|
1994-09-26 21:22:57 +00:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
uid = ci->ci_uid;
|
|
|
|
key.data = &uid;
|
|
|
|
key.size = sizeof uid;
|
|
|
|
|
|
|
|
rv = DB_GET(usracct_db, &key, &data, 0);
|
|
|
|
if (rv < 0) {
|
2007-05-22 06:51:38 +00:00
|
|
|
warn("get key %u from user accounting stats", uid);
|
1994-09-26 21:22:57 +00:00
|
|
|
return (-1);
|
|
|
|
} 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) {
|
2007-05-22 06:51:38 +00:00
|
|
|
warnx("key %u != expected record number %u",
|
1994-09-26 21:22:57 +00:00
|
|
|
newui.ui_uid, uid);
|
|
|
|
warnx("inconsistent user accounting stats");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
|
1995-05-30 03:57:47 +00:00
|
|
|
data.data = &newui;
|
1994-09-26 21:22:57 +00:00
|
|
|
data.size = sizeof newui;
|
|
|
|
rv = DB_PUT(usracct_db, &key, &data, 0);
|
|
|
|
if (rv < 0) {
|
2007-05-22 06:51:38 +00:00
|
|
|
warn("add key %u to user accounting stats", uid);
|
1994-09-26 21:22:57 +00:00
|
|
|
return (-1);
|
|
|
|
} else if (rv != 0) {
|
|
|
|
warnx("DB_PUT returned 1");
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
/* Copy in-memory usracct_db to usrdb_file. */
|
1994-09-26 21:22:57 +00:00
|
|
|
int
|
2009-12-29 22:53:27 +00:00
|
|
|
usracct_update(void)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
|
|
|
BTREEINFO bti;
|
|
|
|
|
|
|
|
bzero(&bti, sizeof bti);
|
|
|
|
bti.compare = uid_compare;
|
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
return (db_copy_out(usracct_db, usrdb_file, "user accounting",
|
|
|
|
&bti));
|
1994-09-26 21:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-29 22:53:27 +00:00
|
|
|
usracct_print(void)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
|
|
|
DBT key, data;
|
2000-10-26 17:35:46 +00:00
|
|
|
struct userinfo uistore, *ui = &uistore;
|
1994-09-26 21:22:57 +00:00
|
|
|
double t;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
|
|
|
|
if (rv < 0)
|
|
|
|
warn("retrieving user accounting stats");
|
|
|
|
|
|
|
|
while (rv == 0) {
|
2000-10-26 17:35:46 +00:00
|
|
|
memcpy(ui, data.data, sizeof(struct userinfo));
|
1994-09-26 21:22:57 +00:00
|
|
|
|
2002-07-15 16:05:15 +00:00
|
|
|
printf("%-*s %9ju ", MAXLOGNAME - 1,
|
|
|
|
user_from_uid(ui->ui_uid, 0), (uintmax_t)ui->ui_calls);
|
1994-09-26 21:22:57 +00:00
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
t = (ui->ui_utime + ui->ui_stime) / 1000000;
|
|
|
|
if (t < 0.000001) /* kill divide by zero */
|
|
|
|
t = 0.000001;
|
1994-09-26 21:22:57 +00:00
|
|
|
|
1996-01-22 09:02:08 +00:00
|
|
|
printf("%12.2f%s ", t / 60.0, "cpu");
|
1994-09-26 21:22:57 +00:00
|
|
|
|
|
|
|
/* ui->ui_calls is always != 0 */
|
|
|
|
if (dflag)
|
2007-05-22 06:51:38 +00:00
|
|
|
printf("%12.0f%s",
|
|
|
|
ui->ui_io / ui->ui_calls, "avio");
|
1994-09-26 21:22:57 +00:00
|
|
|
else
|
2007-05-22 06:51:38 +00:00
|
|
|
printf("%12.0f%s", ui->ui_io, "tio");
|
1994-09-26 21:22:57 +00:00
|
|
|
|
2007-05-22 06:51:38 +00:00
|
|
|
/* t is always >= 0.000001; see above. */
|
1994-09-26 21:22:57 +00:00
|
|
|
if (kflag)
|
1999-08-06 07:03:25 +00:00
|
|
|
printf("%12.0f%s", ui->ui_mem / t, "k");
|
1994-09-26 21:22:57 +00:00
|
|
|
else
|
2007-05-22 06:51:38 +00:00
|
|
|
printf("%12.0f%s", ui->ui_mem, "k*sec");
|
1994-09-26 21:22:57 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
|
|
|
|
if (rv < 0)
|
|
|
|
warn("retrieving user accounting stats");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-02-10 12:43:16 +00:00
|
|
|
uid_compare(const DBT *k1, const DBT *k2)
|
1994-09-26 21:22:57 +00:00
|
|
|
{
|
2007-05-22 06:51:38 +00:00
|
|
|
uid_t d1, d2;
|
1994-09-26 21:22:57 +00:00
|
|
|
|
|
|
|
bcopy(k1->data, &d1, sizeof d1);
|
|
|
|
bcopy(k2->data, &d2, sizeof d2);
|
1995-05-30 03:57:47 +00:00
|
|
|
|
1994-09-26 21:22:57 +00:00
|
|
|
if (d1 < d2)
|
|
|
|
return -1;
|
|
|
|
else if (d1 == d2)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|