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
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]
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
40 #include <sys/types.h>
46 #include <libcmdutils.h>
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 *);
72 static void pdisk(void);
76 main(int argc
, char **argv
)
78 char fbuf
[PATH_MAX
+1], *fb
;
84 while ((c
= getopt(argc
, argv
, "p:u:")) != EOF
) {
87 if ((nchrg
= fopen(optarg
, "w")) == NULL
)
89 (void) chmod(optarg
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
100 if ((pwf
= fopen(pfile
, "r")) == NULL
) {
103 /* fill usglist with the user's in the passwd file */
104 while ((pw
= fgetpwent(pwf
)) != NULL
) {
105 if ((entry
= hash_find(pw
->pw_uid
)) == NULL
)
106 entry
= hash_insert(pw
->pw_uid
);
107 validate_entry(entry
, pw
);
112 /* charge the files listed in names to users listed in the usglist */
113 while (fgets(fbuf
, sizeof (fbuf
), stdin
) != NULL
) {
114 if ((fb
= strchr(fbuf
, '\n')) != NULL
) {
116 * replace the newline char at the end of the
117 * filename with a null character
127 (void) fclose(nchrg
);
135 * create a new entry and insert.
138 hash_insert(uid_t uid
)
140 struct disk
*curdisk
;
141 int key
= HASHKEY(uid
);
143 if ((curdisk
= malloc(sizeof (struct disk
))) == NULL
) {
144 (void) fprintf(stderr
, "acctdusg: cannot allocate memory "
145 "for hash table entry\n");
148 curdisk
->dsk_uid
= uid
;
150 curdisk
->validuser
= 0; /* initially invalid */
151 curdisk
->next
= usglist
[key
];
152 usglist
[key
] = curdisk
;
157 * return the disk entry for given uid. return NULL if not found.
162 struct disk
*curdisk
;
164 for (curdisk
= usglist
[HASHKEY(uid
)];
165 curdisk
!= NULL
; curdisk
= curdisk
->next
) {
166 if (curdisk
->dsk_uid
== uid
) {
176 (void) fprintf(stderr
, "Cannot open %s\n", file
);
186 for (index
= 0; index
< MAXUSERS
; index
++) {
187 for (entry
= usglist
[index
];
188 entry
!= NULL
; entry
= entry
->next
) {
189 if (entry
->dsk_du
!= 0) {
190 (void) printf("%ld\t%s\t%lld\n",
200 * Initialize the disk entry for a valid passwd entry.
203 validate_entry(struct disk
*entry
, struct passwd
*pw
)
205 (void) strlcpy(entry
->dsk_name
, pw
->pw_name
,
206 sizeof (entry
->dsk_name
));
207 entry
->validuser
= 1;
217 if (lstat(n
, &statb
) == -1)
221 * do not count the duplicate entries.
223 if (statb
.st_nlink
> 1) {
224 switch (add_tnode(&tree
, statb
.st_dev
, statb
.st_ino
)) {
238 * st_blocks is not defined for character/block special files.
240 if (S_ISCHR(statb
.st_mode
) || S_ISBLK(statb
.st_mode
))
244 * If -p is given, we've all loaded the passwd entries.
245 * Files with unknown uid should go into nchrg. Otherwise
246 * (without -p), we try creating new entry for the uid.
248 if ((entry
= hash_find(statb
.st_uid
)) == NULL
) {
250 pw
= getpwuid(statb
.st_uid
);
251 entry
= hash_insert(statb
.st_uid
);
253 validate_entry(entry
, pw
);
258 if (entry
!= NULL
&& entry
->validuser
) {
259 entry
->dsk_du
+= statb
.st_blocks
;
261 (void) fprintf(nchrg
, "%9ld\t%7llu\t%s\n",
262 statb
.st_uid
, statb
.st_blocks
, n
);
273 for (index
= 0; index
< MAXUSERS
; index
++) {
274 for (entry
= usglist
[index
];
275 entry
!= NULL
; entry
= entry
->next
) {
276 (void) fprintf(stderr
, "%.8s\t%9ld\t%7llu\n",