A user reports that when running daxctl they do not get a hint to
use sudo or root when an action fails. They provided this example:

        libdaxctl: daxctl_dev_disable: dax0.0: failed to disable
        dax0.0: disable failed: Device or resource busy
        error reconfiguring devices: Device or resource busy
        reconfigured 0 devices

and noted that the message is misleading as the problem was a lack
of privileges, not a busy device.

Add a helpful hint when a sysfs open or write fails with EACCES or
EPERM, advising the user to run with root privileges or use sudo.

Only the log messages are affected and no functional behavior is
changed. To make the new hints visible without debug enabled, make
them error level instead of debug.

Reported-by: Joel C. Chang <[email protected]>
Closes: https://lore.kernel.org/all/ZEJkI2i0GBmhtkI8@joel-gram-ubuntu/
Closes: https://github.com/pmem/ndctl/issues/237
Signed-off-by: Alison Schofield <[email protected]>
---
 util/sysfs.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/util/sysfs.c b/util/sysfs.c
index 5a12c639fe4d..e027e387c997 100644
--- a/util/sysfs.c
+++ b/util/sysfs.c
@@ -24,7 +24,14 @@ int __sysfs_read_attr(struct log_ctx *ctx, const char *path, 
char *buf)
        int n, rc;
 
        if (fd < 0) {
-               log_dbg(ctx, "failed to open %s: %s\n", path, strerror(errno));
+               if (errno == EACCES || errno == EPERM)
+                       log_err(ctx, "failed to open %s: %s "
+                               "hint: try running as root or using sudo\n",
+                               path, strerror(errno));
+               else
+                       log_dbg(ctx, "failed to open %s: %s\n",
+                               path, strerror(errno));
+
                return -errno;
        }
        n = read(fd, buf, SYSFS_ATTR_SIZE);
@@ -49,16 +56,30 @@ static int write_attr(struct log_ctx *ctx, const char *path,
 
        if (fd < 0) {
                rc = -errno;
-               log_dbg(ctx, "failed to open %s: %s\n", path, strerror(errno));
+               if (errno == EACCES || errno == EPERM)
+                       log_err(ctx, "failed to open %s: %s "
+                               "hint: try running as root or using sudo\n",
+                               path, strerror(errno));
+               else
+                       log_dbg(ctx, "failed to open %s: %s\n",
+                               path, strerror(errno));
                return rc;
        }
        n = write(fd, buf, len);
        rc = -errno;
        close(fd);
        if (n < len) {
-               if (!quiet)
-                       log_dbg(ctx, "failed to write %s to %s: %s\n", buf, 
path,
-                                       strerror(-rc));
+               if (quiet)
+                       return rc;
+
+               if (rc == -EACCES || rc == -EPERM)
+                       log_err(ctx, "failed to write %s to %s: %s "
+                               "hint: try running as root or using sudo\n",
+                               buf, path, strerror(-rc));
+               else
+                       log_dbg(ctx, "failed to write %s to %s: %s\n",
+                               buf, path, strerror(-rc));
+
                return rc;
        }
        return 0;
-- 
2.37.3


Reply via email to