2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
27 #include <sys/ioctl.h>
29 #include <sys/types.h>
39 static int filter_sd(const char *p)
41 return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
44 static struct psensor *
45 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
50 t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
52 s = psensor_create(id,
64 * Performs the same tests than sk_disk_open and outputs the result.
66 static void analyze_disk(const char *dname)
72 log_debug("analyze_disk(hdd_atasmart): %s", dname);
74 f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
77 log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s",
83 if (fstat(f, &st) < 0) {
84 log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s",
90 if (!S_ISBLK(st.st_mode)) {
91 log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s",
97 /* So, it's a block device. Let's make sure the ioctls work */
98 if (ioctl(f, BLKGETSIZE64, &size) < 0) {
99 log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s",
105 if (size <= 0 || size == (uint64_t) -1) {
106 log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld",
116 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
117 int values_max_length)
119 char **paths, **tmp, *id;
121 struct psensor *sensor, **tmp_sensors, **result;
123 log_debug("hdd_psensor_list_add(hdd_atasmart)");
125 paths = dir_list("/dev", filter_sd);
130 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
132 if (!sk_disk_open(*tmp, &disk)) {
133 id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
134 strcpy(id, "hdd at");
137 sensor = create_sensor(id,
142 tmp_sensors = psensor_list_add(result, sensor);
144 if (result != sensors)
147 result = tmp_sensors;
149 log_err(_("atasmart: sk_disk_open() failure: %s."),
162 void hdd_psensor_list_update(struct psensor **sensors)
164 struct psensor **cur, *s;
172 if (!(s->type & SENSOR_TYPE_REMOTE)
173 && s->type & SENSOR_TYPE_ATASMART) {
174 ret = sk_disk_smart_read_data(s->disk);
177 ret = sk_disk_smart_get_temperature(s->disk,
181 c = (kelvin - 273150) / 1000;
182 psensor_set_current_value(s, c);
183 log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",