From 39f46daa44430d31e75a6a2ed9f67c21fcb38ff8 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 6 May 2015 08:49:15 +0200 Subject: [PATCH] iscsi-scstd: Rearrange driver major number lookup code Introduce a function for looking up the driver major number. This patch does not change any functionality but makes the source code easier to read and also makes it easier for Coverity to analyze this code. Signed-off-by: Bart Van Assche --- iscsi-scst/usr/misc.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/iscsi-scst/usr/misc.c b/iscsi-scst/usr/misc.c index 13c7676e8..8a2273c28 100644 --- a/iscsi-scst/usr/misc.c +++ b/iscsi-scst/usr/misc.c @@ -25,36 +25,48 @@ #include "iscsid.h" -int create_and_open_dev(const char *dev, int readonly) +int driver_major(const char *dev) { FILE *f; char devname[256]; char buf[256]; int devn; - int ctlfd = -1; - int err; - int flags; f = fopen("/proc/devices", "r"); if (!f) { - err = -errno; + devn = -errno; perror("Cannot open control path to the driver"); goto out; } - devn = 0; + devn = -ENOENT; while (fgets(buf, sizeof(buf), f)) { if (sscanf(buf, "%d %s", &devn, devname) == 2 && devn > 0 && strcmp(devname, dev) == 0) break; - devn = 0; + devn = -ENOENT; } - fclose(f); - if (!devn) { - err = -ENOENT; + + if (devn < 0) printf("cannot find %s in /proc/devices - " "make sure the module is loaded\n", dev); + +out: + return devn; +} + +int create_and_open_dev(const char *dev, int readonly) +{ + char devname[256]; + int devn; + int ctlfd = -1; + int err; + int flags; + + devn = driver_major(dev); + if (devn < 0) { + err = devn; goto out; }