1dnl iov-max.m4 -- Probe for the maximum number of iovecs accepted by writev.
2dnl $Id: iov-max.m4 10352 2019-10-31 01:37:59Z eagle $
3dnl
4dnl Check for the maximum number of elements in an iovec (IOV_MAX).  SVr4
5dnl systems appear to use that name for this limit (checked Solaris 2.6, IRIX
6dnl 6.5, and HP-UX 11.00).  Linux doesn't have it, but instead has UIO_MAXIOV
7dnl defined in <bits/uio.h> or <iovec.h> included from <sys/uio.h>.
8dnl
9dnl The platforms that have IOV_MAX appear to also offer it via sysconf(3),
10dnl but we don't currently handle dynamic values.
11dnl
12dnl If IOV_MAX is not defined by <sys/uio.h> or <limits.h>, probe for its
13dnl value by checking writev calls up to 1024 members of an iovec and set it
14dnl to an appropriate value.
15
16dnl Source used by INN_MACRO_IOV_MAX.
17define([_INN_MACRO_IOV_MAX_SOURCE],
18[AC_LANG_SOURCE([[
19#include <sys/types.h>
20#include <stdio.h>
21#include <sys/uio.h>
22#include <errno.h>
23#include <fcntl.h>
24#ifdef HAVE_UNISTD_H
25# include <unistd.h>
26#endif
27#ifdef HAVE_LIMITS_H
28# include <limits.h>
29#endif
30
31int
32main ()
33{
34  int fd, size;
35  struct iovec array[1024];
36  char data;
37
38  FILE *f = fopen ("conftestval", "w");
39  if (f == NULL)
40    return 1;
41#ifdef UIO_MAXIOV
42  fprintf (f, "%d\n", UIO_MAXIOV);
43#else
44  fd = open ("/dev/null", O_WRONLY, 0666);
45  if (fd < 0)
46    return 1;
47  for (size = 1; size <= 1024; size++)
48    {
49      array[size - 1].iov_base = &data;
50      array[size - 1].iov_len = sizeof data;
51      if (writev (fd, array, size) < 0)
52        {
53          if (errno != EINVAL)
54            return 1;
55          fprintf (f, "%d\n", size - 1);
56          exit (0);
57        }
58    }
59  fprintf (f, "1024\n");
60#endif /* UIO_MAXIOV */
61  return 0;
62}
63]])])
64
65dnl Headers to use for checking for an IOV_MAX definition.
66define([_INN_MACRO_IOV_MAX_HEADERS],
67[AC_INCLUDES_DEFAULT
68#ifdef HAVE_LIMITS_H
69# include <limits.h>
70#endif
71])
72
73dnl Do the actual check.
74AC_DEFUN([INN_MACRO_IOV_MAX],
75[AC_CHECK_DECL([IOV_MAX], [],
76    [AC_CACHE_CHECK([value of IOV_MAX],
77        [inn_cv_macro_iov_max],
78        [AC_RUN_IFELSE([_INN_MACRO_IOV_MAX_SOURCE],
79            inn_cv_macro_iov_max=`cat conftestval`,
80            inn_cv_macro_iov_max=error,
81            16)
82         AS_IF([test x"$inn_cv_macro_iov_max" = xerror],
83            [AC_MSG_WARN([probe failure, assuming 16])
84             inn_cv_macro_iov_max=16])])
85     AC_DEFINE_UNQUOTED([IOV_MAX], [$inn_cv_macro_iov_max],
86         [Define to the max vectors in an iovec.])],
87    [_INN_MACRO_IOV_MAX_HEADERS])])
88