Merge from vendor branch NTPD:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / unix / os.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000, 2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.
8  *
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.
16  */
17
18 /* $Id: os.c,v 1.11.2.2 2004/05/18 01:38:39 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/os.h>
23
24
25 #ifdef HAVE_SYSCONF
26
27 #include <unistd.h>
28
29 static inline long
30 sysconf_ncpus(void) {
31 #if defined(_SC_NPROCESSORS_ONLN)
32         return sysconf((_SC_NPROCESSORS_ONLN));
33 #elif defined(_SC_NPROC_ONLN)
34         return sysconf((_SC_NPROC_ONLN));
35 #else
36         return (0);
37 #endif
38 }
39 #endif /* HAVE_SYSCONF */
40
41
42 #ifdef __hpux
43
44 #include <sys/pstat.h>
45
46 static inline int
47 hpux_ncpus(void) {
48         struct pst_dynamic psd;
49         if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1)
50                 return (psd.psd_proc_cnt);
51         else
52                 return (0);
53 }
54
55 #endif /* __hpux */
56
57 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
58 #include <sys/types.h>  /* for FreeBSD */
59 #include <sys/param.h>  /* for NetBSD */
60 #include <sys/sysctl.h>
61
62 static int
63 sysctl_ncpus(void) {
64         int ncpu, result;
65         size_t len;
66
67         len = sizeof ncpu;
68         result = sysctlbyname("hw.ncpu", &ncpu, &len , 0, 0);
69         if (result != -1)
70                 return (ncpu);
71         return (0);
72 }
73 #endif
74
75 unsigned int
76 isc_os_ncpus(void) {
77         long ncpus = 0;
78
79 #ifdef __hpux
80         ncpus = hpux_ncpus();
81 #elif defined(HAVE_SYSCONF)
82         ncpus = sysconf_ncpus();
83 #endif
84 #if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTLBYNAME)
85         if (ncpus <= 0)
86                 ncpus = sysctl_ncpus();
87 #endif
88         if (ncpus <= 0)
89                 ncpus = 1;
90
91         return ((unsigned int)ncpus);
92 }