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