2 * Copyright (C) 2010-2012 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)
48 s = psensor_create(id,
51 SENSOR_TYPE_HDD_TEMP_ATASMART,
60 * Performs the same tests than sk_disk_open and outputs the result.
62 static void analyze_disk(const char *dname)
68 log_debug("analyze_disk(hdd_atasmart): %s", dname);
70 f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
73 log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s",
79 if (fstat(f, &st) < 0) {
80 log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s",
86 if (!S_ISBLK(st.st_mode)) {
87 log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s",
93 /* So, it's a block device. Let's make sure the ioctls work */
94 if (ioctl(f, BLKGETSIZE64, &size) < 0) {
95 log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s",
101 if (size <= 0 || size == (uint64_t) -1) {
102 log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld",
112 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
113 int values_max_length)
115 char **paths, **tmp, *id;
117 struct psensor *sensor, **tmp_sensors, **result;
119 log_debug("hdd_psensor_list_add(hdd_atasmart)");
121 paths = dir_list("/dev", filter_sd);
126 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
128 if (!sk_disk_open(*tmp, &disk)) {
129 id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
130 strcpy(id, "hdd at");
133 sensor = create_sensor(id,
138 tmp_sensors = psensor_list_add(result, sensor);
140 if (result != sensors)
143 result = tmp_sensors;
145 log_err(_("atasmart: sk_disk_open() failure: %s."),
158 void hdd_psensor_list_update(struct psensor **sensors)
160 struct psensor **cur, *s;
168 if (s->type == SENSOR_TYPE_HDD_TEMP_ATASMART) {
169 ret = sk_disk_smart_read_data(s->disk);
172 ret = sk_disk_smart_get_temperature(s->disk,
176 c = (kelvin - 273150) / 1000;
177 psensor_set_current_value(s, c);
178 log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",