GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/gen/sysconf.c Lines: 0 165 0.0 %
Date: 2017-11-13 Branches: 0 154 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: sysconf.c,v 1.25 2017/09/10 18:20:00 guenther Exp $ */
2
/*-
3
 * Copyright (c) 1993
4
 *	The Regents of the University of California.  All rights reserved.
5
 *
6
 * This code is derived from software contributed to Berkeley by
7
 * Sean Eric Fagan of Cygnus Support.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 * 3. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 */
33
34
#include <sys/types.h>
35
#include <sys/sem.h>
36
#include <sys/sysctl.h>
37
#include <sys/time.h>
38
#include <sys/resource.h>
39
#include <sys/socket.h>
40
41
#include <errno.h>
42
#include <grp.h>
43
#include <pthread.h>
44
#include <pwd.h>
45
#include <stdio.h>
46
#include <unistd.h>
47
48
/*
49
 * sysconf --
50
 *	get configurable system variables.
51
 *
52
 * XXX
53
 * POSIX 1003.1 (ISO/IEC 9945-1, 4.8.1.3) states that the variable values
54
 * not change during the lifetime of the calling process.  This would seem
55
 * to require that any change to system limits kill all running processes.
56
 * A workaround might be to cache the values when they are first retrieved
57
 * and then simply return the cached value on subsequent calls.  This is
58
 * less useful than returning up-to-date values, however.
59
 */
60
long
61
sysconf(int name)
62
{
63
	struct rlimit rl;
64
	size_t len;
65
	int mib[3], value, namelen, sverrno;
66
67
	len = sizeof(value);
68
	namelen = 2;
69
70
	switch (name) {
71
/* 1003.1 */
72
	case _SC_ARG_MAX:
73
		mib[0] = CTL_KERN;
74
		mib[1] = KERN_ARGMAX;
75
		break;
76
	case _SC_CHILD_MAX:
77
		if (getrlimit(RLIMIT_NPROC, &rl) != 0)
78
			return (-1);
79
		if (rl.rlim_cur == RLIM_INFINITY)
80
			return (-1);
81
		if (rl.rlim_cur > LONG_MAX) {
82
			errno = EOVERFLOW;
83
			return (-1);
84
		}
85
		return ((long)rl.rlim_cur);
86
	case _SC_CLK_TCK:
87
		return (CLK_TCK);
88
	case _SC_JOB_CONTROL:
89
		return (_POSIX_JOB_CONTROL);
90
	case _SC_NGROUPS_MAX:
91
		mib[0] = CTL_KERN;
92
		mib[1] = KERN_NGROUPS;
93
		break;
94
	case _SC_OPEN_MAX:
95
		if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
96
			return (-1);
97
		if (rl.rlim_cur == RLIM_INFINITY)
98
			return (-1);
99
		if (rl.rlim_cur > LONG_MAX) {
100
			errno = EOVERFLOW;
101
			return (-1);
102
		}
103
		return ((long)rl.rlim_cur);
104
	case _SC_STREAM_MAX:
105
		if (getrlimit(RLIMIT_NOFILE, &rl) != 0)
106
			return (-1);
107
		if (rl.rlim_cur == RLIM_INFINITY)
108
			return (-1);
109
		if (rl.rlim_cur > LONG_MAX) {
110
			errno = EOVERFLOW;
111
			return (-1);
112
		}
113
		/*
114
		 * struct __sFILE currently has a limitation that
115
		 * file descriptors must fit in a signed short.
116
		 * This doesn't precisely capture the letter of POSIX
117
		 * but approximates the spirit.
118
		 */
119
		if (rl.rlim_cur > SHRT_MAX)
120
			return (SHRT_MAX);
121
122
		return ((long)rl.rlim_cur);
123
	case _SC_TZNAME_MAX:
124
		return (NAME_MAX);
125
	case _SC_SAVED_IDS:
126
		return (_POSIX_SAVED_IDS);
127
	case _SC_VERSION:
128
		mib[0] = CTL_KERN;
129
		mib[1] = KERN_POSIX1;
130
		break;
131
132
/* 1003.1b */
133
	case _SC_PAGESIZE:
134
		if (_pagesize != 0)
135
			return (_pagesize);
136
		mib[0] = CTL_HW;
137
		mib[1] = HW_PAGESIZE;
138
		break;
139
	case _SC_FSYNC:
140
		return (_POSIX_FSYNC);
141
142
/* 1003.1c */
143
	case _SC_LOGIN_NAME_MAX:
144
		return (LOGIN_NAME_MAX);
145
146
	case _SC_THREAD_SAFE_FUNCTIONS:
147
		return (_POSIX_THREAD_SAFE_FUNCTIONS);
148
149
	case _SC_GETGR_R_SIZE_MAX:
150
		return (_GR_BUF_LEN);
151
152
	case _SC_GETPW_R_SIZE_MAX:
153
		return (_PW_BUF_LEN);
154
155
/* 1003.2 */
156
	case _SC_BC_BASE_MAX:
157
		return (BC_BASE_MAX);
158
	case _SC_BC_DIM_MAX:
159
		return (BC_DIM_MAX);
160
	case _SC_BC_SCALE_MAX:
161
		return (BC_SCALE_MAX);
162
	case _SC_BC_STRING_MAX:
163
		return (BC_STRING_MAX);
164
	case _SC_COLL_WEIGHTS_MAX:
165
		return (COLL_WEIGHTS_MAX);
166
	case _SC_EXPR_NEST_MAX:
167
		return (EXPR_NEST_MAX);
168
	case _SC_LINE_MAX:
169
		return (LINE_MAX);
170
	case _SC_RE_DUP_MAX:
171
		return (RE_DUP_MAX);
172
	case _SC_2_VERSION:
173
		return (_POSIX2_VERSION);
174
	case _SC_2_C_BIND:
175
		return (_POSIX2_C_BIND);
176
	case _SC_2_C_DEV:
177
		return (_POSIX2_C_DEV);
178
	case _SC_2_CHAR_TERM:
179
		return (_POSIX2_CHAR_TERM);
180
	case _SC_2_FORT_DEV:
181
		return (_POSIX2_FORT_DEV);
182
	case _SC_2_FORT_RUN:
183
		return (_POSIX2_FORT_RUN);
184
	case _SC_2_LOCALEDEF:
185
		return (_POSIX2_LOCALEDEF);
186
	case _SC_2_SW_DEV:
187
		return (_POSIX2_SW_DEV);
188
	case _SC_2_UPE:
189
		return (_POSIX2_UPE);
190
191
/* XPG 4.2 */
192
	case _SC_XOPEN_SHM:
193
		mib[0] = CTL_KERN;
194
		mib[1] = KERN_SYSVSHM;
195
		if (sysctl(mib, namelen, &value, &len, NULL, 0) == -1)
196
			return (-1);
197
		if (value == 0)
198
			return (-1);
199
		return (value);
200
		break;
201
	case _SC_SEM_NSEMS_MAX:
202
		return (-1);
203
	case _SC_SEM_VALUE_MAX:
204
		return (SEM_VALUE_MAX);
205
206
/* Unsorted */
207
	case _SC_HOST_NAME_MAX:
208
		return (HOST_NAME_MAX);	/* does not include \0 */
209
	case _SC_MONOTONIC_CLOCK:
210
		return (_POSIX_MONOTONIC_CLOCK);
211
	case _SC_2_PBS:
212
	case _SC_2_PBS_ACCOUNTING:
213
	case _SC_2_PBS_CHECKPOINT:
214
	case _SC_2_PBS_LOCATE:
215
	case _SC_2_PBS_MESSAGE:
216
	case _SC_2_PBS_TRACK:
217
		return (_POSIX2_PBS);
218
	case _SC_ADVISORY_INFO:
219
		return (_POSIX_ADVISORY_INFO);
220
	case _SC_AIO_LISTIO_MAX:
221
	case _SC_AIO_MAX:
222
	case _SC_AIO_PRIO_DELTA_MAX:
223
		return (-1);
224
	case _SC_ASYNCHRONOUS_IO:
225
		return (_POSIX_ASYNCHRONOUS_IO);
226
	case _SC_ATEXIT_MAX:
227
		return (-1);
228
	case _SC_BARRIERS:
229
		return (_POSIX_BARRIERS);
230
	case _SC_CLOCK_SELECTION:
231
		return (_POSIX_CLOCK_SELECTION);
232
	case _SC_CPUTIME:
233
		return (_POSIX_CPUTIME);
234
	case _SC_DELAYTIMER_MAX:
235
		return (-1);
236
	case _SC_IOV_MAX:
237
		return (IOV_MAX);
238
	case _SC_IPV6:
239
#if _POSIX_IPV6 == 0
240
		sverrno = errno;
241
		value = socket(PF_INET6, SOCK_DGRAM, 0);
242
		errno = sverrno;
243
		if (value >= 0) {
244
			HIDDEN(close)(value);
245
			return (200112L);
246
		} else
247
			return (0);
248
#else
249
		return (_POSIX_IPV6);
250
#endif
251
	case _SC_MAPPED_FILES:
252
		return (_POSIX_MAPPED_FILES);
253
	case _SC_MEMLOCK:
254
		return (_POSIX_MEMLOCK);
255
	case _SC_MEMLOCK_RANGE:
256
		return (_POSIX_MEMLOCK_RANGE);
257
	case _SC_MEMORY_PROTECTION:
258
		return (_POSIX_MEMORY_PROTECTION);
259
	case _SC_MESSAGE_PASSING:
260
		return (_POSIX_MESSAGE_PASSING);
261
	case _SC_PRIORITIZED_IO:
262
		return (_POSIX_PRIORITIZED_IO);
263
	case _SC_PRIORITY_SCHEDULING:
264
		return (_POSIX_PRIORITY_SCHEDULING);
265
	case _SC_RAW_SOCKETS:
266
		return (_POSIX_RAW_SOCKETS);
267
	case _SC_READER_WRITER_LOCKS:
268
		return (_POSIX_READER_WRITER_LOCKS);
269
	case _SC_REALTIME_SIGNALS:
270
		return (_POSIX_REALTIME_SIGNALS);
271
	case _SC_REGEXP:
272
		return (_POSIX_REGEXP);
273
	case _SC_SEMAPHORES:
274
		return (_POSIX_SEMAPHORES);
275
	case _SC_SHARED_MEMORY_OBJECTS:
276
		return (_POSIX_SHARED_MEMORY_OBJECTS);
277
	case _SC_SHELL:
278
		return (_POSIX_SHELL);
279
	case _SC_SIGQUEUE_MAX:
280
		return (-1);
281
	case _SC_SPAWN:
282
		return (_POSIX_SPAWN);
283
	case _SC_SPIN_LOCKS:
284
		return (_POSIX_SPIN_LOCKS);
285
	case _SC_SPORADIC_SERVER:
286
		return (_POSIX_SPORADIC_SERVER);
287
	case _SC_SYNCHRONIZED_IO:
288
		return (_POSIX_SYNCHRONIZED_IO);
289
	case _SC_SYMLOOP_MAX:
290
		return (SYMLOOP_MAX);
291
	case _SC_THREAD_ATTR_STACKADDR:
292
		return (_POSIX_THREAD_ATTR_STACKADDR);
293
	case _SC_THREAD_ATTR_STACKSIZE:
294
		return (_POSIX_THREAD_ATTR_STACKSIZE);
295
	case _SC_THREAD_CPUTIME:
296
		return (_POSIX_THREAD_CPUTIME);
297
	case _SC_THREAD_DESTRUCTOR_ITERATIONS:
298
		return (PTHREAD_DESTRUCTOR_ITERATIONS);
299
	case _SC_THREAD_KEYS_MAX:
300
		return (PTHREAD_KEYS_MAX);
301
	case _SC_THREAD_PRIO_INHERIT:
302
		return (_POSIX_THREAD_PRIO_INHERIT);
303
	case _SC_THREAD_PRIO_PROTECT:
304
		return (_POSIX_THREAD_PRIO_PROTECT);
305
	case _SC_THREAD_PRIORITY_SCHEDULING:
306
		return (_POSIX_THREAD_PRIORITY_SCHEDULING);
307
	case _SC_THREAD_PROCESS_SHARED:
308
		return (_POSIX_THREAD_PROCESS_SHARED);
309
	case _SC_THREAD_ROBUST_PRIO_INHERIT:
310
		return (_POSIX_THREAD_ROBUST_PRIO_INHERIT);
311
	case _SC_THREAD_ROBUST_PRIO_PROTECT:
312
		return (_POSIX_THREAD_ROBUST_PRIO_PROTECT);
313
	case _SC_THREAD_SPORADIC_SERVER:
314
		return (_POSIX_THREAD_SPORADIC_SERVER);
315
	case _SC_THREAD_STACK_MIN:
316
		return (PTHREAD_STACK_MIN);
317
	case _SC_THREAD_THREADS_MAX:
318
		return (PTHREAD_THREADS_MAX);
319
	case _SC_THREADS:
320
		return (_POSIX_THREADS);
321
	case _SC_TIMEOUTS:
322
		return (_POSIX_TIMEOUTS);
323
	case _SC_TIMER_MAX:
324
		return (-1);
325
	case _SC_TIMERS:
326
		return (_POSIX_TIMERS);
327
	case _SC_TRACE:
328
	case _SC_TRACE_EVENT_FILTER:
329
	case _SC_TRACE_EVENT_NAME_MAX:
330
	case _SC_TRACE_INHERIT:
331
	case _SC_TRACE_LOG:
332
		return (_POSIX_TRACE);
333
	case _SC_TTY_NAME_MAX:
334
		return (TTY_NAME_MAX);
335
	case _SC_TYPED_MEMORY_OBJECTS:
336
		return (_POSIX_TYPED_MEMORY_OBJECTS);
337
	case _SC_V6_ILP32_OFF32:
338
		return (_POSIX_V6_ILP32_OFF32);
339
	case _SC_V6_ILP32_OFFBIG:
340
#if _POSIX_V6_ILP32_OFFBIG == 0
341
		if (sizeof(int) * CHAR_BIT == 32 &&
342
		    sizeof(long) * CHAR_BIT == 32 &&
343
		    sizeof(void *) * CHAR_BIT == 32 &&
344
		    sizeof(off_t) * CHAR_BIT >= 64)
345
			return 1;
346
		else
347
			return -1;
348
#else
349
		return (_POSIX_V6_ILP32_OFFBIG);
350
#endif
351
	case _SC_V6_LP64_OFF64:
352
#if _POSIX_V6_LP64_OFF64 == 0
353
		if (sizeof(int) * CHAR_BIT == 32 &&
354
		    sizeof(long) * CHAR_BIT == 64 &&
355
		    sizeof(void *) * CHAR_BIT == 64 &&
356
		    sizeof(off_t) * CHAR_BIT == 64)
357
			return 1;
358
		else
359
			return -1;
360
#else
361
		return (_POSIX_V6_LP64_OFF64);
362
#endif
363
	case _SC_V6_LPBIG_OFFBIG:
364
#if _POSIX_V6_LPBIG_OFFBIG == 0
365
		if (sizeof(int) * CHAR_BIT >= 32 &&
366
		    sizeof(long) * CHAR_BIT >= 64 &&
367
		    sizeof(void *) * CHAR_BIT >= 64 &&
368
		    sizeof(off_t) * CHAR_BIT >= 64)
369
			return 1;
370
		else
371
			return -1;
372
#else
373
		return (_POSIX_V6_LPBIG_OFFBIG);
374
#endif
375
	case _SC_V7_ILP32_OFF32:
376
		return (_POSIX_V7_ILP32_OFF32);
377
	case _SC_V7_ILP32_OFFBIG:
378
#if _POSIX_V7_ILP32_OFFBIG == 0
379
		if (sizeof(int) * CHAR_BIT == 32 &&
380
		    sizeof(long) * CHAR_BIT == 32 &&
381
		    sizeof(void *) * CHAR_BIT == 32 &&
382
		    sizeof(off_t) * CHAR_BIT >= 64)
383
			return 1;
384
		else
385
			return -1;
386
#else
387
		return (_POSIX_V7_ILP32_OFFBIG);
388
#endif
389
	case _SC_V7_LP64_OFF64:
390
#if _POSIX_V7_LP64_OFF64 == 0
391
		if (sizeof(int) * CHAR_BIT == 32 &&
392
		    sizeof(long) * CHAR_BIT == 64 &&
393
		    sizeof(void *) * CHAR_BIT == 64 &&
394
		    sizeof(off_t) * CHAR_BIT == 64)
395
			return 1;
396
		else
397
			return -1;
398
#else
399
		return (_POSIX_V7_LP64_OFF64);
400
#endif
401
	case _SC_V7_LPBIG_OFFBIG:
402
#if _POSIX_V7_LPBIG_OFFBIG == 0
403
		if (sizeof(int) * CHAR_BIT >= 32 &&
404
		    sizeof(long) * CHAR_BIT >= 64 &&
405
		    sizeof(void *) * CHAR_BIT >= 64 &&
406
		    sizeof(off_t) * CHAR_BIT >= 64)
407
			return 1;
408
		else
409
			return -1;
410
#else
411
		return (_POSIX_V7_LPBIG_OFFBIG);
412
#endif
413
	case _SC_XOPEN_CRYPT:
414
		return (_XOPEN_CRYPT);
415
	case _SC_XOPEN_ENH_I18N:
416
		return (_XOPEN_ENH_I18N);
417
	case _SC_XOPEN_LEGACY:
418
		return (_XOPEN_LEGACY);
419
	case _SC_XOPEN_REALTIME:
420
		return (_XOPEN_REALTIME);
421
	case _SC_XOPEN_REALTIME_THREADS:
422
		return (_XOPEN_REALTIME_THREADS);
423
	case _SC_XOPEN_STREAMS:
424
		return (_XOPEN_STREAMS);
425
	case _SC_XOPEN_UNIX:
426
		return (_XOPEN_UNIX);
427
	case _SC_XOPEN_UUCP:
428
		return (_XOPEN_UUCP);
429
#ifdef _XOPEN_VERSION
430
	case _SC_XOPEN_VERSION:
431
		return (_XOPEN_VERSION);
432
#endif
433
434
/* Extensions */
435
	case _SC_PHYS_PAGES:
436
	{
437
		int64_t physmem;
438
439
		mib[0] = CTL_HW;
440
		mib[1] = HW_PHYSMEM64;
441
		len = sizeof(physmem);
442
		if (sysctl(mib, namelen, &physmem, &len, NULL, 0) == -1)
443
			return (-1);
444
		return (physmem / getpagesize());
445
	}
446
	case _SC_AVPHYS_PAGES:
447
	{
448
		struct uvmexp uvmexp;
449
450
		mib[0] = CTL_VM;
451
		mib[1] = VM_UVMEXP;
452
		len = sizeof(uvmexp);
453
		if (sysctl(mib, namelen, &uvmexp, &len, NULL, 0) == -1)
454
			return (-1);
455
		return (uvmexp.free);
456
	}
457
458
	case _SC_NPROCESSORS_CONF:
459
		mib[0] = CTL_HW;
460
		mib[1] = HW_NCPU;
461
		break;
462
	case _SC_NPROCESSORS_ONLN:
463
		mib[0] = CTL_HW;
464
		mib[1] = HW_NCPU;
465
		break;
466
467
	default:
468
		errno = EINVAL;
469
		return (-1);
470
	}
471
	return (sysctl(mib, namelen, &value, &len, NULL, 0) == -1 ? -1 : value);
472
}
473
DEF_WEAK(sysconf);