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