From e21ad3cd0055f90cc01f43d7a7357d1fabdbc5fa Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Mon, 25 Jul 2016 00:08:53 +0530 Subject: split out fs related helper funcs as lib code This way we can use the funcs in other modules. It makes scanelf a little bigger (~1k), but shouldn't be a big deal overall. --- paxinc.c | 31 +++++++++++++++++++++++++++++++ paxinc.h | 5 +++++ scanelf.c | 29 ----------------------------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/paxinc.c b/paxinc.c index 068aa60..64a3069 100644 --- a/paxinc.c +++ b/paxinc.c @@ -167,3 +167,34 @@ void color_init(bool disable) if (disable) NORM = RED = YELLOW = ""; } + +/* File system helpers. */ +int root_fd = AT_FDCWD; + +FILE *fopenat_r(int dir_fd, const char *path) +{ + int fd = openat(dir_fd, path, O_RDONLY|O_CLOEXEC); + if (fd == -1) + return NULL; + return fdopen(fd, "re"); +} + +const char *root_rel_path(const char *path) +{ + /* + * openat() will ignore the dirfd if path starts with + * a /, so consume all of that noise + * + * XXX: we don't handle relative paths like ../ that + * break out of the --root option, but for now, just + * don't do that :P. + */ + if (root_fd != AT_FDCWD) { + while (*path == '/') + ++path; + if (*path == '\0') + path = "."; + } + + return path; +} diff --git a/paxinc.h b/paxinc.h index e687b3a..f761b2e 100644 --- a/paxinc.h +++ b/paxinc.h @@ -123,4 +123,9 @@ extern const char argv0[]; #define errf(fmt, args...) _err(warnf, fmt, ## args) #define errp(fmt, args...) _err(warnp, fmt , ## args) +/* File system helper functions. */ +extern int root_fd; +FILE *fopenat_r(int dir_fd, const char *path); +const char *root_rel_path(const char *path); + #endif /* _PAX_INC_H */ diff --git a/scanelf.c b/scanelf.c index 5a765b5..89c9695 100644 --- a/scanelf.c +++ b/scanelf.c @@ -60,7 +60,6 @@ static char use_ldpath = 0; static char **qa_textrels = NULL; static char **qa_execstack = NULL; static char **qa_wx_load = NULL; -static int root_fd = AT_FDCWD; static int match_bits = 0; static unsigned int match_perms = 0; @@ -131,34 +130,6 @@ static const char *which(const char *fname, const char *envvar) return NULL; } -static FILE *fopenat_r(int dir_fd, const char *path) -{ - int fd = openat(dir_fd, path, O_RDONLY|O_CLOEXEC); - if (fd == -1) - return NULL; - return fdopen(fd, "re"); -} - -static const char *root_rel_path(const char *path) -{ - /* - * openat() will ignore the dirfd if path starts with - * a /, so consume all of that noise - * - * XXX: we don't handle relative paths like ../ that - * break out of the --root option, but for now, just - * don't do that :P. - */ - if (root_fd != AT_FDCWD) { - while (*path == '/') - ++path; - if (*path == '\0') - path = "."; - } - - return path; -} - /* sub-funcs for scanelf_fileat() */ static void scanelf_file_get_symtabs(elfobj *elf, void **sym, void **str) { -- cgit v1.2.3-65-gdbad