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
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
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/>.
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2005-08-05 01:06:17 +00:00
|
|
|
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "config.h"
|
2009-08-07 07:24:24 +00:00
|
|
|
#include "conky.h"
|
2008-12-15 21:40:24 +00:00
|
|
|
#include "fs.h"
|
|
|
|
#include "logging.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
2009-03-28 23:15:20 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <netinet/in.h>
|
2007-08-30 17:21:30 +00:00
|
|
|
#include <pthread.h>
|
2009-03-30 05:35:42 +00:00
|
|
|
#include <unistd.h>
|
2008-12-25 15:36:29 +00:00
|
|
|
#include "diskio.h"
|
2009-07-22 20:08:31 +00:00
|
|
|
#include <fcntl.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-12-15 21:40:24 +00:00
|
|
|
/* check for OS and include appropriate headers */
|
|
|
|
#if defined(__linux__)
|
|
|
|
#include "linux.h"
|
|
|
|
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
|
|
|
#include "freebsd.h"
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
#include "openbsd.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-21 21:41:47 +00:00
|
|
|
/* folds a string over top of itself, like so:
|
|
|
|
*
|
|
|
|
* if start is "blah", and you call it with count = 1, the result will be "lah"
|
|
|
|
*/
|
|
|
|
void strfold(char *start, int count)
|
|
|
|
{
|
|
|
|
char *curplace;
|
|
|
|
for (curplace = start + count; *curplace != 0; curplace++) {
|
|
|
|
*(curplace - count) = *curplace;
|
|
|
|
}
|
|
|
|
*(curplace - count) = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-02 19:46:09 +00:00
|
|
|
#ifndef HAVE_STRNDUP
|
|
|
|
// use our own strndup() if it's not available
|
|
|
|
char *strndup(const char *s, size_t n)
|
|
|
|
{
|
2008-12-08 23:12:23 +00:00
|
|
|
if (strlen(s) > n) {
|
|
|
|
char *ret = malloc(n + 1);
|
2008-04-02 19:46:09 +00:00
|
|
|
strncpy(ret, s, n);
|
2008-12-08 23:12:23 +00:00
|
|
|
ret[n] = 0;
|
2008-04-02 19:46:09 +00:00
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return strdup(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_STRNDUP */
|
|
|
|
|
2009-08-07 07:24:24 +00:00
|
|
|
void update_uname(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2009-08-07 07:24:24 +00:00
|
|
|
uname(&info.uname_s);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
double get_time(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
gettimeofday(&tv, 0);
|
2005-11-27 06:56:35 +00:00
|
|
|
return tv.tv_sec + (tv.tv_usec / 1000000.0);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2009-05-25 04:33:47 +00:00
|
|
|
/* Converts '~/...' paths to '/home/blah/...' assumes that 'dest' is at least
|
|
|
|
* DEFAULT_TEXT_BUFFER_SIZE. It's similar to variable_substitute, except only
|
|
|
|
* cheques for $HOME and ~/ in path */
|
|
|
|
void to_real_path(char *dest, const char *source)
|
|
|
|
{
|
|
|
|
char tmp[DEFAULT_TEXT_BUFFER_SIZE];
|
|
|
|
if (sscanf(source, "~/%s", tmp) || sscanf(source, "$HOME/%s", tmp)) {
|
|
|
|
char *homedir = getenv("HOME");
|
|
|
|
if (homedir) {
|
|
|
|
snprintf(dest, DEFAULT_TEXT_BUFFER_SIZE, "%s/%s", homedir, tmp);
|
|
|
|
} else {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("$HOME environment variable doesn't exist");
|
2009-05-25 04:33:47 +00:00
|
|
|
strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
|
|
|
|
}
|
2009-08-07 07:24:24 +00:00
|
|
|
} else if (dest != source) { //see changelog 2009-06-29 if you doubt that this check is necessary
|
2009-05-25 04:33:47 +00:00
|
|
|
strncpy(dest, source, DEFAULT_TEXT_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-22 20:08:31 +00:00
|
|
|
int open_fifo(const char *file, int *reported)
|
|
|
|
{
|
|
|
|
char path[DEFAULT_TEXT_BUFFER_SIZE];
|
|
|
|
int fd = 0;
|
|
|
|
|
|
|
|
to_real_path(path, file);
|
|
|
|
fd = open(file, O_RDONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
if (!reported || *reported == 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("can't open %s: %s", file, strerror(errno));
|
2009-07-22 20:08:31 +00:00
|
|
|
if (reported) {
|
|
|
|
*reported = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
FILE *open_file(const char *file, int *reported)
|
|
|
|
{
|
2009-05-25 04:33:47 +00:00
|
|
|
char path[DEFAULT_TEXT_BUFFER_SIZE];
|
|
|
|
FILE *fp = 0;
|
|
|
|
|
|
|
|
to_real_path(path, file);
|
|
|
|
fp = fopen(file, "r");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (!fp) {
|
|
|
|
if (!reported || *reported == 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("can't open %s: %s", file, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reported) {
|
2005-07-20 00:30:40 +00:00
|
|
|
*reported = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2009-07-22 20:08:31 +00:00
|
|
|
return NULL;
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void variable_substitute(const char *s, char *dest, unsigned int n)
|
|
|
|
{
|
|
|
|
while (*s && n > 1) {
|
|
|
|
if (*s == '$') {
|
|
|
|
s++;
|
|
|
|
if (*s != '$') {
|
|
|
|
char buf[256];
|
|
|
|
const char *a, *var;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
/* variable is either $foo or ${foo} */
|
|
|
|
if (*s == '{') {
|
|
|
|
s++;
|
|
|
|
a = s;
|
2008-02-20 20:30:45 +00:00
|
|
|
while (*s && *s != '}') {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
} else {
|
|
|
|
a = s;
|
2008-02-20 20:30:45 +00:00
|
|
|
while (*s && (isalnum((int) *s) || *s == '_')) {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy variable to buffer and look it up */
|
|
|
|
len = (s - a > 255) ? 255 : (s - a);
|
|
|
|
strncpy(buf, a, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (*s == '}') {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
var = getenv(buf);
|
|
|
|
|
|
|
|
if (var) {
|
|
|
|
/* add var to dest */
|
|
|
|
len = strlen(var);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (len >= n) {
|
2005-07-20 00:30:40 +00:00
|
|
|
len = n - 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
strncpy(dest, var, len);
|
|
|
|
dest += len;
|
|
|
|
n -= len;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest++ = *s++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* network interface stuff */
|
|
|
|
|
|
|
|
static struct net_stat netstats[16];
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!dev) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/* find interface stat */
|
|
|
|
for (i = 0; i < 16; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return &netstats[i];
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wasn't found? add it */
|
2009-05-28 13:43:15 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (netstats[i].dev == 0) {
|
|
|
|
netstats[i].dev = strndup(dev, text_buffer_size);
|
|
|
|
return &netstats[i];
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:21:56 +00:00
|
|
|
CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void clear_net_stats(void)
|
2006-12-23 06:01:16 +00:00
|
|
|
{
|
2009-05-28 13:40:59 +00:00
|
|
|
int i;
|
2009-05-29 03:22:04 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (netstats[i].dev) {
|
2009-05-28 13:40:59 +00:00
|
|
|
free(netstats[i].dev);
|
2009-05-29 03:22:04 +00:00
|
|
|
}
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(netstats, 0, sizeof(netstats));
|
2006-12-23 06:01:16 +00:00
|
|
|
}
|
|
|
|
|
2009-03-28 23:15:20 +00:00
|
|
|
/* We should check if this is ok with OpenBSD and NetBSD as well. */
|
2009-08-07 07:21:56 +00:00
|
|
|
int interface_up(const char *dev)
|
2009-03-28 23:15:20 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
2009-08-07 07:21:56 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "could not create sockfd");
|
2009-03-28 23:15:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
|
|
|
/* if device does not exist, treat like not up */
|
|
|
|
if (errno != ENODEV && errno != ENXIO)
|
|
|
|
perror("SIOCGIFFLAGS");
|
|
|
|
goto END_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
|
|
|
|
goto END_FALSE;
|
|
|
|
if (ifup_strictness == IFUP_UP)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
if (!(ifr.ifr_flags & IFF_RUNNING))
|
|
|
|
goto END_FALSE;
|
|
|
|
if (ifup_strictness == IFUP_LINK)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGIFADDR, &ifr)) {
|
|
|
|
perror("SIOCGIFADDR");
|
|
|
|
goto END_FALSE;
|
|
|
|
}
|
|
|
|
if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
END_FALSE:
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
END_TRUE:
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:24:24 +00:00
|
|
|
void free_dns_data(void)
|
2008-03-22 22:58:26 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-08-07 07:24:24 +00:00
|
|
|
struct dns_data *data = &info.nameserver_info;
|
2008-03-22 22:58:26 +00:00
|
|
|
for (i = 0; i < data->nscount; i++)
|
|
|
|
free(data->ns_list[i]);
|
|
|
|
if (data->ns_list)
|
|
|
|
free(data->ns_list);
|
|
|
|
memset(data, 0, sizeof(struct dns_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
//static double last_dns_update;
|
|
|
|
|
2009-09-06 22:14:54 +00:00
|
|
|
void update_dns_data(void)
|
2008-03-22 22:58:26 +00:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[256];
|
2009-08-07 07:24:24 +00:00
|
|
|
struct dns_data *data = &info.nameserver_info;
|
2008-03-22 22:58:26 +00:00
|
|
|
|
|
|
|
/* maybe updating too often causes higher load because of /etc lying on a real FS
|
2009-08-07 07:24:24 +00:00
|
|
|
if (current_update_time - last_dns_update < 10.0)
|
2008-03-22 22:58:26 +00:00
|
|
|
return;
|
|
|
|
else
|
2009-08-07 07:24:24 +00:00
|
|
|
last_dns_update = current_update_time;
|
2008-03-22 22:58:26 +00:00
|
|
|
*/
|
|
|
|
|
2009-08-07 07:24:24 +00:00
|
|
|
free_dns_data();
|
2008-03-22 22:58:26 +00:00
|
|
|
|
|
|
|
if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
|
|
|
|
return;
|
|
|
|
while(!feof(fp)) {
|
2008-04-10 22:45:45 +00:00
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2008-03-22 22:58:26 +00:00
|
|
|
if (!strncmp(line, "nameserver ", 11)) {
|
|
|
|
line[strlen(line) - 1] = '\0'; // remove trailing newline
|
|
|
|
data->nscount++;
|
|
|
|
data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
|
2008-04-02 18:44:49 +00:00
|
|
|
data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
|
2008-03-22 22:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
void format_seconds(char *buf, unsigned int n, long seconds)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-06-19 07:04:11 +00:00
|
|
|
long days;
|
|
|
|
int hours, minutes;
|
|
|
|
|
|
|
|
days = seconds / 86400;
|
|
|
|
seconds %= 86400;
|
|
|
|
hours = seconds / 3600;
|
|
|
|
seconds %= 3600;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
if (days > 0) {
|
|
|
|
snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2008-06-19 07:04:11 +00:00
|
|
|
snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-06-19 07:04:11 +00:00
|
|
|
void format_seconds_short(char *buf, unsigned int n, long seconds)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-06-19 07:04:11 +00:00
|
|
|
long days;
|
|
|
|
int hours, minutes;
|
|
|
|
|
|
|
|
days = seconds / 86400;
|
|
|
|
seconds %= 86400;
|
|
|
|
hours = seconds / 3600;
|
|
|
|
seconds %= 3600;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds %= 60;
|
|
|
|
|
|
|
|
if (days > 0) {
|
|
|
|
snprintf(buf, n, "%ldd %dh", days, hours);
|
|
|
|
} else if (hours > 0) {
|
|
|
|
snprintf(buf, n, "%dh %dm", hours, minutes);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2008-06-19 07:04:11 +00:00
|
|
|
snprintf(buf, n, "%dm %lds", minutes, seconds);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 22:12:56 +00:00
|
|
|
/* Linked list containing the functions to call upon each update interval.
|
|
|
|
* Populated while initialising text objects in construct_text_object(). */
|
2009-09-06 22:14:54 +00:00
|
|
|
static struct update_cb {
|
|
|
|
struct update_cb *next;
|
|
|
|
void (*func)(void);
|
2009-09-10 23:18:38 +00:00
|
|
|
pthread_t thread;
|
2009-09-06 22:14:54 +00:00
|
|
|
} update_cb_head = {
|
|
|
|
.next = NULL,
|
|
|
|
};
|
|
|
|
|
2009-09-09 22:12:56 +00:00
|
|
|
/* Register an update callback. Don't allow duplicates, to minimise side
|
|
|
|
* effects and overhead. */
|
2009-09-06 22:14:54 +00:00
|
|
|
void add_update_callback(void (*func)(void))
|
|
|
|
{
|
|
|
|
struct update_cb *uc = &update_cb_head;
|
|
|
|
|
2009-09-06 23:22:16 +00:00
|
|
|
if (!func)
|
|
|
|
return;
|
|
|
|
|
2009-09-06 22:14:54 +00:00
|
|
|
while (uc->next) {
|
|
|
|
if (uc->next->func == func)
|
|
|
|
return;
|
|
|
|
uc = uc->next;
|
|
|
|
}
|
|
|
|
uc->next = malloc(sizeof(struct update_cb));
|
|
|
|
memset(uc->next, 0, sizeof(struct update_cb));
|
|
|
|
uc->next->func = func;
|
|
|
|
}
|
|
|
|
|
2009-09-09 22:12:56 +00:00
|
|
|
/* Free the list element uc and all decendants recursively. */
|
2009-09-06 22:14:54 +00:00
|
|
|
static void __free_update_callbacks(struct update_cb *uc)
|
|
|
|
{
|
|
|
|
if (uc->next)
|
|
|
|
__free_update_callbacks(uc->next);
|
|
|
|
free(uc);
|
|
|
|
}
|
|
|
|
|
2009-09-09 22:12:56 +00:00
|
|
|
/* Free the whole list of update callbacks. */
|
2009-09-06 22:14:54 +00:00
|
|
|
void free_update_callbacks(void)
|
|
|
|
{
|
|
|
|
if (update_cb_head.next)
|
|
|
|
__free_update_callbacks(update_cb_head.next);
|
|
|
|
update_cb_head.next = NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-10 23:18:38 +00:00
|
|
|
static void *run_update_callback(void *) __attribute__((noreturn));
|
|
|
|
|
|
|
|
static void *run_update_callback(void *data)
|
|
|
|
{
|
|
|
|
struct update_cb *ucb = data;
|
|
|
|
|
|
|
|
(*ucb->func)();
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2009-08-07 07:24:24 +00:00
|
|
|
int no_buffers;
|
|
|
|
|
|
|
|
void update_stuff(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2009-05-24 20:02:33 +00:00
|
|
|
int i;
|
2009-09-06 22:14:54 +00:00
|
|
|
struct update_cb *uc;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
/* clear speeds and up status in case device was removed and doesn't get
|
2008-02-20 20:30:45 +00:00
|
|
|
* updated */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-05-24 15:41:35 +00:00
|
|
|
#ifdef HAVE_OPENMP
|
2009-07-18 19:46:36 +00:00
|
|
|
#pragma omp parallel for schedule(dynamic,10)
|
2009-05-24 15:41:35 +00:00
|
|
|
#endif /* HAVE_OPENMP */
|
2005-07-20 00:30:40 +00:00
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (netstats[i].dev) {
|
|
|
|
netstats[i].up = 0;
|
|
|
|
netstats[i].recv_speed = 0.0;
|
|
|
|
netstats[i].trans_speed = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_update();
|
|
|
|
|
2009-09-10 23:18:38 +00:00
|
|
|
for (uc = update_cb_head.next; uc; uc = uc->next) {
|
|
|
|
pthread_create(&uc->thread, NULL, &run_update_callback, uc);
|
|
|
|
}
|
|
|
|
/* need to synchronise here, otherwise locking is needed (as data
|
|
|
|
* would be printed with some update callbacks still running) */
|
2009-09-06 22:14:54 +00:00
|
|
|
for (uc = update_cb_head.next; uc; uc = uc->next)
|
2009-09-10 23:18:38 +00:00
|
|
|
pthread_join(uc->thread, NULL);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2009-09-06 23:05:33 +00:00
|
|
|
/* XXX: move the following into the update_meminfo() functions? */
|
|
|
|
if (no_buffers) {
|
|
|
|
info.mem -= info.bufmem;
|
|
|
|
info.memeasyfree += info.bufmem;
|
2008-06-21 20:37:58 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
|
2009-05-18 05:30:10 +00:00
|
|
|
/* Ohkie to return negative values for temperatures */
|
|
|
|
int round_to_int_temp(float f)
|
2005-08-30 14:59:37 +00:00
|
|
|
{
|
2008-02-19 03:33:12 +00:00
|
|
|
if (f >= 0.0) {
|
|
|
|
return (int) (f + 0.5);
|
|
|
|
} else {
|
|
|
|
return (int) (f - 0.5);
|
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
}
|
2009-05-18 05:30:10 +00:00
|
|
|
/* Don't return negative values for cpugraph, bar, gauge, percentage.
|
|
|
|
* Causes unreasonable numbers to show */
|
|
|
|
unsigned int round_to_int(float f)
|
|
|
|
{
|
|
|
|
if (f >= 0.0) {
|
|
|
|
return (int) (f + 0.5);
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2009-07-13 05:31:57 +00:00
|
|
|
|