improved messges
[psensor.git] / src / lib / hdd_atasmart.c
1 /*
2  * Copyright (C) 2010-2012 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         s = psensor_create(id,
49                            strdup(name),
50                            SENSOR_TYPE_HDD_TEMP_ATASMART,
51                            values_max_length);
52
53         s->disk = disk;
54
55         return s;
56 }
57
58 /*
59  * Performs the same tests than sk_disk_open and outputs the result.
60  */
61 static void analyze_disk(const char *dname)
62 {
63         int f;
64         struct stat st;
65         uint64_t size;
66
67         log_debug("analyze_disk(hdd_atasmart): %s", dname);
68
69         f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
70
71         if (f < 0) {
72                 log_debug("analyze_disk(hdd_atasmart): Could not open file %s: %s",
73                           dname,
74                           strerror(errno));
75                 goto fail;
76         }
77
78         if (fstat(f, &st) < 0) {
79                 log_debug("analyze_disk(hdd_atasmart): fstat fails %s: %s",
80                           dname,
81                           strerror(errno));
82                 goto fail;
83         }
84
85         if (!S_ISBLK(st.st_mode)) {
86                 log_debug("analyze_disk(hdd_atasmart): !S_ISBLK fails %s",
87                           dname);
88                 goto fail;
89         }
90
91         size = (uint64_t)-1;
92         /* So, it's a block device. Let's make sure the ioctls work */
93         if (ioctl(f, BLKGETSIZE64, &size) < 0) {
94                 log_debug("analyze_disk(hdd_atasmart): ioctl fails %s: %s",
95                           dname,
96                           strerror(errno));
97                 goto fail;
98         }
99
100         if (size <= 0 || size == (uint64_t) -1) {
101                 log_debug("analyze_disk(hdd_atasmart): ioctl wrong size %s: %ld",
102                           dname,
103                           size);
104                 goto fail;
105         }
106
107  fail:
108         close(f);
109 }
110
111 struct psensor **hdd_psensor_list_add(struct psensor **sensors,
112                                       int values_max_length)
113 {
114         char **paths, **tmp, *id;
115         SkDisk *disk;
116         struct psensor *sensor, **tmp_sensors, **result;
117
118         log_debug("hdd_psensor_list_add(hdd_atasmart)");
119
120         paths = dir_list("/dev", filter_sd);
121
122         result = sensors;
123         tmp = paths;
124         while (*tmp) {
125                 log_debug("hdd_psensor_list_add(hdd_atasmart) open %s", *tmp);
126
127                 if (!sk_disk_open(*tmp, &disk)) {
128                         id = malloc(strlen("hdd at") + strlen(*tmp) + 1);
129                         strcpy(id, "hdd at");
130                         strcat(id, *tmp);
131
132                         sensor = create_sensor(id,
133                                                *tmp,
134                                                disk,
135                                                values_max_length);
136
137                         tmp_sensors = psensor_list_add(result, sensor);
138
139                         if (result != sensors)
140                                 free(result);
141
142                         result = tmp_sensors;
143                 } else {
144                         log_err(_("atasmart: sk_disk_open() failure: %s."),
145                                 *tmp);
146                         analyze_disk(*tmp);
147                 }
148
149                 tmp++;
150         }
151
152         paths_free(paths);
153
154         return result;
155 }
156
157 void hdd_psensor_list_update(struct psensor **sensors)
158 {
159         struct psensor **cur, *s;
160         uint64_t kelvin;
161         int ret;
162         double c;
163
164         cur = sensors;
165         while (*cur) {
166                 s = *cur;
167                 if (s->type == SENSOR_TYPE_HDD_TEMP_ATASMART) {
168                         ret = sk_disk_smart_read_data(s->disk);
169
170                         if (!ret) {
171                                 ret = sk_disk_smart_get_temperature(s->disk,
172                                                                     &kelvin);
173
174                                 if (!ret) {
175                                         c = (kelvin - 273150) / 1000;
176                                         psensor_set_current_value(s, c);
177                                         log_debug("hdd_psensor_list_update(hdd_atasmart): %s %.2f",
178                                                   s->id,
179                                                   c);
180                                 }
181                         }
182                 }
183
184                 cur++;
185         }
186 }