getloadavg: don't define SVR4 on cygwin
[gnulib.git] / m4 / getloadavg.m4
1 # Check for getloadavg.
2
3 # Copyright (C) 1992-1996, 1999-2000, 2002-2003, 2006, 2008-2010 Free Software
4 # Foundation, Inc.
5
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 #serial 2
11
12 # Autoconf defines AC_FUNC_GETLOADAVG, but that is obsolescent.
13 # New applications should use gl_GETLOADAVG instead.
14
15 # gl_GETLOADAVG(LIBOBJDIR)
16 # ------------------------
17 AC_DEFUN([gl_GETLOADAVG],
18 [AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
19
20 # Persuade glibc <stdlib.h> to declare getloadavg().
21 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
22
23 gl_have_func=no # yes means we've found a way to get the load average.
24
25 # Make sure getloadavg.c is where it belongs, at configure-time.
26 test -f "$srcdir/$1/getloadavg.c" ||
27   AC_MSG_ERROR([$srcdir/$1/getloadavg.c is missing])
28
29 gl_save_LIBS=$LIBS
30
31 # Check for getloadavg, but be sure not to touch the cache variable.
32 (AC_CHECK_FUNC([getloadavg], [exit 0], [exit 1])) && gl_have_func=yes
33
34 # On HPUX9, an unprivileged user can get load averages through this function.
35 AC_CHECK_FUNCS([pstat_getdynamic])
36
37 # Solaris has libkstat which does not require root.
38 AC_CHECK_LIB([kstat], [kstat_open])
39 test $ac_cv_lib_kstat_kstat_open = yes && gl_have_func=yes
40
41 # AIX has libperfstat which does not require root
42 AC_CHECK_LIB([perfstat], [perfstat_cpu_total])
43 test $ac_cv_lib_perfstat_perfstat_cpu_total = yes && gl_have_func=yes
44
45 # Some systems with -lutil have (and need) -lkvm as well, some do not.
46 # On Solaris, -lkvm requires nlist from -lelf, so check that first
47 # to get the right answer into the cache.
48 # For kstat on solaris, we need libelf to force the definition of SVR4 below.
49 if test $gl_have_func = no; then
50   AC_CHECK_LIB([elf], [elf_begin], [LIBS="-lelf $LIBS"])
51 fi
52 if test $gl_have_func = no; then
53   AC_CHECK_LIB([kvm], [kvm_open], [LIBS="-lkvm $LIBS"])
54   # Check for the 4.4BSD definition of getloadavg.
55   AC_CHECK_LIB([util], [getloadavg],
56     [LIBS="-lutil $LIBS" gl_have_func=yes gl_cv_func_getloadavg_setgid=yes])
57 fi
58
59 if test $gl_have_func = no; then
60   # There is a commonly available library for RS/6000 AIX.
61   # Since it is not a standard part of AIX, it might be installed locally.
62   gl_getloadavg_LIBS=$LIBS
63   LIBS="-L/usr/local/lib $LIBS"
64   AC_CHECK_LIB([getloadavg], [getloadavg],
65                [LIBS="-lgetloadavg $LIBS"], [LIBS=$gl_getloadavg_LIBS])
66 fi
67
68 # Make sure it is really in the library, if we think we found it,
69 # otherwise set up the replacement function.
70 AC_CHECK_FUNCS([getloadavg], [],
71                [gl_PREREQ_GETLOADAVG])
72
73 # Some definitions of getloadavg require that the program be installed setgid.
74 AC_CACHE_CHECK([whether getloadavg requires setgid],
75                gl_cv_func_getloadavg_setgid,
76 [AC_EGREP_CPP([Yowza Am I SETGID yet],
77 [#define CONFIGURING_GETLOADAVG
78 #include "$srcdir/$1/getloadavg.c"
79 #ifdef LDAV_PRIVILEGED
80 Yowza Am I SETGID yet
81 #endif
82 ],
83               gl_cv_func_getloadavg_setgid=yes,
84               gl_cv_func_getloadavg_setgid=no)])
85 if test $gl_cv_func_getloadavg_setgid = yes; then
86   NEED_SETGID=true
87   AC_DEFINE([GETLOADAVG_PRIVILEGED], [1],
88             [Define to 1 if the `getloadavg' function needs to be run setuid
89              or setgid.])
90 else
91   NEED_SETGID=false
92 fi
93 AC_SUBST([NEED_SETGID])dnl
94
95 if test $gl_cv_func_getloadavg_setgid = yes; then
96   AC_CACHE_CHECK([group of /dev/kmem], [gl_cv_group_kmem],
97 [ # On Solaris, /dev/kmem is a symlink.  Get info on the real file.
98   ac_ls_output=`ls -lgL /dev/kmem 2>/dev/null`
99   # If we got an error (system does not support symlinks), try without -L.
100   test -z "$ac_ls_output" && ac_ls_output=`ls -lg /dev/kmem`
101   gl_cv_group_kmem=`echo $ac_ls_output \
102     | sed -ne ['s/[      ][      ]*/ /g
103                s/^.[sSrwx-]* *[0-9]* *\([^0-9]*\)  *.*/\1/
104                / /s/.* //;p']`
105 ])
106   AC_SUBST([KMEM_GROUP], [$gl_cv_group_kmem])dnl
107 fi
108 if test "x$gl_save_LIBS" = x; then
109   GETLOADAVG_LIBS=$LIBS
110 else
111   GETLOADAVG_LIBS=`echo "$LIBS" | sed "s!$gl_save_LIBS!!"`
112 fi
113 LIBS=$gl_save_LIBS
114
115 AC_SUBST([GETLOADAVG_LIBS])dnl
116
117 # Test whether the system declares getloadavg. Solaris has the function
118 # but declares it in <sys/loadavg.h>, not <stdlib.h>.
119 AC_CHECK_HEADERS([sys/loadavg.h])
120 if test $ac_cv_header_sys_loadavg_h = yes; then
121   HAVE_SYS_LOADAVG_H=1
122 else
123   HAVE_SYS_LOADAVG_H=0
124 fi
125 AC_CHECK_DECL([getloadavg], [], [HAVE_DECL_GETLOADAVG=0],
126   [#if HAVE_SYS_LOADAVG_H
127    # include <sys/loadavg.h>
128    #endif
129    #include <stdlib.h>])
130 ])# gl_GETLOADAVG
131
132
133 # gl_PREREQ_GETLOADAVG
134 # --------------------
135 # Set up the AC_LIBOBJ replacement of `getloadavg'.
136 AC_DEFUN([gl_PREREQ_GETLOADAVG],
137 [AC_LIBOBJ([getloadavg])
138 AC_DEFINE([C_GETLOADAVG], [1], [Define to 1 if using `getloadavg.c'.])
139 # Figure out what our getloadavg.c needs.
140 gl_have_func=no
141 AC_CHECK_HEADER([sys/dg_sys_info.h],
142 [gl_have_func=yes
143  AC_DEFINE([DGUX], [1], [Define to 1 for DGUX with <sys/dg_sys_info.h>.])
144  AC_CHECK_LIB([dgc], [dg_sys_info])])
145
146 # We cannot check for <dwarf.h>, because Solaris 2 does not use dwarf (it
147 # uses stabs), but it is still SVR4.  We cannot check for <elf.h> because
148 # Irix 4.0.5F has the header but not the library.
149 if test $gl_have_func = no && test "$ac_cv_lib_elf_elf_begin" = yes \
150     && test "$ac_cv_lib_kvm_kvm_open" = yes; then
151   gl_have_func=yes
152   AC_DEFINE([SVR4], [1], [Define to 1 on System V Release 4.])
153 fi
154
155 if test $gl_have_func = no; then
156   AC_CHECK_HEADER([inq_stats/cpustats.h],
157   [gl_have_func=yes
158    AC_DEFINE([UMAX], [1], [Define to 1 for Encore UMAX.])
159    AC_DEFINE([UMAX4_3], [1],
160              [Define to 1 for Encore UMAX 4.3 that has <inq_status/cpustats.h>
161               instead of <sys/cpustats.h>.])])
162 fi
163
164 if test $gl_have_func = no; then
165   AC_CHECK_HEADER([sys/cpustats.h],
166   [gl_have_func=yes; AC_DEFINE([UMAX])])
167 fi
168
169 if test $gl_have_func = no; then
170   AC_CHECK_HEADERS([mach/mach.h])
171 fi
172
173 AC_CHECK_HEADERS([nlist.h],
174 [AC_CHECK_MEMBERS([struct nlist.n_un.n_name],
175                   [AC_DEFINE([NLIST_NAME_UNION], [1],
176                              [Define to 1 if your `struct nlist' has an
177                               `n_un' member.  Obsolete, depend on
178                               `HAVE_STRUCT_NLIST_N_UN_N_NAME])], [],
179                   [@%:@include <nlist.h>])
180  AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <nlist.h>]],
181                    [[struct nlist x;
182                     #ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
183                     x.n_un.n_name = "";
184                     #else
185                     x.n_name = "";
186                     #endif]])],
187                 [AC_DEFINE([N_NAME_POINTER], [1],
188                            [Define to 1 if the nlist n_name member is a pointer])])
189 ])dnl
190 ])# gl_PREREQ_GETLOADAVG