2005-09-28 Marcus Brinkmann <marcus@g10code.de>
[pinentry.git] / acinclude.m4
1 dnl Autoconf macros used by PINENTRY
2 dnl
3 dnl Copyright (C) 2002 g10 Code GmbH
4 dnl
5 dnl
6 dnl GNUPG_CHECK_TYPEDEF(TYPE, HAVE_NAME)
7 dnl Check whether a typedef exists and create a #define $2 if it exists
8 dnl
9 AC_DEFUN([GNUPG_CHECK_TYPEDEF],
10   [ AC_MSG_CHECKING(for $1 typedef)
11     AC_CACHE_VAL(gnupg_cv_typedef_$1,
12     [AC_TRY_COMPILE([#define _GNU_SOURCE 1
13     #include <stdlib.h>
14     #include <sys/types.h>], [
15     #undef $1
16     int a = sizeof($1);
17     ], gnupg_cv_typedef_$1=yes, gnupg_cv_typedef_$1=no )])
18     AC_MSG_RESULT($gnupg_cv_typedef_$1)
19     if test "$gnupg_cv_typedef_$1" = yes; then
20         AC_DEFINE($2,1,[Defined if a `]$1[' is typedef'd])
21     fi
22   ])
23
24
25 ######################################################################
26 # Check whether mlock is broken (hpux 10.20 raises a SIGBUS if mlock
27 # is not called from uid 0 (not tested whether uid 0 works)
28 # For DECs Tru64 we have also to check whether mlock is in librt
29 # mlock is there a macro using memlk()
30 ######################################################################
31 dnl GNUPG_CHECK_MLOCK
32 dnl
33 define([GNUPG_CHECK_MLOCK],
34   [ AC_CHECK_FUNCS(mlock)
35     if test "$ac_cv_func_mlock" = "no"; then
36         AC_CHECK_HEADERS(sys/mman.h)
37         if test "$ac_cv_header_sys_mman_h" = "yes"; then
38             # Add librt to LIBS:
39             AC_CHECK_LIB(rt, memlk)
40             AC_CACHE_CHECK([whether mlock is in sys/mman.h],
41                             gnupg_cv_mlock_is_in_sys_mman,
42                 [AC_TRY_LINK([
43                     #include <assert.h>
44                     #ifdef HAVE_SYS_MMAN_H
45                     #include <sys/mman.h>
46                     #endif
47                 ], [
48                     int i;
49
50                     /* glibc defines this for functions which it implements
51                      * to always fail with ENOSYS.  Some functions are actually
52                      * named something starting with __ and the normal name
53                      * is an alias.  */
54                     #if defined (__stub_mlock) || defined (__stub___mlock)
55                     choke me
56                     #else
57                     mlock(&i, 4);
58                     #endif
59                     ; return 0;
60                 ],
61                 gnupg_cv_mlock_is_in_sys_mman=yes,
62                 gnupg_cv_mlock_is_in_sys_mman=no)])
63             if test "$gnupg_cv_mlock_is_in_sys_mman" = "yes"; then
64                 AC_DEFINE(HAVE_MLOCK,1,
65                           [Defined if the system supports an mlock() call])
66             fi
67         fi
68     fi
69     if test "$ac_cv_func_mlock" = "yes"; then
70         AC_MSG_CHECKING(whether mlock is broken)
71           AC_CACHE_VAL(gnupg_cv_have_broken_mlock,
72              AC_TRY_RUN([
73                 #include <stdlib.h>
74                 #include <unistd.h>
75                 #include <errno.h>
76                 #include <sys/mman.h>
77                 #include <sys/types.h>
78                 #include <fcntl.h>
79
80                 int main()
81                 {
82                     char *pool;
83                     int err;
84                     long int pgsize = getpagesize();
85
86                     pool = malloc( 4096 + pgsize );
87                     if( !pool )
88                         return 2;
89                     pool += (pgsize - ((long int)pool % pgsize));
90
91                     err = mlock( pool, 4096 );
92                     if( !err || errno == EPERM )
93                         return 0; /* okay */
94
95                     return 1;  /* hmmm */
96                 }
97
98             ],
99             gnupg_cv_have_broken_mlock="no",
100             gnupg_cv_have_broken_mlock="yes",
101             gnupg_cv_have_broken_mlock="assume-no"
102            )
103          )
104          if test "$gnupg_cv_have_broken_mlock" = "yes"; then
105              AC_DEFINE(HAVE_BROKEN_MLOCK,1,
106                        [Defined if the mlock() call does not work])
107              AC_MSG_RESULT(yes)
108              AC_CHECK_FUNCS(plock)
109          else
110             if test "$gnupg_cv_have_broken_mlock" = "no"; then
111                 AC_MSG_RESULT(no)
112             else
113                 AC_MSG_RESULT(assuming no)
114             fi
115          fi
116     fi
117   ])