1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 24*7c478bd9Sstevel@tonic-gate 25*7c478bd9Sstevel@tonic-gate /* 26*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 27*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 28*7c478bd9Sstevel@tonic-gate */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.8 */ 32*7c478bd9Sstevel@tonic-gate /* 33*7c478bd9Sstevel@tonic-gate * acctdusg [-u file] [-p file] > dtmp-file 34*7c478bd9Sstevel@tonic-gate * -u file for names of files not charged to anyone 35*7c478bd9Sstevel@tonic-gate * -p get password info from file 36*7c478bd9Sstevel@tonic-gate * reads std input (normally from find / -print) 37*7c478bd9Sstevel@tonic-gate * and computes disk resource consumption by login 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate #include <stdio.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 42*7c478bd9Sstevel@tonic-gate #include <pwd.h> 43*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 44*7c478bd9Sstevel@tonic-gate #include <string.h> 45*7c478bd9Sstevel@tonic-gate #include <limits.h> 46*7c478bd9Sstevel@tonic-gate #include <libcmdutils.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate #include "acctdef.h" 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate struct disk { 51*7c478bd9Sstevel@tonic-gate struct disk *next; /* next entry at same hash tbl index */ 52*7c478bd9Sstevel@tonic-gate uid_t dsk_uid; /* user id of login name */ 53*7c478bd9Sstevel@tonic-gate blkcnt_t dsk_du; /* disk usage */ 54*7c478bd9Sstevel@tonic-gate char dsk_name[NSZ+1]; /* login name */ 55*7c478bd9Sstevel@tonic-gate char validuser; /* set if the uid exists */ 56*7c478bd9Sstevel@tonic-gate }; 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate static char *pfile = NULL; 59*7c478bd9Sstevel@tonic-gate static FILE *nchrg = NULL; 60*7c478bd9Sstevel@tonic-gate static avl_tree_t *tree = NULL; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate static struct disk *usglist[MAXUSERS]; /* holds data on disk usg by uid */ 63*7c478bd9Sstevel@tonic-gate #define HASHKEY(x) ((int)((unsigned int)(x) % MAXUSERS)) 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate static struct disk *hash_insert(uid_t); 66*7c478bd9Sstevel@tonic-gate static struct disk *hash_find(uid_t); 67*7c478bd9Sstevel@tonic-gate static void openerr(char *); 68*7c478bd9Sstevel@tonic-gate static void output(void); 69*7c478bd9Sstevel@tonic-gate static void validate_entry(struct disk *, struct passwd *); 70*7c478bd9Sstevel@tonic-gate static void charge(char *); 71*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 72*7c478bd9Sstevel@tonic-gate static void pdisk(void); 73*7c478bd9Sstevel@tonic-gate #endif 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate int 76*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 77*7c478bd9Sstevel@tonic-gate { 78*7c478bd9Sstevel@tonic-gate char fbuf[PATH_MAX+1], *fb; 79*7c478bd9Sstevel@tonic-gate FILE *pwf; 80*7c478bd9Sstevel@tonic-gate int c; 81*7c478bd9Sstevel@tonic-gate struct passwd *pw; 82*7c478bd9Sstevel@tonic-gate struct disk *entry; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "p:u:")) != EOF) { 85*7c478bd9Sstevel@tonic-gate switch (c) { 86*7c478bd9Sstevel@tonic-gate case 'u': 87*7c478bd9Sstevel@tonic-gate if ((nchrg = fopen(optarg, "w")) == NULL) 88*7c478bd9Sstevel@tonic-gate openerr(optarg); 89*7c478bd9Sstevel@tonic-gate (void) chmod(optarg, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); 90*7c478bd9Sstevel@tonic-gate break; 91*7c478bd9Sstevel@tonic-gate case 'p': 92*7c478bd9Sstevel@tonic-gate pfile = optarg; 93*7c478bd9Sstevel@tonic-gate break; 94*7c478bd9Sstevel@tonic-gate default: 95*7c478bd9Sstevel@tonic-gate exit(1); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate } 98*7c478bd9Sstevel@tonic-gate 99*7c478bd9Sstevel@tonic-gate if (pfile) { 100*7c478bd9Sstevel@tonic-gate if ((pwf = fopen(pfile, "r")) == NULL) { 101*7c478bd9Sstevel@tonic-gate openerr(pfile); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate /* fill usglist with the user's in the passwd file */ 104*7c478bd9Sstevel@tonic-gate while ((pw = fgetpwent(pwf)) != NULL) { 105*7c478bd9Sstevel@tonic-gate if ((entry = hash_find(pw->pw_uid)) == NULL) 106*7c478bd9Sstevel@tonic-gate entry = hash_insert(pw->pw_uid); 107*7c478bd9Sstevel@tonic-gate validate_entry(entry, pw); 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate (void) fclose(pwf); 110*7c478bd9Sstevel@tonic-gate } 111*7c478bd9Sstevel@tonic-gate 112*7c478bd9Sstevel@tonic-gate /* charge the files listed in names to users listed in the usglist */ 113*7c478bd9Sstevel@tonic-gate while (fgets(fbuf, sizeof (fbuf), stdin) != NULL) { 114*7c478bd9Sstevel@tonic-gate if ((fb = strchr(fbuf, '\n')) != NULL) { 115*7c478bd9Sstevel@tonic-gate /* 116*7c478bd9Sstevel@tonic-gate * replace the newline char at the end of the 117*7c478bd9Sstevel@tonic-gate * filename with a null character 118*7c478bd9Sstevel@tonic-gate */ 119*7c478bd9Sstevel@tonic-gate *fb = '\0'; 120*7c478bd9Sstevel@tonic-gate } 121*7c478bd9Sstevel@tonic-gate charge(fbuf); 122*7c478bd9Sstevel@tonic-gate } 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate output(); 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate if (nchrg) 127*7c478bd9Sstevel@tonic-gate (void) fclose(nchrg); 128*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 129*7c478bd9Sstevel@tonic-gate pdisk(); 130*7c478bd9Sstevel@tonic-gate #endif 131*7c478bd9Sstevel@tonic-gate return (0); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate /* 135*7c478bd9Sstevel@tonic-gate * create a new entry and insert. 136*7c478bd9Sstevel@tonic-gate */ 137*7c478bd9Sstevel@tonic-gate static struct disk * 138*7c478bd9Sstevel@tonic-gate hash_insert(uid_t uid) 139*7c478bd9Sstevel@tonic-gate { 140*7c478bd9Sstevel@tonic-gate struct disk *curdisk; 141*7c478bd9Sstevel@tonic-gate int key = HASHKEY(uid); 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate if ((curdisk = malloc(sizeof (struct disk))) == NULL) { 144*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "acctdusg: cannot allocate memory " 145*7c478bd9Sstevel@tonic-gate "for hash table entry\n"); 146*7c478bd9Sstevel@tonic-gate exit(1); 147*7c478bd9Sstevel@tonic-gate } 148*7c478bd9Sstevel@tonic-gate curdisk->dsk_uid = uid; 149*7c478bd9Sstevel@tonic-gate curdisk->dsk_du = 0; 150*7c478bd9Sstevel@tonic-gate curdisk->validuser = 0; /* initially invalid */ 151*7c478bd9Sstevel@tonic-gate curdisk->next = usglist[key]; 152*7c478bd9Sstevel@tonic-gate usglist[key] = curdisk; 153*7c478bd9Sstevel@tonic-gate return (curdisk); 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate /* 157*7c478bd9Sstevel@tonic-gate * return the disk entry for given uid. return NULL if not found. 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate static struct disk * 160*7c478bd9Sstevel@tonic-gate hash_find(uid_t uid) 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate struct disk *curdisk; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate for (curdisk = usglist[HASHKEY(uid)]; 165*7c478bd9Sstevel@tonic-gate curdisk != NULL; curdisk = curdisk->next) { 166*7c478bd9Sstevel@tonic-gate if (curdisk->dsk_uid == uid) { 167*7c478bd9Sstevel@tonic-gate return (curdisk); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate return (NULL); 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate static void 174*7c478bd9Sstevel@tonic-gate openerr(char *file) 175*7c478bd9Sstevel@tonic-gate { 176*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Cannot open %s\n", file); 177*7c478bd9Sstevel@tonic-gate exit(1); 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate 180*7c478bd9Sstevel@tonic-gate static void 181*7c478bd9Sstevel@tonic-gate output(void) 182*7c478bd9Sstevel@tonic-gate { 183*7c478bd9Sstevel@tonic-gate int index; 184*7c478bd9Sstevel@tonic-gate struct disk *entry; 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate for (index = 0; index < MAXUSERS; index++) { 187*7c478bd9Sstevel@tonic-gate for (entry = usglist[index]; 188*7c478bd9Sstevel@tonic-gate entry != NULL; entry = entry->next) { 189*7c478bd9Sstevel@tonic-gate if (entry->dsk_du != 0) { 190*7c478bd9Sstevel@tonic-gate (void) printf("%ld\t%s\t%lld\n", 191*7c478bd9Sstevel@tonic-gate entry->dsk_uid, 192*7c478bd9Sstevel@tonic-gate entry->dsk_name, 193*7c478bd9Sstevel@tonic-gate entry->dsk_du); 194*7c478bd9Sstevel@tonic-gate } 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate } 198*7c478bd9Sstevel@tonic-gate 199*7c478bd9Sstevel@tonic-gate /* 200*7c478bd9Sstevel@tonic-gate * Initialize the disk entry for a valid passwd entry. 201*7c478bd9Sstevel@tonic-gate */ 202*7c478bd9Sstevel@tonic-gate static void 203*7c478bd9Sstevel@tonic-gate validate_entry(struct disk *entry, struct passwd *pw) 204*7c478bd9Sstevel@tonic-gate { 205*7c478bd9Sstevel@tonic-gate (void) strlcpy(entry->dsk_name, pw->pw_name, 206*7c478bd9Sstevel@tonic-gate sizeof (entry->dsk_name)); 207*7c478bd9Sstevel@tonic-gate entry->validuser = 1; 208*7c478bd9Sstevel@tonic-gate } 209*7c478bd9Sstevel@tonic-gate 210*7c478bd9Sstevel@tonic-gate static void 211*7c478bd9Sstevel@tonic-gate charge(char *n) 212*7c478bd9Sstevel@tonic-gate { 213*7c478bd9Sstevel@tonic-gate struct stat statb; 214*7c478bd9Sstevel@tonic-gate struct disk *entry; 215*7c478bd9Sstevel@tonic-gate struct passwd *pw; 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate if (lstat(n, &statb) == -1) 218*7c478bd9Sstevel@tonic-gate return; 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate /* 221*7c478bd9Sstevel@tonic-gate * do not count the duplicate entries. 222*7c478bd9Sstevel@tonic-gate */ 223*7c478bd9Sstevel@tonic-gate if (statb.st_nlink > 1) { 224*7c478bd9Sstevel@tonic-gate switch (add_tnode(&tree, statb.st_dev, statb.st_ino)) { 225*7c478bd9Sstevel@tonic-gate case 0: 226*7c478bd9Sstevel@tonic-gate /* already exist */ 227*7c478bd9Sstevel@tonic-gate return; 228*7c478bd9Sstevel@tonic-gate case 1: 229*7c478bd9Sstevel@tonic-gate /* added */ 230*7c478bd9Sstevel@tonic-gate break; 231*7c478bd9Sstevel@tonic-gate default: 232*7c478bd9Sstevel@tonic-gate perror("acctdusg"); 233*7c478bd9Sstevel@tonic-gate exit(1); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate } 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate /* 238*7c478bd9Sstevel@tonic-gate * st_blocks is not defined for character/block special files. 239*7c478bd9Sstevel@tonic-gate */ 240*7c478bd9Sstevel@tonic-gate if (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode)) 241*7c478bd9Sstevel@tonic-gate statb.st_blocks = 0; 242*7c478bd9Sstevel@tonic-gate 243*7c478bd9Sstevel@tonic-gate /* 244*7c478bd9Sstevel@tonic-gate * If -p is given, we've all loaded the passwd entries. 245*7c478bd9Sstevel@tonic-gate * Files with unknown uid should go into nchrg. Otherwise 246*7c478bd9Sstevel@tonic-gate * (without -p), we try creating new entry for the uid. 247*7c478bd9Sstevel@tonic-gate */ 248*7c478bd9Sstevel@tonic-gate if ((entry = hash_find(statb.st_uid)) == NULL) { 249*7c478bd9Sstevel@tonic-gate if (pfile == NULL) { 250*7c478bd9Sstevel@tonic-gate pw = getpwuid(statb.st_uid); 251*7c478bd9Sstevel@tonic-gate entry = hash_insert(statb.st_uid); 252*7c478bd9Sstevel@tonic-gate if (pw != NULL) { 253*7c478bd9Sstevel@tonic-gate validate_entry(entry, pw); 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate } 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate if (entry != NULL && entry->validuser) { 259*7c478bd9Sstevel@tonic-gate entry->dsk_du += statb.st_blocks; 260*7c478bd9Sstevel@tonic-gate } else if (nchrg) { 261*7c478bd9Sstevel@tonic-gate (void) fprintf(nchrg, "%9ld\t%7llu\t%s\n", 262*7c478bd9Sstevel@tonic-gate statb.st_uid, statb.st_blocks, n); 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate #ifdef DEBUG 267*7c478bd9Sstevel@tonic-gate static void 268*7c478bd9Sstevel@tonic-gate pdisk() 269*7c478bd9Sstevel@tonic-gate { 270*7c478bd9Sstevel@tonic-gate int index; 271*7c478bd9Sstevel@tonic-gate struct disk *entry; 272*7c478bd9Sstevel@tonic-gate 273*7c478bd9Sstevel@tonic-gate for (index = 0; index < MAXUSERS; index++) { 274*7c478bd9Sstevel@tonic-gate for (entry = usglist[index]; 275*7c478bd9Sstevel@tonic-gate entry != NULL; entry = entry->next) { 276*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%.8s\t%9ld\t%7llu\n", 277*7c478bd9Sstevel@tonic-gate entry->dsk_name, 278*7c478bd9Sstevel@tonic-gate entry->dsk_uid, 279*7c478bd9Sstevel@tonic-gate entry->dsk_du); 280*7c478bd9Sstevel@tonic-gate } 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate } 284*7c478bd9Sstevel@tonic-gate #endif 285