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)
31 #include <sys/ioctl.h>
33 #include <sys/types.h>
43 static const char *PROVIDER_NAME = "atasmart";
45 static int filter_sd(const char *p)
47 return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
50 static void provider_data_free(void *data)
52 sk_disk_free((SkDisk *)data);
55 static SkDisk *get_disk(struct psensor *s)
57 return (SkDisk *)s->provider_data;
60 static struct psensor *
61 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
66 t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
68 s = psensor_create(id,
74 s->provider_data = disk;
75 s->provider_data_free_fct = &provider_data_free;
81 * Performs the same tests than sk_disk_open and outputs the result.
83 static void analyze_disk(const char *dname)
89 log_fct("Analyze %s", dname);
91 f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
94 log_fct("Could not open file %s: %s", dname, strerror(errno));
98 if (fstat(f, &st) < 0) {
99 log_fct("fstat fails %s: %s", dname, strerror(errno));
103 if (!S_ISBLK(st.st_mode)) {
104 log_fct("!S_ISBLK fails %s", dname);
109 /* So, it's a block device. Let's make sure the ioctls work */
110 if (ioctl(f, BLKGETSIZE64, &size) < 0) {
111 log_fct("ioctl fails %s: %s", dname, strerror(errno));
115 if (size <= 0 || size == (uint64_t) -1) {
116 log_fct("ioctl wrong size %s: %ld", dname, size);
125 atasmart_psensor_list_append(struct psensor ***sensors, int values_max_length)
127 char **paths, **tmp, *id;
129 struct psensor *sensor;
133 paths = dir_list("/dev", filter_sd);
137 log_fct("Open %s", *tmp);
139 if (!sk_disk_open(*tmp, &disk)) {
140 id = malloc(strlen(PROVIDER_NAME)
144 sprintf(id, "%s %s", PROVIDER_NAME, *tmp);
146 sensor = create_sensor(id,
151 psensor_list_append(sensors, sensor);
153 log_err(_("%s: sk_disk_open() failure: %s."),
167 void atasmart_psensor_list_update(struct psensor **sensors)
169 struct psensor **cur, *s;
181 if (!(s->type & SENSOR_TYPE_REMOTE)
182 && s->type & SENSOR_TYPE_ATASMART) {
185 ret = sk_disk_smart_read_data(disk);
188 ret = sk_disk_smart_get_temperature(disk,
192 c = (kelvin - 273150) / 1000;
193 psensor_set_current_value(s, c);
194 log_fct("%s %.2f", s->id, c);