normalize and cleanup
[psensor.git] / src / lib / hdd_atasmart.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #define _LARGEFILE_SOURCE 1
20 #include "config.h"
21
22 #include <locale.h>
23 #include <libintl.h>
24 #define _(str) gettext(str)
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #include <atasmart.h>
36 #include <linux/fs.h>
37
38 #include <pio.h>
39 #include <hdd.h>
40 #include <plog.h>
41
42 static const char *PROVIDER_NAME = "atasmart";
43
44 static int filter_sd(const char *p)
45 {
46         return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
47 }
48
49 static struct psensor *
50 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
51 {
52         struct psensor *s;
53         int t;
54
55         t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
56
57         s = psensor_create(id,
58                            strdup(name),
59                            strdup(_("Disk")),
60                            t,
61                            values_max_length);
62
63         s->disk = disk;
64
65         return s;
66 }
67
68 /*
69  * Performs the same tests than sk_disk_open and outputs the result.
70  */
71 static void analyze_disk(const char *dname)
72 {
73         int f;
74         struct stat st;
75         uint64_t size;
76
77         log_debug("analyze_disk(hdd_atasmart): %s", dname);
78
79         f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
80
81         if (f < 0) {
82                 log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s",
83                           dname,
84                           strerror(errno));
85                 goto fail;
86         }
87
88         if (fstat(f, &st) < 0) {
89                 log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s",
90                           dname,
91                           strerror(errno));
92                 goto fail;
93         }
94
95         if (!S_ISBLK(st.st_mode)) {
96                 log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s",
97                           dname);
98                 goto fail;
99         }
100
101         size = (uint64_t)-1;
102         /* So, it's a block device. Let's make sure the ioctls work */
103         if (ioctl(f, BLKGETSIZE64, &size) < 0) {
104                 log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s",
105                           dname,
106                           strerror(errno));
107                 goto fail;
108         }
109
110         if (size <= 0 || size == (uint64_t) -1) {
111                 log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld",
112                           dname,
113                           size);
114                 goto fail;
115         }
116
117  fail:
118         close(f);
119 }
120
121 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
122                                       int values_max_length)
123 {
124         char **paths, **tmp, *id;
125         SkDisk *disk;
126         struct psensor *sensor, **tmp_sensors, **result;
127
128         log_debug("hdd_psensor_list_add(hdd_atasmart)");
129
130         paths = dir_list("/dev", filter_sd);
131
132         result = sensors;
133         tmp = paths;
134         while (*tmp) {
135                 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
136
137                 if (!sk_disk_open(*tmp, &disk)) {
138                         id = malloc(strlen(PROVIDER_NAME)
139                                     + 1
140                                     + strlen(*tmp)
141                                     + 1);
142                         sprintf(id, "%s %s", PROVIDER_NAME, *tmp);
143
144                         sensor = create_sensor(id,
145                                                *tmp,
146                                                disk,
147                                                values_max_length);
148
149                         tmp_sensors = psensor_list_add(result, sensor);
150
151                         if (result != sensors)
152                                 free(result);
153
154                         result = tmp_sensors;
155                 } else {
156                         log_err(_("atasmart: sk_disk_open() failure: %s."),
157                                 *tmp);
158                         analyze_disk(*tmp);
159                 }
160
161                 tmp++;
162         }
163
164         paths_free(paths);
165
166         return result;
167 }
168
169 void hdd_psensor_list_update(struct psensor **sensors)
170 {
171         struct psensor **cur, *s;
172         uint64_t kelvin;
173         int ret;
174         double c;
175
176         cur = sensors;
177         while (*cur) {
178                 s = *cur;
179                 if (!(s->type & SENSOR_TYPE_REMOTE)
180                     && s->type & SENSOR_TYPE_ATASMART) {
181                         ret = sk_disk_smart_read_data(s->disk);
182
183                         if (!ret) {
184                                 ret = sk_disk_smart_get_temperature(s->disk,
185                                                                     &kelvin);
186
187                                 if (!ret) {
188                                         c = (kelvin - 273150) / 1000;
189                                         psensor_set_current_value(s, c);
190                                         log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",
191                                                   s->id,
192                                                   c);
193                                 }
194                         }
195                 }
196
197                 cur++;
198         }
199 }