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