Skip to contentSkip to conversation

Improved libmount configure checks

PATCH Applied Markus Mayer 2 participants 4 messages Aug 1, 2023 – Aug 3, 2023 Util-Linux package

lore
Share & tools
lore
#0 lore raw

cover Improved libmount configure checks

To increase compatibility and prevent avoidable build errors, add two explicit configure checks that allow libmount to successfully build in more diverse environments rather than producing compiler errors in those situations.

This applies to:
- cross compiling
- using older kernel headers (e.g. 4.9)
- using alternate C libraries (e.g. musl)

Markus Mayer (2):
libmount: check for linux/mount.h
libmount: check for struct statx

 configure.ac              | 9 +++++++++
 include/fileutils.h       | 2 +-
 include/mount-api-utils.h | 4 ++--
 libmount/src/hook_idmap.c | 4 ++--
 libmount/src/hooks.c      | 2 +-
 libmount/src/utils.c      | 2 +-
 libmount/src/version.c    | 2 +-
 7 files changed, 17 insertions(+), 8 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/2 libmount: check for linux/mount.h

Check that linux/mount.h is present before using features that rely on it.

It is necessary to check for the presence of linux/mount.h explicitly. Simply relying on the presence of several syscalls (e.g. SYS_move_mount, SYS_open_tree) does not work reliably, at least not in a cross-compiling environment.

The syscall definitions are provided by the C library. Meanwhile, linux/mount.h is provided by the kernel. This opens the possibility for discrepancies.

A problem arises if the C library (e.g. musl-1.2.3) defines all "mount fd API" syscalls (and HAVE_MOUNTFD_API becomes true), but the kernel headers are old enough to not provide linux/mount.h. The resulting error looks as follows. This example is using an LLVM-13 cross-compiler from x86_64 to aarch64 with musl-1.2.3 as the C library.

CC libmount/src/la-hooks.lo
In file included from libmount/src/hooks.c:30:
./include/mount-api-utils.h:11:10: fatal error: 'linux/mount.h' file not found

         ^~~~~~~~~~~~~~~

1 error generated.
make[4]: *** [Makefile:11185: libmount/src/la-hooks.lo] Error 1

To prevent this condition, we add a check to configure that will test for the presence of linux/mount.h in addition to testing for the mount fd API. Only if both conditions are met can we actually use the mount fd API.

---
 configure.ac              | 8 ++++++++
 include/mount-api-utils.h | 4 ++--
 libmount/src/hook_idmap.c | 4 ++--
 libmount/src/hooks.c      | 2 +-
 libmount/src/version.c    | 2 +-
 5 files changed, 14 insertions(+), 6 deletions(-)
configure.ac +8 -0
index 11b2e155dd2d..dd582f2a4bd1 100644
@@ -389,6 +389,12 @@ AC_CHECK_HEADERS([linux/fs.h ], [], [],
   #endif
 ])
 
+AC_CHECK_HEADERS([linux/mount.h ], [], [],
+ [#ifdef HAVE_LINUX_MOUNT_H
+   # include <linux/mount.h>
+   #endif
+])
+
 AC_CHECK_HEADERS([linux/gsmmux.h ], [], [],
  [#ifdef LINUX_GSMMUX_H
    # include <linux/gsmmux.h>
@@ -479,6 +485,7 @@ have_linux_blkzoned_h=$ac_cv_header_linux_blkzoned_h
 have_linux_btrfs_h=$ac_cv_header_linux_btrfs_h
 have_linux_capability_h=$ac_cv_header_linux_capability_h
 have_linux_kcmp_h=$ac_cv_header_linux_kcmp_h
+have_linux_mount_h=$ac_cv_header_linux_mount_h
 have_linux_pr_h=$ac_cv_header_linux_pr_h
 have_linux_raw_h=$ac_cv_header_linux_raw_h
 have_linux_securebits_h=$ac_cv_header_linux_securebits_h
@@ -1247,6 +1254,7 @@ UL_BUILD_INIT([libmount_mountfd_support])
 UL_REQUIRES_BUILD([libmount_mountfd_support], [libmount])
 UL_REQUIRES_LINUX([libmount_mountfd_support])
 UL_REQUIRES_HAVE([libmount_mountfd_support], [mountfd_api], [mount FDs based API])
+UL_REQUIRES_HAVE([libmount_mountfd_support], [linux_mount_h], [linux/mount.h])
 AS_IF([test "x$build_libmount_mountfd_support" = xyes ], [
   AC_DEFINE([USE_LIBMOUNT_MOUNTFD_SUPPORT], [1], [Enable support for new mount FD based kernel API])
 ])
include/mount-api-utils.h +2 -2
index 9044e1cd9d61..8ae546b7ed9f 100644
@@ -5,7 +5,7 @@
 #ifndef UTIL_LINUX_MOUNT_API_UTILS
 #define UTIL_LINUX_MOUNT_API_UTILS
 
-#ifdef HAVE_MOUNTFD_API
+#if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H)
 
 #include <sys/syscall.h>
 #include <linux/mount.h>
@@ -203,6 +203,6 @@ static inline int fspick(int dfd, const char *pathname, unsigned int flags)
 }
 #endif
 
-#endif /* HAVE_MOUNTFD_API */
+#endif /* HAVE_MOUNTFD_API && HAVE_LINUX_MOUNT_H */
 #endif /* UTIL_LINUX_MOUNT_API_UTILS */
 
libmount/src/hook_idmap.c +2 -2
index b3e1d82d4b97..9b2425a77def 100644
@@ -33,7 +33,7 @@
 # include <linux/nsfs.h>
 #endif
 
-#ifdef HAVE_MOUNTFD_API
+#if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H)
 
 typedef enum idmap_type_t {
 	ID_TYPE_UID,	/* uidmap entry */
@@ -518,4 +518,4 @@ const struct libmnt_hookset hookset_idmap =
 	.deinit = hookset_deinit
 };
 
-#endif /* HAVE_MOUNTFD_API */
+#endif /* HAVE_MOUNTFD_API && HAVE_LINUX_MOUNT_H */
libmount/src/hooks.c +1 -1
index 88cf68d61110..affa162d3445 100644
@@ -46,7 +46,7 @@ static const struct libmnt_hookset *hooksets[] =
 	&hookset_mount,
 #endif
 	&hookset_mount_legacy,
-#ifdef HAVE_MOUNTFD_API
+#if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H)
 	&hookset_idmap,
 #endif
 	&hookset_owner
libmount/src/version.c +1 -1
index 948b1a01de98..8e5f165f4920 100644
@@ -38,7 +38,7 @@ static const char *lib_features[] = {
 #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES
 	"namespaces",
 #endif
-#ifdef HAVE_MOUNTFD_API
+#if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H)
 	"idmapping",
 #endif
 #ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT
Signature · 1 line
2.40.1
#2 lore raw

PATCH 2/2 libmount: check for struct statx

In reply to Markus Mayer

Let's ensure struct statx exists before we try to use it. Checking for the existence of linux/stat.h is not sufficient. This is because [uapi/]linux/stat.h has existed since Linux 3.7, however struct statx was only introduced with Linux 4.11.

The problem arises if one happens ot be using kernel headers from within the aforementioned range, such as Linux 4.9.

CC libmount/src/la-utils.lo
In file included from libmount/src/utils.c:31:
./include/fileutils.h:100:33: warning: declaration of 'struct statx' will not be visible outside of this function [-Wvisibility]
unsigned int mask, struct statx *stx)
^
libmount/src/utils.c:117:16: error: variable has incomplete type 'struct statx'

                struct statx stx = { 0 };
                             ^
libmount/src/utils.c:117:10: note: forward declaration of 'struct statx'
                struct statx stx = { 0 };
                       ^
libmount/src/utils.c:125:5: error: use of undeclared identifier 'STATX_TYPE'
                                STATX_TYPE
                                ^
libmount/src/utils.c:126:8: error: use of undeclared identifier 'STATX_MODE'
                                        | STATX_MODE
                                          ^
libmount/src/utils.c:127:8: error: use of undeclared identifier 'STATX_INO'
                                        | STATX_INO,
                                          ^

1 warning and 4 errors generated.
make[4]: *** [Makefile:11269: libmount/src/la-utils.lo] Error 1

Checking for the presence of struct statx explicitly avoids this problem.

---
 configure.ac         | 1 +
 include/fileutils.h  | 2 +-
 libmount/src/utils.c | 2 +-
 3 files changed, 3 insertions(+), 2 deletions(-)
configure.ac +1 -0
index dd582f2a4bd1..a3cf330b5ec3 100644
@@ -518,6 +518,7 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
 ])
 
 AC_CHECK_TYPES([struct mount_attr], [], [], [[#include <linux/mount.h>]])
+AC_CHECK_TYPES([struct statx], [], [], [[#include <linux/stat.h>]])
 AC_CHECK_TYPES([enum fsconfig_command], [], [], [[#include <linux/mount.h>]])
 
 AC_CHECK_MEMBERS([struct termios.c_line],,,
include/fileutils.h +1 -1
index 13d2bdf0c6e5..a5fe517266f5 100644
@@ -94,7 +94,7 @@ static inline int close_range(unsigned int first, unsigned int last, int flags)
 #  define HAVE_CLOSE_RANGE 1
 # endif	/* SYS_close_range */
 
-# if !defined(HAVE_STATX) && defined(SYS_statx) && defined(HAVE_LINUX_STAT_H)
+# if !defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(SYS_statx) && defined(HAVE_LINUX_STAT_H)
 #  include <linux/stat.h>
 static inline int statx(int fd, const char *restrict path, int flags,
 		    unsigned int mask, struct statx *stx)
libmount/src/utils.c +1 -1
index 50553f1d5a14..e550b2d079e8 100644
@@ -111,7 +111,7 @@ static int safe_stat(const char *target, struct stat *st, int nofollow)
 
 	memset(st, 0, sizeof(struct stat));
 
-#ifdef AT_STATX_DONT_SYNC
+#if defined(AT_STATX_DONT_SYNC) && defined (HAVE_STRUCT_STATX)
 	{
 		int rc;
 		struct statx stx = { 0 };
Signature · 1 line
2.40.1
maintainer Applied
#3 lore raw
In reply to Markus Mayer
Quoted 3 lines · Markus Mayer

Markus Mayer (2):
libmount: check for linux/mount.h
libmount: check for struct statx

Applied, thanks!

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com