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