xref: /freebsd/lib/libc/gen/sysconf.c (revision 8e6b01171e30297084bb0b4457c4183c2746aacc)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Sean Eric Fagan of Cygnus Support.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)sysconf.c	8.2 (Berkeley) 3/20/94";
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include <sys/param.h>
42 #include <sys/sysctl.h>
43 #include <sys/resource.h>
44 
45 #include <errno.h>
46 #include <time.h>
47 #include <unistd.h>
48 
49 /*
50  * sysconf --
51  *	get configurable system variables.
52  *
53  * XXX
54  * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
55  * not change during the lifetime of the calling process.  This would seem
56  * to require that any change to system limits kill all running processes.
57  * A workaround might be to cache the values when they are first retrieved
58  * and then simply return the cached value on subsequent calls.  This is
59  * less useful than returning up-to-date values, however.
60  */
61 long
62 sysconf(name)
63 	int name;
64 {
65 	struct rlimit rl;
66 	size_t len;
67 	int mib[2], value;
68 
69 	len = sizeof(value);
70 
71 	switch (name) {
72 /* 1003.1 */
73 	case _SC_ARG_MAX:
74 		mib[0] = CTL_KERN;
75 		mib[1] = KERN_ARGMAX;
76 		break;
77 	case _SC_CHILD_MAX:
78 		return (getrlimit(RLIMIT_NPROC, &rl) ? -1 : rl.rlim_cur);
79 	case _SC_CLK_TCK:
80 		return (CLK_TCK);
81 	case _SC_JOB_CONTROL:
82 		mib[0] = CTL_KERN;
83 		mib[1] = KERN_JOB_CONTROL;
84 		goto yesno;
85 	case _SC_NGROUPS_MAX:
86 		mib[0] = CTL_KERN;
87 		mib[1] = KERN_NGROUPS;
88 		break;
89 	case _SC_OPEN_MAX:
90 		return (getrlimit(RLIMIT_NOFILE, &rl) ? -1 : rl.rlim_cur);
91 	case _SC_STREAM_MAX:
92 		mib[0] = CTL_USER;
93 		mib[1] = USER_STREAM_MAX;
94 		break;
95 	case _SC_TZNAME_MAX:
96 		mib[0] = CTL_USER;
97 		mib[1] = USER_TZNAME_MAX;
98 		break;
99 	case _SC_SAVED_IDS:
100 		mib[0] = CTL_KERN;
101 		mib[1] = KERN_SAVED_IDS;
102 		goto yesno;
103 	case _SC_VERSION:
104 		mib[0] = CTL_KERN;
105 		mib[1] = KERN_POSIX1;
106 		break;
107 
108 /* 1003.2 */
109 	case _SC_BC_BASE_MAX:
110 		mib[0] = CTL_USER;
111 		mib[1] = USER_BC_BASE_MAX;
112 		break;
113 	case _SC_BC_DIM_MAX:
114 		mib[0] = CTL_USER;
115 		mib[1] = USER_BC_DIM_MAX;
116 		break;
117 	case _SC_BC_SCALE_MAX:
118 		mib[0] = CTL_USER;
119 		mib[1] = USER_BC_SCALE_MAX;
120 		break;
121 	case _SC_BC_STRING_MAX:
122 		mib[0] = CTL_USER;
123 		mib[1] = USER_BC_STRING_MAX;
124 		break;
125 	case _SC_COLL_WEIGHTS_MAX:
126 		mib[0] = CTL_USER;
127 		mib[1] = USER_COLL_WEIGHTS_MAX;
128 		break;
129 	case _SC_EXPR_NEST_MAX:
130 		mib[0] = CTL_USER;
131 		mib[1] = USER_EXPR_NEST_MAX;
132 		break;
133 	case _SC_LINE_MAX:
134 		mib[0] = CTL_USER;
135 		mib[1] = USER_LINE_MAX;
136 		break;
137 	case _SC_RE_DUP_MAX:
138 		mib[0] = CTL_USER;
139 		mib[1] = USER_RE_DUP_MAX;
140 		break;
141 	case _SC_2_VERSION:
142 		mib[0] = CTL_USER;
143 		mib[1] = USER_POSIX2_VERSION;
144 		break;
145 	case _SC_2_C_BIND:
146 		mib[0] = CTL_USER;
147 		mib[1] = USER_POSIX2_C_BIND;
148 		goto yesno;
149 	case _SC_2_C_DEV:
150 		mib[0] = CTL_USER;
151 		mib[1] = USER_POSIX2_C_DEV;
152 		goto yesno;
153 	case _SC_2_CHAR_TERM:
154 		mib[0] = CTL_USER;
155 		mib[1] = USER_POSIX2_CHAR_TERM;
156 		goto yesno;
157 	case _SC_2_FORT_DEV:
158 		mib[0] = CTL_USER;
159 		mib[1] = USER_POSIX2_FORT_DEV;
160 		goto yesno;
161 	case _SC_2_FORT_RUN:
162 		mib[0] = CTL_USER;
163 		mib[1] = USER_POSIX2_FORT_RUN;
164 		goto yesno;
165 	case _SC_2_LOCALEDEF:
166 		mib[0] = CTL_USER;
167 		mib[1] = USER_POSIX2_LOCALEDEF;
168 		goto yesno;
169 	case _SC_2_SW_DEV:
170 		mib[0] = CTL_USER;
171 		mib[1] = USER_POSIX2_SW_DEV;
172 		goto yesno;
173 	case _SC_2_UPE:
174 		mib[0] = CTL_USER;
175 		mib[1] = USER_POSIX2_UPE;
176 yesno:		if (sysctl(mib, 2, &value, &len, NULL, 0) == -1)
177 			return (-1);
178 		if (value == 0)
179 			return (-1);
180 		return (value);
181 		break;
182 	default:
183 		errno = EINVAL;
184 		return (-1);
185 	}
186 	return (sysctl(mib, 2, &value, &len, NULL, 0) == -1 ? -1 : value);
187 }
188