X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fhdd_atasmart.c;h=ae94a1754fb4d52359b85b6ee1e0f6fe8310321a;hb=b01d095d6b123bcc7ed221cccf2c04295834cc84;hp=8c781bb0e80b83515790395164ffcc0107467c7b;hpb=a8a666424ff0d391fd44cab6884f288f33a6b556;p=psensor.git diff --git a/src/lib/hdd_atasmart.c b/src/lib/hdd_atasmart.c index 8c781bb..ae94a17 100644 --- a/src/lib/hdd_atasmart.c +++ b/src/lib/hdd_atasmart.c @@ -24,10 +24,13 @@ #include #include #include +#include +#include +#include #include - #include +#include #include "pio.h" #include "hdd.h" @@ -44,6 +47,7 @@ create_sensor(char *id, char *name, SkDisk *disk, int values_max_length) struct psensor *s; s = psensor_create(id, strdup(name), + strdup("HDD"), SENSOR_TYPE_HDD_TEMP_ATASMART, values_max_length); @@ -52,20 +56,58 @@ create_sensor(char *id, char *name, SkDisk *disk, int values_max_length) return s; } +/* + * Performs the same tests than sk_disk_open and outputs the result. + */ static void analyze_disk(const char *dname) { int f; + struct stat st; + uint64_t size; log_debug("analyze_disk(hdd_atasmart): %s", dname); f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC); - if (f != -1) - close(f); - else - log_debug("Could not open file %s: %s", dname, strerror(errno)); -} + if (f < 0) { + log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s", + dname, + strerror(errno)); + goto fail; + } + + if (fstat(f, &st) < 0) { + log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s", + dname, + strerror(errno)); + goto fail; + } + if (!S_ISBLK(st.st_mode)) { + log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s", + dname); + goto fail; + } + + size = (uint64_t)-1; + /* So, it's a block device. Let's make sure the ioctls work */ + if (ioctl(f, BLKGETSIZE64, &size) < 0) { + log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s", + dname, + strerror(errno)); + goto fail; + } + + if (size <= 0 || size == (uint64_t) -1) { + log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld", + dname, + size); + goto fail; + } + + fail: + close(f); +} struct psensor **hdd_psensor_list_add(struct psensor **sensors, int values_max_length) @@ -100,7 +142,8 @@ struct psensor **hdd_psensor_list_add(struct psensor **sensors, result = tmp_sensors; } else { - log_err("sk_disk_open %s failure", *tmp); + log_err(_("atasmart: sk_disk_open() failure: %s."), + *tmp); analyze_disk(*tmp); }