world - Fix sysctlbyname() errno handling cases
authorMatthew Dillon <dillon@apollo.backplane.com>
Thu, 9 Jun 2016 06:04:18 +0000 (23:04 -0700)
committerMatthew Dillon <dillon@apollo.backplane.com>
Thu, 9 Jun 2016 06:04:18 +0000 (23:04 -0700)
* A number of routines inherited some bad code from each other,
  The return value from sysctlbyname() was not being tested prior
  to checking errno.  Reformulate.

Reported-by: Stephen Welker stephen.welker@nemostar.com.au
sbin/fdisk/fdisk.c
sbin/mount_ufs/mount_ufs.c
sbin/newfs/newfs.c
sbin/newfs_hammer/newfs_hammer.c
sbin/swapon/swapon.c

index 94ba8c7..f4d61e2 100644 (file)
@@ -783,8 +783,7 @@ erase_partition(int i)
 
        dev_name = strtok(dev_name + strlen("/dev/da"),"s");
        sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
-       sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
-       if (errno == ENOENT) {
+       if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
                printf("Device:%s does not support the TRIM command\n", disk);
                usage();
        }
index 660749c..70ec05c 100644 (file)
@@ -105,8 +105,7 @@ mount_ufs(int argc, const char **argv)
                char *dev_name = strdup(args.fspec);
                dev_name = strtok(dev_name + strlen("/dev/da"),"s");
                sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
-               sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
-               if(errno == ENOENT) {
+               if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
                        printf("Device:%s does not support the TRIM command\n",
                            args.fspec);
                        ufs_usage();
index 7ea5afb..5e3b659 100644 (file)
@@ -432,9 +432,7 @@ main(int argc, char **argv)
                sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled",
                    dev_name);
 
-               sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
-
-               if(errno == ENOENT) {
+               if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
                        printf("Device:%s does not support the TRIM command\n",
                            special);
                        usage();
index 8fac259..124015b 100644 (file)
@@ -219,8 +219,7 @@ main(int ac, char **av)
                        sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled",
                            dev_name);
                        errno=0;
-                       sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
-                       if(errno == ENOENT) {
+                       if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
                                printf("%s %s (%s) does not support the TRIM "
                                    "command\n",
                                    vol->type, vol->name, sysctl_name);
index 508a7fe..fb8a578 100644 (file)
@@ -369,8 +369,7 @@ swap_on_off(char *name, int doingall, int trim, int ask)
                char *dev_name = strdup(name);
                dev_name = strtok(dev_name + strlen("/dev/da"),"s");
                sprintf(sysctl_name, "kern.cam.da.%s.trim_enabled", dev_name);
-               sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0);
-               if (errno == ENOENT) {
+               if (sysctlbyname(sysctl_name, &trim_enabled, &olen, NULL, 0) < 0) {
                        if (qflag == 0) {
                                printf("TRIM not supported on %s, "
                                       "ignoring\n",