#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
-
#include <atasmart.h>
+#include <linux/fs.h>
#include "pio.h"
#include "hdd.h"
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 error for disk: %s", dname);
+ 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)
SkDisk *disk;
struct psensor *sensor, **tmp_sensors, **result;
- log_debug("hdd_psensor_list_add");
+ log_debug("hdd_psensor_list_add(hdd_atasmart)");
paths = dir_list("/dev", filter_sd);
result = sensors;
tmp = paths;
while (*tmp) {
- log_debug("hdd_psensor_list_add open %s", *tmp);
+ log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
if (!sk_disk_open(*tmp, &disk)) {
id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
result = tmp_sensors;
} else {
- log_err("sk_disk_open %s failure", *tmp);
+ log_err(_("atasmart: sk_disk_open() failure: %s."),
+ *tmp);
analyze_disk(*tmp);
}
if (!ret) {
c = (kelvin - 273150) / 1000;
psensor_set_current_value(s, c);
- log_debug("hdd atasmart: %s %.2f",
+ log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",
s->id,
c);
}