From 42cd8d284bdd8e4b5b6a7b76888e27bda2c4e686 Mon Sep 17 00:00:00 2001 From: Stargirl-chan Date: Fri, 29 Dec 2023 10:44:40 +0100 Subject: [PATCH] Implement custom basename function (basenm) There exist two possible basename functions, a xpg compliant one in libgen.h and a GLIBC exclusive one declared in string.h, that was previously also declared by musl libc. Both implementations are expecting different parameter types (`const char *` for GLIBC and `char *` for xpg) With the removal of the basename function from string.h in musl libc, we could only rely on the xpg implementation. Unfortunately, the xpg implementation of basename does modify the contents of whatever you put in it, even though that there really is no need for it. This is an issue in some cases, where we might want to get the basename of a read-only variable, e.g. a `const char *`, as trying to modify something read-only is undefined behavior. So in order to keep things consistent for us, we implement our own version of basename called `basenm`, that does not modify the passed argument. --- plugins/modprobe.c | 5 ++--- src/conf.c | 3 +-- src/mdadm.c | 4 ++-- src/plugin.c | 14 ++++++-------- src/tmpfiles.c | 2 +- src/util.c | 18 ++++++++++++++++++ src/util.h | 1 + 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/plugins/modprobe.c b/plugins/modprobe.c index 16f08dc7..24e9f056 100644 --- a/plugins/modprobe.c +++ b/plugins/modprobe.c @@ -132,11 +132,10 @@ static FILE *maybe_fopen_alias(const char *file, const char *path) { const char *basename; - basename = rindex(path, '/'); - if (!basename) + basename = basenm(path); + if (!strcmp(basename, path)) return NULL; - basename++; if (strcmp(file, basename)) return NULL; diff --git a/src/conf.c b/src/conf.c index 167d753b..c912ac07 100644 --- a/src/conf.c +++ b/src/conf.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #ifdef _LIBITE_LITE @@ -1312,7 +1311,7 @@ int conf_reload(void) if (strncmp(path, FINIT_SYSPATH_, strlen(FINIT_SYSPATH_)) && strncmp(path, FINIT_RUNPATH_, strlen(FINIT_RUNPATH_))) continue; - if (strcmp(basename(path), basename(gl.gl_pathv[j]))) + if (strcmp(basenm(path), basenm(gl.gl_pathv[j]))) continue; path = NULL; /* replacement later in list, skip this */ break; diff --git a/src/mdadm.c b/src/mdadm.c index d8bb3c53..b95d9c38 100644 --- a/src/mdadm.c +++ b/src/mdadm.c @@ -26,9 +26,9 @@ #include #include #include -#include #include "helpers.h" +#include "util.h" static glob_t *get_arrays(void) @@ -59,7 +59,7 @@ void mdadm_wait(void) for (i = 0; i < gl->gl_pathc; i++) { char *array; - array = basename(gl->gl_pathv[i]); + array = basenm(gl->gl_pathv[i]); snprintf(cmd, sizeof(cmd), "mdadm --wait-clean /dev/%s >/dev/null", array); run_interactive(cmd, "Marking MD array %s as clean", array); } diff --git a/src/plugin.c b/src/plugin.c index cb459336..957f47c3 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -28,7 +28,6 @@ #include /* readdir() et al */ #include #include -#include #ifdef _LIBITE_LITE # include # include /* BSD sys/queue.h API */ @@ -44,6 +43,7 @@ #include "private.h" #include "service.h" #include "sig.h" +#include "util.h" #define is_io_plugin(p) ((p)->io.cb && (p)->io.fd > 0) #define SEARCH_PLUGIN(str) \ @@ -87,11 +87,9 @@ int plugin_register(plugin_t *plugin) if (!plugin->name) { #ifndef ENABLE_STATIC Dl_info info; - char *dli_fname = strdup(info.dli_fname); - if (dladdr(plugin, &info) && dli_fname) - plugin->name = basename(dli_fname); - free(dli_fname); + if (dladdr(plugin, &info) && info.dli_fname) + plugin->name = basenm(info.dli_fname); #endif if (!plugin->name) plugin->name = "unknown"; @@ -256,7 +254,7 @@ void plugin_run_hook(hook_point_t no, void *arg) PLUGIN_ITERATOR(p, tmp) { if (p->hook[no].cb) { - dbg("Calling %s hook n:o %d (arg: %p) ...", basename(p->name), no, arg ?: "NIL"); + dbg("Calling %s hook n:o %d (arg: %p) ...", basenm(p->name), no, arg ?: "NIL"); p->hook[no].cb(arg ? arg : p->hook[no].arg); } } @@ -304,9 +302,9 @@ int plugin_io_init(plugin_t *p) if (!is_io_plugin(p)) return 0; - dbg("Initializing plugin %s for I/O", basename(p->name)); + dbg("Initializing plugin %s for I/O", basenm(p->name)); if (uev_io_init(ctx, &p->watcher, generic_io_cb, p, p->io.fd, p->io.flags)) { - warn("Failed setting up I/O plugin %s", basename(p->name)); + warn("Failed setting up I/O plugin %s", basenm(p->name)); return 1; } diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 69c70148..f9472285 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -492,7 +492,7 @@ void tmpfilesd(void) /* check for overrides */ for (j = i + 1; j < gl.gl_pathc; j++) { - if (strcmp(basename(fn), basename(gl.gl_pathv[j]))) + if (strcmp(basenm(fn), basenm(gl.gl_pathv[j]))) continue; fn = NULL; break; diff --git a/src/util.c b/src/util.c index afc9e9f7..8d7c8f63 100644 --- a/src/util.c +++ b/src/util.c @@ -151,6 +151,24 @@ char *progname(char *arg0) return prognm; } +/** + * basenm - Our custom basename implementation + * @path: The path to parse + * + * Returns either the parsed basename, or + * a copy of the given path +*/ +char *basenm(const char *path) { + char *basename; + + basename = rindex(path, '/'); + if (!basename) + return (char *) path; + + basename++; + return basename; +} + char *str(char *fmt, ...) { static char buf[32]; diff --git a/src/util.h b/src/util.h index c999beac..05eecc48 100644 --- a/src/util.h +++ b/src/util.h @@ -55,6 +55,7 @@ extern char *prognm; #endif char *progname (char *arg0); +char *basenm (const char *path); char *str (char *fmt, ...) __attribute__ ((format (printf, 1, 2))); int fnread (char *buf, size_t len, char *fmt, ...) __attribute__ ((format (printf, 3, 4)));