getloadavg: simplify code
[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 to test for libelf and libkvm to force the
49 # definition of SVR4 below.
50 if test $gl_have_func = no; then
51   AC_CHECK_LIB([elf], [elf_begin], [LIBS="-lelf $LIBS"])
52   AC_CHECK_LIB([kvm], [kvm_open], [LIBS="-lkvm $LIBS"])
53   # Check for the 4.4BSD definition of getloadavg.
54   AC_CHECK_LIB([util], [getloadavg],
55     [LIBS="-lutil $LIBS" gl_have_func=yes gl_cv_func_getloadavg_setgid=yes])
56 fi
57
58 if test $gl_have_func = no; then
59   # There is a commonly available library for RS/6000 AIX.
60   # Since it is not a standard part of AIX, it might be installed locally.
61   gl_getloadavg_LIBS=$LIBS
62   LIBS="-L/usr/local/lib $LIBS"
63   AC_CHECK_LIB([getloadavg], [getloadavg],
64                [LIBS="-lgetloadavg $LIBS"], [LIBS=$gl_getloadavg_LIBS])
65 fi
66
67 # Make sure it is really in the library, if we think we found it,
68 # otherwise set up the replacement function.
69 AC_CHECK_FUNCS([getloadavg], [],
70                [gl_PREREQ_GETLOADAVG])
71
72 # Some definitions of getloadavg require that the program be installed setgid.
73 AC_CACHE_CHECK([whether getloadavg requires setgid],
74                gl_cv_func_getloadavg_setgid,
75 [AC_EGREP_CPP([Yowza Am I SETGID yet],
76 [#define CONFIGURING_GETLOADAVG
77 #include "$srcdir/$1/getloadavg.c"
78 #ifdef LDAV_PRIVILEGED
79 Yowza Am I SETGID yet
80 #endif
81 ],
82               gl_cv_func_getloadavg_setgid=yes,
83               gl_cv_func_getloadavg_setgid=no)])
84 if test $gl_cv_func_getloadavg_setgid = yes; then
85   NEED_SETGID=true
86   AC_DEFINE([GETLOADAVG_PRIVILEGED], [1],
87             [Define to 1 if the `getloadavg' function needs to be run setuid
88              or setgid.])
89 else
90   NEED_SETGID=false
91 fi
92 AC_SUBST([NEED_SETGID])dnl
93
94 if test $gl_cv_func_getloadavg_setgid = yes; then
95   AC_CACHE_CHECK([group of /dev/kmem], [gl_cv_group_kmem],
96 [ # On Solaris, /dev/kmem is a symlink.  Get info on the real file.
97   ac_ls_output=`ls -lgL /dev/kmem 2>/dev/null`
98   # If we got an error (system does not support symlinks), try without -L.
99   test -z "$ac_ls_output" && ac_ls_output=`ls -lg /dev/kmem`
100   gl_cv_group_kmem=`echo $ac_ls_output \
101     | sed -ne ['s/[      ][      ]*/ /g
102                s/^.[sSrwx-]* *[0-9]* *\([^0-9]*\)  *.*/\1/
103                / /s/.* //;p']`
104 ])
105   AC_SUBST([KMEM_GROUP], [$gl_cv_group_kmem])dnl
106 fi
107 if test "x$gl_save_LIBS" = x; then
108   GETLOADAVG_LIBS=$LIBS
109 else
110   GETLOADAVG_LIBS=`echo "$LIBS" | sed "s!$gl_save_LIBS!!"`
111 fi
112 LIBS=$gl_save_LIBS
113
114 AC_SUBST([GETLOADAVG_LIBS])dnl
115
116 # Test whether the system declares getloadavg. Solaris has the function
117 # but declares it in <sys/loadavg.h>, not <stdlib.h>.
118 AC_CHECK_HEADERS([sys/loadavg.h])
119 if test $ac_cv_header_sys_loadavg_h = yes; then
120   HAVE_SYS_LOADAVG_H=1
121 else
122   HAVE_SYS_LOADAVG_H=0
123 fi
124 AC_CHECK_DECL([getloadavg], [], [HAVE_DECL_GETLOADAVG=0],
125   [#if HAVE_SYS_LOADAVG_H
126    # include <sys/loadavg.h>
127    #endif
128    #include <stdlib.h>])
129 ])# gl_GETLOADAVG
130
131
132 # gl_PREREQ_GETLOADAVG
133 # --------------------
134 # Set up the AC_LIBOBJ replacement of `getloadavg'.
135 AC_DEFUN([gl_PREREQ_GETLOADAVG],
136 [AC_LIBOBJ([getloadavg])
137 AC_DEFINE([C_GETLOADAVG], [1], [Define to 1 if using `getloadavg.c'.])
138 # Figure out what our getloadavg.c needs.
139 gl_have_func=no
140 AC_CHECK_HEADER([sys/dg_sys_info.h],
141 [gl_have_func=yes
142  AC_DEFINE([DGUX], [1], [Define to 1 for DGUX with <sys/dg_sys_info.h>.])
143  AC_CHECK_LIB([dgc], [dg_sys_info])])
144
145 # We cannot check for <dwarf.h>, because Solaris 2 does not use dwarf (it
146 # uses stabs), but it is still SVR4.  We cannot check for <elf.h> because
147 # Irix 4.0.5F has the header but not the library.
148 if test $gl_have_func = no && test "$ac_cv_lib_elf_elf_begin" = yes \
149     && test "$ac_cv_lib_kvm_kvm_open" = yes; then
150   gl_have_func=yes
151   AC_DEFINE([SVR4], [1], [Define to 1 on System V Release 4.])
152 fi
153
154 if test $gl_have_func = no; then
155   AC_CHECK_HEADER([inq_stats/cpustats.h],
156   [gl_have_func=yes
157    AC_DEFINE([UMAX], [1], [Define to 1 for Encore UMAX.])
158    AC_DEFINE([UMAX4_3], [1],
159              [Define to 1 for Encore UMAX 4.3 that has <inq_status/cpustats.h>
160               instead of <sys/cpustats.h>.])])
161 fi
162
163 if test $gl_have_func = no; then
164   AC_CHECK_HEADER([sys/cpustats.h],
165   [gl_have_func=yes; AC_DEFINE([UMAX])])
166 fi
167
168 if test $gl_have_func = no; then
169   AC_CHECK_HEADERS([mach/mach.h])
170 fi
171
172 AC_CHECK_HEADERS([nlist.h],
173 [AC_CHECK_MEMBERS([struct nlist.n_un.n_name],
174                   [AC_DEFINE([NLIST_NAME_UNION], [1],
175                              [Define to 1 if your `struct nlist' has an
176                               `n_un' member.  Obsolete, depend on
177                               `HAVE_STRUCT_NLIST_N_UN_N_NAME])], [],
178                   [@%:@include <nlist.h>])
179  AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <nlist.h>]],
180                    [[struct nlist x;
181                     #ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
182                     x.n_un.n_name = "";
183                     #else
184                     x.n_name = "";
185                     #endif]])],
186                 [AC_DEFINE([N_NAME_POINTER], [1],
187                            [Define to 1 if the nlist n_name member is a pointer])])
188 ])dnl
189 ])# gl_PREREQ_GETLOADAVG