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