[PATCH 5/6] include errno string in more messages

Jonathan Nieder jrnieder at gmail.com
Mon Feb 20 23:49:07 PST 2012


Introduce a die_error() helper that includes strerror in a fatal
error message and use it where possible.  In particular, when
running radeontool as non-root, instead of the cryptic

	fatal error: mapping ctrl region

the operator will get a more helpful diagnosis:

	fatal error: cannot map ctrl region: Permission denied

Inspired by a patch by Tormod Volden.

Signed-off-by: Jonathan Nieder <jrnieder at gmail.com>
---
 avivotool.c  |   34 ++++++++++++++++++++--------------
 radeonreg.c  |   25 ++++++++++++++++++-------
 radeontool.c |   21 +++++++++++++++------
 3 files changed, 53 insertions(+), 27 deletions(-)

diff --git a/avivotool.c b/avivotool.c
index 1871fa93..5a2c5a84 100644
--- a/avivotool.c
+++ b/avivotool.c
@@ -69,6 +69,13 @@ static void die(const char *why)
     exit(-1);
 }
 
+static void die_error(int err, const char *why)
+{
+    fprintf(stderr, "fatal error: %s: %s\n", why, strerror(err));
+    pci_system_cleanup();
+    exit(-1);
+}
+
 static void radeon_set(unsigned long offset, const char *name, unsigned int value)
 {
     if (debug) 
@@ -669,10 +676,8 @@ void radeon_dump_img(char *type)
     i = 0;
     while (i < len) {
         ret = write(STDOUT_FILENO, &(fb[i]), len - i);
-        if (ret < 0) {
-            fprintf(stderr, "write died: %s\n", strerror(errno));
-            die("writing to stdout");
-        }
+        if (ret < 0)
+            die_error(errno, "writing to stdout");
         i += ret;
     }
 
@@ -695,10 +700,8 @@ void radeon_load_img(char *type)
     i = 0;
     while (i < len) {
         ret = read(STDIN_FILENO, &(fb[i]), len - i);
-        if (ret < 0) {
-            fprintf(stderr, "read died: %s\n", strerror(errno));
-            die("reading from stdin");
-        }
+        if (ret < 0)
+            die_error(errno, "reading from stdin");
         i += ret;
     }
 
@@ -1797,10 +1800,12 @@ static int map_radeon_mem(void)
     struct pci_device *device;
     pciaddr_t ctrl_base, ctrl_size = 0;
     pciaddr_t fb_size = 0;
-    int i = 0;
+    int i = 0, ret;
 
-    if (pci_system_init() != 0) {
-        fprintf(stderr, "error: failed to initialise libpciaccess\n");
+    ret = pci_system_init();
+    if (ret) {
+        fprintf(stderr, "error: failed to initialise libpciaccess: %s\n",
+		strerror(ret));
         return -1;
     }
 
@@ -1884,9 +1889,10 @@ static int map_radeon_mem(void)
       return -1;
     }
       
-    if (pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
-			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem)) {
-        fprintf(stderr, "error: mapping ctrl region\n");
+    ret = pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
+			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
+    if (ret) {
+        fprintf(stderr, "error: cannot map ctrl region: %s\n", strerror(ret));
         return -1;
     }
 
diff --git a/radeonreg.c b/radeonreg.c
index 78c20fb0..9e43f31c 100644
--- a/radeonreg.c
+++ b/radeonreg.c
@@ -51,6 +51,13 @@ static void die(const char *why)
     exit(-1);
 }
 
+static void die_error(int err, const char *why)
+{
+    fprintf(stderr, "fatal error: %s: %s\n", why, strerror(err));
+    pci_system_cleanup();
+    exit(-1);
+}
+
 static void radeon_set(unsigned long offset, const char *name, unsigned int value)
 {
 
@@ -366,10 +373,11 @@ static int map_radeon_mem(void)
     struct pci_device *device;
     pciaddr_t ctrl_base, ctrl_size = 0;
     pciaddr_t fb_base, fb_size = 0;
-    int i = 0;
+    int i = 0, ret;
 
-    if (pci_system_init() != 0)
-        die("failed to initialise libpciaccess");
+    ret = pci_system_init();
+    if (ret)
+        die_error(ret, "failed to initialise libpciaccess");
 
 #if 0
     match.vendor_id = 0x1002;
@@ -416,10 +424,13 @@ static int map_radeon_mem(void)
       return -1;
     }
 
-    if (!ctrl_size ||
-        pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
-			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem))
-        die("mapping ctrl region");
+    if (!ctrl_size)
+        die("missing ctrl region");
+
+    ret = pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
+			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
+    if (ret)
+        die_error(ret, "cannot map ctrl region");
 
     if (!fb_size ||
         pci_device_map_range(avivo_device, fb_base, fb_size,
diff --git a/radeontool.c b/radeontool.c
index 3b3c72e5..2c215e5a 100644
--- a/radeontool.c
+++ b/radeontool.c
@@ -43,6 +43,13 @@ static void die(const char *why)
     exit(-1);
 }
 
+static void die_error(int err, const char *why)
+{
+    fprintf(stderr, "fatal error: %s: %s\n", why, strerror(err));
+    pci_system_cleanup();
+    exit(-1);
+}
+
 static unsigned int radeon_get(unsigned long offset, const char *name)
 {
     unsigned int value;
@@ -919,10 +926,11 @@ static void map_radeon_cntl_mem(void)
     pciaddr_t ctrl_base, ctrl_size = 0;
     pciaddr_t fb_base, fb_size = 0;
 
-    int i = 0;
+    int i = 0, ret;
 
-    if (pci_system_init() != 0)
-        die("failed to initialise libpciaccess");
+    ret = pci_system_init();
+    if (ret)
+        die_error(ret, "failed to initialise libpciaccess");
 
     match.domain = PCI_MATCH_ANY;
     match.bus = PCI_MATCH_ANY;
@@ -970,9 +978,10 @@ static void map_radeon_cntl_mem(void)
     if (!avivo_device)
         die("cannot find Radeon device");
 
-    if (pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
-			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem))
-        die("mapping ctrl region");
+    ret = pci_device_map_range(avivo_device, ctrl_base, ctrl_size,
+			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
+    if (ret)
+        die_error(ret, "cannot map ctrl region");
 
     if (pci_device_map_range(avivo_device, fb_base, fb_size,
 			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &fb_mem))
-- 
1.7.9.1



More information about the xorg-driver-ati mailing list