Skip to contentSkip to conversation

Fix compilation errors when not using GNU libc

PATCH In review Luis Henriques 2 participants 8 messages Mar 4, 2025 – Mar 9, 2025

lore
Share & tools
lore
#0 lore raw

cover Fix compilation errors when not using GNU libc

Hi!

Following this patch I'm sending 2 simple patches that fix build failures when trying to compile fstests in a system that does not have GNU libc.

Luis Henriques (2):
src: include libgen.h for function basename() if not using GNU libc src/locktest: add missing struct cast with syscall getsockname()

 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/locktest.c                                       | 2 +-
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 7 files changed, 19 insertions(+), 1 deletion(-)
#1 lore raw

PATCH 1/2 src: include libgen.h for function basename() if not using GNU libc

Compiling fstests on a system without the GNU libc fails because the basename() is defined in libgen.h by POSIX and not in string.h.

Fix that by including the libgen.h header if __USE_GNU isn't defined.

---
 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 6 files changed, 18 insertions(+)
src/aio-dio-regress/aio-dio-append-write-read-race.c +3 -0
index d9f8982f007c..1a0c8e99c61f 100644
@@ -19,6 +19,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #include <libaio.h>
 #include <pthread.h>
src/splice-test.c +3 -0
index eb8636738064..e6753a15dc47 100644
@@ -18,6 +18,9 @@
 #include <string.h>
 #include <errno.h>
 #include <malloc.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 unsigned int sector_size;
 unsigned int buffer_size;
src/t_ext4_dax_inline_corruption.c +3 -0
index e1a39a6c1e46..a6cb768512fc 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_ext4_dax_journal_corruption.c +3 -0
index ba7a96e43559..4e5762d77058 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_mmap_collision.c +3 -0
index c872f4e26940..638424d2e619 100644
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void *dax_data;
 int nodax_fd;
src/t_mmap_dio.c +3 -0
index 6c8ca1a39181..342e2c6deb4b 100644
@@ -14,6 +14,9 @@
 #include <libaio.h>
 #include <errno.h>
 #include <sys/time.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void usage(char *prog)
 {
#2 lore raw

Re: [PATCH 1/2] src: include libgen.h for function basename() if not using GNU libc

Quoted 20 lines · Luis Henriques

Compiling fstests on a system without the GNU libc fails because the basename() is defined in libgen.h by POSIX and not in string.h.

Fix that by including the libgen.h header if __USE_GNU isn't defined.

---
 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 6 files changed, 18 insertions(+)
src/aio-dio-regress/aio-dio-append-write-read-race.c +0 -0
index d9f8982f007c..1a0c8e99c61f 100644

src/aio-dio-regress/aio-dio-append-write-read-race.c line 24

Quoted 6 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +3 -0
@@ -19,6 +19,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif

Do you use clang and musl to build fstests? I'm even not sure if fstests can be built with them :) I don't have experience on this part, but I don't mind letting fstests to be supported by other libc. The patch looks safe for current users.

If anyone has this experience, please feel free to review. I'll think about merging this patch if no objection from others.

Thanks,
Zorro

Quoted 74 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +0 -0
 #include <libaio.h>
 #include <pthread.h>
src/splice-test.c +3 -0
index eb8636738064..e6753a15dc47 100644
@@ -18,6 +18,9 @@
 #include <string.h>
 #include <errno.h>
 #include <malloc.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 unsigned int sector_size;
 unsigned int buffer_size;
src/t_ext4_dax_inline_corruption.c +3 -0
index e1a39a6c1e46..a6cb768512fc 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_ext4_dax_journal_corruption.c +3 -0
index ba7a96e43559..4e5762d77058 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_mmap_collision.c +3 -0
index c872f4e26940..638424d2e619 100644
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void *dax_data;
 int nodax_fd;
src/t_mmap_dio.c +3 -0
index 6c8ca1a39181..342e2c6deb4b 100644
@@ -14,6 +14,9 @@
 #include <libaio.h>
 #include <errno.h>
 #include <sys/time.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void usage(char *prog)
 {
#3 lore raw

Re: [PATCH 1/2] src: include libgen.h for function basename() if not using GNU libc

Quoted 16 lines · Zorro Lang
Quoted 15 lines · Luis Henriques

Compiling fstests on a system without the GNU libc fails because the basename() is defined in libgen.h by POSIX and not in string.h.

Fix that by including the libgen.h header if __USE_GNU isn't defined.

---
 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 6 files changed, 18 insertions(+)
Quoted 14 lines

src/aio-dio-regress/aio-dio-append-write-read-race.c line 24

Quoted 11 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +3 -0
index d9f8982f007c..1a0c8e99c61f 100644
@@ -19,6 +19,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif

Do you use clang and musl to build fstests? I'm even not sure if fstests can be built with them :)

Yes, I'm able to compile and run fstests using gcc (not clang) and musl. Though there's a hack I still need to use to compile, which is to define CFLAGS="-D_LARGEFILE64_SOURCE". Since that doesn't require changing anything, I haven't yet investigated how that could be fixed.

Quoted 5 lines

I don't have experience on this part, but I don't mind letting fstests to be supported by other libc. The patch looks safe for current users.

If anyone has this experience, please feel free to review. I'll think about merging this patch if no objection from others.

The only links I can provide you with some extra info are [1] (section about basename) and the basename(3) man page. Looking at it again makes me think that including libgen.h unconditionally should also be OK, forcing the POSIX implementation to be used instead. But using this #ifndef seems to be used in other projects.

And thanks, Zorro, for your review!

[1] https://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html

Cheers,

Signature · 1 line
Luís
Quoted 79 lines

Thanks,
Zorro

Quoted 74 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +0 -0
 #include <libaio.h>
 #include <pthread.h>
src/splice-test.c +3 -0
index eb8636738064..e6753a15dc47 100644
@@ -18,6 +18,9 @@
 #include <string.h>
 #include <errno.h>
 #include <malloc.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 unsigned int sector_size;
 unsigned int buffer_size;
src/t_ext4_dax_inline_corruption.c +3 -0
index e1a39a6c1e46..a6cb768512fc 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_ext4_dax_journal_corruption.c +3 -0
index ba7a96e43559..4e5762d77058 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_mmap_collision.c +3 -0
index c872f4e26940..638424d2e619 100644
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void *dax_data;
 int nodax_fd;
src/t_mmap_dio.c +3 -0
index 6c8ca1a39181..342e2c6deb4b 100644
@@ -14,6 +14,9 @@
 #include <libaio.h>
 #include <errno.h>
 #include <sys/time.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void usage(char *prog)
 {
#4 lore raw

Re: [PATCH 1/2] src: include libgen.h for function basename() if not using GNU libc

Hi Zorro,

Quoted 41 lines · Luis Henriques
Quoted 16 lines · Zorro Lang
Quoted 15 lines · Luis Henriques

Compiling fstests on a system without the GNU libc fails because the basename() is defined in libgen.h by POSIX and not in string.h.

Fix that by including the libgen.h header if __USE_GNU isn't defined.

---
 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 6 files changed, 18 insertions(+)
Quoted 14 lines

src/aio-dio-regress/aio-dio-append-write-read-race.c line 24

Quoted 11 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +3 -0
index d9f8982f007c..1a0c8e99c61f 100644
@@ -19,6 +19,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif

Do you use clang and musl to build fstests? I'm even not sure if fstests can be built with them :)

Yes, I'm able to compile and run fstests using gcc (not clang) and musl. Though there's a hack I still need to use to compile, which is to define CFLAGS="-D_LARGEFILE64_SOURCE". Since that doesn't require changing anything, I haven't yet investigated how that could be fixed.

Quoted 3 lines

I don't have experience on this part, but I don't mind letting fstests to be supported by other libc. The patch looks safe for current users.

Quoted 9 lines
Quoted 2 lines

If anyone has this experience, please feel free to review. I'll think about merging this patch if no objection from others.

The only links I can provide you with some extra info are [1] (section about basename) and the basename(3) man page. Looking at it again makes me think that including libgen.h unconditionally should also be OK, forcing the POSIX implementation to be used instead. But using this #ifndef seems to be used in other projects.

Please hold on this patch as I think some of the changes are broken. The POSIX basename() is a bit different from the GNU version, and needs to be handled with a bit more care. I'll resend this later in a few days.

(But feel free to pick the second patch, as I believe it's still a valid fix.)

Cheers,

Signature · 1 line
Luís
Quoted 88 lines

And thanks, Zorro, for your review!

[1] https://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html

Cheers,

Signature · 1 line
Luís
Quoted 79 lines

Thanks,
Zorro

Quoted 74 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +0 -0
 #include <libaio.h>
 #include <pthread.h>
src/splice-test.c +3 -0
index eb8636738064..e6753a15dc47 100644
@@ -18,6 +18,9 @@
 #include <string.h>
 #include <errno.h>
 #include <malloc.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 unsigned int sector_size;
 unsigned int buffer_size;
src/t_ext4_dax_inline_corruption.c +3 -0
index e1a39a6c1e46..a6cb768512fc 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_ext4_dax_journal_corruption.c +3 -0
index ba7a96e43559..4e5762d77058 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_mmap_collision.c +3 -0
index c872f4e26940..638424d2e619 100644
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void *dax_data;
 int nodax_fd;
src/t_mmap_dio.c +3 -0
index 6c8ca1a39181..342e2c6deb4b 100644
@@ -14,6 +14,9 @@
 #include <libaio.h>
 #include <errno.h>
 #include <sys/time.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void usage(char *prog)
 {
#5 lore raw

Re: [PATCH 1/2] src: include libgen.h for function basename() if not using GNU libc

Quoted 55 lines · Luis Henriques

Hi Zorro,

Quoted 41 lines · Luis Henriques
Quoted 16 lines · Zorro Lang
Quoted 15 lines · Luis Henriques

Compiling fstests on a system without the GNU libc fails because the basename() is defined in libgen.h by POSIX and not in string.h.

Fix that by including the libgen.h header if __USE_GNU isn't defined.

---
 src/aio-dio-regress/aio-dio-append-write-read-race.c | 3 +++
 src/splice-test.c                                    | 3 +++
 src/t_ext4_dax_inline_corruption.c                   | 3 +++
 src/t_ext4_dax_journal_corruption.c                  | 3 +++
 src/t_mmap_collision.c                               | 3 +++
 src/t_mmap_dio.c                                     | 3 +++
 6 files changed, 18 insertions(+)
Quoted 14 lines

src/aio-dio-regress/aio-dio-append-write-read-race.c line 24

Quoted 11 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +3 -0
index d9f8982f007c..1a0c8e99c61f 100644
@@ -19,6 +19,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <errno.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif

Do you use clang and musl to build fstests? I'm even not sure if fstests can be built with them :)

Yes, I'm able to compile and run fstests using gcc (not clang) and musl. Though there's a hack I still need to use to compile, which is to define CFLAGS="-D_LARGEFILE64_SOURCE". Since that doesn't require changing anything, I haven't yet investigated how that could be fixed.

Quoted 3 lines

I don't have experience on this part, but I don't mind letting fstests to be supported by other libc. The patch looks safe for current users.

Quoted 9 lines
Quoted 2 lines

If anyone has this experience, please feel free to review. I'll think about merging this patch if no objection from others.

The only links I can provide you with some extra info are [1] (section about basename) and the basename(3) man page. Looking at it again makes me think that including libgen.h unconditionally should also be OK, forcing the POSIX implementation to be used instead. But using this #ifndef seems to be used in other projects.

Quoted 3 lines

Please hold on this patch as I think some of the changes are broken. The POSIX basename() is a bit different from the GNU version, and needs to be handled with a bit more care. I'll resend this later in a few days.

Thanks Luis, breaking the building with musl is not a big issue, but please never break the current glibc build :)

Quoted 3 lines

(But feel free to pick the second patch, as I believe it's still a valid fix.)

Today I'll make a fstests release, your second patch will be in the next release (after today). You can find it in patches-in-queue branch soon. Feel free to resend the whole patchset or resend this single one.

Thanks,
Zorro

Quoted 94 lines

Cheers,

Signature · 1 line
Luís
Quoted 88 lines

And thanks, Zorro, for your review!

[1] https://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html

Cheers,

Signature · 1 line
Luís
Quoted 79 lines

Thanks,
Zorro

Quoted 74 lines
src/aio-dio-regress/aio-dio-append-write-read-race.c +0 -0
 #include <libaio.h>
 #include <pthread.h>
src/splice-test.c +3 -0
index eb8636738064..e6753a15dc47 100644
@@ -18,6 +18,9 @@
 #include <string.h>
 #include <errno.h>
 #include <malloc.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 unsigned int sector_size;
 unsigned int buffer_size;
src/t_ext4_dax_inline_corruption.c +3 -0
index e1a39a6c1e46..a6cb768512fc 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_ext4_dax_journal_corruption.c +3 -0
index ba7a96e43559..4e5762d77058 100644
@@ -10,6 +10,9 @@
 #include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 #define PAGE(a) ((a)*0x1000)
 #define STRLEN 256
src/t_mmap_collision.c +3 -0
index c872f4e26940..638424d2e619 100644
@@ -24,6 +24,9 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void *dax_data;
 int nodax_fd;
src/t_mmap_dio.c +3 -0
index 6c8ca1a39181..342e2c6deb4b 100644
@@ -14,6 +14,9 @@
 #include <libaio.h>
 #include <errno.h>
 #include <sys/time.h>
+#ifndef __USE_GNU
+#include <libgen.h>
+#endif
 
 void usage(char *prog)
 {
#6 lore raw

PATCH 2/2 src/locktest: add missing struct cast with syscall getsockname()

In reply to Luis Henriques

The usage of variable 'myAddr' (struct sockaddr_in) is being casted into a 'struct sockaddr' when used with bind() and with connect(). This patch adds that cast when used with getsockname() as well, otherwise we'll get the following error:

locktest.c: In function 'main':
locktest.c:1155:39: error: passing argument 2 of 'getsockname' from incompatible pointer type [-Wincompatible-pointer-types]

 1155 |                 if (getsockname(s_fd, &myAddr, &addr_len)) {
      |                                       ^~~~~~~
      |                                       |
      |                                       struct sockaddr_in *
In file included from /usr/include/fortify/sys/socket.h:23,
                 from locktest.c:19:

/usr/include/sys/socket.h:391:23: note: expected 'struct sockaddr * restrict' but argument is of type 'struct sockaddr_in *'
391 | int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
| ^

---
 src/locktest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/locktest.c +1 -1
index 0e7c3008d1d0..a6cf3b1d5a99 100644
@@ -1152,7 +1152,7 @@ main(int argc, char *argv[])
 	if (port == 0) {
 	        socklen_t addr_len = sizeof(myAddr);
 
-		if (getsockname(s_fd, &myAddr, &addr_len)) {
+		if (getsockname(s_fd, (struct sockaddr *)&myAddr, &addr_len)) {
 		    perror("getsockname");
 		    exit(1);
 		}
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] src/locktest: add missing struct cast with syscall getsockname()

Quoted 13 lines · Luis Henriques

The usage of variable 'myAddr' (struct sockaddr_in) is being casted into a 'struct sockaddr' when used with bind() and with connect(). This patch adds that cast when used with getsockname() as well, otherwise we'll get the following error:

locktest.c: In function 'main':
locktest.c:1155:39: error: passing argument 2 of 'getsockname' from incompatible pointer type [-Wincompatible-pointer-types]

 1155 |                 if (getsockname(s_fd, &myAddr, &addr_len)) {
      |                                       ^~~~~~~
      |                                       |
      |                                       struct sockaddr_in *
In file included from /usr/include/fortify/sys/socket.h:23,
                 from locktest.c:19:
Quoted 6 lines

/usr/include/sys/socket.h:391:23: note: expected 'struct sockaddr * restrict' but argument is of type 'struct sockaddr_in *'
391 | int getsockname (int, struct sockaddr *__restrict, socklen_t *__restrict);
| ^

---

From the manual of getsockname:

int getsockname(int sockfd, struct sockaddr *restrict addr,
socklen_t *restrict addrlen);

looks like the 2nd argument is "struct sockaddr *" in GNU libc too. So I think this patch is good to me.

Quoted 17 lines
 src/locktest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/locktest.c +1 -1
index 0e7c3008d1d0..a6cf3b1d5a99 100644
@@ -1152,7 +1152,7 @@ main(int argc, char *argv[])
 	if (port == 0) {
 	        socklen_t addr_len = sizeof(myAddr);
 
-		if (getsockname(s_fd, &myAddr, &addr_len)) {
+		if (getsockname(s_fd, (struct sockaddr *)&myAddr, &addr_len)) {
 		    perror("getsockname");
 		    exit(1);
 		}