From 5f6f6b2933fa608f6dd5c8d9769eba8b8d5e9c2b Mon Sep 17 00:00:00 2001 From: Diomidis Spinellis Date: Fri, 18 May 2007 12:36:10 +0000 Subject: [PATCH] Add -U and -P options that allow the specification of the per-user and per-process summary file location. These make the program more flexible, and also make it possible to write sane regression tests. --- usr.sbin/sa/extern.h | 1 + usr.sbin/sa/main.c | 14 ++++++++++++-- usr.sbin/sa/pdb.c | 4 ++-- usr.sbin/sa/sa.8 | 16 +++++++++++++++- usr.sbin/sa/usrdb.c | 4 ++-- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/usr.sbin/sa/extern.h b/usr.sbin/sa/extern.h index bc5f199e997e..ccdd763c1dbd 100644 --- a/usr.sbin/sa/extern.h +++ b/usr.sbin/sa/extern.h @@ -82,6 +82,7 @@ extern int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag; extern int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag; extern u_quad_t cutoff; extern cmpf_t sa_cmp; +extern const char *pdb_file, *usrdb_file; /* some #defines to help with db's stupidity */ diff --git a/usr.sbin/sa/main.c b/usr.sbin/sa/main.c index 1796c9c0872d..3e10c1bf6378 100644 --- a/usr.sbin/sa/main.c +++ b/usr.sbin/sa/main.c @@ -71,6 +71,8 @@ static void usage(void); int aflag, bflag, cflag, dflag, Dflag, fflag, iflag, jflag, kflag; int Kflag, lflag, mflag, qflag, rflag, sflag, tflag, uflag, vflag; u_quad_t cutoff = 1; +const char *pdb_file = _PATH_SAVACCT; +const char *usrdb_file = _PATH_USRACCT; static char *dfltargv[] = { NULL }; static int dfltargc = (sizeof dfltargv/sizeof(char *)); @@ -86,7 +88,7 @@ main(int argc, char **argv) dfltargv[0] = pathacct; - while ((ch = getopt(argc, argv, "abcdDfijkKlmnqrstuv:")) != -1) + while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1) switch (ch) { case 'a': /* print all commands */ @@ -145,6 +147,10 @@ main(int argc, char **argv) /* sort by number of calls */ sa_cmp = cmp_calls; break; + case 'P': + /* specify program database summary file */ + pdb_file = optarg; + break; case 'q': /* quiet; error messages only */ qflag = 1; @@ -165,6 +171,10 @@ main(int argc, char **argv) /* first, print uid and command name */ uflag = 1; break; + case 'U': + /* specify user database summary file */ + usrdb_file = optarg; + break; case 'v': /* cull junk */ vflag = 1; @@ -294,7 +304,7 @@ static void usage() { (void)fprintf(stderr, - "usage: sa [-abcdDfijkKlmnqrstu] [-v cutoff] [file ...]\n"); + "usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n"); exit(1); } diff --git a/usr.sbin/sa/pdb.c b/usr.sbin/sa/pdb.c index 4dd9d2789e12..0c92d341b083 100644 --- a/usr.sbin/sa/pdb.c +++ b/usr.sbin/sa/pdb.c @@ -63,7 +63,7 @@ pacct_init() DBT key, data; int serr, nerr; - saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDONLY, 0, DB_BTREE, + saved_pacct_db = dbopen(pdb_file, O_RDONLY, 0, DB_BTREE, NULL); if (saved_pacct_db == NULL) { error = errno == ENOENT ? 0 : -1; @@ -161,7 +161,7 @@ pacct_update() DBT key, data; int error, serr, nerr; - saved_pacct_db = dbopen(_PATH_SAVACCT, O_RDWR|O_CREAT|O_TRUNC, 0644, + saved_pacct_db = dbopen(pdb_file, O_RDWR|O_CREAT|O_TRUNC, 0644, DB_BTREE, NULL); if (saved_pacct_db == NULL) { warn("creating process accounting summary"); diff --git a/usr.sbin/sa/sa.8 b/usr.sbin/sa/sa.8 index c122098110d2..e7630642a01a 100644 --- a/usr.sbin/sa/sa.8 +++ b/usr.sbin/sa/sa.8 @@ -29,7 +29,7 @@ .\" .\" $FreeBSD$ .\" -.Dd February 25, 1994 +.Dd May 18, 2007 .Dt SA 8 .Os .Sh NAME @@ -38,6 +38,8 @@ .Sh SYNOPSIS .Nm .Op Fl abcdDfijkKlmnqrstu +.Op Fl P Ar file +.Op Fl U Ar file .Op Fl v Ar cutoff .Op Ar .Sh DESCRIPTION @@ -143,6 +145,12 @@ Separate system and user time; normally they are combined. Print per-user statistics rather than per-command statistics. .It Fl n Sort by number of calls. +.It Fl P Ar file +Use the specified +.Ar file +for accessing the per-command accounting summary database, +instead of the default +.Pa /var/account/savacct . .It Fl q Create no output other than error messages. .It Fl r @@ -155,6 +163,12 @@ For each command, report the ratio of real time to the sum of user and system cpu times. If the cpu time is too small to report, ``*ignore*'' appears in this field. +.It Fl U Ar file +Use the specified +.Ar file +for accessing the per-user accounting summary database, +instead of the default +.Pa /var/account/usracct . .It Fl u Superseding all other flags, for each entry in the accounting file, print the user ID, total seconds of cpu usage, diff --git a/usr.sbin/sa/usrdb.c b/usr.sbin/sa/usrdb.c index 991fc906b7ad..2f76d65cb665 100644 --- a/usr.sbin/sa/usrdb.c +++ b/usr.sbin/sa/usrdb.c @@ -68,7 +68,7 @@ usracct_init() DBT key, data; int serr, nerr; - saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE, + saved_usracct_db = dbopen(usrdb_file, O_RDONLY, 0, DB_BTREE, &bti); if (saved_usracct_db == NULL) { error = (errno == ENOENT) ? 0 : -1; @@ -180,7 +180,7 @@ usracct_update() bzero(&bti, sizeof bti); bti.compare = uid_compare; - saved_usracct_db = dbopen(_PATH_USRACCT, O_RDWR|O_CREAT|O_TRUNC, 0644, + saved_usracct_db = dbopen(usrdb_file, O_RDWR|O_CREAT|O_TRUNC, 0644, DB_BTREE, &bti); if (saved_usracct_db == NULL) { warn("creating user accounting summary");