Rewrite hddtemp support for better scaling
[monky] / src / hddtemp.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
13  *      (see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  * vim: ts=4 sw=4 noet ai cindent syntax=c
29  *
30  */
31
32 #include "conky.h"
33 #include "logging.h"
34 #include <errno.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <netdb.h>
38 #include <sys/select.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41
42 #define BUFLEN 512
43 #define DEFAULT_PORT "7634"
44 #define DEFAULT_HOST "127.0.0.1"
45
46 static char *hddtemp_host = NULL;
47 static char *hddtemp_port = NULL;
48
49 static struct hdd_info {
50         struct hdd_info *next;
51         char *dev;
52         short temp;
53         char unit;
54 } hdd_info_head = {
55         .next = NULL,
56 };
57
58 void set_hddtemp_host(const char *host)
59 {
60         if (hddtemp_host)
61                 free(hddtemp_host);
62         hddtemp_host = strdup(host);
63 }
64
65 void set_hddtemp_port(const char *port)
66 {
67         if (hddtemp_port)
68                 free(hddtemp_port);
69         hddtemp_port = strdup(port);
70 }
71
72 static void __free_hddtemp_info(struct hdd_info *hdi)
73 {
74         if (hdi->next)
75                 __free_hddtemp_info(hdi->next);
76         free(hdi->dev);
77         free(hdi);
78 }
79
80 static void free_hddtemp_info(void)
81 {
82         DBGP("free_hddtemp_info() called");
83         if (!hdd_info_head.next)
84                 return;
85         __free_hddtemp_info(hdd_info_head.next);
86         hdd_info_head.next = NULL;
87 }
88
89 static void add_hddtemp_info(char *dev, short temp, char unit)
90 {
91         struct hdd_info *hdi = &hdd_info_head;
92
93         DBGP("add_hddtemp_info(%s, %d, %c) being called", dev, temp, unit);
94         while (hdi->next)
95                 hdi = hdi->next;
96
97         hdi->next = malloc(sizeof(struct hdd_info));
98         memset(hdi->next, 0, sizeof(struct hdd_info));
99         hdi->next->dev = strdup(dev);
100         hdi->next->temp = temp;
101         hdi->next->unit = unit;
102 }
103
104 static char *fetch_hddtemp_output(void)
105 {
106         int sockfd;
107         const char *dst_host, *dst_port;
108         char *buf = NULL;
109         int buflen, offset = 0, rlen;
110         struct addrinfo hints, *result, *rp;
111         int i;
112
113         dst_host = hddtemp_host ? hddtemp_host : DEFAULT_HOST;
114         dst_port = hddtemp_port ? hddtemp_port : DEFAULT_PORT;
115
116         memset(&hints, 0, sizeof(hints));
117         hints.ai_family = AF_INET;      /* XXX: hddtemp has no ipv6 support (yet?) */
118         hints.ai_socktype = SOCK_STREAM;
119
120         if ((i = getaddrinfo(dst_host, dst_port, &hints, &result))) {
121                 NORM_ERR("getaddrinfo(): %s", gai_strerror(i));
122                 return NULL;
123         }
124
125         for (rp = result; rp; rp = rp->ai_next) {
126                 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
127                 if (sockfd == -1)
128                         continue;
129                 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
130                         break;
131                 close(sockfd);
132         }
133         if (!rp) {
134                 NORM_ERR("could not connect to mpd host");
135                 goto GET_OUT;
136         }
137
138         buflen = 1024;
139         buf = malloc(buflen);
140         while ((rlen = recv(sockfd, buf + offset, buflen - offset - 1, 0)) > 0) {
141                 offset += rlen;
142                 if (buflen - offset < 1) {
143                         buflen += 1024;
144                         buf = realloc(buf, buflen);
145                 }
146         }
147         if (rlen < 0)
148                 perror("recv");
149
150         buf[offset] = '\0';
151
152         close(sockfd);
153 GET_OUT:
154         freeaddrinfo(result);
155         return buf;
156 }
157
158 /* this is an iterator:
159  * set line to NULL in consecutive calls to get the next field
160  * note that exhausing iteration is assumed - otherwise *saveptr
161  * is not being freed!
162  */
163 static int read_hdd_val(const char *line, char **dev, short *val, char *unit,
164                 char **saveptr)
165 {
166         char *line_s, *cval, *endptr;
167         static char *p = 0;
168
169         if (line) {
170                 *saveptr = strdup(line);
171                 p = *saveptr;
172         }
173         line_s = *saveptr;
174
175         /* read the device */
176         *dev = ++p;
177         if (!(p = strchr(p, line_s[0])))
178                 goto out_fail;
179         *(p++) = '\0';
180         /* jump over the devname */
181         if (!(p = strchr(p, line_s[0])))
182                 goto out_fail;
183         /* read the value */
184         cval = ++p;
185         if (!(p = strchr(p, line_s[0])))
186                 goto out_fail;
187         *(p++) = '\0';
188         *unit = *(p++);
189         *val = strtol(cval, &endptr, 10);
190         if (*endptr)
191                 goto out_fail;
192         
193         /* preset p for next call */
194         p++;
195
196         return 0;
197 out_fail:
198         free(*saveptr);
199         return 1;
200 }
201
202 void update_hddtemp(void) {
203         char *data, *dev, unit, *saveptr;
204         short val;
205         static double last_hddtemp_update = 0.0;
206
207         /* limit tcp connection overhead */
208         if (current_update_time - last_hddtemp_update < 5)
209                 return;
210         last_hddtemp_update = current_update_time;
211
212         free_hddtemp_info();
213
214         if (!(data = fetch_hddtemp_output()))
215                 return;
216
217         if (read_hdd_val(data, &dev, &val, &unit, &saveptr)) {
218                 free(data);
219                 return;
220         }
221         do {
222                 add_hddtemp_info(dev, val, unit);
223         } while (!read_hdd_val(NULL, &dev, &val, &unit, &saveptr));
224         free(data);
225 }
226
227 void free_hddtemp(void)
228 {
229         free_hddtemp_info();
230         if (hddtemp_host) {
231                 free(hddtemp_host);
232                 hddtemp_host = NULL;
233         }
234         if (hddtemp_port) {
235                 free(hddtemp_port);
236                 hddtemp_port = NULL;
237         }
238 }
239
240 int get_hddtemp_info(const char *dev, short *val, char *unit)
241 {
242         struct hdd_info *hdi = hdd_info_head.next;
243
244         /* if no dev is given, just use hdd_info_head->next */
245         while(dev && hdi) {
246                 if (!strcmp(dev, hdi->dev))
247                         break;
248                 hdi = hdi->next;
249         }
250         if (!hdi)
251                 return 1;
252         
253         *val = hdi->temp;
254         *unit = hdi->unit;
255         return 0;
256 }