Undoes mod: xfs-cmds:slinx:120772a
[xfstests-dev.git] / src / feature.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 /*
34  * Test for filesystem features on given mount point or device
35  *   -c  test for 32bit chown support (via libc)
36  *   -t  test for working rlimit/ftruncate64 (via libc)
37  *   -q  test for quota support (kernel compile option)
38  *   -u  test for user quota enforcement support (mount option)
39  *   -g  test for group quota enforcement support (mount option)
40  *   -U  test for user quota accounting support (mount option)
41  *   -G  test for group quota accounting support (mount option)
42  * Return code: 0 is true, anything else is error/not supported
43  */
44
45 #include <libxfs.h>
46 #include <sys/quota.h>
47 #include <sys/resource.h>
48 #include <signal.h>
49 #include <xqm.h>
50
51 int verbose = 0;
52
53 void
54 usage(void)
55 {
56         fprintf(stderr, "Usage: feature [-v] -<q|u|g|U|G> <filesystem>\n");
57         fprintf(stderr, "       feature [-v] -c <file>\n");
58         fprintf(stderr, "       feature [-v] -t <file>\n");
59         exit(1);
60 }
61
62 int check_big_ID(char *filename)
63 {
64         struct stat64   sbuf;
65
66         memset(&sbuf, 0, sizeof(struct stat64));
67         if (lstat64(filename, &sbuf) < 0) {
68                 fprintf(stderr, "lstat64 failed on ");
69                 perror(filename);
70                 return(1);
71         }
72
73         /* 98789 is greater than 2^16 (65536) */
74         if ((__u32)sbuf.st_uid == 98789 || (__u32)sbuf.st_gid == 98789)
75                 return(0);
76         if (verbose)
77                 fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
78                         filename, sbuf.st_uid, sbuf.st_gid);
79         return(1);
80 }
81
82 int
83 haschown32(char *filename)
84 {
85         if (check_big_ID(filename) == 0)
86                 return(0);
87
88         if (chown(filename, 98789, 98789) < 0) {
89                 fprintf(stderr, "chown failed on ");
90                 perror(filename);
91                 return(1);
92         }
93
94         if (check_big_ID(filename) == 0)
95                 return(0);
96         return (1);
97 }
98
99 int
100 hastruncate64(char *filename)
101 {
102         struct rlimit64 rlimit64;
103         off64_t bigoff = 4294967307;    /* > 2^32 */
104         struct stat64 bigst;
105         int fd;
106
107         getrlimit64(RLIMIT_FSIZE, &rlimit64);
108         rlimit64.rlim_cur = RLIM64_INFINITY;
109         setrlimit64(RLIMIT_FSIZE, &rlimit64);
110
111         signal(SIGXFSZ, SIG_IGN);
112
113         if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
114                 fprintf(stderr, "open failed on ");
115                 perror(filename);
116                 return(1);
117         }
118
119         if (ftruncate64(fd, bigoff) < 0)
120                 return(1);
121
122         if (fstat64(fd, &bigst) < 0) {
123                 fprintf(stderr, "fstat64 failed on ");
124                 perror(filename);
125                 return(1);
126         }
127
128         if (verbose)
129                 fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
130                         filename, bigst.st_size, bigoff);
131
132         if (bigst.st_size != bigoff)
133                 return(1);
134         return(0);
135 }
136
137 int
138 hasxfsquota(int type, int q, char *device)
139 {
140         fs_quota_stat_t qstat;
141         int             qcmd;
142
143         memset(&qstat, 0, sizeof(fs_quota_stat_t));
144         qcmd = QCMD(Q_XGETQSTAT, type);
145         if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
146                 if (verbose)
147                         perror("quotactl");
148                 return (1);
149         }
150         else if (q == 0)
151                 return (0);
152         else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
153                 return (0);
154         else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
155                 return (0);
156         else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
157                 return (0);
158         else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
159                 return (0);
160         if (verbose)
161                 fprintf(stderr, "quota type (%d) not available\n", q);
162         return (1);
163 }
164
165 int
166 main(int argc, char **argv)
167 {
168         int     c;
169         int     cflag = 0;
170         int     tflag = 0;
171         int     gflag = 0;
172         int     Gflag = 0;
173         int     qflag = 0;
174         int     uflag = 0;
175         int     Uflag = 0;
176         char    *fs;
177
178         while ((c = getopt(argc, argv, "ctgGquUv")) != EOF) {
179                 switch (c) {
180                 case 'c':
181                         cflag++;
182                         break;
183                 case 't':
184                         tflag++;
185                         break;
186                 case 'g':
187                         gflag++;
188                         break;
189                 case 'G':
190                         Gflag++;
191                         break;
192                 case 'q':
193                         qflag++;
194                         break;
195                 case 'u':
196                         uflag++;
197                         break;
198                 case 'U':
199                         Uflag++;
200                         break;
201                 case 'v':
202                         verbose++;
203                         break;
204                 default:
205                         usage();
206                 }
207         }
208
209         if (!cflag && !tflag && !uflag && !gflag && !qflag && !Uflag && !Gflag)
210                 usage();
211         if (optind != argc-1)
212                 usage();
213         fs = argv[argc-1];
214
215         if (cflag)
216                 return(haschown32(fs));
217         if (tflag)
218                 return(hastruncate64(fs));
219         if (qflag)
220                 return(hasxfsquota(0, 0, fs));
221         if (gflag)
222                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
223         if (uflag)
224                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
225         if (Gflag)
226                 return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
227         if (Uflag)
228                 return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));
229
230         fprintf(stderr, "feature: dunno what you're doing?\n");
231         return(1);
232 }