]> www.infradead.org Git - users/hch/nvme-cli.git/commitdiff
Code reuse: character device check
authorKeith Busch <keith.busch@intel.com>
Fri, 18 Mar 2016 14:52:06 +0000 (08:52 -0600)
committerKeith Busch <keith.busch@intel.com>
Tue, 29 Mar 2016 18:19:19 +0000 (12:19 -0600)
Moving character device sanity check to common function to reduce
duplication.

Signed-off-by: Keith Busch <keith.busch@intel.com>
nvme-ioctl.c

index 950f579c766ed668a5d30ad5f5540c745809441b..6acfb85a769894c715e5f80a4749297636320a07 100644 (file)
 
 #include "nvme-ioctl.h"
 
-int nvme_subsystem_reset(int fd)
+void nvme_verify_chr(int fd)
 {
        static struct stat nvme_stat;
        int err = fstat(fd, &nvme_stat);
 
-       if (err < 0)
-               return err;
+       if (err < 0) {
+               perror("fstat");
+               exit(err);
+       }
        if (!S_ISCHR(nvme_stat.st_mode)) {
                fprintf(stderr,
-                       "Error: requesting subsystem reset on non-controller handle\n");
+                       "Error: requesting reset on non-controller handle\n");
                exit(ENOTBLK);
        }
+}
+
+int nvme_subsystem_reset(int fd)
+{
+       nvme_verify_chr(fd);
        return ioctl(fd, NVME_IOCTL_SUBSYS_RESET);
 }
 
 int nvme_reset_controller(int fd)
 {
-       static struct stat nvme_stat;
-       int err = fstat(fd, &nvme_stat);
-
-       if (err < 0)
-               return err;
-       if (!S_ISCHR(nvme_stat.st_mode)) {
-               fprintf(stderr,
-                       "Error: requesting reset on non-controller handle\n");
-               exit(ENOTBLK);
-       }
+       nvme_verify_chr(fd);
        return ioctl(fd, NVME_IOCTL_RESET);
 }