2 * Copyright (C) 2010-2014 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
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
30 #include <sys/ioctl.h>
32 #include <sys/types.h>
42 static int filter_sd(const char *p)
44 return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
47 static struct psensor *
48 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
53 t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
55 s = psensor_create(id,
67 * Performs the same tests than sk_disk_open and outputs the result.
69 static void analyze_disk(const char *dname)
75 log_debug("analyze_disk(hdd_atasmart): %s", dname);
77 f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
80 log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s",
86 if (fstat(f, &st) < 0) {
87 log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s",
93 if (!S_ISBLK(st.st_mode)) {
94 log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s",
100 /* So, it's a block device. Let's make sure the ioctls work */
101 if (ioctl(f, BLKGETSIZE64, &size) < 0) {
102 log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s",
108 if (size <= 0 || size == (uint64_t) -1) {
109 log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld",
119 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
120 int values_max_length)
122 char **paths, **tmp, *id;
124 struct psensor *sensor, **tmp_sensors, **result;
126 log_debug("hdd_psensor_list_add(hdd_atasmart)");
128 paths = dir_list("/dev", filter_sd);
133 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
135 if (!sk_disk_open(*tmp, &disk)) {
136 id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
137 strcpy(id, "hdd at");
140 sensor = create_sensor(id,
145 tmp_sensors = psensor_list_add(result, sensor);
147 if (result != sensors)
150 result = tmp_sensors;
152 log_err(_("atasmart: sk_disk_open() failure: %s."),
165 void hdd_psensor_list_update(struct psensor **sensors)
167 struct psensor **cur, *s;
175 if (!(s->type & SENSOR_TYPE_REMOTE)
176 && s->type & SENSOR_TYPE_ATASMART) {
177 ret = sk_disk_smart_read_data(s->disk);
180 ret = sk_disk_smart_get_temperature(s->disk,
184 c = (kelvin - 273150) / 1000;
185 psensor_set_current_value(s, c);
186 log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",