17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d1a180b0Smaheshvs  * Common Development and Distribution License (the "License").
6d1a180b0Smaheshvs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d1a180b0Smaheshvs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*8509e9caSToomas Soome /*	  All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Quota report
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <strings.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/param.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <fcntl.h>
517c478bd9Sstevel@tonic-gate #include <sys/filio.h>
527c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
537c478bd9Sstevel@tonic-gate #include <sys/time.h>
547c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <sys/mnttab.h>
577c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
587c478bd9Sstevel@tonic-gate #include <pwd.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	LOGINNAMESIZE	8
617c478bd9Sstevel@tonic-gate struct username {
627c478bd9Sstevel@tonic-gate 	struct username *u_next;
637c478bd9Sstevel@tonic-gate 	uid_t u_uid;
647c478bd9Sstevel@tonic-gate 	char u_name[LOGINNAMESIZE + 1];
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate #define	UHASH 997
677c478bd9Sstevel@tonic-gate static struct username *uhead[UHASH];
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate static struct username *lookup(uid_t);
707c478bd9Sstevel@tonic-gate static struct username *adduid(uid_t);
717c478bd9Sstevel@tonic-gate static int repquota(char *, char *, char *);
727c478bd9Sstevel@tonic-gate static void prquota(uid_t, struct dqblk *);
737c478bd9Sstevel@tonic-gate static void header(void);
747c478bd9Sstevel@tonic-gate static void usage(void);
757c478bd9Sstevel@tonic-gate static void fmttime(char *, long);
767c478bd9Sstevel@tonic-gate static char *hasvfsopt(struct vfstab *, char *);
777c478bd9Sstevel@tonic-gate static int quotactl(int, char *, uid_t, caddr_t);
787c478bd9Sstevel@tonic-gate static int oneof(char *, char **, int);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate extern char *mntopt();
817c478bd9Sstevel@tonic-gate extern char *hasmntopt();
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static int	vflag;		/* verbose */
847c478bd9Sstevel@tonic-gate static int	aflag;		/* all file systems */
857c478bd9Sstevel@tonic-gate static char **listbuf;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define	QFNAME "quotas"
887c478bd9Sstevel@tonic-gate #define	CHUNK	50
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #if DEV_BSIZE < 1024
917c478bd9Sstevel@tonic-gate #define	dbtok(x)	((x) / (1024 / DEV_BSIZE))
927c478bd9Sstevel@tonic-gate #else
937c478bd9Sstevel@tonic-gate #define	dbtok(x)	((x) * (DEV_BSIZE / 1024))
947c478bd9Sstevel@tonic-gate #endif
957c478bd9Sstevel@tonic-gate 
96d1a180b0Smaheshvs int
main(int argc,char ** argv)977c478bd9Sstevel@tonic-gate main(int argc, char **argv)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	struct mnttab mntp;
1007c478bd9Sstevel@tonic-gate 	struct vfstab vfsbuf;
1017c478bd9Sstevel@tonic-gate 	char **listp;
1027c478bd9Sstevel@tonic-gate 	int listcnt;
1037c478bd9Sstevel@tonic-gate 	int listmax = 0;
1047c478bd9Sstevel@tonic-gate 	char quotafile[MAXPATHLEN];
1057c478bd9Sstevel@tonic-gate 	FILE *mtab, *vfstab;
1067c478bd9Sstevel@tonic-gate 	int errs = 0;
1077c478bd9Sstevel@tonic-gate 	int	opt;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	if ((listbuf = malloc(sizeof (char *) * CHUNK)) == NULL) {
1107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't alloc lisbuf array.");
1117c478bd9Sstevel@tonic-gate 		exit(31+1);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	listmax = CHUNK;
1147c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "avV")) != EOF)
1157c478bd9Sstevel@tonic-gate 		switch (opt) {
1167c478bd9Sstevel@tonic-gate 		case 'v':
1177c478bd9Sstevel@tonic-gate 			vflag++;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		case 'a':
1217c478bd9Sstevel@tonic-gate 			aflag++;
1227c478bd9Sstevel@tonic-gate 			break;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		case 'V': {
1257c478bd9Sstevel@tonic-gate 				/* Print command line */
1267c478bd9Sstevel@tonic-gate 				char	*optt;
1277c478bd9Sstevel@tonic-gate 				int	optc;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 				(void) printf("repquota -F ufs ");
1307c478bd9Sstevel@tonic-gate 				for (optc = 1; optc < argc; optc++) {
1317c478bd9Sstevel@tonic-gate 					optt = argv[optc];
1327c478bd9Sstevel@tonic-gate 					if (optt)
1337c478bd9Sstevel@tonic-gate 						(void) printf(" %s ", optt);
1347c478bd9Sstevel@tonic-gate 				}
1357c478bd9Sstevel@tonic-gate 				(void) putchar('\n');
1367c478bd9Sstevel@tonic-gate 			}
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		case '?':
1407c478bd9Sstevel@tonic-gate 		default:
1417c478bd9Sstevel@tonic-gate 			usage();
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	if (argc <= optind && !aflag)
1457c478bd9Sstevel@tonic-gate 		usage();
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * Sync quota information to disk (as userdata).  On logging
1497c478bd9Sstevel@tonic-gate 	 * file systems, this operation does nothing because quota
1507c478bd9Sstevel@tonic-gate 	 * information is treated as metadata.  Logging file systems
1517c478bd9Sstevel@tonic-gate 	 * are dealt with below in repquota().
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	if (quotactl(Q_ALLSYNC, NULL, 0, NULL) < 0 && errno == EINVAL && vflag)
1547c478bd9Sstevel@tonic-gate 		(void) printf("Warning: "
1557c478bd9Sstevel@tonic-gate 			"Quotas are not available in this kernel\n");
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	/*
1587c478bd9Sstevel@tonic-gate 	 * If aflag go through vfstab and make a list of appropriate
1597c478bd9Sstevel@tonic-gate 	 * filesystems.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	if (aflag) {
1627c478bd9Sstevel@tonic-gate 		listp = listbuf;
1637c478bd9Sstevel@tonic-gate 		listcnt = 0;
1647c478bd9Sstevel@tonic-gate 		if ((vfstab = fopen(VFSTAB, "r")) == NULL) {
1657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Can't open ");
1667c478bd9Sstevel@tonic-gate 			perror(VFSTAB);
1677c478bd9Sstevel@tonic-gate 			exit(31+8);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		while (getvfsent(vfstab, &vfsbuf) == 0) {
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			if (strcmp(vfsbuf.vfs_fstype, MNTTYPE_UFS) != 0 ||
1727c478bd9Sstevel@tonic-gate 			    (vfsbuf.vfs_mntopts == 0) ||
1737c478bd9Sstevel@tonic-gate 			    hasvfsopt(&vfsbuf, MNTOPT_RO) ||
1747c478bd9Sstevel@tonic-gate 			    (!hasvfsopt(&vfsbuf, MNTOPT_RQ) &&
1757c478bd9Sstevel@tonic-gate 			    !hasvfsopt(&vfsbuf, MNTOPT_QUOTA)))
1767c478bd9Sstevel@tonic-gate 				continue;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			*listp = malloc(strlen(vfsbuf.vfs_special) + 1);
1797c478bd9Sstevel@tonic-gate 			(void) strcpy(*listp, vfsbuf.vfs_special);
1807c478bd9Sstevel@tonic-gate 			listp++;
1817c478bd9Sstevel@tonic-gate 			listcnt++;
1827c478bd9Sstevel@tonic-gate 			/* grow listbuf if needed */
1837c478bd9Sstevel@tonic-gate 			if (listcnt >= listmax) {
1847c478bd9Sstevel@tonic-gate 				listmax += CHUNK;
1857c478bd9Sstevel@tonic-gate 				listbuf = realloc(listbuf,
1867c478bd9Sstevel@tonic-gate 					sizeof (char *) * listmax);
1877c478bd9Sstevel@tonic-gate 				if (listbuf == NULL) {
1887c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
1897c478bd9Sstevel@tonic-gate 						"Can't grow listbuf.\n");
1907c478bd9Sstevel@tonic-gate 					exit(31+1);
1917c478bd9Sstevel@tonic-gate 				}
1927c478bd9Sstevel@tonic-gate 				listp = &listbuf[listcnt];
1937c478bd9Sstevel@tonic-gate 			}
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		(void) fclose(vfstab);
1967c478bd9Sstevel@tonic-gate 		*listp = (char *)0;
1977c478bd9Sstevel@tonic-gate 		listp = listbuf;
1987c478bd9Sstevel@tonic-gate 	} else {
1997c478bd9Sstevel@tonic-gate 		listp = &argv[optind];
2007c478bd9Sstevel@tonic-gate 		listcnt = argc - optind;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	if ((mtab = fopen(MNTTAB, "r")) == NULL) {
2037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't open ");
2047c478bd9Sstevel@tonic-gate 		perror(MNTTAB);
2057c478bd9Sstevel@tonic-gate 		exit(31+8);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	while (getmntent(mtab, &mntp) == 0) {
2087c478bd9Sstevel@tonic-gate 		if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) == 0 &&
2097c478bd9Sstevel@tonic-gate 		    !hasmntopt(&mntp, MNTOPT_RO) &&
2107c478bd9Sstevel@tonic-gate 		    (oneof(mntp.mnt_special, listp, listcnt) ||
2117c478bd9Sstevel@tonic-gate 		    oneof(mntp.mnt_mountp, listp, listcnt))) {
2127c478bd9Sstevel@tonic-gate 			(void) snprintf(quotafile, sizeof (quotafile), "%s/%s",
2137c478bd9Sstevel@tonic-gate 				mntp.mnt_mountp, QFNAME);
2147c478bd9Sstevel@tonic-gate 			errs += repquota(mntp.mnt_special,
2157c478bd9Sstevel@tonic-gate 				mntp.mnt_mountp, quotafile);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	(void) fclose(mtab);
2197c478bd9Sstevel@tonic-gate 	while (listcnt--) {
2207c478bd9Sstevel@tonic-gate 		if (*listp)
2217c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "Cannot report on %s\n", *listp);
2227c478bd9Sstevel@tonic-gate 		listp++;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	if (errs > 0)
2257c478bd9Sstevel@tonic-gate 		exit(31+1);
2267c478bd9Sstevel@tonic-gate 	return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static int
repquota(char * fsdev,char * fsfile,char * qffile)2307c478bd9Sstevel@tonic-gate repquota(char *fsdev, char *fsfile, char *qffile)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	FILE *qf;
2337c478bd9Sstevel@tonic-gate 	uid_t uid;
2347c478bd9Sstevel@tonic-gate 	struct dqblk dqbuf;
2357c478bd9Sstevel@tonic-gate 	struct stat64 statb;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (vflag || aflag)
2387c478bd9Sstevel@tonic-gate 		(void) printf("%s (%s):\n", fsdev, fsfile);
2397c478bd9Sstevel@tonic-gate 	qf = fopen64(qffile, "r");
2407c478bd9Sstevel@tonic-gate 	if (qf == NULL) {
2417c478bd9Sstevel@tonic-gate 		perror(qffile);
2427c478bd9Sstevel@tonic-gate 		return (1);
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	if (fstat64(fileno(qf), &statb) < 0) {
2457c478bd9Sstevel@tonic-gate 		perror(qffile);
2467c478bd9Sstevel@tonic-gate 		(void) fclose(qf);
2477c478bd9Sstevel@tonic-gate 		return (1);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Flush the file system. On logging file systems, this makes
2517c478bd9Sstevel@tonic-gate 	 * sure that the quota information (as metadata) gets rolled
2527c478bd9Sstevel@tonic-gate 	 * forward.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	if (ioctl(fileno(qf), _FIOFFS, NULL) == -1) {
2557c478bd9Sstevel@tonic-gate 		perror(qffile);
2567c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: cannot flush file system.\n",
2577c478bd9Sstevel@tonic-gate 				qffile);
2587c478bd9Sstevel@tonic-gate 		(void) fclose(qf);
2597c478bd9Sstevel@tonic-gate 		return (1);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	header();
2627c478bd9Sstevel@tonic-gate 	for (uid = 0; uid <= MAXUID && uid >= 0; uid++) {
2637c478bd9Sstevel@tonic-gate 		(void) fread(&dqbuf, sizeof (struct dqblk), 1, qf);
2647c478bd9Sstevel@tonic-gate 		if (feof(qf))
2657c478bd9Sstevel@tonic-gate 			break;
2667c478bd9Sstevel@tonic-gate 		if (!vflag &&
2677c478bd9Sstevel@tonic-gate 		    dqbuf.dqb_curfiles == 0 && dqbuf.dqb_curblocks == 0)
2687c478bd9Sstevel@tonic-gate 			continue;
2697c478bd9Sstevel@tonic-gate 		prquota(uid, &dqbuf);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 	(void) fclose(qf);
2727c478bd9Sstevel@tonic-gate 	return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static void
header(void)2767c478bd9Sstevel@tonic-gate header(void)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	(void) printf("                      Block limits"
2797c478bd9Sstevel@tonic-gate 		"                      File limits\n");
2807c478bd9Sstevel@tonic-gate 	(void) printf("User           used   soft   hard    timeleft"
2817c478bd9Sstevel@tonic-gate 		"    used   soft   hard    timeleft\n");
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate static void
prquota(uid_t uid,struct dqblk * dqp)2857c478bd9Sstevel@tonic-gate prquota(uid_t uid, struct dqblk *dqp)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	struct timeval tv;
288d1a180b0Smaheshvs 	struct username *up;
2897c478bd9Sstevel@tonic-gate 	char ftimeleft[80], btimeleft[80];
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (dqp->dqb_bsoftlimit == 0 && dqp->dqb_bhardlimit == 0 &&
2927c478bd9Sstevel@tonic-gate 	    dqp->dqb_fsoftlimit == 0 && dqp->dqb_fhardlimit == 0)
2937c478bd9Sstevel@tonic-gate 		return;
2947c478bd9Sstevel@tonic-gate 	(void) time(&(tv.tv_sec));
2957c478bd9Sstevel@tonic-gate 	tv.tv_usec = 0;
2967c478bd9Sstevel@tonic-gate 	up = lookup(uid);
2977c478bd9Sstevel@tonic-gate 	if (up)
2987c478bd9Sstevel@tonic-gate 		(void) printf("%-10s", up->u_name);
2997c478bd9Sstevel@tonic-gate 	else
3007c478bd9Sstevel@tonic-gate 		(void) printf("#%-9ld", uid);
3017c478bd9Sstevel@tonic-gate 	if (dqp->dqb_bsoftlimit &&
3027c478bd9Sstevel@tonic-gate 	    dqp->dqb_curblocks >= dqp->dqb_bsoftlimit) {
3037c478bd9Sstevel@tonic-gate 		if (dqp->dqb_btimelimit == 0)
3047c478bd9Sstevel@tonic-gate 			(void) strcpy(btimeleft, "NOT STARTED");
3057c478bd9Sstevel@tonic-gate 		else if (dqp->dqb_btimelimit > tv.tv_sec)
3067c478bd9Sstevel@tonic-gate 			fmttime(btimeleft,
3077c478bd9Sstevel@tonic-gate 			    (long)(dqp->dqb_btimelimit - tv.tv_sec));
3087c478bd9Sstevel@tonic-gate 		else
3097c478bd9Sstevel@tonic-gate 			(void) strcpy(btimeleft, "EXPIRED");
3107c478bd9Sstevel@tonic-gate 	} else
3117c478bd9Sstevel@tonic-gate 		btimeleft[0] = '\0';
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (dqp->dqb_fsoftlimit && dqp->dqb_curfiles >= dqp->dqb_fsoftlimit) {
3147c478bd9Sstevel@tonic-gate 		if (dqp->dqb_ftimelimit == 0)
3157c478bd9Sstevel@tonic-gate 			(void) strcpy(ftimeleft, "NOT STARTED");
3167c478bd9Sstevel@tonic-gate 		else if (dqp->dqb_ftimelimit > tv.tv_sec)
3177c478bd9Sstevel@tonic-gate 			fmttime(ftimeleft,
3187c478bd9Sstevel@tonic-gate 			    (long)(dqp->dqb_ftimelimit - tv.tv_sec));
3197c478bd9Sstevel@tonic-gate 		else
3207c478bd9Sstevel@tonic-gate 			(void) strcpy(ftimeleft, "EXPIRED");
3217c478bd9Sstevel@tonic-gate 	} else
3227c478bd9Sstevel@tonic-gate 		ftimeleft[0] = '\0';
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	(void) printf("%c%c %6lu %6lu %6lu %11s %7lu %6lu %6lu %11s\n",
3257c478bd9Sstevel@tonic-gate 		(dqp->dqb_bsoftlimit &&
3267c478bd9Sstevel@tonic-gate 		    dqp->dqb_curblocks >= dqp->dqb_bsoftlimit) ? '+' : '-',
3277c478bd9Sstevel@tonic-gate 		(dqp->dqb_fsoftlimit &&
3287c478bd9Sstevel@tonic-gate 		    dqp->dqb_curfiles >= dqp->dqb_fsoftlimit) ? '+' : '-',
3297c478bd9Sstevel@tonic-gate 		dbtok(dqp->dqb_curblocks),
3307c478bd9Sstevel@tonic-gate 		dbtok(dqp->dqb_bsoftlimit),
3317c478bd9Sstevel@tonic-gate 		dbtok(dqp->dqb_bhardlimit),
3327c478bd9Sstevel@tonic-gate 		btimeleft,
3337c478bd9Sstevel@tonic-gate 		dqp->dqb_curfiles,
3347c478bd9Sstevel@tonic-gate 		dqp->dqb_fsoftlimit,
3357c478bd9Sstevel@tonic-gate 		dqp->dqb_fhardlimit,
3367c478bd9Sstevel@tonic-gate 		ftimeleft);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static void
fmttime(char * buf,long time)3407c478bd9Sstevel@tonic-gate fmttime(char *buf, long time)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int i;
3437c478bd9Sstevel@tonic-gate 	static struct {
3447c478bd9Sstevel@tonic-gate 		int c_secs;		/* conversion units in secs */
3457c478bd9Sstevel@tonic-gate 		char *c_str;		/* unit string */
3467c478bd9Sstevel@tonic-gate 	} cunits [] = {
3477c478bd9Sstevel@tonic-gate 		{60*60*24*28, "months"},
3487c478bd9Sstevel@tonic-gate 		{60*60*24*7, "weeks"},
3497c478bd9Sstevel@tonic-gate 		{60*60*24, "days"},
3507c478bd9Sstevel@tonic-gate 		{60*60, "hours"},
3517c478bd9Sstevel@tonic-gate 		{60, "mins"},
3527c478bd9Sstevel@tonic-gate 		{1, "secs"}
3537c478bd9Sstevel@tonic-gate 	};
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (time <= 0) {
3567c478bd9Sstevel@tonic-gate 		(void) strcpy(buf, "EXPIRED");
3577c478bd9Sstevel@tonic-gate 		return;
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (cunits) / sizeof (cunits[0]); i++) {
3607c478bd9Sstevel@tonic-gate 		if (time >= cunits[i].c_secs)
3617c478bd9Sstevel@tonic-gate 			break;
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%.1f %s",
3647c478bd9Sstevel@tonic-gate 	    (double)time / cunits[i].c_secs, cunits[i].c_str);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int
oneof(char * target,char ** olistp,int on)3687c478bd9Sstevel@tonic-gate oneof(char *target, char **olistp, int on)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	char **listp = olistp;
3717c478bd9Sstevel@tonic-gate 	int n = on;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	while (n--) {
3747c478bd9Sstevel@tonic-gate 		if (*listp && strcmp(target, *listp) == 0) {
3757c478bd9Sstevel@tonic-gate 			*listp = (char *)0;
3767c478bd9Sstevel@tonic-gate 			return (1);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 		listp++;
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 	return (0);
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static struct username *
lookup(uid_t uid)3847c478bd9Sstevel@tonic-gate lookup(uid_t uid)
3857c478bd9Sstevel@tonic-gate {
386d1a180b0Smaheshvs 	struct passwd *pwp;
387d1a180b0Smaheshvs 	struct username *up;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	for (up = uhead[uid % UHASH]; up != 0; up = up->u_next)
3907c478bd9Sstevel@tonic-gate 		if (up->u_uid == uid)
3917c478bd9Sstevel@tonic-gate 			return (up);
3927c478bd9Sstevel@tonic-gate 	if ((pwp = getpwuid((uid_t)uid)) == NULL)
3937c478bd9Sstevel@tonic-gate 		return ((struct username *)0);
3947c478bd9Sstevel@tonic-gate 	up = adduid(pwp->pw_uid);
3957c478bd9Sstevel@tonic-gate 	(void) strncpy(up->u_name, pwp->pw_name, sizeof (up->u_name));
3967c478bd9Sstevel@tonic-gate 	return (up);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * adduid() should *ONLY* be called from lookup in order
4017c478bd9Sstevel@tonic-gate  * to avoid duplicate entries.
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate static struct username *
adduid(uid_t uid)4047c478bd9Sstevel@tonic-gate adduid(uid_t uid)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	struct username *up, **uhp;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	up = calloc(1, sizeof (struct username));
4097c478bd9Sstevel@tonic-gate 	if (up == 0) {
4107c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
4117c478bd9Sstevel@tonic-gate 			"out of memory for username structures\n");
4127c478bd9Sstevel@tonic-gate 		exit(31+1);
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 	uhp = &uhead[uid % UHASH];
4157c478bd9Sstevel@tonic-gate 	up->u_next = *uhp;
4167c478bd9Sstevel@tonic-gate 	*uhp = up;
4177c478bd9Sstevel@tonic-gate 	up->u_uid = uid;
4187c478bd9Sstevel@tonic-gate 	return (up);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate static void
usage(void)4227c478bd9Sstevel@tonic-gate usage(void)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "ufs usage:\n");
4257c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\trepquota [-v] -a \n");
4267c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\trepquota [-v] filesys ...\n");
4277c478bd9Sstevel@tonic-gate 	exit(31+1);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate static int
quotactl(int cmd,char * special,uid_t uid,caddr_t addr)4317c478bd9Sstevel@tonic-gate quotactl(int cmd, char *special, uid_t uid, caddr_t addr)
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	int		fd;
4347c478bd9Sstevel@tonic-gate 	int		status;
4357c478bd9Sstevel@tonic-gate 	struct quotctl	quota;
4367c478bd9Sstevel@tonic-gate 	char		qfile[MAXPATHLEN];
4377c478bd9Sstevel@tonic-gate 	FILE		*fstab;
4387c478bd9Sstevel@tonic-gate 	struct mnttab	mntp;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if ((special == NULL) && (cmd == Q_ALLSYNC)) {
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * Find the mount point of the special device.   This is
4447c478bd9Sstevel@tonic-gate 	 * because the ioctl that implements the quotactl call has
4457c478bd9Sstevel@tonic-gate 	 * to go to a real file, and not to the block device.
4467c478bd9Sstevel@tonic-gate 	 */
4477c478bd9Sstevel@tonic-gate 		if ((fstab = fopen(MNTTAB, "r")) == NULL) {
4487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s: ", MNTTAB);
4497c478bd9Sstevel@tonic-gate 			perror("open");
4507c478bd9Sstevel@tonic-gate 			exit(31+1);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 		fd = -1;
453*8509e9caSToomas Soome 		while ((status = getmntent(fstab, &mntp)) == 0) {
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 			if (strcmp(mntp.mnt_fstype, MNTTYPE_UFS) != 0 ||
4567c478bd9Sstevel@tonic-gate 			    hasmntopt(&mntp, MNTOPT_RO))
4577c478bd9Sstevel@tonic-gate 				continue;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 			if ((strlcpy(qfile, mntp.mnt_mountp,
4607c478bd9Sstevel@tonic-gate 				sizeof (qfile)) >= sizeof (qfile)) ||
4617c478bd9Sstevel@tonic-gate 			    (strlcat(qfile, "/" QFNAME, sizeof (qfile)) >=
4627c478bd9Sstevel@tonic-gate 				sizeof (qfile))) {
4637c478bd9Sstevel@tonic-gate 				continue;
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 			/* If we find *ANY* valid "quotas" file, use it */
4677c478bd9Sstevel@tonic-gate 			if ((fd = open64(qfile, O_RDONLY)) >= 0)
4687c478bd9Sstevel@tonic-gate 				break;
4697c478bd9Sstevel@tonic-gate 		}
4707c478bd9Sstevel@tonic-gate 		(void) fclose(fstab);
4717c478bd9Sstevel@tonic-gate 		if (fd == -1) {
4727c478bd9Sstevel@tonic-gate 			errno = ENOENT;
4737c478bd9Sstevel@tonic-gate 			(void) printf("quotactl: no quotas file "
4747c478bd9Sstevel@tonic-gate 				"on any mounted file system\n");
4757c478bd9Sstevel@tonic-gate 			return (-1);
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	quota.op = cmd;
4797c478bd9Sstevel@tonic-gate 	quota.uid = uid;
4807c478bd9Sstevel@tonic-gate 	quota.addr = addr;
4817c478bd9Sstevel@tonic-gate 	status = ioctl(fd, Q_QUOTACTL, &quota);
4827c478bd9Sstevel@tonic-gate 	(void) close(fd);
4837c478bd9Sstevel@tonic-gate 	return (status);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate static char *
hasvfsopt(struct vfstab * vfs,char * opt)4877c478bd9Sstevel@tonic-gate hasvfsopt(struct vfstab *vfs, char *opt)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	char *f, *opts;
4907c478bd9Sstevel@tonic-gate 	static char *tmpopts;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (tmpopts == 0) {
4937c478bd9Sstevel@tonic-gate 		tmpopts = calloc(256, sizeof (char));
4947c478bd9Sstevel@tonic-gate 		if (tmpopts == 0)
4957c478bd9Sstevel@tonic-gate 			return (0);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 	(void) strcpy(tmpopts, vfs->vfs_mntopts);
4987c478bd9Sstevel@tonic-gate 	opts = tmpopts;
4997c478bd9Sstevel@tonic-gate 	f = mntopt(&opts);
5007c478bd9Sstevel@tonic-gate 	for (; *f; f = mntopt(&opts)) {
5017c478bd9Sstevel@tonic-gate 		if (strncmp(opt, f, strlen(opt)) == 0)
5027c478bd9Sstevel@tonic-gate 			return (f - tmpopts + vfs->vfs_mntopts);
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	return (NULL);
5057c478bd9Sstevel@tonic-gate }
506