Skip to content

Commit 8dbd326

Browse files
committed
build: fix musl build errors
libgen.h must be included to silence a compiler error regarding the basename() function missing when building with musl libc. The basename() function call in libnvme/src/nvme/linux.c:__nvme_transport_handle_open_direct() causes a (fatal) compilation warning due to GNU using this declaration of basename: char *basename (const char *filename) and POSIX (and also musl) ommiting const. It is thus possible that the const char *devname would be modified by basename(). The solution to the problem above is to use the internal implementation of basename from glibc directly and avoid these API compatibility problems. Signed-off-by: Michal Rábek <[email protected]>
1 parent b048af7 commit 8dbd326

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

libnvme/src/nvme/linux.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <string.h>
1313
#include <errno.h>
1414

15+
#ifndef _GNU_SOURCE
16+
#include <libgen.h>
17+
#endif
18+
1519
#include <sys/param.h>
1620
#include <sys/stat.h>
1721
#include <fcntl.h>
@@ -83,7 +87,11 @@ static int __nvme_transport_handle_open_direct(struct nvme_transport_handle *hdl
8387
{
8488
struct nvme_passthru_cmd dummy = { 0 };
8589
_cleanup_free_ char *path = NULL;
86-
char *name = basename(devname);
90+
/* This used instead of basename() due to behavioral differences between
91+
* the POSIX and the GNU version. These two lines are the glibc implementation.
92+
* Original source: https://github.com/bminor/glibc/blob/master/string/basename.c */
93+
const char *name = strrchr(devname, '/');
94+
name = name ? name + 1 : devname;
8795
int ret, id, ns;
8896
bool c = true;
8997

0 commit comments

Comments
 (0)