summaryrefslogtreecommitdiff
path: root/src/feature.c
blob: a7eb75950b4ba2e70e3cefc32f49db08d63f9f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
 * All Rights Reserved.
 */

/*
 * Test for filesystem features on given mount point or device
 *   -c  test for 32bit chown support (via libc)
 *   -t  test for working rlimit/ftruncate64 (via libc)
 *   -q  test for quota support (kernel compile option)
 *   -u  test for user quota enforcement support (mount option)
 *   -p  test for project quota enforcement support (mount option)
 *   -g  test for group quota enforcement support (mount option)
 *   -U  test for user quota accounting support (mount option)
 *   -G  test for group quota accounting support (mount option)
 *   -P  test for project quota accounting support (mount option)
 * Return code: 0 is true, anything else is error/not supported
 *
 * Test for machine features
 *   -A  test whether AIO syscalls are available
 *   -o  report a number of online cpus
 *   -s  report pagesize
 *   -w  report bits per long
 */

#include "global.h"

#include <sys/quota.h>
#include <sys/resource.h>
#include <signal.h>
#include <unistd.h>

#ifdef HAVE_XFS_XQM_H
#include <xfs/xqm.h>
#endif

#ifdef HAVE_LIBAIO_H
#include <libaio.h>
#endif

#ifndef USRQUOTA
#define USRQUOTA  0
#endif

#ifndef GRPQUOTA
#define GRPQUOTA  1
#endif

#ifndef PRJQUOTA
#define PRJQUOTA  2
#endif

int verbose = 0;

void
usage(void)
{
	fprintf(stderr, "Usage: feature [-v] -<q|u|g|p|U|G|P> <filesystem>\n");
	fprintf(stderr, "       feature [-v] -c <file>\n");
	fprintf(stderr, "       feature [-v] -t <file>\n");
	fprintf(stderr, "       feature -A | -o | -s | -w\n");
	exit(1);
}

int check_big_ID(char *filename)
{
	struct stat64	sbuf;

	memset(&sbuf, 0, sizeof(struct stat64));
	if (lstat64(filename, &sbuf) < 0) {
		fprintf(stderr, "lstat64 failed on ");
		perror(filename);
		return(1);
	}

	/* 98789 is greater than 2^16 (65536) */
	if ((uint32_t)sbuf.st_uid == 98789 || (uint32_t)sbuf.st_gid == 98789)
		return(0);
	if (verbose)
		fprintf(stderr, "lstat64 on %s gave uid=%d, gid=%d\n",
			filename, (int)sbuf.st_uid, (int)sbuf.st_gid);
	return(1);
}

int
haschown32(char *filename)
{
	if (check_big_ID(filename) == 0)
		return(0);

	if (chown(filename, 98789, 98789) < 0) {
		fprintf(stderr, "chown failed on ");
		perror(filename);
		return(1);
	}

	if (check_big_ID(filename) == 0)
		return(0);
	return (1);
}

int
hastruncate64(char *filename)
{
	struct rlimit64 rlimit64;
	off64_t bigoff = 4294967307LL;	/* > 2^32 */
	struct stat64 bigst;
	int fd;

	getrlimit64(RLIMIT_FSIZE, &rlimit64);
	rlimit64.rlim_cur = RLIM64_INFINITY;
	setrlimit64(RLIMIT_FSIZE, &rlimit64);

	signal(SIGXFSZ, SIG_IGN);

	if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
		fprintf(stderr, "open failed on ");
		perror(filename);
		return(1);
	}

	if (ftruncate64(fd, bigoff) < 0)
		return(1);

	if (fstat64(fd, &bigst) < 0) {
		fprintf(stderr, "fstat64 failed on ");
		perror(filename);
		return(1);
	}

	if (verbose)
		fprintf(stderr, "fstat64 on %s gave sz=%lld (truncsz=%lld)\n",
			filename, (long long)bigst.st_size, (long long)bigoff);

	if (bigst.st_size != bigoff)
		return(1);
	return(0);
}

int
hasxfsquota(int type, int q, char *device)
{
	fs_quota_stat_t	qstat;
	int		qcmd;

	memset(&qstat, 0, sizeof(fs_quota_stat_t));

#ifdef QCMD
	if (q == 0) {
		if (access("/proc/fs/xfs/xqm", F_OK) < 0) {
			if (verbose) {
				fprintf(stderr, "can't access /proc/fs/xfs/xqm\n");
			}
			return 1;
		}
		return 0;
	}
	qcmd = QCMD(Q_XGETQSTAT, type);
#else
	if (q == 0) {
		if (quotactl(Q_SYNC, device, 0, (caddr_t)&qstat) == ENOPKG) {
			if (verbose) {
				fprintf(stderr, "Q_SYNC not supported\n");
			}
			return 1;
		}
		return 0;
	}
	qcmd = Q_GETQSTAT;
#endif

	if (quotactl(qcmd, device, 0, (caddr_t)&qstat) < 0) {
		if (verbose)
			perror("quotactl");
		return (1);
	}
	else if (q == XFS_QUOTA_UDQ_ENFD && qstat.qs_flags & XFS_QUOTA_UDQ_ENFD)
		return (0);
	else if (q == XFS_QUOTA_GDQ_ENFD && qstat.qs_flags & XFS_QUOTA_GDQ_ENFD)
		return (0);
	else if (q == XFS_QUOTA_PDQ_ENFD && qstat.qs_flags & XFS_QUOTA_PDQ_ENFD)
		return (0);
	else if (q == XFS_QUOTA_UDQ_ACCT && qstat.qs_flags & XFS_QUOTA_UDQ_ACCT)
		return (0);
	else if (q == XFS_QUOTA_GDQ_ACCT && qstat.qs_flags & XFS_QUOTA_GDQ_ACCT)
		return (0);
	else if (q == XFS_QUOTA_PDQ_ACCT && qstat.qs_flags & XFS_QUOTA_PDQ_ACCT)
		return (0);
	if (verbose)
		fprintf(stderr, "quota type (%d) not available\n", q);
	return (1);
}

static int
check_aio_support(void)
{
#ifdef HAVE_LIBAIO_H
	struct io_context *ctx = NULL;
	int err;

	err = io_setup(1, &ctx);
	if (err == 0)
		return 0;

	if (err == -ENOSYS) /* CONFIG_AIO=n */
		return 1;

	fprintf(stderr, "unexpected error from io_setup(): %s\n",
		strerror(-err));
	return 2;
#else
	/* libaio was unavailable at build time; assume AIO is unsupported */
	return 1;
#endif
}


int
main(int argc, char **argv)
{
	int	c;
	int	Aflag = 0;
	int	cflag = 0;
	int	tflag = 0;
	int	gflag = 0;
	int	Gflag = 0;
	int	pflag = 0;
	int	Pflag = 0;
	int	qflag = 0;
	int	sflag = 0;
	int	uflag = 0;
	int	Uflag = 0;
	int	wflag = 0;
	int	oflag = 0;
	char	*fs = NULL;

	while ((c = getopt(argc, argv, "ActgGopPqsuUvw")) != EOF) {
		switch (c) {
		case 'A':
			Aflag++;
			break;
		case 'c':
			cflag++;
			break;
		case 't':
			tflag++;
			break;
		case 'g':
			gflag++;
			break;
		case 'G':
			Gflag++;
			break;
		case 'o':
			oflag++;
			break;
		case 'p':
			pflag++;
			break;
		case 'P':
			Pflag++;
			break;
		case 'q':
			qflag++;
			break;
		case 's':
			sflag++;
			break;
		case 'u':
			uflag++;
			break;
		case 'U':
			Uflag++;
			break;
		case 'w':
			wflag++;
			break;
		case 'v':
			verbose++;
			break;
		default:
			usage();
		}
	}

	/* filesystem features */
	if (cflag|tflag|uflag|gflag|pflag|qflag|Uflag|Gflag|Pflag) {
		if (optind != argc-1)	/* need a device */
			usage();
		fs = argv[argc-1];
	} else if (Aflag || wflag || sflag || oflag) {
		if (optind != argc)
			usage();
	} else 
		usage();

	if (cflag)
		return(haschown32(fs));
	if (tflag)
		return(hastruncate64(fs));
	if (qflag)
		return(hasxfsquota(0, 0, fs));
	if (gflag)
		return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ENFD, fs));
	if (pflag)
		return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ENFD, fs));
	if (uflag)
		return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ENFD, fs));
	if (Gflag)
		return(hasxfsquota(GRPQUOTA, XFS_QUOTA_GDQ_ACCT, fs));
	if (Pflag)
		return(hasxfsquota(PRJQUOTA, XFS_QUOTA_PDQ_ACCT, fs));
	if (Uflag)
		return(hasxfsquota(USRQUOTA, XFS_QUOTA_UDQ_ACCT, fs));

	if (Aflag)
		return(check_aio_support());

	if (sflag) {
		printf("%d\n", getpagesize());
		exit(0);
	}
	if (wflag) {
#ifdef BITS_PER_LONG
		printf("%d\n", BITS_PER_LONG);
#else
#ifdef NBBY
		/* This can change under IRIX depending on whether we compile
		 * with -n32/-32 or -64
		 */
		printf("%d\n", (int)(NBBY * sizeof(long)));
#else
bozo!
#endif
#endif
		exit(0);
	}
	if (oflag) {
		long ncpus = -1;

#if defined(_SC_NPROCESSORS_ONLN)
		ncpus = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(_SC_NPROC_ONLN)
		ncpus = sysconf(_SC_NPROC_ONLN);
#endif
		if (ncpus == -1)
			ncpus = 1;

		printf("%ld\n", ncpus);

		exit(0);
	}

	fprintf(stderr, "feature: dunno what you're after.\n");
	return(1);
}