Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / ntp / dist / lib / isc / unix / os.c
blob3821726aba7b431cf53ab4dd6375691663d87d7a
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: os.c,v 1.18 2007/06/19 23:47:18 tbox Exp */
22 #include <config.h>
24 #include <isc/os.h>
27 #ifdef HAVE_SYSCONF
29 #include <unistd.h>
31 #ifndef __hpux
32 static inline long
33 sysconf_ncpus(void) {
34 #if defined(_SC_NPROCESSORS_ONLN)
35 return sysconf((_SC_NPROCESSORS_ONLN));
36 #elif defined(_SC_NPROC_ONLN)
37 return sysconf((_SC_NPROC_ONLN));
38 #else
39 return (0);
40 #endif
42 #endif
43 #endif /* HAVE_SYSCONF */
46 #ifdef __hpux
48 #include <sys/pstat.h>
50 static inline int
51 hpux_ncpus(void) {
52 struct pst_dynamic psd;
53 if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
54 return (psd.psd_proc_cnt);
55 else
56 return (0);
59 #endif /* __hpux */
61 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
62 #include <sys/types.h> /* for FreeBSD */
63 #include <sys/param.h> /* for NetBSD */
64 #include <sys/sysctl.h>
66 static int
67 sysctl_ncpus(void) {
68 int ncpu, result;
69 size_t len;
71 len = sizeof(ncpu);
72 result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
73 if (result != -1)
74 return (ncpu);
75 return (0);
77 #endif
79 unsigned int
80 isc_os_ncpus(void) {
81 long ncpus = 0;
83 #ifdef __hpux
84 ncpus = hpux_ncpus();
85 #elif defined(HAVE_SYSCONF)
86 ncpus = sysconf_ncpus();
87 #endif
88 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
89 if (ncpus <= 0)
90 ncpus = sysctl_ncpus();
91 #endif
92 if (ncpus <= 0)
93 ncpus = 1;
95 return ((unsigned int)ncpus);