diff options
author | Dan McGee <dan@archlinux.org> | 2012-02-03 15:01:34 -0600 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2012-02-06 22:18:08 -0600 |
commit | 2c6198e19b9fb6c07fddedae9d161354d0ad9900 (patch) | |
tree | 40c71d9a559d4d00b0535ee5b39af626111679a1 | |
parent | 9b4f47c7b20168989b6f05660927b4e8c71eb9e9 (diff) | |
download | kmod-2c6198e19b9fb6c07fddedae9d161354d0ad9900.tar.gz kmod-2c6198e19b9fb6c07fddedae9d161354d0ad9900.zip |
Use realpath() to canonicalize provided paths
The existing function choked in several corner cases:
* '/tmp/../tmp' was seen as absolute, so not cleaned up.
* '/tmp/' and '/tmp' were not equal, causing depmod to act differently
when called with the -b option for the two paths.
Don't reinvent the wheel; just use the standard library function.
Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r-- | libkmod/libkmod-module.c | 6 | ||||
-rw-r--r-- | libkmod/libkmod-util.c | 36 | ||||
-rw-r--r-- | libkmod/libkmod-util.h | 2 | ||||
-rw-r--r-- | libkmod/libkmod.c | 13 | ||||
-rw-r--r-- | tools/kmod-depmod.c | 8 |
5 files changed, 18 insertions, 47 deletions
diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 0af3e2e..adc055f 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -355,10 +355,10 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, if (ctx == NULL || path == NULL || mod == NULL) return -ENOENT; - abspath = path_make_absolute_cwd(path); + abspath = canonicalize_file_name(path); if (abspath == NULL) { - DBG(ctx, "no absolute path for %s\n", path); - return -ENOMEM; + DBG(ctx, "no canonical filename returned for %s\n", path); + return -errno; } err = stat(abspath, &st); diff --git a/libkmod/libkmod-util.c b/libkmod/libkmod-util.c index f499578..ba89ecf 100644 --- a/libkmod/libkmod-util.c +++ b/libkmod/libkmod-util.c @@ -303,42 +303,6 @@ char *strchr_replace(char *s, int c, char r) return s; } -bool path_is_absolute(const char *p) -{ - assert(p != NULL); - - return p[0] == '/'; -} - -char *path_make_absolute_cwd(const char *p) -{ - char *cwd, *r; - size_t plen; - size_t cwdlen; - - if (path_is_absolute(p)) - return strdup(p); - - cwd = get_current_dir_name(); - if (cwd == NULL) - return NULL; - - plen = strlen(p); - cwdlen = strlen(cwd); - - /* cwd + '/' + p + '\0' */ - r = realloc(cwd, cwdlen + 1 + plen + 1); - if (r == NULL) { - free(cwd); - return NULL; - } - - r[cwdlen] = '/'; - memcpy(&r[cwdlen + 1], p, plen + 1); - - return r; -} - #define USEC_PER_SEC 1000000ULL #define NSEC_PER_USEC 1000ULL unsigned long long stat_mstamp(const struct stat *st) diff --git a/libkmod/libkmod-util.h b/libkmod/libkmod-util.h index 317b2f7..655e8fb 100644 --- a/libkmod/libkmod-util.h +++ b/libkmod/libkmod-util.h @@ -19,8 +19,6 @@ ssize_t write_str_safe(int fd, const char *buf, size_t buflen) __attribute__((no int read_str_long(int fd, long *value, int base) __must_check __attribute__((nonnull(2))); int read_str_ulong(int fd, unsigned long *value, int base) __must_check __attribute__((nonnull(2))); char *strchr_replace(char *s, int c, char r); -bool path_is_absolute(const char *p) __must_check __attribute__((nonnull(1))); -char *path_make_absolute_cwd(const char *p) __must_check __attribute__((nonnull(1))); int alias_normalize(const char *alias, char buf[PATH_MAX], size_t *len) __must_check __attribute__((nonnull(1,2))); char *modname_normalize(const char *modname, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(1, 2))); char *path_to_modname(const char *path, char buf[PATH_MAX], size_t *len) __attribute__((nonnull(2))); diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index 4990d3e..21297fb 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -195,13 +195,18 @@ static int log_priority(const char *priority) static const char *dirname_default_prefix = ROOTPREFIX "/lib/modules"; -static char *get_kernel_release(const char *dirname) +static char *get_kernel_release(struct kmod_ctx *ctx, const char *dirname) { struct utsname u; char *p; - if (dirname != NULL) - return path_make_absolute_cwd(dirname); + if (dirname != NULL) { + p = canonicalize_file_name(dirname); + if (p) + return p; + INFO(ctx, "could not canonicalize directory %s: %m\n", dirname); + return strdup(dirname); + } if (uname(&u) < 0) return NULL; @@ -250,7 +255,7 @@ KMOD_EXPORT struct kmod_ctx *kmod_new(const char *dirname, ctx->log_data = stderr; ctx->log_priority = LOG_ERR; - ctx->dirname = get_kernel_release(dirname); + ctx->dirname = get_kernel_release(ctx, dirname); /* environment overwrites config */ env = getenv("KMOD_LOG"); diff --git a/tools/kmod-depmod.c b/tools/kmod-depmod.c index 1871e18..c545916 100644 --- a/tools/kmod-depmod.c +++ b/tools/kmod-depmod.c @@ -1160,7 +1160,7 @@ static int depmod_module_add(struct depmod *depmod, struct kmod_module *kmod) } } - DBG("add %p kmod=%p, path=%s\n", mod, kmod, mod->path); + DBG("add %p kmod=%p, path=%s, relpath=%s\n", mod, kmod, mod->path, mod->relpath); return 0; } @@ -2549,7 +2549,11 @@ static int do_depmod(int argc, char *argv[]) maybe_all = 1; break; case 'b': - root = path_make_absolute_cwd(optarg); + root = canonicalize_file_name(optarg); + if (!root) { + CRIT("could not resolve path %s\n", optarg); + goto cmdline_failed; + } break; case 'C': { size_t bytes = sizeof(char *) * (n_config_paths + 2); |