Imported Upstream version 1.2.0
[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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35
36 #include <atasmart.h>
37 #include <linux/fs.h>
38
39 #include <pio.h>
40 #include <hdd.h>
41 #include <plog.h>
42
43 static const char *PROVIDER_NAME = "atasmart";
44
45 static int filter_sd(const char *p)
46 {
47         return strlen(p) == 8 && !strncmp(p, "/dev/sd", 7);
48 }
49
50 static void provider_data_free(void *data)
51 {
52         sk_disk_free((SkDisk *)data);
53 }
54
55 static SkDisk *get_disk(struct psensor *s)
56 {
57         return (SkDisk *)s->provider_data;
58 }
59
60 static struct psensor *
61 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
62 {
63         struct psensor *s;
64         int t;
65
66         t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
67
68         s = psensor_create(id,
69                            strdup(name),
70                            strdup(_("Disk")),
71                            t,
72                            values_max_length);
73
74         s->provider_data = disk;
75         s->provider_data_free_fct = &provider_data_free;
76
77         return s;
78 }
79
80 /*
81  * Performs the same tests than sk_disk_open and outputs the result.
82  */
83 static void analyze_disk(const char *dname)
84 {
85         int f;
86         struct stat st;
87         uint64_t size;
88
89         log_fct("Analyze %s", dname);
90
91         f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
92
93         if (f < 0) {
94                 log_fct("Could not open file %s: %s", dname, strerror(errno));
95                 goto fail;
96         }
97
98         if (fstat(f, &st) < 0) {
99                 log_fct("fstat fails %s: %s", dname, strerror(errno));
100                 goto fail;
101         }
102
103         if (!S_ISBLK(st.st_mode)) {
104                 log_fct("!S_ISBLK fails %s", dname);
105                 goto fail;
106         }
107
108         size = (uint64_t)-1;
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));
112                 goto fail;
113         }
114
115         if (size <= 0 || size == (uint64_t) -1) {
116                 log_fct("ioctl wrong size %s: %ld", dname, size);
117                 goto fail;
118         }
119
120  fail:
121         close(f);
122 }
123
124 void
125 atasmart_psensor_list_append(struct psensor ***sensors, int values_max_length)
126 {
127         char **paths, **tmp, *id;
128         SkDisk *disk;
129         struct psensor *sensor;
130
131         log_fct_enter();
132
133         paths = dir_list("/dev", filter_sd);
134
135         tmp = paths;
136         while (*tmp) {
137                 log_fct("Open %s", *tmp);
138
139                 if (!sk_disk_open(*tmp, &disk)) {
140                         id = malloc(strlen(PROVIDER_NAME)
141                                     + 1
142                                     + strlen(*tmp)
143                                     + 1);
144                         sprintf(id, "%s %s", PROVIDER_NAME, *tmp);
145
146                         sensor = create_sensor(id,
147                                                *tmp,
148                                                disk,
149                                                values_max_length);
150
151                         psensor_list_append(sensors, sensor);
152                 } else {
153                         log_err(_("%s: sk_disk_open() failure: %s."),
154                                 PROVIDER_NAME,
155                                 *tmp);
156                         analyze_disk(*tmp);
157                 }
158
159                 tmp++;
160         }
161
162         paths_free(paths);
163
164         log_fct_exit();
165 }
166
167 void atasmart_psensor_list_update(struct psensor **sensors)
168 {
169         struct psensor **cur, *s;
170         uint64_t kelvin;
171         int ret;
172         double c;
173         SkDisk *disk;
174
175         if (!sensors)
176                 return;
177
178         cur = sensors;
179         while (*cur) {
180                 s = *cur;
181                 if (!(s->type & SENSOR_TYPE_REMOTE)
182                     && s->type & SENSOR_TYPE_ATASMART) {
183                         disk = get_disk(s);
184
185                         ret = sk_disk_smart_read_data(disk);
186
187                         if (!ret) {
188                                 ret = sk_disk_smart_get_temperature(disk,
189                                                                     &kelvin);
190
191                                 if (!ret) {
192                                         c = (kelvin - 273150) / 1000;
193                                         psensor_set_current_value(s, c);
194                                         log_fct("%s %.2f", s->id, c);
195                                 }
196                         }
197                 }
198
199                 cur++;
200         }
201 }