1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
3 * Conky, a system monitor, based on torsmo
5 * Any original torsmo code is licensed under the BSD license
7 * All code written since the fork of torsmo is licensed under the GPL
9 * Please see COPYING for details
11 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14 * All rights reserved.
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.
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/>.
28 * vim: ts=4 sw=4 noet ai cindent syntax=c
38 #include <sys/select.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
43 #define DEFAULT_PORT "7634"
44 #define DEFAULT_HOST "127.0.0.1"
46 static char *hddtemp_host = NULL;
47 static char *hddtemp_port = NULL;
49 static struct hdd_info {
50 struct hdd_info *next;
58 void set_hddtemp_host(const char *host)
62 hddtemp_host = strdup(host);
65 void set_hddtemp_port(const char *port)
69 hddtemp_port = strdup(port);
72 static void __free_hddtemp_info(struct hdd_info *hdi)
75 __free_hddtemp_info(hdi->next);
80 static void free_hddtemp_info(void)
82 DBGP("free_hddtemp_info() called");
83 if (!hdd_info_head.next)
85 __free_hddtemp_info(hdd_info_head.next);
86 hdd_info_head.next = NULL;
89 static void add_hddtemp_info(char *dev, short temp, char unit)
91 struct hdd_info *hdi = &hdd_info_head;
93 DBGP("add_hddtemp_info(%s, %d, %c) being called", dev, temp, unit);
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;
104 static char *fetch_hddtemp_output(void)
107 const char *dst_host, *dst_port;
109 int buflen, offset = 0, rlen;
110 struct addrinfo hints, *result, *rp;
113 dst_host = hddtemp_host ? hddtemp_host : DEFAULT_HOST;
114 dst_port = hddtemp_port ? hddtemp_port : DEFAULT_PORT;
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;
120 if ((i = getaddrinfo(dst_host, dst_port, &hints, &result))) {
121 NORM_ERR("getaddrinfo(): %s", gai_strerror(i));
125 for (rp = result; rp; rp = rp->ai_next) {
126 sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
129 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
134 NORM_ERR("could not connect to mpd host");
139 buf = malloc(buflen);
140 while ((rlen = recv(sockfd, buf + offset, buflen - offset - 1, 0)) > 0) {
142 if (buflen - offset < 1) {
144 buf = realloc(buf, buflen);
154 freeaddrinfo(result);
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!
163 static int read_hdd_val(const char *line, char **dev, short *val, char *unit,
166 char *line_s, *cval, *endptr;
170 *saveptr = strdup(line);
175 /* read the device */
177 if (!(p = strchr(p, line_s[0])))
180 /* jump over the devname */
181 if (!(p = strchr(p, line_s[0])))
185 if (!(p = strchr(p, line_s[0])))
189 *val = strtol(cval, &endptr, 10);
193 /* preset p for next call */
202 void update_hddtemp(void) {
203 char *data, *dev, unit, *saveptr;
205 static double last_hddtemp_update = 0.0;
207 /* limit tcp connection overhead */
208 if (current_update_time - last_hddtemp_update < 5)
210 last_hddtemp_update = current_update_time;
214 if (!(data = fetch_hddtemp_output()))
217 if (read_hdd_val(data, &dev, &val, &unit, &saveptr)) {
222 add_hddtemp_info(dev, val, unit);
223 } while (!read_hdd_val(NULL, &dev, &val, &unit, &saveptr));
227 void free_hddtemp(void)
240 int get_hddtemp_info(const char *dev, short *val, char *unit)
242 struct hdd_info *hdi = hdd_info_head.next;
244 /* if no dev is given, just use hdd_info_head->next */
246 if (!strcmp(dev, hdi->dev))