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