avoid to use cpp in code
[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 <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 struct psensor *
51 create_sensor(char *id, char *name, SkDisk *disk, int values_max_length)
52 {
53         struct psensor *s;
54         int t;
55
56         t = SENSOR_TYPE_ATASMART | SENSOR_TYPE_HDD | SENSOR_TYPE_TEMP;
57
58         s = psensor_create(id,
59                            strdup(name),
60                            strdup(_("Disk")),
61                            t,
62                            values_max_length);
63
64         s->disk = disk;
65
66         return s;
67 }
68
69 /*
70  * Performs the same tests than sk_disk_open and outputs the result.
71  */
72 static void analyze_disk(const char *dname)
73 {
74         int f;
75         struct stat st;
76         uint64_t size;
77
78         log_fct("Analyze %s", dname);
79
80         f = open(dname, O_RDONLY|O_NOCTTY|O_NONBLOCK|O_CLOEXEC);
81
82         if (f < 0) {
83                 log_fct("Could not open file %s: %s", dname, strerror(errno));
84                 goto fail;
85         }
86
87         if (fstat(f, &st) < 0) {
88                 log_fct("fstat fails %s: %s", dname, strerror(errno));
89                 goto fail;
90         }
91
92         if (!S_ISBLK(st.st_mode)) {
93                 log_fct("!S_ISBLK fails %s", dname);
94                 goto fail;
95         }
96
97         size = (uint64_t)-1;
98         /* So, it's a block device. Let's make sure the ioctls work */
99         if (ioctl(f, BLKGETSIZE64, &size) < 0) {
100                 log_fct("ioctl fails %s: %s", dname, strerror(errno));
101                 goto fail;
102         }
103
104         if (size <= 0 || size == (uint64_t) -1) {
105                 log_fct("ioctl wrong size %s: %ld", dname, size);
106                 goto fail;
107         }
108
109  fail:
110         close(f);
111 }
112
113 void
114 atasmart_psensor_list_append(struct psensor ***sensors, int values_max_length)
115 {
116         char **paths, **tmp, *id;
117         SkDisk *disk;
118         struct psensor *sensor;
119
120         log_fct_enter();
121
122         paths = dir_list("/dev", filter_sd);
123
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                         psensor_list_append(sensors, sensor);
141                 } else {
142                         log_err(_("%s: sk_disk_open() failure: %s."),
143                                 PROVIDER_NAME,
144                                 *tmp);
145                         analyze_disk(*tmp);
146                 }
147
148                 tmp++;
149         }
150
151         paths_free(paths);
152
153         log_fct_exit();
154 }
155
156 void atasmart_psensor_list_update(struct psensor **sensors)
157 {
158         struct psensor **cur, *s;
159         uint64_t kelvin;
160         int ret;
161         double c;
162
163         cur = sensors;
164         while (*cur) {
165                 s = *cur;
166                 if (!(s->type & SENSOR_TYPE_REMOTE)
167                     && s->type & SENSOR_TYPE_ATASMART) {
168                         ret = sk_disk_smart_read_data(s->disk);
169
170                         if (!ret) {
171                                 ret = sk_disk_smart_get_temperature(s->disk,
172                                                                     &kelvin);
173
174                                 if (!ret) {
175                                         c = (kelvin - 273150) / 1000;
176                                         psensor_set_current_value(s, c);
177                                         log_fct("%s %.2f", s->id, c);
178                                 }
179                         }
180                 }
181
182                 cur++;
183         }
184 }