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