From dfcb53a9b207e341ed02b41026bf398c6e7d2772 Mon Sep 17 00:00:00 2001 From: Bart Van Assche Date: Wed, 17 Jun 2015 00:22:14 +0000 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. git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@6368 d57e44dd-8a1f-0410-8b47-8ef2f437770f --- 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; }