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