Skip to content

Commit c6f4c26

Browse files
authored
Check major, minor and makedev with Autoconf's AC_HEADER_MAJOR (php#13706)
The non-standard major(), minor(), and makedev() can be defined as macros. These are usually used together with the Autoconf macro AC_HEADER_MAJOR, which defines the MAJOR_IN_MKDEV if sys/mkdev.h is available, or MAJOR_IN_SYSMACROS if sys/sysmacros.h is available. On Solaris/illumos they are in the sys/mkdev.h header (macro defined to libc implementation) and in sys/sysmacros.h (macro defined with binary operators and bits shifting). On systems with musl and glibc 2.28 or later they are defined in sys/sysmacros.h, in glibc 2.27 and earlier they were in sys/types.h. On BSD-based systems and macOS they are in the sys/types.h. Autoconf 2.70 has fixed the AC_HEADER_MAJOR macro, so it detects the headers properly due to glibc 2.25 throwing deprecation warnings when using the macros from sys/types.h. With Autoconf 2.69 and earlier the ac_cv_header_sys_types_h_makedev cache variable can skip the improper sys/types.h check in the macro. This change syncs the usage within the ext/fileinfo/libmagic bundled library and ext/posix. When sys/mkdev.h header is available, code includes that, otherwise it conditionally includes the sys/sysmacros.h. The ext/posix has additional check whether linker sees the makedev, otherwise it checks if makedev is declared within the given set of headers accoring to the AC_HEADER_MAJOR logic. Previously the AC_CHECK_FUNCS didn't detect it.
1 parent e1181a6 commit c6f4c26

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

configure.ac

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ if test "$GCC" = "yes"; then
454454
PHP_BROKEN_GCC_STRLEN_OPT
455455
fi
456456

457+
dnl Detect the headers required to use makedev, major, and minor.
458+
dnl Autoconf <= 2.69 didn't check glibc 2.25 deprecated macros in sys/types.h.
459+
m4_version_prereq([2.70],,[ac_cv_header_sys_types_h_makedev=no])
460+
AC_HEADER_MAJOR
461+
457462
dnl Checks for typedefs, structures, and compiler characteristics.
458463
dnl ----------------------------------------------------------------------------
459464

ext/fileinfo/config.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ if test "$PHP_FILEINFO" != "no"; then
1414
libmagic/readcdf.c libmagic/softmagic.c libmagic/der.c \
1515
libmagic/buffer.c libmagic/is_csv.c"
1616

17+
AC_CHECK_HEADERS([sys/sysmacros.h])
18+
1719
AC_CHECK_FUNCS([strcasestr],,[
1820
AC_MSG_NOTICE(using libmagic strcasestr implementation)
1921
libmagic_sources="$libmagic_sources libmagic/strcasestr.c"

ext/posix/config.m4

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ if test "$PHP_POSIX" = "yes"; then
88
AC_DEFINE(HAVE_POSIX, 1, [whether to include POSIX-like functions])
99
PHP_NEW_EXTENSION(posix, posix.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
1010

11-
AC_CHECK_HEADERS([sys/mkdev.h sys/sysmacros.h])
12-
13-
AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups makedev initgroups getgrgid_r eaccess)
11+
AC_CHECK_FUNCS(seteuid setegid setsid getsid getpgid ctermid mkfifo mknod setrlimit getrlimit getgroups initgroups getgrgid_r eaccess)
12+
13+
dnl Check for makedev. If it's defined as a macro, AC_CHECK_FUNCS won't work.
14+
dnl Required headers are included by the AC_HEADER_MAJOR logic.
15+
AC_CHECK_FUNCS([makedev],,
16+
[AC_CHECK_DECL([makedev], [AC_DEFINE([HAVE_MAKEDEV], [1])],, [
17+
#include <sys/types.h>
18+
#ifdef MAJOR_IN_MKDEV
19+
# include <sys/mkdev.h>
20+
#elif defined(MAJOR_IN_SYSMACROS)
21+
# include <sys/sysmacros.h>
22+
#endif
23+
])])
1424

1525
dnl Skip pathconf and fpathconf check on musl libc due to limited implementation
1626
dnl (first argument is not validated and has different error).

ext/posix/posix.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@
3939
#include <errno.h>
4040
#include <grp.h>
4141
#include <pwd.h>
42-
#ifdef HAVE_SYS_MKDEV_H
42+
#ifdef MAJOR_IN_MKDEV
4343
# include <sys/mkdev.h>
44-
#endif
45-
#ifdef HAVE_SYS_SYSMACROS_H
44+
#elif defined(MAJOR_IN_SYSMACROS)
4645
# include <sys/sysmacros.h>
4746
#endif
4847

@@ -620,7 +619,7 @@ PHP_FUNCTION(posix_mknod)
620619
zend_argument_value_error(3, "cannot be 0 for the POSIX_S_IFCHR and POSIX_S_IFBLK modes");
621620
RETURN_THROWS();
622621
} else {
623-
#if defined(HAVE_MAKEDEV) || defined(makedev)
622+
#ifdef HAVE_MAKEDEV
624623
php_dev = makedev(major, minor);
625624
#else
626625
php_error_docref(NULL, E_WARNING, "Cannot create a block or character device, creating a normal file instead");

0 commit comments

Comments
 (0)