Skip to content

Commit 08d7878

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 08d7878

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

libnvme/src/nvme/linux.c

Lines changed: 5 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,7 @@ 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+
const char *name = nvme_basename(devname);
8791
int ret, id, ns;
8892
bool c = true;
8993

libnvme/src/nvme/util.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,3 +1179,12 @@ const struct ifaddrs *nvme_getifaddrs(struct nvme_global_ctx *ctx)
11791179

11801180
return ctx->ifaddrs_cache;
11811181
}
1182+
1183+
/* This used instead of basename() due to behavioral differences between
1184+
* the POSIX and the GNU version. This is the glibc implementation.
1185+
* Original source: https://github.com/bminor/glibc/blob/master/string/basename.c */
1186+
char *nvme_basename(const char *path)
1187+
{
1188+
char *p = strrchr (path, '/');
1189+
return p ? p + 1 : (char *) path;
1190+
}

libnvme/src/nvme/util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,4 +785,12 @@ const char *nvme_iface_matching_addr(const struct ifaddrs *iface_list, const cha
785785
*/
786786
bool nvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, const char *iface, const char *addr);
787787

788+
/**
789+
* nvme_basename - Return the final path component (the one after the last '/')
790+
* @path: A string containing a filesystem path
791+
*
792+
* Return: A pointer into the original null-terminated path string.
793+
*/
794+
char *nvme_basename(const char *path);
795+
788796
#endif /* _LIBNVME_UTIL_H */

0 commit comments

Comments
 (0)