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