xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/sysconf/sysconf01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2002
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * http://www.opengroup.org/onlinepubs/009695399/functions/sysconf.html
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * NAME :
24*49cdfc7eSAndroid Build Coastguard Worker  * sysconf01 :  test for sysconf( get configurable system variables) sys call.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * USAGE :
27*49cdfc7eSAndroid Build Coastguard Worker  *      sysconf01
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #define INVAL_FLAG	-1
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker /** LTP Port **/
39*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "sysconf01";
42*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 56;
43*49cdfc7eSAndroid Build Coastguard Worker 
_test_sysconf(long name,const char * strname)44*49cdfc7eSAndroid Build Coastguard Worker static void _test_sysconf(long name, const char *strname)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	long retval;
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker 	/* make sure we reset this as sysconf() will not */
49*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
50*49cdfc7eSAndroid Build Coastguard Worker 	retval = sysconf(name);
51*49cdfc7eSAndroid Build Coastguard Worker 	if (retval == -1) {
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 		/*
54*49cdfc7eSAndroid Build Coastguard Worker 		 * The manpage for sysconf(2) specifically states that:
55*49cdfc7eSAndroid Build Coastguard Worker 		 * 1. If -1 is returned and errno is EINVAL, then the resource
56*49cdfc7eSAndroid Build Coastguard Worker 		 * name doesn't exist.
57*49cdfc7eSAndroid Build Coastguard Worker 		 * 2. If errno remains 0, then the limit isn't implemented.
58*49cdfc7eSAndroid Build Coastguard Worker 		 * 3. Else, something weird happened with the syscall.
59*49cdfc7eSAndroid Build Coastguard Worker 		 */
60*49cdfc7eSAndroid Build Coastguard Worker 		switch (errno) {
61*49cdfc7eSAndroid Build Coastguard Worker 		case EINVAL:
62*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TCONF, "Resource doesn't exist: %s", strname);
63*49cdfc7eSAndroid Build Coastguard Worker 			break;
64*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
65*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TCONF, "Not supported sysconf resource: %s",
66*49cdfc7eSAndroid Build Coastguard Worker 				 strname);
67*49cdfc7eSAndroid Build Coastguard Worker 			break;
68*49cdfc7eSAndroid Build Coastguard Worker 		default:
69*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TERRNO, "Unexpected errno value for "
70*49cdfc7eSAndroid Build Coastguard Worker 				 "%s", strname);
71*49cdfc7eSAndroid Build Coastguard Worker 			break;
72*49cdfc7eSAndroid Build Coastguard Worker 		}
73*49cdfc7eSAndroid Build Coastguard Worker 	} else
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "%s = %li", strname, retval);
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker #define test_sysconf(name) _test_sysconf(name, #name)
79*49cdfc7eSAndroid Build Coastguard Worker 
main(void)80*49cdfc7eSAndroid Build Coastguard Worker int main(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	/* 1 - 5 */
83*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_CLK_TCK);
84*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_ARG_MAX);
85*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_CHILD_MAX);
86*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_OPEN_MAX);
87*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_JOB_CONTROL);
88*49cdfc7eSAndroid Build Coastguard Worker 	/* 6 - 10 */
89*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_SAVED_IDS);
90*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_VERSION);
91*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_PASS_MAX);
92*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_LOGIN_NAME_MAX);
93*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_VERSION);
94*49cdfc7eSAndroid Build Coastguard Worker 	/* 11 - 15 */
95*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_TZNAME_MAX);
96*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_STREAM_MAX);
97*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_CRYPT);
98*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_ENH_I18N);
99*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_SHM);
100*49cdfc7eSAndroid Build Coastguard Worker 	/* 16 - 20 */
101*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_XCU_VERSION);
102*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_ATEXIT_MAX);
103*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_C_BIND);
104*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_C_DEV);
105*49cdfc7eSAndroid Build Coastguard Worker #ifdef _SC_2_C_VERSION
106*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_C_VERSION);
107*49cdfc7eSAndroid Build Coastguard Worker #else
108*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TCONF, "_SC_2_C_VERSION not defined");
109*49cdfc7eSAndroid Build Coastguard Worker #endif
110*49cdfc7eSAndroid Build Coastguard Worker 	/* 21 - 25 */
111*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_CHAR_TERM);
112*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_FORT_DEV);
113*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_FORT_RUN);
114*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_LOCALEDEF);
115*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_SW_DEV);
116*49cdfc7eSAndroid Build Coastguard Worker 	/* 26 - 30 */
117*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_UPE);
118*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_2_VERSION);
119*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_BC_BASE_MAX);
120*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_BC_DIM_MAX);
121*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_BC_SCALE_MAX);
122*49cdfc7eSAndroid Build Coastguard Worker 	/* 31 - 35 */
123*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_BC_STRING_MAX);
124*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_COLL_WEIGHTS_MAX);
125*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_EXPR_NEST_MAX);
126*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_LINE_MAX);
127*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_RE_DUP_MAX);
128*49cdfc7eSAndroid Build Coastguard Worker 	/* 36 - 40 */
129*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_UNIX);
130*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_PAGESIZE);
131*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_PHYS_PAGES);
132*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_AVPHYS_PAGES);
133*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_AIO_MAX);
134*49cdfc7eSAndroid Build Coastguard Worker 	/* 41 - 45 */
135*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_AIO_PRIO_DELTA_MAX);
136*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_SEMAPHORES);
137*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_SEM_NSEMS_MAX);
138*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_SEM_VALUE_MAX);
139*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_MEMORY_PROTECTION);
140*49cdfc7eSAndroid Build Coastguard Worker 	/* 46 - 50 */
141*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_FSYNC);
142*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_MEMORY_PROTECTION);
143*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_TIMERS);
144*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_TIMER_MAX);
145*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_MAPPED_FILES);
146*49cdfc7eSAndroid Build Coastguard Worker 	/* 51 - 55 */
147*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_THREAD_PRIORITY_SCHEDULING);
148*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XOPEN_LEGACY);
149*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_MEMLOCK);
150*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XBS5_ILP32_OFF32);
151*49cdfc7eSAndroid Build Coastguard Worker 	test_sysconf(_SC_XBS5_ILP32_OFFBIG);
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	/* 56 */
154*49cdfc7eSAndroid Build Coastguard Worker 	{
155*49cdfc7eSAndroid Build Coastguard Worker 		int retval, actual;
156*49cdfc7eSAndroid Build Coastguard Worker 		errno = 0;
157*49cdfc7eSAndroid Build Coastguard Worker 		retval = sysconf(INVAL_FLAG);
158*49cdfc7eSAndroid Build Coastguard Worker 		actual = errno;
159*49cdfc7eSAndroid Build Coastguard Worker 		if (retval != -1) {
160*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
161*49cdfc7eSAndroid Build Coastguard Worker 				 "sysconf succeeded for invalid flag (%i), "
162*49cdfc7eSAndroid Build Coastguard Worker 				 " retval=%d errno=%d: %s",
163*49cdfc7eSAndroid Build Coastguard Worker 				 INVAL_FLAG, retval, actual, strerror(actual));
164*49cdfc7eSAndroid Build Coastguard Worker 		} else if (actual != EINVAL) {
165*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL,
166*49cdfc7eSAndroid Build Coastguard Worker 				 "sysconf correctly failed, but expected "
167*49cdfc7eSAndroid Build Coastguard Worker 				 "errno (%i) != actual (%i)", EINVAL, actual);
168*49cdfc7eSAndroid Build Coastguard Worker 		} else
169*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "The invalid sysconf key was trapped "
170*49cdfc7eSAndroid Build Coastguard Worker 				 "appropriately");
171*49cdfc7eSAndroid Build Coastguard Worker 	}
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
174*49cdfc7eSAndroid Build Coastguard Worker }
175