Home | History | Annotate | Line # | Download | only in sa
usrdb.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1994 Christopher G. Demetriou
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *      This product includes software developed by Christopher G. Demetriou.
     16  1.1      cgd  * 4. The name of the author may not be used to endorse or promote products
     17  1.1      cgd  *    derived from this software without specific prior written permission
     18  1.1      cgd  *
     19  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1      cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1      cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1      cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1      cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1      cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1      cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1      cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1      cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1      cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1      cgd  */
     30  1.1      cgd 
     31  1.1      cgd #ifndef LINT
     32  1.4      cgd static char rcsid[] = "$Id: usrdb.c,v 1.4 1995/04/24 13:26:26 cgd Exp $";
     33  1.1      cgd #endif
     34  1.1      cgd 
     35  1.1      cgd #include <sys/types.h>
     36  1.1      cgd #include <sys/acct.h>
     37  1.1      cgd #include <err.h>
     38  1.1      cgd #include <errno.h>
     39  1.1      cgd #include <fcntl.h>
     40  1.4      cgd #include <string.h>
     41  1.1      cgd #include "extern.h"
     42  1.1      cgd #include "pathnames.h"
     43  1.1      cgd 
     44  1.1      cgd static int uid_compare __P((const DBT *, const DBT *));
     45  1.1      cgd 
     46  1.1      cgd static DB	*usracct_db;
     47  1.1      cgd 
     48  1.1      cgd int
     49  1.1      cgd usracct_init()
     50  1.1      cgd {
     51  1.1      cgd 	DB *saved_usracct_db;
     52  1.1      cgd 	BTREEINFO bti;
     53  1.1      cgd 	int error;
     54  1.1      cgd 
     55  1.3  mycroft 	memset(&bti, 0, sizeof(bti));
     56  1.1      cgd 	bti.compare = uid_compare;
     57  1.1      cgd 
     58  1.1      cgd 	usracct_db = dbopen(NULL, O_RDWR, 0, DB_BTREE, &bti);
     59  1.1      cgd 	if (usracct_db == NULL)
     60  1.1      cgd 		return (-1);
     61  1.1      cgd 
     62  1.1      cgd 	error = 0;
     63  1.1      cgd 	if (!iflag) {
     64  1.1      cgd 		DBT key, data;
     65  1.1      cgd 		int serr, nerr;
     66  1.1      cgd 
     67  1.1      cgd 		saved_usracct_db = dbopen(_PATH_USRACCT, O_RDONLY, 0, DB_BTREE,
     68  1.1      cgd 		    &bti);
     69  1.1      cgd 		if (saved_usracct_db == NULL) {
     70  1.1      cgd 			error = (errno == ENOENT) ? 0 : -1;
     71  1.1      cgd 			if (error)
     72  1.1      cgd 				warn("retrieving user accounting summary");
     73  1.1      cgd 			goto out;
     74  1.1      cgd 		}
     75  1.1      cgd 
     76  1.1      cgd 		serr = DB_SEQ(saved_usracct_db, &key, &data, R_FIRST);
     77  1.1      cgd 		if (serr < 0) {
     78  1.1      cgd 			warn("retrieving user accounting summary");
     79  1.1      cgd 			error = -1;
     80  1.1      cgd 			goto closeout;
     81  1.1      cgd 		}
     82  1.1      cgd 		while (serr == 0) {
     83  1.1      cgd 			nerr = DB_PUT(usracct_db, &key, &data, 0);
     84  1.1      cgd 			if (nerr < 0) {
     85  1.1      cgd 				warn("initializing user accounting stats");
     86  1.1      cgd 				error = -1;
     87  1.1      cgd 				break;
     88  1.1      cgd 			}
     89  1.1      cgd 
     90  1.1      cgd 			serr = DB_SEQ(saved_usracct_db, &key, &data, R_NEXT);
     91  1.1      cgd 			if (serr < 0) {
     92  1.1      cgd 				warn("retrieving user accounting summary");
     93  1.1      cgd 				error = -1;
     94  1.1      cgd 				break;
     95  1.1      cgd 			}
     96  1.1      cgd 		}
     97  1.1      cgd 
     98  1.1      cgd closeout:
     99  1.1      cgd 		if (DB_CLOSE(saved_usracct_db) < 0) {
    100  1.1      cgd 			warn("closing user accounting summary");
    101  1.1      cgd 			error = -1;
    102  1.1      cgd 		}
    103  1.1      cgd 	}
    104  1.1      cgd 
    105  1.1      cgd out:
    106  1.1      cgd 	if (error != 0)
    107  1.1      cgd 		usracct_destroy();
    108  1.1      cgd 	return (error);
    109  1.1      cgd }
    110  1.1      cgd 
    111  1.1      cgd void
    112  1.1      cgd usracct_destroy()
    113  1.1      cgd {
    114  1.1      cgd 	if (DB_CLOSE(usracct_db) < 0)
    115  1.1      cgd 		warn("destroying user accounting stats");
    116  1.1      cgd }
    117  1.1      cgd 
    118  1.1      cgd int
    119  1.1      cgd usracct_add(ci)
    120  1.1      cgd 	const struct cmdinfo *ci;
    121  1.1      cgd {
    122  1.1      cgd 	DBT key, data;
    123  1.1      cgd 	struct userinfo newui;
    124  1.1      cgd 	u_long uid;
    125  1.1      cgd 	int rv;
    126  1.1      cgd 
    127  1.1      cgd 	uid = ci->ci_uid;
    128  1.1      cgd 	key.data = &uid;
    129  1.3  mycroft 	key.size = sizeof(uid);
    130  1.1      cgd 
    131  1.1      cgd 	rv = DB_GET(usracct_db, &key, &data, 0);
    132  1.1      cgd 	if (rv < 0) {
    133  1.1      cgd 		warn("get key %d from user accounting stats", uid);
    134  1.1      cgd 		return (-1);
    135  1.1      cgd 	} else if (rv == 0) {	/* it's there; copy whole thing */
    136  1.1      cgd 		/* add the old data to the new data */
    137  1.3  mycroft 		memcpy(&newui, data.data, data.size);
    138  1.1      cgd 		if (newui.ui_uid != uid) {
    139  1.1      cgd 			warnx("key %d != expected record number %d",
    140  1.1      cgd 			    newui.ui_uid, uid);
    141  1.1      cgd 			warnx("inconsistent user accounting stats");
    142  1.1      cgd 			return (-1);
    143  1.1      cgd 		}
    144  1.1      cgd 	} else {		/* it's not there; zero it and copy the key */
    145  1.3  mycroft 		memset(&newui, 0, sizeof(newui));
    146  1.1      cgd 		newui.ui_uid = ci->ci_uid;
    147  1.1      cgd 	}
    148  1.1      cgd 
    149  1.1      cgd 	newui.ui_calls += ci->ci_calls;
    150  1.1      cgd 	newui.ui_utime += ci->ci_utime;
    151  1.1      cgd 	newui.ui_stime += ci->ci_stime;
    152  1.1      cgd 	newui.ui_mem += ci->ci_mem;
    153  1.1      cgd 	newui.ui_io += ci->ci_io;
    154  1.1      cgd 
    155  1.1      cgd 	data.data = &newui;
    156  1.3  mycroft 	data.size = sizeof(newui);
    157  1.1      cgd 	rv = DB_PUT(usracct_db, &key, &data, 0);
    158  1.1      cgd 	if (rv < 0) {
    159  1.1      cgd 		warn("add key %d to user accounting stats", uid);
    160  1.1      cgd 		return (-1);
    161  1.1      cgd 	} else if (rv != 0) {
    162  1.1      cgd 		warnx("DB_PUT returned 1");
    163  1.1      cgd 		return (-1);
    164  1.1      cgd 	}
    165  1.1      cgd 
    166  1.1      cgd 	return (0);
    167  1.1      cgd }
    168  1.1      cgd 
    169  1.1      cgd int
    170  1.1      cgd usracct_update()
    171  1.1      cgd {
    172  1.1      cgd 	DB *saved_usracct_db;
    173  1.1      cgd 	DBT key, data;
    174  1.1      cgd 	BTREEINFO bti;
    175  1.1      cgd 	u_long uid;
    176  1.1      cgd 	int error, serr, nerr;
    177  1.1      cgd 
    178  1.3  mycroft 	memset(&bti, 0, sizeof(bti));
    179  1.1      cgd 	bti.compare = uid_compare;
    180  1.1      cgd 
    181  1.1      cgd 	saved_usracct_db = dbopen(_PATH_USRACCT, O_RDWR|O_CREAT|O_TRUNC, 0644,
    182  1.1      cgd 	    DB_BTREE, &bti);
    183  1.1      cgd 	if (saved_usracct_db == NULL) {
    184  1.1      cgd 		warn("creating user accounting summary");
    185  1.1      cgd 		return (-1);
    186  1.1      cgd 	}
    187  1.1      cgd 
    188  1.1      cgd 	error = 0;
    189  1.1      cgd 
    190  1.1      cgd 	serr = DB_SEQ(usracct_db, &key, &data, R_FIRST);
    191  1.1      cgd 	if (serr < 0) {
    192  1.1      cgd 		warn("retrieving user accounting stats");
    193  1.1      cgd 		error = -1;
    194  1.1      cgd 	}
    195  1.1      cgd 	while (serr == 0) {
    196  1.1      cgd 		nerr = DB_PUT(saved_usracct_db, &key, &data, 0);
    197  1.1      cgd 		if (nerr < 0) {
    198  1.1      cgd 			warn("saving user accounting summary");
    199  1.1      cgd 			error = -1;
    200  1.1      cgd 			break;
    201  1.1      cgd 		}
    202  1.1      cgd 
    203  1.1      cgd 		serr = DB_SEQ(usracct_db, &key, &data, R_NEXT);
    204  1.1      cgd 		if (serr < 0) {
    205  1.1      cgd 			warn("retrieving user accounting stats");
    206  1.1      cgd 			error = -1;
    207  1.1      cgd 			break;
    208  1.1      cgd 		}
    209  1.1      cgd 	}
    210  1.1      cgd 
    211  1.1      cgd 	if (DB_SYNC(saved_usracct_db, 0) < 0) {
    212  1.1      cgd 		warn("syncing process accounting summary");
    213  1.1      cgd 		error = -1;
    214  1.1      cgd 	}
    215  1.1      cgd out:
    216  1.1      cgd 	if (DB_CLOSE(saved_usracct_db) < 0) {
    217  1.1      cgd 		warn("closing process accounting summary");
    218  1.1      cgd 		error = -1;
    219  1.1      cgd 	}
    220  1.1      cgd 	return error;
    221  1.1      cgd }
    222  1.1      cgd 
    223  1.1      cgd void
    224  1.1      cgd usracct_print()
    225  1.1      cgd {
    226  1.1      cgd 	DBT key, data;
    227  1.2       pk 	struct userinfo uistore, *ui = &uistore;
    228  1.1      cgd 	double t;
    229  1.1      cgd 	int rv;
    230  1.1      cgd 
    231  1.1      cgd 	rv = DB_SEQ(usracct_db, &key, &data, R_FIRST);
    232  1.1      cgd 	if (rv < 0)
    233  1.1      cgd 		warn("retrieving user accounting stats");
    234  1.1      cgd 
    235  1.1      cgd 	while (rv == 0) {
    236  1.3  mycroft 		memcpy(ui, data.data, sizeof(struct userinfo));
    237  1.1      cgd 
    238  1.1      cgd 		printf("%-8s %9qu ",
    239  1.1      cgd 		    user_from_uid(ui->ui_uid, 0), ui->ui_calls);
    240  1.1      cgd 
    241  1.1      cgd 		t = (double) (ui->ui_utime + ui->ui_stime) /
    242  1.1      cgd 		    (double) AHZ;
    243  1.1      cgd 		if (t < 0.0001)		/* kill divide by zero */
    244  1.1      cgd 			t = 0.0001;
    245  1.1      cgd 
    246  1.1      cgd 		printf("%12.2lf%s ", t / 60.0, "cpu");
    247  1.1      cgd 
    248  1.1      cgd 		/* ui->ui_calls is always != 0 */
    249  1.1      cgd 		if (dflag)
    250  1.1      cgd 			printf("%12qu%s", ui->ui_io / ui->ui_calls, "avio");
    251  1.1      cgd 		else
    252  1.1      cgd 			printf("%12qu%s", ui->ui_io, "tio");
    253  1.1      cgd 
    254  1.1      cgd 		/* t is always >= 0.0001; see above */
    255  1.1      cgd 		if (kflag)
    256  1.1      cgd 			printf("%12qu%s", ui->ui_mem / t, "k");
    257  1.1      cgd 		else
    258  1.1      cgd 			printf("%12qu%s", ui->ui_mem, "k*sec");
    259  1.1      cgd 
    260  1.1      cgd 		printf("\n");
    261  1.1      cgd 
    262  1.1      cgd 		rv = DB_SEQ(usracct_db, &key, &data, R_NEXT);
    263  1.1      cgd 		if (rv < 0)
    264  1.1      cgd 			warn("retrieving user accounting stats");
    265  1.1      cgd 	}
    266  1.1      cgd }
    267  1.1      cgd 
    268  1.1      cgd static int
    269  1.1      cgd uid_compare(k1, k2)
    270  1.1      cgd 	const DBT *k1, *k2;
    271  1.1      cgd {
    272  1.1      cgd 	u_long d1, d2;
    273  1.1      cgd 
    274  1.3  mycroft 	memcpy(&d1, k1->data, sizeof(d1));
    275  1.3  mycroft 	memcpy(&d2, k2->data, sizeof(d2));
    276  1.1      cgd 
    277  1.1      cgd 	if (d1 < d2)
    278  1.1      cgd 		return -1;
    279  1.1      cgd 	else if (d1 == d2)
    280  1.1      cgd 		return 0;
    281  1.1      cgd 	else
    282  1.1      cgd 		return 1;
    283  1.1      cgd }
    284