1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2 * vim: ts=4 sw=4 noet ai cindent syntax=c
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #include <sys/select.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
42 #define DEFAULT_PORT "7634"
43 #define DEFAULT_HOST "127.0.0.1"
45 static char *hddtemp_host = NULL;
46 static char *hddtemp_port = NULL;
48 static struct hdd_info {
49 struct hdd_info *next;
57 void set_hddtemp_host(const char *host)
61 hddtemp_host = strdup(host);
64 void set_hddtemp_port(const char *port)
68 hddtemp_port = strdup(port);
71 static void __free_hddtemp_info(struct hdd_info *hdi)
74 __free_hddtemp_info(hdi->next);
79 static void free_hddtemp_info(void)
81 DBGP("free_hddtemp_info() called");
82 if (!hdd_info_head.next)
84 __free_hddtemp_info(hdd_info_head.next);
85 hdd_info_head.next = NULL;
88 static void add_hddtemp_info(char *dev, short temp, char unit)
90 struct hdd_info *hdi = &hdd_info_head;
92 DBGP("add_hddtemp_info(%s, %d, %c) being called", dev, temp, unit);
96 hdi->next = malloc(sizeof(struct hdd_info));
97 memset(hdi->next, 0, sizeof(struct hdd_info));
98 hdi->next->dev = strdup(dev);
99 hdi->next->temp = temp;
100 hdi->next->unit = unit;
103 static char *fetch_hddtemp_output(void)
106 const char *dst_host, *dst_port;
108 int buflen, offset = 0, rlen;
109 struct addrinfo hints, *result, *rp;
112 dst_host = hddtemp_host ? hddtemp_host : DEFAULT_HOST;
113 dst_port = hddtemp_port ? hddtemp_port : DEFAULT_PORT;
115 memset(&hints, 0, sizeof(hints));
116 hints.ai_family = AF_INET; /* XXX: hddtemp has no ipv6 support (yet?) */
117 hints.ai_socktype = SOCK_STREAM;
119 if ((i = getaddrinfo(dst_host, dst_port, &hints, &result))) {
120 NORM_ERR("getaddrinfo(): %s", gai_strerror(i));
124 for (rp = result; rp; rp = rp->ai_next) {
125 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
128 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
133 NORM_ERR("could not connect to hddtemp host");
138 buf = malloc(buflen);
139 while ((rlen = recv(sockfd, buf + offset, buflen - offset - 1, 0)) > 0) {
141 if (buflen - offset < 1) {
143 buf = realloc(buf, buflen);
153 freeaddrinfo(result);
157 /* this is an iterator:
158 * set line to NULL in consecutive calls to get the next field
159 * note that exhausing iteration is assumed - otherwise *saveptr
160 * is not being freed!
162 static int read_hdd_val(const char *line, char **dev, short *val, char *unit,
165 char *line_s, *cval, *endptr;
169 *saveptr = strdup(line);
177 /* read the device */
179 if (!(p = strchr(p, line_s[0])))
182 /* jump over the devname */
183 if (!(p = strchr(p, line_s[0])))
187 if (!(p = strchr(p, line_s[0])))
191 *val = strtol(cval, &endptr, 10);
193 if (!(p = strchr(p, line_s[0])))
200 /* preset p for next call */
209 int update_hddtemp(void) {
210 char *data, *dev, unit, *saveptr;
212 static double last_hddtemp_update = 0.0;
214 /* limit tcp connection overhead */
215 if (current_update_time - last_hddtemp_update < 5)
217 last_hddtemp_update = current_update_time;
221 if (!(data = fetch_hddtemp_output()))
224 if (read_hdd_val(data, &dev, &val, &unit, &saveptr)) {
229 add_hddtemp_info(dev, val, unit);
230 } while (!read_hdd_val(NULL, &dev, &val, &unit, &saveptr));
235 void free_hddtemp(void)
248 int get_hddtemp_info(const char *dev, short *val, char *unit)
250 struct hdd_info *hdi = hdd_info_head.next;
252 /* if no dev is given, just use hdd_info_head->next */
254 if (!strcmp(dev, hdi->dev))