lib/replace: xattr wrappers in lib/replace rather than source3/lib/system.c
[kai/samba.git] / lib / replace / libreplace.m4
1 AC_DEFUN_ONCE(AC_LIBREPLACE_LOCATION_CHECKS,
2 [
3 echo "LIBREPLACE_LOCATION_CHECKS: START"
4
5 dnl find the libreplace sources. This is meant to work both for 
6 dnl libreplace standalone builds, and builds of packages using libreplace
7 libreplacedir=""
8 libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
9 for d in $libreplacepaths; do
10         if test -f "$d/replace.c"; then
11                 libreplacedir="$d"              
12                 AC_SUBST(libreplacedir)
13                 break;
14         fi
15 done
16 if test x"$libreplacedir" = "x"; then
17         AC_MSG_ERROR([cannot find libreplace in $libreplacepaths])
18 fi
19 LIBREPLACEOBJ="$libreplacedir/replace.o"
20 AC_SUBST(LIBREPLACEOBJ)
21
22 AC_CANONICAL_BUILD
23 AC_CANONICAL_HOST
24 AC_CANONICAL_TARGET
25
26 echo "LIBREPLACE_LOCATION_CHECKS: END"
27 ]) dnl end AC_LIBREPLACE_LOCATION_CHECKS
28
29
30 AC_DEFUN_ONCE(AC_LIBREPLACE_BROKEN_CHECKS,
31 [
32 echo "LIBREPLACE_BROKEN_CHECKS: START"
33
34 dnl find the libreplace sources. This is meant to work both for 
35 dnl libreplace standalone builds, and builds of packages using libreplace
36 libreplacedir=""
37 libreplacepaths="$srcdir $srcdir/lib/replace $srcdir/libreplace $srcdir/../libreplace $srcdir/../replace $srcdir/../lib/replace $srcdir/../../../lib/replace"
38 for d in $libreplacepaths; do
39         if test -f "$d/replace.c"; then
40                 libreplacedir="$d"              
41                 AC_SUBST(libreplacedir)
42                 break;
43         fi
44 done
45 if test x"$libreplacedir" = "x"; then
46         AC_MSG_ERROR([cannot find libreplace in $libreplacepaths])
47 fi
48
49 LIBREPLACEOBJ="$libreplacedir/replace.o"
50 AC_SUBST(LIBREPLACEOBJ)
51
52 LIBREPLACEOBJ="${LIBREPLACEOBJ} $libreplacedir/snprintf.o"
53
54 AC_TYPE_UID_T
55 AC_TYPE_MODE_T
56 AC_TYPE_OFF_T
57 AC_TYPE_SIZE_T
58 AC_TYPE_PID_T
59 AC_STRUCT_ST_RDEV
60 AC_CHECK_TYPE(ino_t,unsigned)
61 AC_CHECK_TYPE(loff_t,off_t)
62 AC_CHECK_TYPE(offset_t,loff_t)
63 AC_CHECK_TYPE(blksize_t,long)
64 AC_CHECK_TYPE(blkcnt_t,long)
65
66 AC_FUNC_MEMCMP
67
68 AC_CHECK_FUNCS([pipe strftime srandom random srand rand usleep setbuffer lstat getpgrp utime utimes])
69
70 AC_CHECK_HEADERS(stdbool.h stdint.h sys/select.h)
71 AC_CHECK_HEADERS(setjmp.h utime.h)
72
73 LIBREPLACE_PROVIDE_HEADER([stdint.h])
74 LIBREPLACE_PROVIDE_HEADER([stdbool.h])
75
76 AC_DEFINE(HAVE_LIBREPLACE, 1, [We have libreplace])
77
78 AC_CHECK_TYPE(bool, 
79 [AC_DEFINE(HAVE_BOOL, 1, [Whether the bool type is available])],,
80 [
81 AC_INCLUDES_DEFAULT
82 #ifdef HAVE_STDBOOL_H
83 #include <stdbool.h>
84 #endif]
85 )
86
87 AC_CHECK_TYPE(_Bool, 
88 [AC_DEFINE(HAVE__Bool, 1, [Whether the _Bool type is available])],,
89 [
90 AC_INCLUDES_DEFAULT
91 #ifdef HAVE_STDBOOL_H
92 #include <stdbool.h>
93 #endif]
94 )
95
96 AC_CHECK_HEADERS(linux/types.h)
97
98 AC_CACHE_CHECK([for working mmap],libreplace_cv_HAVE_MMAP,[
99 AC_TRY_RUN([#include "$libreplacedir/test/shared_mmap.c"],
100            libreplace_cv_HAVE_MMAP=yes,libreplace_cv_HAVE_MMAP=no,libreplace_cv_HAVE_MMAP=cross)])
101 if test x"$libreplace_cv_HAVE_MMAP" = x"yes"; then
102     AC_DEFINE(HAVE_MMAP,1,[Whether mmap works])
103 fi
104
105 AC_CACHE_CHECK([for working mremap],libreplace_cv_HAVE_MREMAP,[
106 AC_TRY_RUN([#include "$libreplacedir/test/shared_mremap.c"],
107            libreplace_cv_HAVE_MREMAP=yes,libreplace_cv_HAVE_MREMAP=no,libreplace_cv_HAVE_MREMAP=cross)])
108 if test x"$libreplace_cv_HAVE_MREMAP" = x"yes"; then
109     AC_DEFINE(HAVE_MREMAP,1,[Whether mremap works])
110 fi
111
112 AC_CACHE_CHECK([for incoherent mmap],libreplace_cv_HAVE_INCOHERENT_MMAP,[
113 AC_TRY_RUN([#include "$libreplacedir/test/incoherent_mmap.c"],
114            libreplace_cv_HAVE_INCOHERENT_MMAP=yes,libreplace_cv_HAVE_INCOHERENT_MMAP=no,libreplace_cv_HAVE_INCOHERENT_MMAP=cross)])
115 if test x"$libreplace_cv_HAVE_INCOHERENT_MMAP" = x"yes"; then
116     AC_DEFINE(HAVE_INCOHERENT_MMAP,1,[Whether mmap is incoherent against write])
117 fi
118
119
120 AC_CHECK_HEADERS(sys/syslog.h syslog.h)
121 AC_CHECK_HEADERS(sys/time.h time.h)
122 AC_CHECK_HEADERS(stdarg.h vararg.h)
123 AC_CHECK_HEADERS(sys/mount.h mntent.h)
124 AC_CHECK_HEADERS(stropts.h)
125 AC_CHECK_HEADERS(unix.h)
126 AC_CHECK_HEADERS(malloc.h)
127
128 AC_CHECK_FUNCS(seteuid setreuid setresuid setegid setregid setresgid)
129 AC_CHECK_FUNCS(chroot bzero strerror strerror_r memalign posix_memalign getpagesize)
130 AC_CHECK_FUNCS(vsyslog setlinebuf mktime ftruncate chsize rename)
131 AC_CHECK_FUNCS(waitpid wait4 strlcpy strlcat initgroups memmove strdup)
132 AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2 dprintf vdprintf)
133 AC_CHECK_FUNCS(isatty chown lchown link readlink symlink realpath)
134 AC_CHECK_FUNCS(fdatasync,,[
135         # if we didn't find it, look in librt (Solaris hides it there...)
136         AC_CHECK_LIB(rt, fdatasync,
137                 [libreplace_cv_HAVE_FDATASYNC_IN_LIBRT=yes
138                 AC_DEFINE(HAVE_FDATASYNC, 1, Define to 1 if there is support for fdatasync)])
139 ])
140 AC_HAVE_DECL(fdatasync, [#include <unistd.h>])
141 AC_CHECK_FUNCS(clock_gettime,libreplace_cv_have_clock_gettime=yes,[
142         AC_CHECK_LIB(rt, clock_gettime,
143                 [libreplace_cv_HAVE_CLOCK_GETTIME_IN_LIBRT=yes
144                 libreplace_cv_have_clock_gettime=yes
145                 AC_DEFINE(HAVE_CLOCK_GETTIME, 1, Define to 1 if there is support for clock_gettime)])
146 ])
147
148 AC_CHECK_HEADERS(sys/attributes.h attr/xattr.h sys/xattr.h sys/extattr.h sys/uio.h)
149 AC_CHECK_HEADERS(sys/ea.h sys/proplist.h)
150
151 ############################################
152 # Check for EA implementations
153 case "$host_os" in
154   *freebsd4* | *dragonfly* )
155         AC_DEFINE(BROKEN_EXTATTR, 1, [Does extattr API work])
156   ;;
157   *)
158         AC_SEARCH_LIBS(getxattr, [attr])
159         AC_CHECK_FUNCS(attr_get attr_getf attr_list attr_listf attropen attr_remove)
160         AC_CHECK_FUNCS(attr_removef attr_set attr_setf extattr_delete_fd extattr_delete_file)
161         AC_CHECK_FUNCS(extattr_get_fd extattr_get_file extattr_list_fd extattr_list_file)
162         AC_CHECK_FUNCS(extattr_set_fd extattr_set_file fgetea fgetxattr flistea flistxattr)
163         AC_CHECK_FUNCS(fremoveea fremovexattr fsetea fsetxattr getea getxattr listea)
164         AC_CHECK_FUNCS(listxattr removeea removexattr setea setxattr)
165
166   ;;
167 esac
168
169
170 ########################################################
171 # Do xattr functions take additional options like on Darwin?
172 if test x"$ac_cv_func_getxattr" = x"yes" ; then
173         AC_CACHE_CHECK([whether xattr interface takes additional options], smb_attr_cv_xattr_add_opt, [
174                 old_LIBS=$LIBS
175                 LIBS="$LIBS $ACL_LIBS"
176                 AC_TRY_COMPILE([
177                         #include <sys/types.h>
178                         #if HAVE_ATTR_XATTR_H
179                         #include <attr/xattr.h>
180                         #elif HAVE_SYS_XATTR_H
181                         #include <sys/xattr.h>
182                         #endif
183                 ],[
184                         getxattr(0, 0, 0, 0, 0, 0);
185                 ],
186                 [smb_attr_cv_xattr_add_opt=yes],
187                 [smb_attr_cv_xattr_add_opt=no;LIBS=$old_LIBS])
188         ])
189         if test x"$smb_attr_cv_xattr_add_opt" = x"yes"; then
190                 AC_DEFINE(XATTR_ADD_OPT, 1, [xattr functions have additional options])
191         fi
192 fi
193
194 AC_CHECK_FUNCS(get_current_dir_name)
195 AC_HAVE_DECL(setresuid, [#include <unistd.h>])
196 AC_HAVE_DECL(setresgid, [#include <unistd.h>])
197 AC_HAVE_DECL(errno, [#include <errno.h>])
198
199 AC_CACHE_CHECK([for secure mkstemp],libreplace_cv_HAVE_SECURE_MKSTEMP,[
200 AC_TRY_RUN([#include <stdlib.h>
201 #include <sys/types.h>
202 #include <sys/stat.h>
203 #include <unistd.h>
204 main() { 
205   struct stat st;
206   char tpl[20]="/tmp/test.XXXXXX"; 
207   int fd = mkstemp(tpl); 
208   if (fd == -1) exit(1);
209   unlink(tpl);
210   if (fstat(fd, &st) != 0) exit(1);
211   if ((st.st_mode & 0777) != 0600) exit(1);
212   exit(0);
213 }],
214 libreplace_cv_HAVE_SECURE_MKSTEMP=yes,
215 libreplace_cv_HAVE_SECURE_MKSTEMP=no,
216 libreplace_cv_HAVE_SECURE_MKSTEMP=cross)])
217 if test x"$libreplace_cv_HAVE_SECURE_MKSTEMP" = x"yes"; then
218     AC_DEFINE(HAVE_SECURE_MKSTEMP,1,[Whether mkstemp is secure])
219 fi
220
221 dnl Provided by snprintf.c:
222 AC_CHECK_HEADERS(stdio.h strings.h)
223 AC_CHECK_DECLS([snprintf, vsnprintf, asprintf, vasprintf])
224 AC_CHECK_FUNCS(snprintf vsnprintf asprintf vasprintf)
225
226 AC_CACHE_CHECK([for C99 vsnprintf],libreplace_cv_HAVE_C99_VSNPRINTF,[
227 AC_TRY_RUN([
228 #include <sys/types.h>
229 #include <stdio.h>
230 #include <stdarg.h>
231 #include <stdlib.h>
232 void foo(const char *format, ...) { 
233        va_list ap;
234        int len;
235        char buf[20];
236        long long l = 1234567890;
237        l *= 100;
238
239        va_start(ap, format);
240        len = vsnprintf(buf, 0, format, ap);
241        va_end(ap);
242        if (len != 5) exit(1);
243
244        va_start(ap, format);
245        len = vsnprintf(0, 0, format, ap);
246        va_end(ap);
247        if (len != 5) exit(2);
248
249        if (snprintf(buf, 3, "hello") != 5 || strcmp(buf, "he") != 0) exit(3);
250
251        if (snprintf(buf, 20, "%lld", l) != 12 || strcmp(buf, "123456789000") != 0) exit(4);
252        if (snprintf(buf, 20, "%zu", 123456789) != 9 || strcmp(buf, "123456789") != 0) exit(5);
253        if (snprintf(buf, 20, "%2\$d %1\$d", 3, 4) != 3 || strcmp(buf, "4 3") != 0) exit(6);
254        if (snprintf(buf, 20, "%s", 0) < 3) exit(7);
255
256        exit(0);
257 }
258 main() { foo("hello"); }
259 ],
260 libreplace_cv_HAVE_C99_VSNPRINTF=yes,libreplace_cv_HAVE_C99_VSNPRINTF=no,libreplace_cv_HAVE_C99_VSNPRINTF=cross)])
261 if test x"$libreplace_cv_HAVE_C99_VSNPRINTF" = x"yes"; then
262     AC_DEFINE(HAVE_C99_VSNPRINTF,1,[Whether there is a C99 compliant vsnprintf])
263 fi
264
265
266 dnl VA_COPY
267 AC_CACHE_CHECK([for va_copy],libreplace_cv_HAVE_VA_COPY,[
268 AC_TRY_LINK([#include <stdarg.h>
269 va_list ap1,ap2;], [va_copy(ap1,ap2);],
270 libreplace_cv_HAVE_VA_COPY=yes,libreplace_cv_HAVE_VA_COPY=no)])
271 if test x"$libreplace_cv_HAVE_VA_COPY" = x"yes"; then
272     AC_DEFINE(HAVE_VA_COPY,1,[Whether va_copy() is available])
273 fi
274
275 if test x"$libreplace_cv_HAVE_VA_COPY" != x"yes"; then
276 AC_CACHE_CHECK([for __va_copy],libreplace_cv_HAVE___VA_COPY,[
277 AC_TRY_LINK([#include <stdarg.h>
278 va_list ap1,ap2;], [__va_copy(ap1,ap2);],
279 libreplace_cv_HAVE___VA_COPY=yes,libreplace_cv_HAVE___VA_COPY=no)])
280 if test x"$libreplace_cv_HAVE___VA_COPY" = x"yes"; then
281     AC_DEFINE(HAVE___VA_COPY,1,[Whether __va_copy() is available])
282 fi
283 fi
284
285 dnl __FUNCTION__ macro
286 AC_CACHE_CHECK([for __FUNCTION__ macro],libreplace_cv_HAVE_FUNCTION_MACRO,[
287 AC_TRY_COMPILE([#include <stdio.h>], [printf("%s\n", __FUNCTION__);],
288 libreplace_cv_HAVE_FUNCTION_MACRO=yes,libreplace_cv_HAVE_FUNCTION_MACRO=no)])
289 if test x"$libreplace_cv_HAVE_FUNCTION_MACRO" = x"yes"; then
290     AC_DEFINE(HAVE_FUNCTION_MACRO,1,[Whether there is a __FUNCTION__ macro])
291 else
292     dnl __func__ macro
293     AC_CACHE_CHECK([for __func__ macro],libreplace_cv_HAVE_func_MACRO,[
294     AC_TRY_COMPILE([#include <stdio.h>], [printf("%s\n", __func__);],
295     libreplace_cv_HAVE_func_MACRO=yes,libreplace_cv_HAVE_func_MACRO=no)])
296     if test x"$libreplace_cv_HAVE_func_MACRO" = x"yes"; then
297        AC_DEFINE(HAVE_func_MACRO,1,[Whether there is a __func__ macro])
298     fi
299 fi
300
301 AC_CHECK_HEADERS([sys/param.h limits.h])
302
303 AC_CHECK_TYPE(comparison_fn_t, 
304 [AC_DEFINE(HAVE_COMPARISON_FN_T, 1,[Whether or not we have comparison_fn_t])])
305
306 AC_HAVE_DECL(setenv, [#include <stdlib.h>])
307 AC_CHECK_FUNCS(setenv unsetenv)
308 AC_HAVE_DECL(environ, [#include <unistd.h>])
309
310 AC_CHECK_FUNCS(strnlen)
311 AC_CHECK_FUNCS(strtoull __strtoull strtouq strtoll __strtoll strtoq)
312
313 AC_CHECK_FUNCS(memmem)
314
315 # this test disabled as we don't actually need __VA_ARGS__ yet
316 AC_TRY_CPP([
317 #define eprintf(...) fprintf(stderr, __VA_ARGS__)
318 eprintf("bla", "bar");
319 ], AC_DEFINE(HAVE__VA_ARGS__MACRO, 1, [Whether the __VA_ARGS__ macro is available]))
320
321
322 AC_CACHE_CHECK([for sig_atomic_t type],libreplace_cv_sig_atomic_t, [
323     AC_TRY_COMPILE([
324 #include <sys/types.h>
325 #if STDC_HEADERS
326 #include <stdlib.h>
327 #include <stddef.h>
328 #endif
329 #include <signal.h>],[sig_atomic_t i = 0],
330         libreplace_cv_sig_atomic_t=yes,libreplace_cv_sig_atomic_t=no)])
331 if test x"$libreplace_cv_sig_atomic_t" = x"yes"; then
332    AC_DEFINE(HAVE_SIG_ATOMIC_T_TYPE,1,[Whether we have the atomic_t variable type])
333 fi
334
335
336 AC_CACHE_CHECK([for O_DIRECT flag to open(2)],libreplace_cv_HAVE_OPEN_O_DIRECT,[
337 AC_TRY_COMPILE([
338 #include <unistd.h>
339 #ifdef HAVE_FCNTL_H
340 #include <fcntl.h>
341 #endif],
342 [int fd = open("/dev/null", O_DIRECT);],
343 libreplace_cv_HAVE_OPEN_O_DIRECT=yes,libreplace_cv_HAVE_OPEN_O_DIRECT=no)])
344 if test x"$libreplace_cv_HAVE_OPEN_O_DIRECT" = x"yes"; then
345     AC_DEFINE(HAVE_OPEN_O_DIRECT,1,[Whether the open(2) accepts O_DIRECT])
346 fi
347
348
349 dnl Check if the C compiler understands volatile (it should, being ANSI).
350 AC_CACHE_CHECK([that the C compiler understands volatile],libreplace_cv_volatile, [
351         AC_TRY_COMPILE([#include <sys/types.h>],[volatile int i = 0],
352                 libreplace_cv_volatile=yes,libreplace_cv_volatile=no)])
353 if test x"$libreplace_cv_volatile" = x"yes"; then
354         AC_DEFINE(HAVE_VOLATILE, 1, [Whether the C compiler understands volatile])
355 fi
356
357 m4_include(system/config.m4)
358
359 m4_include(dlfcn.m4)
360 m4_include(getpass.m4)
361 m4_include(strptime.m4)
362 m4_include(win32.m4)
363 m4_include(timegm.m4)
364 m4_include(repdir.m4)
365 m4_include(crypt.m4)
366
367 if test x$libreplace_cv_have_clock_gettime = xyes ; then
368         SMB_CHECK_CLOCK_ID(CLOCK_MONOTONIC)
369         SMB_CHECK_CLOCK_ID(CLOCK_PROCESS_CPUTIME_ID)
370         SMB_CHECK_CLOCK_ID(CLOCK_REALTIME)
371 fi
372
373 AC_CACHE_CHECK([for struct timespec type],libreplace_cv_struct_timespec, [
374     AC_TRY_COMPILE([
375 #include <sys/types.h>
376 #if STDC_HEADERS
377 #include <stdlib.h>
378 #include <stddef.h>
379 #endif
380 #if TIME_WITH_SYS_TIME
381 # include <sys/time.h>
382 # include <time.h>
383 #else
384 # if HAVE_SYS_TIME_H
385 #  include <sys/time.h>
386 # else
387 #  include <time.h>
388 # endif
389 #endif
390 ],[struct timespec ts;],
391         libreplace_cv_struct_timespec=yes,libreplace_cv_struct_timespec=no)])
392 if test x"$libreplace_cv_struct_timespec" = x"yes"; then
393    AC_DEFINE(HAVE_STRUCT_TIMESPEC,1,[Whether we have struct timespec])
394 fi
395
396 AC_CHECK_FUNCS([printf memset memcpy],,[AC_MSG_ERROR([Required function not found])])
397
398 echo "LIBREPLACE_BROKEN_CHECKS: END"
399 ]) dnl end AC_LIBREPLACE_BROKEN_CHECKS
400
401 AC_DEFUN_ONCE(AC__LIBREPLACE_ALL_CHECKS_START,
402 [
403 #LIBREPLACE_ALL_CHECKS: START"
404 ])
405 AC_DEFUN_ONCE(AC__LIBREPLACE_ALL_CHECKS_END,
406 [
407 #LIBREPLACE_ALL_CHECKS: END"
408 ])
409 m4_define(AC_LIBREPLACE_ALL_CHECKS,
410 [
411 AC__LIBREPLACE_ALL_CHECKS_START
412 AC_LIBREPLACE_LOCATION_CHECKS
413 AC_LIBREPLACE_CC_CHECKS
414 AC_LIBREPLACE_BROKEN_CHECKS
415 AC__LIBREPLACE_ALL_CHECKS_END
416 CFLAGS="$CFLAGS -I$libreplacedir"
417 ])
418
419 m4_include(libreplace_cc.m4)
420 m4_include(libreplace_ld.m4)
421 m4_include(libreplace_network.m4)
422 m4_include(libreplace_macros.m4)
423
424
425 dnl SMB_CHECK_CLOCK_ID(clockid)
426 dnl Test whether the specified clock_gettime clock ID is available. If it
427 dnl is, we define HAVE_clockid
428 AC_DEFUN([SMB_CHECK_CLOCK_ID],
429 [
430     AC_MSG_CHECKING(for $1)
431     AC_TRY_LINK([
432 #if TIME_WITH_SYS_TIME
433 # include <sys/time.h>
434 # include <time.h>
435 #else
436 # if HAVE_SYS_TIME_H
437 #  include <sys/time.h>
438 # else
439 #  include <time.h>
440 # endif
441 #endif
442     ],
443     [
444 clockid_t clk = $1;
445     ],
446     [
447         AC_MSG_RESULT(yes)
448         AC_DEFINE(HAVE_$1, 1,
449             [Whether the clock_gettime clock ID $1 is available])
450     ],
451     [
452         AC_MSG_RESULT(no)
453     ])
454 ])
455 m4_ifndef([AC_USE_SYSTEM_EXTENSIONS],[m4_include(autoconf-2.60.m4)])