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