2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
2009-09-12 10:50:51 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2007-08-10 19:53:44 +00:00
|
|
|
|
2006-08-10 23:49:59 +00:00
|
|
|
#include "conky.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "logging.h"
|
2009-11-08 16:12:37 +00:00
|
|
|
#include "temphelper.h"
|
2009-11-08 03:18:19 +00:00
|
|
|
#include "text_object.h"
|
2006-08-11 19:13:32 +00:00
|
|
|
#include <errno.h>
|
2006-08-10 23:49:59 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
2006-08-11 15:40:21 +00:00
|
|
|
#include <netinet/in.h>
|
2006-08-10 23:49:59 +00:00
|
|
|
|
|
|
|
#define BUFLEN 512
|
2009-09-06 19:53:53 +00:00
|
|
|
#define DEFAULT_PORT "7634"
|
|
|
|
#define DEFAULT_HOST "127.0.0.1"
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
static char *hddtemp_host = NULL;
|
|
|
|
static char *hddtemp_port = NULL;
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
static struct hdd_info {
|
|
|
|
struct hdd_info *next;
|
|
|
|
char *dev;
|
|
|
|
short temp;
|
|
|
|
char unit;
|
|
|
|
} hdd_info_head = {
|
|
|
|
.next = NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_hddtemp_host(const char *host)
|
2006-08-10 23:49:59 +00:00
|
|
|
{
|
2009-09-06 19:53:53 +00:00
|
|
|
if (hddtemp_host)
|
|
|
|
free(hddtemp_host);
|
|
|
|
hddtemp_host = strdup(host);
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
void set_hddtemp_port(const char *port)
|
|
|
|
{
|
|
|
|
if (hddtemp_port)
|
|
|
|
free(hddtemp_port);
|
|
|
|
hddtemp_port = strdup(port);
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
static void __free_hddtemp_info(struct hdd_info *hdi)
|
|
|
|
{
|
|
|
|
if (hdi->next)
|
|
|
|
__free_hddtemp_info(hdi->next);
|
|
|
|
free(hdi->dev);
|
|
|
|
free(hdi);
|
|
|
|
}
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
static void free_hddtemp_info(void)
|
|
|
|
{
|
|
|
|
DBGP("free_hddtemp_info() called");
|
|
|
|
if (!hdd_info_head.next)
|
|
|
|
return;
|
|
|
|
__free_hddtemp_info(hdd_info_head.next);
|
|
|
|
hdd_info_head.next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_hddtemp_info(char *dev, short temp, char unit)
|
|
|
|
{
|
|
|
|
struct hdd_info *hdi = &hdd_info_head;
|
|
|
|
|
|
|
|
DBGP("add_hddtemp_info(%s, %d, %c) being called", dev, temp, unit);
|
|
|
|
while (hdi->next)
|
|
|
|
hdi = hdi->next;
|
|
|
|
|
|
|
|
hdi->next = malloc(sizeof(struct hdd_info));
|
|
|
|
memset(hdi->next, 0, sizeof(struct hdd_info));
|
|
|
|
hdi->next->dev = strdup(dev);
|
|
|
|
hdi->next->temp = temp;
|
|
|
|
hdi->next->unit = unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *fetch_hddtemp_output(void)
|
|
|
|
{
|
|
|
|
int sockfd;
|
|
|
|
const char *dst_host, *dst_port;
|
|
|
|
char *buf = NULL;
|
|
|
|
int buflen, offset = 0, rlen;
|
|
|
|
struct addrinfo hints, *result, *rp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
dst_host = hddtemp_host ? hddtemp_host : DEFAULT_HOST;
|
|
|
|
dst_port = hddtemp_port ? hddtemp_port : DEFAULT_PORT;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET; /* XXX: hddtemp has no ipv6 support (yet?) */
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
if ((i = getaddrinfo(dst_host, dst_port, &hints, &result))) {
|
|
|
|
NORM_ERR("getaddrinfo(): %s", gai_strerror(i));
|
|
|
|
return NULL;
|
2008-03-31 15:33:54 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
for (rp = result; rp; rp = rp->ai_next) {
|
|
|
|
sockfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
|
|
|
if (sockfd == -1)
|
|
|
|
continue;
|
|
|
|
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) != -1)
|
|
|
|
break;
|
|
|
|
close(sockfd);
|
|
|
|
}
|
|
|
|
if (!rp) {
|
|
|
|
NORM_ERR("could not connect to mpd host");
|
|
|
|
goto GET_OUT;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
buflen = 1024;
|
|
|
|
buf = malloc(buflen);
|
|
|
|
while ((rlen = recv(sockfd, buf + offset, buflen - offset - 1, 0)) > 0) {
|
|
|
|
offset += rlen;
|
|
|
|
if (buflen - offset < 1) {
|
|
|
|
buflen += 1024;
|
|
|
|
buf = realloc(buf, buflen);
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2009-09-06 19:53:53 +00:00
|
|
|
if (rlen < 0)
|
|
|
|
perror("recv");
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
buf[offset] = '\0';
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
GET_OUT:
|
|
|
|
freeaddrinfo(result);
|
|
|
|
return buf;
|
2006-08-10 23:49:59 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 15:01:15 +00:00
|
|
|
/* this is an iterator:
|
|
|
|
* set line to NULL in consecutive calls to get the next field
|
2009-09-06 19:53:53 +00:00
|
|
|
* note that exhausing iteration is assumed - otherwise *saveptr
|
|
|
|
* is not being freed!
|
2008-12-08 15:01:15 +00:00
|
|
|
*/
|
2009-09-06 19:53:53 +00:00
|
|
|
static int read_hdd_val(const char *line, char **dev, short *val, char *unit,
|
|
|
|
char **saveptr)
|
2008-12-08 15:01:15 +00:00
|
|
|
{
|
2009-09-06 19:53:53 +00:00
|
|
|
char *line_s, *cval, *endptr;
|
2008-12-08 15:01:15 +00:00
|
|
|
static char *p = 0;
|
|
|
|
|
|
|
|
if (line) {
|
2009-09-06 19:53:53 +00:00
|
|
|
*saveptr = strdup(line);
|
|
|
|
p = *saveptr;
|
2008-12-08 15:01:15 +00:00
|
|
|
}
|
2009-09-06 19:53:53 +00:00
|
|
|
line_s = *saveptr;
|
|
|
|
|
2008-12-08 15:01:15 +00:00
|
|
|
/* read the device */
|
2009-09-06 19:53:53 +00:00
|
|
|
*dev = ++p;
|
2008-12-08 15:01:15 +00:00
|
|
|
if (!(p = strchr(p, line_s[0])))
|
2009-09-06 19:53:53 +00:00
|
|
|
goto out_fail;
|
2008-12-08 15:01:15 +00:00
|
|
|
*(p++) = '\0';
|
|
|
|
/* jump over the devname */
|
|
|
|
if (!(p = strchr(p, line_s[0])))
|
2009-09-06 19:53:53 +00:00
|
|
|
goto out_fail;
|
2008-12-08 15:01:15 +00:00
|
|
|
/* read the value */
|
2009-09-06 19:53:53 +00:00
|
|
|
cval = ++p;
|
2008-12-08 15:01:15 +00:00
|
|
|
if (!(p = strchr(p, line_s[0])))
|
2009-09-06 19:53:53 +00:00
|
|
|
goto out_fail;
|
2008-12-08 15:01:15 +00:00
|
|
|
*(p++) = '\0';
|
2009-09-06 19:53:53 +00:00
|
|
|
*unit = *(p++);
|
|
|
|
*val = strtol(cval, &endptr, 10);
|
|
|
|
if (*endptr)
|
|
|
|
goto out_fail;
|
|
|
|
|
2008-12-08 15:01:15 +00:00
|
|
|
/* preset p for next call */
|
2009-09-06 19:53:53 +00:00
|
|
|
p++;
|
2008-12-08 15:01:15 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
return 0;
|
|
|
|
out_fail:
|
|
|
|
free(*saveptr);
|
|
|
|
return 1;
|
2008-12-08 15:01:15 +00:00
|
|
|
}
|
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
void update_hddtemp(void) {
|
|
|
|
char *data, *dev, unit, *saveptr;
|
|
|
|
short val;
|
|
|
|
static double last_hddtemp_update = 0.0;
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
/* limit tcp connection overhead */
|
|
|
|
if (current_update_time - last_hddtemp_update < 5)
|
|
|
|
return;
|
|
|
|
last_hddtemp_update = current_update_time;
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
free_hddtemp_info();
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
if (!(data = fetch_hddtemp_output()))
|
|
|
|
return;
|
2006-08-10 23:49:59 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
if (read_hdd_val(data, &dev, &val, &unit, &saveptr)) {
|
|
|
|
free(data);
|
|
|
|
return;
|
2008-12-08 15:01:15 +00:00
|
|
|
}
|
|
|
|
do {
|
2009-09-06 19:53:53 +00:00
|
|
|
add_hddtemp_info(dev, val, unit);
|
|
|
|
} while (!read_hdd_val(NULL, &dev, &val, &unit, &saveptr));
|
|
|
|
free(data);
|
|
|
|
}
|
2008-04-10 22:45:45 +00:00
|
|
|
|
2009-11-08 03:18:19 +00:00
|
|
|
void free_hddtemp(struct text_object *obj)
|
2009-09-06 19:53:53 +00:00
|
|
|
{
|
|
|
|
free_hddtemp_info();
|
|
|
|
if (hddtemp_host) {
|
|
|
|
free(hddtemp_host);
|
|
|
|
hddtemp_host = NULL;
|
2008-12-08 15:01:15 +00:00
|
|
|
}
|
2009-09-06 19:53:53 +00:00
|
|
|
if (hddtemp_port) {
|
|
|
|
free(hddtemp_port);
|
|
|
|
hddtemp_port = NULL;
|
|
|
|
}
|
2009-11-08 03:18:19 +00:00
|
|
|
if (obj->data.s) {
|
|
|
|
free(obj->data.s);
|
|
|
|
obj->data.s = NULL;
|
|
|
|
}
|
2009-09-06 19:53:53 +00:00
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2009-11-08 16:12:37 +00:00
|
|
|
static int get_hddtemp_info(const char *dev, short *val, char *unit)
|
2009-09-06 19:53:53 +00:00
|
|
|
{
|
|
|
|
struct hdd_info *hdi = hdd_info_head.next;
|
2008-12-08 15:01:15 +00:00
|
|
|
|
2009-09-06 19:53:53 +00:00
|
|
|
/* if no dev is given, just use hdd_info_head->next */
|
|
|
|
while(dev && hdi) {
|
|
|
|
if (!strcmp(dev, hdi->dev))
|
|
|
|
break;
|
|
|
|
hdi = hdi->next;
|
|
|
|
}
|
|
|
|
if (!hdi)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
*val = hdi->temp;
|
|
|
|
*unit = hdi->unit;
|
|
|
|
return 0;
|
2006-08-10 23:49:59 +00:00
|
|
|
}
|
2009-11-08 16:12:37 +00:00
|
|
|
|
|
|
|
void print_hddtemp(struct text_object *obj, char *p, int p_max_size)
|
|
|
|
{
|
|
|
|
short val;
|
|
|
|
char unit;
|
|
|
|
|
|
|
|
if (get_hddtemp_info(obj->data.s, &val, &unit)) {
|
|
|
|
snprintf(p, p_max_size, "N/A");
|
|
|
|
} else {
|
|
|
|
temp_print(p, p_max_size, (double)val,
|
|
|
|
(unit == 'C' ? TEMP_CELSIUS : TEMP_FAHRENHEIT));
|
|
|
|
}
|
|
|
|
}
|