#include <sys/types.h>
#include <sys/acct.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#include "pathnames.h"
static FILE *acct_load(const char *, int);
static int cmp_comm(const char *, const char *);
static int cmp_usrsys(const DBT *, const DBT *);
static int cmp_avgusrsys(const DBT *, const DBT *);
static int cmp_dkio(const DBT *, const DBT *);
static int cmp_avgdkio(const DBT *, const DBT *);
static int cmp_cpumem(const DBT *, const DBT *);
static int cmp_avgcpumem(const DBT *, const DBT *);
static int cmp_calls(const DBT *, const DBT *);
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 *));
cmpf_t sa_cmp = cmp_usrsys;
int
main(int argc, char **argv)
{
FILE *f;
char pathacct[] = _PATH_ACCT;
int ch, error = 0;
dfltargv[0] = pathacct;
while ((ch = getopt(argc, argv, "abcdDfijkKlmnP:qrstuU:v:")) != -1)
switch (ch) {
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
sa_cmp = cmp_avgusrsys;
break;
case 'c':
cflag = 1;
break;
case 'd':
dflag = 1;
sa_cmp = cmp_avgdkio;
break;
case 'D':
Dflag = 1;
sa_cmp = cmp_dkio;
break;
case 'f':
fflag = 1;
break;
case 'i':
iflag = 1;
break;
case 'j':
jflag = 1;
break;
case 'k':
kflag = 1;
sa_cmp = cmp_avgcpumem;
break;
case 'K':
sa_cmp = cmp_cpumem;
Kflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'm':
mflag = 1;
break;
case 'n':
sa_cmp = cmp_calls;
break;
case 'P':
pdb_file = optarg;
break;
case 'q':
qflag = 1;
break;
case 'r':
rflag = 1;
break;
case 's':
sflag = 1;
break;
case 't':
tflag = 1;
break;
case 'u':
uflag = 1;
break;
case 'U':
usrdb_file = optarg;
break;
case 'v':
vflag = 1;
cutoff = atoi(optarg);
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (fflag && !vflag)
errx(1, "only one of -f requires -v");
if (fflag && aflag)
errx(1, "only one of -a and -v may be specified");
if (!uflag) {
if ((sflag || (!mflag && !qflag)) && pacct_init() != 0)
errx(1, "process accounting initialization failed");
if ((sflag || (mflag && !qflag)) && usracct_init() != 0)
errx(1, "user accounting initialization failed");
}
if (argc == 0) {
argc = dfltargc;
argv = dfltargv;
}
for (; argc > 0; argc--, argv++) {
f = acct_load(argv[0], sflag);
if (f == NULL)
continue;
if (!uflag && sflag) {
#ifndef DEBUG
sigset_t nmask, omask;
int unmask = 1;
if (sigfillset(&nmask) == -1) {
warn("sigfillset");
unmask = 0;
error = 1;
}
if (unmask &&
(sigprocmask(SIG_BLOCK, &nmask, &omask) == -1)) {
warn("couldn't set signal mask");
unmask = 0;
error = 1;
}
#endif
if (ftruncate(fileno(f), 0) == -1) {
warn("couldn't truncate %s", *argv);
error = 1;
}
if (pacct_update() != 0 || usracct_update() != 0)
error = 1;
#ifndef DEBUG
if (unmask &&
(sigprocmask(SIG_SETMASK, &omask, NULL) == -1)) {
warn("couldn't restore signal mask");
error = 1;
}
#endif
}
if (fclose(f) == EOF) {
warn("fclose %s", *argv);
error = 1;
}
}
if (!uflag && !qflag) {
if (!mflag)
pacct_print();
else
usracct_print();
}
if (!uflag) {
if (sflag || (!mflag && !qflag))
pacct_destroy();
if (sflag || (mflag && !qflag))
usracct_destroy();
}
exit(error);
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: sa [-abcdDfijkKlmnqrstu] [-P file] [-U file] [-v cutoff] [file ...]\n");
exit(1);
}
static FILE *
acct_load(const char *pn, int wr)
{
struct acctv3 ac;
struct cmdinfo ci;
ssize_t rv;
FILE *f;
int i;
f = fopen(pn, wr ? "r+" : "r");
if (f == NULL) {
warn("open %s %s", pn, wr ? "for read/write" : "read-only");
return (NULL);
}
while (1) {
rv = readrec_forward(f, &ac);
if (rv != 1) {
if (rv == EOF)
warn("error reading %s", pn);
break;
}
ci.ci_calls = 1;
for (i = 0; i < (int)sizeof ac.ac_comm && ac.ac_comm[i] != '\0';
i++) {
char c = ac.ac_comm[i];
if (!isascii(c) || iscntrl(c)) {
ci.ci_comm[i] = '?';
ci.ci_flags |= CI_UNPRINTABLE;
} else
ci.ci_comm[i] = c;
}
if (ac.ac_flagx & AFORK)
ci.ci_comm[i++] = '*';
ci.ci_comm[i++] = '\0';
ci.ci_etime = ac.ac_etime;
ci.ci_utime = ac.ac_utime;
ci.ci_stime = ac.ac_stime;
ci.ci_uid = ac.ac_uid;
ci.ci_mem = ac.ac_mem;
ci.ci_io = ac.ac_io;
if (!uflag) {
if (sflag || (!mflag && !qflag))
pacct_add(&ci);
if (sflag || (mflag && !qflag))
usracct_add(&ci);
} else if (!qflag)
printf("%6u %12.3lf cpu %12.0lfk mem %12.0lf io %s\n",
ci.ci_uid,
(ci.ci_utime + ci.ci_stime) / 1000000,
ci.ci_mem, ci.ci_io,
ci.ci_comm);
}
return (f);
}
static int
cmp_comm(const char *s1, const char *s2)
{
int rv;
rv = strcmp(s1, s2);
if (rv == 0)
rv = -1;
return (rflag ? rv : -rv);
}
static int
cmp_usrsys(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
double t1, t2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
t1 = c1.ci_utime + c1.ci_stime;
t2 = c2.ci_utime + c2.ci_stime;
if (t1 < t2)
return -1;
else if (t1 == t2)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_avgusrsys(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
double t1, t2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
t1 = c1.ci_utime + c1.ci_stime;
t1 /= (double) (c1.ci_calls ? c1.ci_calls : 1);
t2 = c2.ci_utime + c2.ci_stime;
t2 /= (double) (c2.ci_calls ? c2.ci_calls : 1);
if (t1 < t2)
return -1;
else if (t1 == t2)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_dkio(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
if (c1.ci_io < c2.ci_io)
return -1;
else if (c1.ci_io == c2.ci_io)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_avgdkio(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
double n1, n2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
n1 = c1.ci_io / (double) (c1.ci_calls ? c1.ci_calls : 1);
n2 = c2.ci_io / (double) (c2.ci_calls ? c2.ci_calls : 1);
if (n1 < n2)
return -1;
else if (n1 == n2)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_cpumem(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
if (c1.ci_mem < c2.ci_mem)
return -1;
else if (c1.ci_mem == c2.ci_mem)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_avgcpumem(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
double t1, t2;
double n1, n2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
t1 = c1.ci_utime + c1.ci_stime;
t2 = c2.ci_utime + c2.ci_stime;
n1 = c1.ci_mem / (t1 ? t1 : 1);
n2 = c2.ci_mem / (t2 ? t2 : 1);
if (n1 < n2)
return -1;
else if (n1 == n2)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}
static int
cmp_calls(const DBT *d1, const DBT *d2)
{
struct cmdinfo c1, c2;
memcpy(&c1, d1->data, sizeof(c1));
memcpy(&c2, d2->data, sizeof(c2));
if (c1.ci_calls < c2.ci_calls)
return -1;
else if (c1.ci_calls == c2.ci_calls)
return (cmp_comm(c1.ci_comm, c2.ci_comm));
else
return 1;
}