From 81995ccc8d51eb083373eae9b188082a4995dae0 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Thu, 21 May 2026 12:21:36 +0200 Subject: [PATCH] Use eaccess() instead of access() --- cmdline/mingw.c | 15 +++++++++++++++ cmdline/portable.h | 5 +++++ cmdline/snapraid.c | 34 +--------------------------------- cmdline/unix.c | 35 ++++++++++++++++++++++++++++++++--- configure.ac | 1 + 5 files changed, 54 insertions(+), 36 deletions(-) diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 0ed3fdb4..ff4b6e18 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -110,6 +110,21 @@ static void exedir_init(void) slash[1] = 0; } +void os_default_conf(char* conf, size_t conf_size, const char* argv0) +{ + char* slash; + + pathimport(conf, conf_size, argv0); + + slash = strrchr(conf, '/'); + if (slash) { + slash[1] = 0; + pathcat(conf, conf_size, PACKAGE ".conf"); + } else { + pathcpy(conf, conf_size, PACKAGE ".conf"); + } +} + void os_init(int opt) { HMODULE ntdll, kernel32; diff --git a/cmdline/portable.h b/cmdline/portable.h index c2818459..f9133db0 100644 --- a/cmdline/portable.h +++ b/cmdline/portable.h @@ -247,6 +247,11 @@ /****************************************************************************/ /* os */ +/** + * Default paths. + */ +void os_default_conf(char* dst, size_t dst_size, const char* argv0); + /** * Get the os_tick counter value. * diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index f537a88c..950b1a93 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -593,38 +593,6 @@ void log_close(const char* file) stdlog = 0; } -/****************************************************************************/ -/* config */ - -void config(char* conf, size_t conf_size, const char* argv0) -{ -#ifdef _WIN32 - char* slash; - - pathimport(conf, conf_size, argv0); - - slash = strrchr(conf, '/'); - if (slash) { - slash[1] = 0; - pathcat(conf, conf_size, PACKAGE ".conf"); - } else { - pathcpy(conf, conf_size, PACKAGE ".conf"); - } -#else - (void)argv0; - -#ifdef SYSCONFDIR - /* if it exists, give precedence at sysconfdir, usually /usr/local/etc */ - if (access(SYSCONFDIR "/" PACKAGE ".conf", F_OK) == 0) { - pathcpy(conf, conf_size, SYSCONFDIR "/" PACKAGE ".conf"); - } else /* otherwise fallback to plain /etc */ -#endif - { - pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf"); - } -#endif -} - /****************************************************************************/ /* main */ @@ -1008,7 +976,7 @@ int snapraid_main(int argc, char* argv[]) lock_init(); /* defaults */ - config(conf, sizeof(conf), argv[0]); + os_default_conf(conf, sizeof(conf), argv[0]); memset(&opt, 0, sizeof(opt)); opt.io_error_limit = 100; blockstart = 0; diff --git a/cmdline/unix.c b/cmdline/unix.c index 4c7cea85..2805f2c5 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -7,6 +7,20 @@ #include "support.h" +/****************************************************************************/ +/* access */ + +#if !HAVE_EACCESS +/** + * Check effective user's permissions for a file. + * Conceptually identical to access(), but uses the effective UID/GID. + */ +int eaccess(const char* pathname, int mode) +{ + return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS); +} +#endif + /** * Exit codes. */ @@ -126,7 +140,7 @@ const char* find_smartctl(void) int i; for (i = 0; smartctl_paths[i]; ++i) { - if (access(smartctl_paths[i], X_OK) == 0) + if (eaccess(smartctl_paths[i], X_OK) == 0) return smartctl_paths[i]; } @@ -1597,7 +1611,7 @@ static int devtree(devinfo_t* parent, dev_t device, tommy_list* list) /* check if it's a real device */ pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/device", major(device), minor(device)); - if (access(path, F_OK) != 0) { + if (eaccess(path, F_OK) != 0) { /* get the parent device */ pathprint(path, sizeof(path), "/sys/dev/block/%u:%u/../dev", major(device), minor(device)); @@ -2434,6 +2448,21 @@ int devquery(tommy_list* high, tommy_list* low, int operation) return device_thread(low, func); } +void os_default_conf(char* conf, size_t conf_size, const char* argv0) +{ + (void)argv0; + +#ifdef SYSCONFDIR + /* if it exists, give precedence at sysconfdir, usually /usr/local/etc */ + if (eaccess(SYSCONFDIR "/" PACKAGE ".conf", F_OK) == 0) { + pathcpy(conf, conf_size, SYSCONFDIR "/" PACKAGE ".conf"); + } else /* otherwise fallback to plain /etc */ +#endif + { + pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf"); + } +} + void os_init(int opt) { #if HAVE_BLKID @@ -2700,7 +2729,7 @@ int devmap(void) /* verify it has a hardware device link (excludes virtual disks) */ pathprint(path, sizeof(path), "/sys/block/%s/device", dd->d_name); - if (access(path, F_OK) != 0) + if (eaccess(path, F_OK) != 0) continue; /* device/serial */ diff --git a/configure.ac b/configure.ac index 228cd3b9..3bc9ae32 100644 --- a/configure.ac +++ b/configure.ac @@ -94,6 +94,7 @@ AC_CHECK_FUNCS([clock_gettime]) AC_CHECK_CC_OPT([-pthread], CFLAGS="$CFLAGS -pthread", CFLAGS="$CFLAGS -D_REENTRANT") AC_CHECK_FUNCS([pthread_create]) AC_SEARCH_LIBS([exp], [m]) +AC_CHECK_FUNCS([eaccess faccessat]) dnl Checks for statfs for linux avoiding to include sys/mount.h that is required only in darwin dnl In glibc since 7eae6a91e9b1670330c9f15730082c91c0b1d570, milestone 2.36, sys/mount.h defines fsconfig_command which conflicts with linux/mount.h