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);
74 struct passwd
*fgetpwent(FILE *);
77 main(int argc
, char **argv
)
79 char fbuf
[PATH_MAX
+1], *fb
;
85 while ((c
= getopt(argc
, argv
, "p:u:")) != EOF
) {
88 if ((nchrg
= fopen(optarg
, "w")) == NULL
)
90 (void) chmod(optarg
, S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IROTH
);
101 if ((pwf
= fopen(pfile
, "r")) == NULL
) {
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
);
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
128 (void) fclose(nchrg
);
136 * create a new entry and insert.
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");
149 curdisk
->dsk_uid
= uid
;
151 curdisk
->validuser
= 0; /* initially invalid */
152 curdisk
->next
= usglist
[key
];
153 usglist
[key
] = curdisk
;
158 * return the disk entry for given uid. return NULL if not found.
163 struct disk
*curdisk
;
165 for (curdisk
= usglist
[HASHKEY(uid
)];
166 curdisk
!= NULL
; curdisk
= curdisk
->next
) {
167 if (curdisk
->dsk_uid
== uid
) {
177 (void) fprintf(stderr
, "Cannot open %s\n", file
);
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",
201 * Initialize the disk entry for a valid passwd entry.
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;
218 if (lstat(n
, &statb
) == -1)
222 * do not count the duplicate entries.
224 if (statb
.st_nlink
> 1) {
225 switch (add_tnode(&tree
, statb
.st_dev
, statb
.st_ino
)) {
239 * st_blocks is not defined for character/block special files.
241 if (S_ISCHR(statb
.st_mode
) || S_ISBLK(statb
.st_mode
))
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
);
252 validate_entry(entry
, pw
);
256 if (entry
!= NULL
&& entry
->validuser
) {
257 entry
->dsk_du
+= statb
.st_blocks
;
259 (void) fprintf(nchrg
, "%9ld\t%7llu\t%s\n",
260 statb
.st_uid
, statb
.st_blocks
, n
);
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",