[PATCH 08/10] include errno string in more messages

Jonathan Nieder jrnieder at gmail.com
Fri Feb 24 19:57:34 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  |   38 ++++++++++++++++++++++++--------------
 radeonreg.c  |   23 +++++++++++++++++------
 radeontool.c |   23 +++++++++++++++++------
 3 files changed, 58 insertions(+), 26 deletions(-)

diff --git a/avivotool.c b/avivotool.c
index 2f7d4e57..02ee6cc6 100644
--- a/avivotool.c
+++ b/avivotool.c
@@ -68,6 +68,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) 
@@ -668,10 +675,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;
     }
 
@@ -694,10 +699,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;
     }
 
@@ -1795,10 +1798,12 @@ static int map_radeon_mem(void)
     struct pci_device_iterator *iter;
     struct pci_device *device;
     pciaddr_t fb_size, ctrl_base, ctrl_size;
-    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;
     }
 
@@ -1858,9 +1863,14 @@ static int map_radeon_mem(void)
 
     ctrl_base = device->regions[2].base_addr;
     ctrl_size = device->regions[2].size;
-    if (!ctrl_size || pci_device_map_range(device, ctrl_base, ctrl_size,
-			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem)) {
-        fprintf(stderr, "error: mapping ctrl region\n");
+    if (!ctrl_size) {
+        fprintf(stderr, "error: missing ctrl region\n");
+        return -1;
+    }
+    ret = pci_device_map_range(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 7b763518..0dc14fe7 100644
--- a/radeonreg.c
+++ b/radeonreg.c
@@ -50,6 +50,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)
 {
 
@@ -364,10 +371,11 @@ static int map_radeon_mem(void)
     struct pci_device_iterator *iter;
     struct pci_device *device;
     pciaddr_t fb_base, fb_size, ctrl_base, ctrl_size;
-    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;
@@ -412,9 +420,12 @@ static int map_radeon_mem(void)
 
     ctrl_base = device->regions[2].base_addr;
     ctrl_size = device->regions[2].size;
-    if (!ctrl_size || pci_device_map_range(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(device, ctrl_base, ctrl_size,
+			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
+    if (ret)
+        die_error(ret, "cannot map ctrl region");
 
     fb_base = device->regions[0].base_addr;
     fb_size = device->regions[0].size;
diff --git a/radeontool.c b/radeontool.c
index dbab7631..bf1dc3c5 100644
--- a/radeontool.c
+++ b/radeontool.c
@@ -42,6 +42,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;
@@ -916,10 +923,11 @@ static void map_radeon_cntl_mem(void)
     struct pci_device_iterator *iter;
     struct pci_device *device;
     pciaddr_t fb_base, fb_size, ctrl_base, ctrl_size;
-    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;
@@ -949,9 +957,12 @@ static void map_radeon_cntl_mem(void)
 
     ctrl_base = device->regions[2].base_addr;
     ctrl_size = device->regions[2].size;
-    if (!ctrl_size || pci_device_map_range(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(device, ctrl_base, ctrl_size,
+			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
+    if (ret)
+        die_error(ret, "cannot map ctrl region");
 
     fb_base = device->regions[0].base_addr;
     fb_size = device->regions[0].size;
-- 
1.7.9.2



More information about the xorg-driver-ati mailing list