2008-02-20 20:30:45 +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
|
2007-08-10 20:38:58 +00:00
|
|
|
|
* Copyright (c) 2007 Toni Spets
|
2008-03-31 04:56:39 +00:00
|
|
|
|
* Copyright (c) 2005-2008 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-02-20 20:30:45 +00:00
|
|
|
|
* $Id$ */
|
2005-08-05 01:06:17 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#include "conky.h"
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/sysinfo.h>
|
|
|
|
|
#include <sys/stat.h>
|
2007-01-15 00:02:38 +00:00
|
|
|
|
#ifndef HAVE_CLOCK_GETTIME
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
// #include <assert.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include "top.h"
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
|
#include <net/if.h>
|
2008-03-22 19:06:09 +00:00
|
|
|
|
#include <arpa/inet.h>
|
2008-03-24 03:38:28 +00:00
|
|
|
|
#ifdef _NET_IF_H
|
|
|
|
|
#define _LINUX_IF_H
|
|
|
|
|
#endif
|
2008-03-22 19:06:09 +00:00
|
|
|
|
#include <linux/route.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#include <math.h>
|
|
|
|
|
|
2007-08-07 12:51:08 +00:00
|
|
|
|
#ifdef HAVE_IWLIB
|
|
|
|
|
#include <iwlib.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
#define SHORTSTAT_TEMPL "%*s %llu %llu %llu"
|
|
|
|
|
#define LONGSTAT_TEMPL "%*s %llu %llu %llu "
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
static int show_nice_processes;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* This flag tells the linux routines to use the /proc system where possible,
|
|
|
|
|
* even if other api's are available, e.g. sysinfo() or getloadavg().
|
|
|
|
|
* the reason for this is to allow for /proc-based distributed monitoring.
|
|
|
|
|
* using a flag in this manner creates less confusing code. */
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int prefer_proc = 0;
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void prepare_update(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_uptime(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
#ifdef HAVE_SYSINFO
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!prefer_proc) {
|
|
|
|
|
struct sysinfo s_info;
|
|
|
|
|
|
|
|
|
|
sysinfo(&s_info);
|
|
|
|
|
info.uptime = (double) s_info.uptime;
|
|
|
|
|
} else
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#endif
|
2008-02-20 20:30:45 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep = 0;
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if (!(fp = open_file("/proc/uptime", &rep))) {
|
|
|
|
|
info.uptime = 0.0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%lf", &info.uptime);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
info.mask |= (1 << INFO_UPTIME);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
int check_mount(char *s)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
FILE *mtab = fopen("/etc/mtab", "r");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (mtab) {
|
|
|
|
|
char buf1[256], buf2[128];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
while (fgets(buf1, 256, mtab)) {
|
|
|
|
|
sscanf(buf1, "%*s %128s", buf2);
|
|
|
|
|
if (!strcmp(s, buf2)) {
|
|
|
|
|
ret = 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(mtab);
|
|
|
|
|
} else {
|
|
|
|
|
ERR("Could not open mtab");
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* these things are also in sysinfo except Buffers:
|
|
|
|
|
* (that's why I'm reading them from proc) */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_meminfo(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
FILE *meminfo_fp;
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
/* unsigned int a; */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
|
2008-06-21 20:37:58 +00:00
|
|
|
|
info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
while (!feof(meminfo_fp)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(buf, 255, meminfo_fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
if (strncmp(buf, "MemTotal:", 9) == 0) {
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.memmax);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "MemFree:", 8) == 0) {
|
2008-06-21 20:37:58 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.memfree);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "SwapTotal:", 10) == 0) {
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.swapmax);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "SwapFree:", 9) == 0) {
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.swap);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "Buffers:", 8) == 0) {
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.buffers);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else if (strncmp(buf, "Cached:", 7) == 0) {
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(buf, "%*s %llu", &info.cached);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-06-21 20:37:58 +00:00
|
|
|
|
info.mem = info.memmax - info.memfree;
|
|
|
|
|
info.memeasyfree = info.memfree;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
info.swap = info.swapmax - info.swap;
|
|
|
|
|
|
|
|
|
|
info.bufmem = info.cached + info.buffers;
|
|
|
|
|
|
|
|
|
|
info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(meminfo_fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
int get_laptop_mode(void)
|
2008-03-22 20:13:51 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
int val = -1;
|
|
|
|
|
|
|
|
|
|
if ((fp = fopen("/proc/sys/vm/laptop_mode", "r")) != NULL)
|
|
|
|
|
fscanf(fp, "%d\n", &val);
|
2008-03-24 20:35:20 +00:00
|
|
|
|
fclose(fp);
|
2008-03-22 20:13:51 +00:00
|
|
|
|
return val;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-22 21:10:43 +00:00
|
|
|
|
/* my system says:
|
|
|
|
|
* # cat /sys/block/sda/queue/scheduler
|
|
|
|
|
* noop [anticipatory] cfq
|
|
|
|
|
*/
|
|
|
|
|
char *get_ioscheduler(char *disk)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
|
|
if (!disk)
|
2008-04-02 18:44:49 +00:00
|
|
|
|
return strndup("n/a", text_buffer_size);
|
2008-03-22 21:10:43 +00:00
|
|
|
|
|
|
|
|
|
snprintf(buf, 127, "/sys/block/%s/queue/scheduler", disk);
|
|
|
|
|
if ((fp = fopen(buf, "r")) == NULL) {
|
2008-04-02 18:44:49 +00:00
|
|
|
|
return strndup("n/a", text_buffer_size);
|
2008-03-22 21:10:43 +00:00
|
|
|
|
}
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
fscanf(fp, "%127s", buf);
|
|
|
|
|
if (buf[0] == '[') {
|
|
|
|
|
buf[strlen(buf) - 1] = '\0';
|
|
|
|
|
fclose(fp);
|
2008-04-02 18:44:49 +00:00
|
|
|
|
return strndup(buf + 1, text_buffer_size);
|
2008-03-22 21:10:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
2008-04-02 18:44:49 +00:00
|
|
|
|
return strndup("n/a", text_buffer_size);
|
2008-03-22 21:10:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-18 00:23:16 +00:00
|
|
|
|
int interface_up(const char *dev)
|
|
|
|
|
{
|
2008-05-30 11:30:58 +00:00
|
|
|
|
int fd;
|
|
|
|
|
struct ifreq ifr;
|
2008-04-10 22:45:45 +00:00
|
|
|
|
|
2008-05-30 11:30:58 +00:00
|
|
|
|
if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
|
CRIT_ERR("could not create sockfd");
|
2008-04-10 22:45:45 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-05-30 11:30:58 +00:00
|
|
|
|
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
|
|
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
|
|
|
|
|
/* if device does not exist, treat like not up */
|
2008-03-18 00:23:16 +00:00
|
|
|
|
if (errno != ENODEV)
|
|
|
|
|
perror("SIOCGIFFLAGS");
|
2008-05-30 11:30:58 +00:00
|
|
|
|
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;
|
2008-03-18 00:23:16 +00:00
|
|
|
|
}
|
2008-05-30 11:30:58 +00:00
|
|
|
|
if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
|
|
|
|
|
goto END_TRUE;
|
|
|
|
|
|
|
|
|
|
END_FALSE:
|
2008-03-18 00:23:16 +00:00
|
|
|
|
close(fd);
|
|
|
|
|
return 0;
|
2008-05-30 11:30:58 +00:00
|
|
|
|
END_TRUE:
|
|
|
|
|
close(fd);
|
|
|
|
|
return 1;
|
2008-03-18 00:23:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-22 19:06:09 +00:00
|
|
|
|
#define COND_FREE(x) if(x) free(x); x = 0
|
|
|
|
|
#define SAVE_SET_STRING(x, y) \
|
|
|
|
|
if (x && strcmp((char *)x, (char *)y)) { \
|
|
|
|
|
free(x); \
|
2008-04-02 18:44:49 +00:00
|
|
|
|
x = strndup("multiple", text_buffer_size); \
|
2008-03-22 19:06:09 +00:00
|
|
|
|
} else if (!x) { \
|
2008-04-02 18:44:49 +00:00
|
|
|
|
x = strndup(y, text_buffer_size); \
|
2008-03-22 19:06:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-06 12:14:15 +00:00
|
|
|
|
void update_gateway_info_failure(const char *reason)
|
2008-06-04 08:09:49 +00:00
|
|
|
|
{
|
|
|
|
|
if(reason != NULL) {
|
|
|
|
|
perror(reason);
|
|
|
|
|
}
|
|
|
|
|
//2 pointers to 1 location causes a crash when we try to free them both
|
|
|
|
|
info.gw_info.iface = strndup("failed", text_buffer_size);
|
|
|
|
|
info.gw_info.ip = strndup("failed", text_buffer_size);
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_gateway_info(void)
|
2008-03-22 19:06:09 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
struct in_addr ina;
|
|
|
|
|
char iface[64];
|
|
|
|
|
unsigned long dest, gate, mask;
|
|
|
|
|
unsigned int flags;
|
|
|
|
|
short ref, use, metric, mtu, win, irtt;
|
|
|
|
|
|
|
|
|
|
struct gateway_info *gw_info = &info.gw_info;
|
|
|
|
|
|
|
|
|
|
COND_FREE(gw_info->iface);
|
|
|
|
|
COND_FREE(gw_info->ip);
|
|
|
|
|
gw_info->count = 0;
|
|
|
|
|
|
|
|
|
|
if ((fp = fopen("/proc/net/route", "r")) == NULL) {
|
2008-06-04 08:09:49 +00:00
|
|
|
|
update_gateway_info_failure("fopen()");
|
|
|
|
|
return;
|
2008-03-22 19:06:09 +00:00
|
|
|
|
}
|
|
|
|
|
if (fscanf(fp, "%*[^\n]\n") == EOF) {
|
2008-06-04 08:09:49 +00:00
|
|
|
|
//NULL because a empty table is not a error
|
|
|
|
|
update_gateway_info_failure(NULL);
|
2008-04-10 22:45:45 +00:00
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
2008-03-22 19:06:09 +00:00
|
|
|
|
}
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
// Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
|
|
|
|
|
if(fscanf(fp, "%63s %lx %lx %x %hd %hd %hd %lx %hd %hd %hd\n",
|
2008-04-10 22:45:45 +00:00
|
|
|
|
iface, &dest, &gate, &flags, &ref, &use,
|
|
|
|
|
&metric, &mask, &mtu, &win, &irtt) != 11) {
|
2008-06-04 08:09:49 +00:00
|
|
|
|
update_gateway_info_failure("fscanf()");
|
2008-04-10 22:45:45 +00:00
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
2008-03-22 19:06:09 +00:00
|
|
|
|
}
|
|
|
|
|
if (flags & RTF_GATEWAY && dest == 0 && mask == 0) {
|
|
|
|
|
gw_info->count++;
|
|
|
|
|
SAVE_SET_STRING(gw_info->iface, iface)
|
2008-04-10 22:45:45 +00:00
|
|
|
|
ina.s_addr = gate;
|
2008-03-22 19:06:09 +00:00
|
|
|
|
SAVE_SET_STRING(gw_info->ip, inet_ntoa(ina))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-21 14:18:44 +00:00
|
|
|
|
void update_net_stats(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
FILE *net_dev_fp;
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
// FIXME: arbitrary size chosen to keep code simple.
|
|
|
|
|
int i, i2;
|
|
|
|
|
unsigned int curtmp1, curtmp2;
|
|
|
|
|
unsigned int k;
|
|
|
|
|
struct ifconf conf;
|
|
|
|
|
char buf[256];
|
|
|
|
|
double delta;
|
|
|
|
|
|
2007-08-07 12:51:08 +00:00
|
|
|
|
#ifdef HAVE_IWLIB
|
|
|
|
|
// wireless info variables
|
2007-08-07 22:05:06 +00:00
|
|
|
|
int skfd, has_bitrate = 0;
|
2007-08-07 12:51:08 +00:00
|
|
|
|
struct wireless_info *winfo;
|
|
|
|
|
struct iwreq wrq;
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* get delta */
|
|
|
|
|
delta = current_update_time - last_update_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (delta <= 0.0001) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* open file and ignore first two lines */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) {
|
|
|
|
|
clear_net_stats();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
fgets(buf, 255, net_dev_fp); /* garbage */
|
|
|
|
|
fgets(buf, 255, net_dev_fp); /* garbage (field names) */
|
|
|
|
|
|
|
|
|
|
/* read each interface */
|
|
|
|
|
for (i2 = 0; i2 < 16; i2++) {
|
|
|
|
|
struct net_stat *ns;
|
|
|
|
|
char *s, *p;
|
2008-09-08 04:48:13 +00:00
|
|
|
|
char temp_addr[18];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
long long r, t, last_recv, last_trans;
|
|
|
|
|
|
2005-11-19 23:05:30 +00:00
|
|
|
|
if (fgets(buf, 255, net_dev_fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2005-11-19 23:05:30 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
p = buf;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
while (isspace((int) *p)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
p++;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
s = p;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
while (*p && *p != ':') {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
p++;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (*p == '\0') {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
*p = '\0';
|
|
|
|
|
p++;
|
|
|
|
|
|
|
|
|
|
ns = get_net_stat(s);
|
|
|
|
|
ns->up = 1;
|
2005-11-19 23:05:30 +00:00
|
|
|
|
memset(&(ns->addr.sa_data), 0, 14);
|
2008-03-24 20:43:55 +00:00
|
|
|
|
|
|
|
|
|
if(NULL == ns->addrs)
|
2008-09-08 04:48:13 +00:00
|
|
|
|
ns->addrs = (char*) malloc(17 * 16 + 1);
|
2008-03-24 20:43:55 +00:00
|
|
|
|
if(NULL != ns->addrs)
|
2008-09-08 04:48:13 +00:00
|
|
|
|
memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
|
2008-03-24 20:43:55 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
last_recv = ns->recv;
|
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* bytes packets errs drop fifo frame compressed multicast|bytes ... */
|
2008-03-29 02:28:38 +00:00
|
|
|
|
sscanf(p, "%lld %*d %*d %*d %*d %*d %*d %*d %lld",
|
2008-02-20 20:30:45 +00:00
|
|
|
|
&r, &t);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* if recv or trans is less than last time, an overflow happened */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (r < ns->last_read_recv) {
|
2007-08-07 23:18:02 +00:00
|
|
|
|
last_recv = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ns->recv += (r - ns->last_read_recv);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ns->last_read_recv = r;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (t < ns->last_read_trans) {
|
2007-08-07 23:18:02 +00:00
|
|
|
|
last_trans = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ns->trans += (t - ns->last_read_trans);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ns->last_read_trans = t;
|
|
|
|
|
|
|
|
|
|
/*** ip addr patch ***/
|
2005-07-25 01:54:01 +00:00
|
|
|
|
i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
conf.ifc_buf = malloc(sizeof(struct ifreq) * 16);
|
|
|
|
|
conf.ifc_len = sizeof(struct ifreq) * 16;
|
2008-03-24 20:35:20 +00:00
|
|
|
|
memset(conf.ifc_buf, 0, conf.ifc_len);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
ioctl((long) i, SIOCGIFCONF, &conf);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
struct net_stat *ns2;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2008-03-24 20:35:20 +00:00
|
|
|
|
if (!(((struct ifreq *) conf.ifc_buf) + k))
|
|
|
|
|
break;
|
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
ns2 = get_net_stat(
|
2008-02-20 20:30:45 +00:00
|
|
|
|
((struct ifreq *) conf.ifc_buf)[k].ifr_ifrn.ifrn_name);
|
2008-03-29 09:58:09 +00:00
|
|
|
|
ns2->addr = ((struct ifreq *) conf.ifc_buf)[k].ifr_ifru.ifru_addr;
|
|
|
|
|
if(NULL != ns2->addrs) {
|
2008-03-24 20:43:55 +00:00
|
|
|
|
sprintf(temp_addr, "%u.%u.%u.%u, ",
|
2008-03-29 09:58:09 +00:00
|
|
|
|
ns2->addr.sa_data[2] & 255,
|
|
|
|
|
ns2->addr.sa_data[3] & 255,
|
|
|
|
|
ns2->addr.sa_data[4] & 255,
|
|
|
|
|
ns2->addr.sa_data[5] & 255);
|
|
|
|
|
if(NULL == strstr(ns2->addrs, temp_addr))
|
|
|
|
|
strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17);
|
2008-03-24 20:43:55 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
close((long) i);
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
free(conf.ifc_buf);
|
|
|
|
|
|
|
|
|
|
/*** end ip addr patch ***/
|
|
|
|
|
|
|
|
|
|
/* calculate speeds */
|
|
|
|
|
ns->net_rec[0] = (ns->recv - last_recv) / delta;
|
|
|
|
|
ns->net_trans[0] = (ns->trans - last_trans) / delta;
|
|
|
|
|
curtmp1 = 0;
|
|
|
|
|
curtmp2 = 0;
|
|
|
|
|
// get an average
|
|
|
|
|
for (i = 0; (unsigned) i < info.net_avg_samples; i++) {
|
|
|
|
|
curtmp1 += ns->net_rec[i];
|
|
|
|
|
curtmp2 += ns->net_trans[i];
|
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (curtmp1 == 0) {
|
|
|
|
|
curtmp1 = 1;
|
|
|
|
|
}
|
|
|
|
|
if (curtmp2 == 0) {
|
|
|
|
|
curtmp2 = 1;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ns->recv_speed = curtmp1 / (double) info.net_avg_samples;
|
|
|
|
|
ns->trans_speed = curtmp2 / (double) info.net_avg_samples;
|
|
|
|
|
if (info.net_avg_samples > 1) {
|
|
|
|
|
for (i = info.net_avg_samples; i > 1; i--) {
|
|
|
|
|
ns->net_rec[i - 1] = ns->net_rec[i - 2];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
ns->net_trans[i - 1] = ns->net_trans[i - 2];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-07 12:51:08 +00:00
|
|
|
|
#ifdef HAVE_IWLIB
|
|
|
|
|
/* update wireless info */
|
|
|
|
|
winfo = malloc(sizeof(struct wireless_info));
|
|
|
|
|
memset(winfo, 0, sizeof(struct wireless_info));
|
|
|
|
|
|
|
|
|
|
skfd = iw_sockets_open();
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (iw_get_basic_config(skfd, s, &(winfo->b)) > -1) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
|
|
|
|
|
// set present winfo variables
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (iw_get_stats(skfd, s, &(winfo->stats),
|
|
|
|
|
&winfo->range, winfo->has_range) >= 0) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
winfo->has_stats = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (iw_get_range_info(skfd, s, &(winfo->range)) >= 0) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
winfo->has_range = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (iw_get_ext(skfd, s, SIOCGIWAP, &wrq) >= 0) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
winfo->has_ap_addr = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
memcpy(&(winfo->ap_addr), &(wrq.u.ap_addr), sizeof(sockaddr));
|
2007-08-07 12:51:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get bitrate
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (iw_get_ext(skfd, s, SIOCGIWRATE, &wrq) >= 0) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
memcpy(&(winfo->bitrate), &(wrq.u.bitrate), sizeof(iwparam));
|
|
|
|
|
iw_print_bitrate(ns->bitrate, 16, winfo->bitrate.value);
|
|
|
|
|
has_bitrate = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get link quality
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (winfo->has_range && winfo->has_stats
|
|
|
|
|
&& ((winfo->stats.qual.level != 0)
|
|
|
|
|
|| (winfo->stats.qual.updated & IW_QUAL_DBM))) {
|
|
|
|
|
if (!(winfo->stats.qual.updated & IW_QUAL_QUAL_INVALID)) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
ns->link_qual = winfo->stats.qual.qual;
|
|
|
|
|
ns->link_qual_max = winfo->range.max_qual.qual;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get ap mac
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (winfo->has_ap_addr) {
|
2007-08-07 12:51:08 +00:00
|
|
|
|
iw_sawap_ntop(&winfo->ap_addr, ns->ap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get essid
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (winfo->b.has_essid) {
|
|
|
|
|
if (winfo->b.essid_on) {
|
|
|
|
|
snprintf(ns->essid, 32, "%s", winfo->b.essid);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(ns->essid, 32, "off/any");
|
|
|
|
|
}
|
2007-08-07 12:51:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]);
|
|
|
|
|
}
|
|
|
|
|
iw_sockets_close(skfd);
|
|
|
|
|
free(winfo);
|
|
|
|
|
#endif
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-12-23 06:01:16 +00:00
|
|
|
|
fclose(net_dev_fp);
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
info.mask |= (1 << INFO_NET);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int result;
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_total_processes(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
#ifdef HAVE_SYSINFO
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!prefer_proc) {
|
|
|
|
|
struct sysinfo s_info;
|
|
|
|
|
|
|
|
|
|
sysinfo(&s_info);
|
|
|
|
|
info.procs = s_info.procs;
|
|
|
|
|
} else
|
2006-12-23 06:01:16 +00:00
|
|
|
|
#endif
|
2008-02-20 20:30:45 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep = 0;
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if (!(fp = open_file("/proc/loadavg", &rep))) {
|
|
|
|
|
info.procs = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-03-29 03:01:48 +00:00
|
|
|
|
fscanf(fp, "%*f %*f %*f %*d/%hu", &info.procs);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
info.mask |= (1 << INFO_PROCS);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 14:55:10 +00:00
|
|
|
|
#define CPU_SAMPLE_COUNT 15
|
2005-08-27 09:29:24 +00:00
|
|
|
|
struct cpu_info {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
unsigned long long cpu_user;
|
|
|
|
|
unsigned long long cpu_system;
|
|
|
|
|
unsigned long long cpu_nice;
|
|
|
|
|
unsigned long long cpu_idle;
|
|
|
|
|
unsigned long long cpu_iowait;
|
|
|
|
|
unsigned long long cpu_irq;
|
|
|
|
|
unsigned long long cpu_softirq;
|
|
|
|
|
unsigned long long cpu_steal;
|
|
|
|
|
unsigned long long cpu_total;
|
|
|
|
|
unsigned long long cpu_active_total;
|
|
|
|
|
unsigned long long cpu_last_total;
|
|
|
|
|
unsigned long long cpu_last_active_total;
|
2005-08-27 14:55:10 +00:00
|
|
|
|
double cpu_val[CPU_SAMPLE_COUNT];
|
2005-08-27 09:29:24 +00:00
|
|
|
|
};
|
|
|
|
|
static short cpu_setup = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* Determine if this kernel gives us "extended" statistics information in
|
|
|
|
|
* /proc/stat.
|
|
|
|
|
* Kernels around 2.5 and earlier only reported user, system, nice, and
|
|
|
|
|
* idle values in proc stat.
|
|
|
|
|
* Kernels around 2.6 and greater report these PLUS iowait, irq, softirq,
|
|
|
|
|
* and steal */
|
|
|
|
|
void determine_longstat(char *buf)
|
|
|
|
|
{
|
|
|
|
|
unsigned long long iowait = 0;
|
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
KFLAG_SETOFF(KFLAG_IS_LONGSTAT);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* scanf will either return -1 or 1 because there is only 1 assignment */
|
|
|
|
|
if (sscanf(buf, "%*s %*d %*d %*d %*d %llu", &iowait) > 0) {
|
|
|
|
|
KFLAG_SETON(KFLAG_IS_LONGSTAT);
|
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void get_cpu_count(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
FILE *stat_fp;
|
|
|
|
|
static int rep = 0;
|
2008-03-29 06:24:04 +00:00
|
|
|
|
char buf[256];
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2006-05-22 03:26:44 +00:00
|
|
|
|
if (info.cpu_usage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!(stat_fp = open_file("/proc/stat", &rep))) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
info.cpu_count = 0;
|
|
|
|
|
|
|
|
|
|
while (!feof(stat_fp)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(buf, 255, stat_fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-08-27 09:29:24 +00:00
|
|
|
|
if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
if (info.cpu_count == 0) {
|
|
|
|
|
determine_longstat(buf);
|
|
|
|
|
}
|
2005-08-27 09:29:24 +00:00
|
|
|
|
info.cpu_count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-08-27 14:55:10 +00:00
|
|
|
|
info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(stat_fp);
|
2005-08-27 09:29:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-11-27 06:56:35 +00:00
|
|
|
|
#define TMPL_LONGSTAT "%*s %llu %llu %llu %llu %llu %llu %llu %llu"
|
|
|
|
|
#define TMPL_SHORTSTAT "%*s %llu %llu %llu %llu"
|
2005-08-27 09:29:24 +00:00
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
inline static void update_stat(void)
|
2005-08-27 09:29:24 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
FILE *stat_fp;
|
|
|
|
|
static int rep = 0;
|
2005-08-27 09:29:24 +00:00
|
|
|
|
static struct cpu_info *cpu = NULL;
|
|
|
|
|
char buf[256];
|
|
|
|
|
unsigned int i;
|
2008-03-29 09:58:09 +00:00
|
|
|
|
unsigned int idx;
|
2005-08-27 09:29:24 +00:00
|
|
|
|
double curtmp;
|
2008-03-29 03:45:36 +00:00
|
|
|
|
const char *stat_template = NULL;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
unsigned int malloc_cpu_size = 0;
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2006-11-08 03:06:42 +00:00
|
|
|
|
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
|
2006-11-08 00:09:06 +00:00
|
|
|
|
if (!cpu_setup || !info.cpu_usage) {
|
2005-08-27 09:29:24 +00:00
|
|
|
|
get_cpu_count();
|
|
|
|
|
cpu_setup = 1;
|
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2006-12-23 06:01:16 +00:00
|
|
|
|
if (!stat_template) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
stat_template =
|
|
|
|
|
KFLAG_ISSET(KFLAG_IS_LONGSTAT) ? TMPL_LONGSTAT : TMPL_SHORTSTAT;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2006-12-23 06:01:16 +00:00
|
|
|
|
if (!cpu) {
|
|
|
|
|
malloc_cpu_size = (info.cpu_count + 1) * sizeof(struct cpu_info);
|
2005-11-27 06:56:35 +00:00
|
|
|
|
cpu = malloc(malloc_cpu_size);
|
|
|
|
|
memset(cpu, 0, malloc_cpu_size);
|
2005-08-27 09:29:24 +00:00
|
|
|
|
}
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!(stat_fp = open_file("/proc/stat", &rep))) {
|
|
|
|
|
info.run_procs = 0;
|
|
|
|
|
if (info.cpu_usage) {
|
|
|
|
|
memset(info.cpu_usage, 0, info.cpu_count * sizeof(float));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
idx = 0;
|
2005-08-27 09:49:09 +00:00
|
|
|
|
while (!feof(stat_fp)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(buf, 255, stat_fp) == NULL) {
|
2005-08-27 09:29:24 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-08-27 09:29:24 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (strncmp(buf, "procs_running ", 14) == 0) {
|
2005-11-27 06:56:35 +00:00
|
|
|
|
sscanf(buf, "%*s %hu", &info.run_procs);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
info.mask |= (1 << INFO_RUN_PROCS);
|
2005-11-27 06:56:35 +00:00
|
|
|
|
} else if (strncmp(buf, "cpu", 3) == 0) {
|
2008-03-29 06:24:04 +00:00
|
|
|
|
double delta;
|
2008-12-07 22:44:10 +00:00
|
|
|
|
if (isdigit(buf[3])) {
|
|
|
|
|
idx = atoi(&buf[3]);
|
|
|
|
|
} else {
|
|
|
|
|
idx = 0;
|
|
|
|
|
}
|
2008-03-29 09:58:09 +00:00
|
|
|
|
sscanf(buf, stat_template, &(cpu[idx].cpu_user),
|
|
|
|
|
&(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
|
|
|
|
|
&(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
|
|
|
|
|
&(cpu[idx].cpu_irq), &(cpu[idx].cpu_softirq),
|
|
|
|
|
&(cpu[idx].cpu_steal));
|
|
|
|
|
|
|
|
|
|
cpu[idx].cpu_total = cpu[idx].cpu_user + cpu[idx].cpu_nice +
|
|
|
|
|
cpu[idx].cpu_system + cpu[idx].cpu_idle +
|
|
|
|
|
cpu[idx].cpu_iowait + cpu[idx].cpu_irq +
|
|
|
|
|
cpu[idx].cpu_softirq + cpu[idx].cpu_steal;
|
|
|
|
|
|
|
|
|
|
cpu[idx].cpu_active_total = cpu[idx].cpu_total -
|
|
|
|
|
(cpu[idx].cpu_idle + cpu[idx].cpu_iowait);
|
2005-08-27 09:29:24 +00:00
|
|
|
|
info.mask |= (1 << INFO_CPU);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-03-29 06:24:04 +00:00
|
|
|
|
delta = current_update_time - last_update_time;
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (delta <= 0.001) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
cpu[idx].cpu_val[0] = (cpu[idx].cpu_active_total -
|
|
|
|
|
cpu[idx].cpu_last_active_total) /
|
|
|
|
|
(float) (cpu[idx].cpu_total - cpu[idx].cpu_last_total);
|
2005-11-27 06:56:35 +00:00
|
|
|
|
curtmp = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
for (i = 0; i < info.cpu_avg_samples; i++) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
curtmp += cpu[idx].cpu_val[i];
|
2005-11-27 06:56:35 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* TESTING -- I've removed this, because I don't think it is right.
|
|
|
|
|
* You shouldn't divide by the cpu count here ...
|
|
|
|
|
* removing for testing */
|
2008-03-29 09:58:09 +00:00
|
|
|
|
/* if (idx == 0) {
|
|
|
|
|
info.cpu_usage[idx] = curtmp / info.cpu_avg_samples /
|
2008-02-20 20:30:45 +00:00
|
|
|
|
info.cpu_count;
|
|
|
|
|
} else {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} */
|
2005-11-27 06:56:35 +00:00
|
|
|
|
/* TESTING -- this line replaces the prev. "suspect" if/else */
|
2008-03-29 09:58:09 +00:00
|
|
|
|
info.cpu_usage[idx] = curtmp / info.cpu_avg_samples;
|
2005-11-27 06:56:35 +00:00
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
cpu[idx].cpu_last_total = cpu[idx].cpu_total;
|
|
|
|
|
cpu[idx].cpu_last_active_total = cpu[idx].cpu_active_total;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
for (i = info.cpu_avg_samples - 1; i > 0; i--) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
cpu[idx].cpu_val[i] = cpu[idx].cpu_val[i - 1];
|
2005-11-27 06:56:35 +00:00
|
|
|
|
}
|
2005-08-27 10:36:28 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(stat_fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_running_processes(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
update_stat();
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_cpu_usage(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
update_stat();
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_load_average(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
#ifdef HAVE_GETLOADAVG
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!prefer_proc) {
|
|
|
|
|
double v[3];
|
|
|
|
|
|
|
|
|
|
getloadavg(v, 3);
|
|
|
|
|
info.loadavg[0] = (float) v[0];
|
|
|
|
|
info.loadavg[1] = (float) v[1];
|
|
|
|
|
info.loadavg[2] = (float) v[2];
|
|
|
|
|
} else
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#endif
|
2008-02-20 20:30:45 +00:00
|
|
|
|
{
|
|
|
|
|
static int rep = 0;
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
|
|
if (!(fp = open_file("/proc/loadavg", &rep))) {
|
|
|
|
|
info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fscanf(fp, "%f %f %f", &info.loadavg[0], &info.loadavg[1],
|
|
|
|
|
&info.loadavg[2]);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
info.mask |= (1 << INFO_LOADAVG);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-27 07:15:44 +00:00
|
|
|
|
#define PROC_I8K "/proc/i8k"
|
|
|
|
|
#define I8K_DELIM " "
|
2005-08-27 07:58:29 +00:00
|
|
|
|
static char *i8k_procbuf = NULL;
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_i8k(void)
|
2005-08-27 07:15:44 +00:00
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2005-08-27 07:58:29 +00:00
|
|
|
|
if (!i8k_procbuf) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
i8k_procbuf = (char *) malloc(128 * sizeof(char));
|
2005-08-27 07:58:29 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if ((fp = fopen(PROC_I8K, "r")) == NULL) {
|
|
|
|
|
CRIT_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel "
|
|
|
|
|
"driver is loaded...");
|
2005-08-27 07:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
memset(&i8k_procbuf[0], 0, 128);
|
|
|
|
|
if (fread(&i8k_procbuf[0], sizeof(char), 128, fp) == 0) {
|
2005-08-27 07:15:44 +00:00
|
|
|
|
ERR("something wrong with /proc/i8k...");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
i8k.version = strtok(&i8k_procbuf[0], I8K_DELIM);
|
|
|
|
|
i8k.bios = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.serial = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.cpu_temp = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.left_fan_status = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.right_fan_status = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.left_fan_rpm = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.right_fan_rpm = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.ac_status = strtok(NULL, I8K_DELIM);
|
|
|
|
|
i8k.buttons_status = strtok(NULL, I8K_DELIM);
|
2005-08-27 07:15:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************/
|
|
|
|
|
/***********************************************************/
|
|
|
|
|
/***********************************************************/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
static int no_dots(const struct dirent *d)
|
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (d->d_name[0] == '.') {
|
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
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
static int get_first_file_in_a_directory(const char *dir, char *s, int *rep)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
struct dirent **namelist;
|
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
|
|
n = scandir(dir, &namelist, no_dots, alphasort);
|
|
|
|
|
if (n < 0) {
|
|
|
|
|
if (!rep || !*rep) {
|
|
|
|
|
ERR("scandir for %s: %s", dir, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (rep) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
*rep = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (n == 0) {
|
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
|
|
|
|
|
|
|
|
|
strncpy(s, namelist[0]->d_name, 255);
|
|
|
|
|
s[255] = '\0';
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
for (i = 0; i < n; i++) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
free(namelist[i]);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
free(namelist);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
|
2008-03-29 09:58:09 +00:00
|
|
|
|
int *divisor, char *devtype)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
char path[256];
|
2005-10-15 16:20:25 +00:00
|
|
|
|
char buf[256];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
int fd;
|
|
|
|
|
int divfd;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2007-08-13 23:14:01 +00:00
|
|
|
|
|
|
|
|
|
/* if device is NULL or *, get first */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (dev == NULL || strcmp(dev, "*") == 0) {
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (!get_first_file_in_a_directory(dir, buf, &rep)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
dev = buf;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (strcmp(dir, "/sys/class/hwmon/") == 0) {
|
|
|
|
|
if (*buf) {
|
|
|
|
|
/* buf holds result from get_first_file_in_a_directory() above,
|
|
|
|
|
* e.g. "hwmon0" -- append "/device" */
|
|
|
|
|
strcat(buf, "/device");
|
|
|
|
|
} else {
|
|
|
|
|
/* dev holds device number N as a string,
|
|
|
|
|
* e.g. "0", -- convert to "hwmon0/device" */
|
|
|
|
|
sprintf(buf, "hwmon%s/device", dev);
|
|
|
|
|
dev = buf;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-08-13 23:14:01 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* change vol to in */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (strcmp(type, "vol") == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
type = "in";
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
2007-08-13 00:28:48 +00:00
|
|
|
|
snprintf(path, 255, "%s%s/%s%d_input", dir, dev, "temp", n);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
} else {
|
2007-08-13 00:28:48 +00:00
|
|
|
|
snprintf(path, 255, "%s%s/%s%d_input", dir, dev, type, n);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2005-09-05 02:09:31 +00:00
|
|
|
|
strncpy(devtype, path, 255);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* open file */
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2005-08-06 02:36:59 +00:00
|
|
|
|
if (fd < 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
CRIT_ERR("can't open '%s': %s\nplease check your device or remove this "
|
2008-07-12 10:25:05 +00:00
|
|
|
|
"var from "PACKAGE_NAME, path, strerror(errno));
|
2005-08-06 02:36:59 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2005-07-28 04:48:27 +00:00
|
|
|
|
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|| strcmp(type, "tempf") == 0) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
*divisor = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
*divisor = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* fan does not use *_div as a read divisor */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (strcmp("fan", type) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return fd;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* test if *_div file exist, open it and use it as divisor */
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(path, 255, "%s%s/%s%d_div", dir, "one", "two", n);
|
2005-07-28 04:48:27 +00:00
|
|
|
|
} else {
|
2007-08-13 00:28:48 +00:00
|
|
|
|
snprintf(path, 255, "%s%s/%s%d_div", dir, dev, type, n);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
divfd = open(path, O_RDONLY);
|
|
|
|
|
if (divfd > 0) {
|
|
|
|
|
/* read integer */
|
|
|
|
|
char divbuf[64];
|
2008-03-21 23:17:37 +00:00
|
|
|
|
int divn;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
divn = read(divfd, divbuf, 63);
|
|
|
|
|
/* should read until n == 0 but I doubt that kernel will give these
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* in multiple pieces. :) */
|
2008-03-20 21:28:19 +00:00
|
|
|
|
if (divn < 0) {
|
|
|
|
|
ERR("open_sysfs_sensor(): can't read from sysfs");
|
|
|
|
|
} else {
|
|
|
|
|
divbuf[divn] = '\0';
|
2008-03-29 09:58:09 +00:00
|
|
|
|
*divisor = atoi(divbuf);
|
2008-03-20 21:28:19 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(divfd);
|
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
double get_sysfs_info(int *fd, int divisor, char *devtype, char *type)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (*fd <= 0) {
|
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
|
|
|
|
|
|
|
|
|
lseek(*fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* read integer */
|
|
|
|
|
{
|
|
|
|
|
char buf[64];
|
2008-03-18 00:23:16 +00:00
|
|
|
|
int n;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
n = read(*fd, buf, 63);
|
|
|
|
|
/* should read until n == 0 but I doubt that kernel will give these
|
2005-07-28 04:48:27 +00:00
|
|
|
|
* in multiple pieces. :) */
|
2008-03-18 00:23:16 +00:00
|
|
|
|
if (n < 0) {
|
2008-03-20 21:28:19 +00:00
|
|
|
|
ERR("get_sysfs_info(): read from %s failed\n", devtype);
|
2008-03-18 00:23:16 +00:00
|
|
|
|
} else {
|
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
val = atoi(buf);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(*fd);
|
|
|
|
|
/* open file */
|
|
|
|
|
*fd = open(devtype, O_RDONLY);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (*fd < 0) {
|
2005-07-28 04:48:27 +00:00
|
|
|
|
ERR("can't open '%s': %s", devtype, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
/* My dirty hack for computing CPU value
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* Filedil, from forums.gentoo.org */
|
|
|
|
|
/* if (strstr(devtype, "temp1_input") != NULL) {
|
|
|
|
|
return -15.096 + 1.4893 * (val / 1000.0);
|
|
|
|
|
} */
|
2005-07-28 04:48:27 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* divide voltage and temperature by 1000 */
|
|
|
|
|
/* or if any other divisor is given, use that */
|
|
|
|
|
if (strcmp(type, "tempf") == 0) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
if (divisor > 1) {
|
|
|
|
|
return ((val / divisor + 40) * 9.0 / 5) - 40;
|
|
|
|
|
} else if (divisor) {
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return ((val / 1000.0 + 40) * 9.0 / 5) - 40;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-07-28 04:48:27 +00:00
|
|
|
|
return ((val + 40) * 9.0 / 5) - 40;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-28 04:48:27 +00:00
|
|
|
|
} else {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
if (divisor > 1) {
|
|
|
|
|
return val / divisor;
|
|
|
|
|
} else if (divisor) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return val / 1000.0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return val;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* Prior to kernel version 2.6.12, the CPU fan speed was available in
|
|
|
|
|
* ADT746X_FAN_OLD, whereas later kernel versions provide this information in
|
|
|
|
|
* ADT746X_FAN. */
|
2006-04-07 18:04:07 +00:00
|
|
|
|
#define ADT746X_FAN "/sys/devices/temperatures/sensor1_fan_speed"
|
|
|
|
|
#define ADT746X_FAN_OLD "/sys/devices/temperatures/cpu_fan_speed"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_adt746x_fan(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
char adt746x_fan_state[64];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-13 04:04:00 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
if ((fp = open_file(ADT746X_FAN, &rep)) == NULL
|
2008-02-20 20:30:45 +00:00
|
|
|
|
&& (fp = open_file(ADT746X_FAN_OLD, &rep)) == NULL) {
|
2005-11-13 04:04:00 +00:00
|
|
|
|
sprintf(adt746x_fan_state, "adt746x not found");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2006-04-07 18:04:07 +00:00
|
|
|
|
fgets(adt746x_fan_state, sizeof(adt746x_fan_state), fp);
|
|
|
|
|
adt746x_fan_state[strlen(adt746x_fan_state) - 1] = 0;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
fclose(fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_fan_state);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* Prior to kernel version 2.6.12, the CPU temperature was found in
|
|
|
|
|
* ADT746X_CPU_OLD, whereas later kernel versions provide this information in
|
|
|
|
|
* ADT746X_CPU. */
|
2006-04-07 18:04:07 +00:00
|
|
|
|
#define ADT746X_CPU "/sys/devices/temperatures/sensor1_temperature"
|
|
|
|
|
#define ADT746X_CPU_OLD "/sys/devices/temperatures/cpu_temperature"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_adt746x_cpu(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2005-11-13 04:04:00 +00:00
|
|
|
|
char adt746x_cpu_state[64];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-13 04:04:00 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2006-04-07 18:04:07 +00:00
|
|
|
|
if ((fp = open_file(ADT746X_CPU, &rep)) == NULL
|
2008-02-20 20:30:45 +00:00
|
|
|
|
&& (fp = open_file(ADT746X_CPU_OLD, &rep)) == NULL) {
|
2005-11-13 04:04:00 +00:00
|
|
|
|
sprintf(adt746x_cpu_state, "adt746x not found");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-11-13 04:04:00 +00:00
|
|
|
|
fscanf(fp, "%2s", adt746x_cpu_state);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-22 02:49:22 +00:00
|
|
|
|
#define CPUFREQ_PREFIX "/sys/devices/system/cpu"
|
|
|
|
|
#define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
|
2005-08-26 02:16:35 +00:00
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
|
2008-03-29 03:45:36 +00:00
|
|
|
|
char get_freq(char *p_client_buffer, size_t client_buffer_size,
|
|
|
|
|
const char *p_format, int divisor, unsigned int cpu)
|
2005-08-26 02:16:35 +00:00
|
|
|
|
{
|
2005-08-05 01:06:17 +00:00
|
|
|
|
FILE *f;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
static int rep = 0;
|
2005-11-13 03:33:26 +00:00
|
|
|
|
char frequency[32];
|
|
|
|
|
char s[256];
|
|
|
|
|
double freq = 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0 || !p_format
|
|
|
|
|
|| divisor <= 0) {
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!prefer_proc) {
|
|
|
|
|
char current_freq_file[128];
|
|
|
|
|
|
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu - 1,
|
|
|
|
|
CPUFREQ_POSTFIX);
|
|
|
|
|
f = fopen(current_freq_file, "r");
|
|
|
|
|
if (f) {
|
|
|
|
|
/* if there's a cpufreq /sys node, read the current frequency from
|
|
|
|
|
* this node and divide by 1000 to get Mhz. */
|
|
|
|
|
if (fgets(s, sizeof(s), f)) {
|
|
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
|
freq = strtod(s, NULL);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
|
(freq / 1000) / divisor);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// open the CPU information file
|
|
|
|
|
f = open_file("/proc/cpuinfo", &rep);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
if (!f) {
|
2008-07-12 10:25:05 +00:00
|
|
|
|
perror(PACKAGE_NAME": Failed to access '/proc/cpuinfo' at get_freq()");
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-11-13 03:33:26 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// read the file
|
|
|
|
|
while (fgets(s, sizeof(s), f) != NULL) {
|
2006-05-22 02:49:22 +00:00
|
|
|
|
|
2005-08-26 02:16:35 +00:00
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// and search for the cpu mhz
|
|
|
|
|
if (strncmp(s, "cpu MHz", 7) == 0 && cpu == 0) {
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#else
|
|
|
|
|
#if defined(__alpha)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// different on alpha
|
|
|
|
|
if (strncmp(s, "cycle frequency [Hz]", 20) == 0 && cpu == 0) {
|
2005-08-26 02:16:35 +00:00
|
|
|
|
#else
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// this is different on ppc for some reason
|
|
|
|
|
if (strncmp(s, "clock", 5) == 0 && cpu == 0) {
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#endif // defined(__alpha)
|
|
|
|
|
#endif // defined(__i386) || defined(__x86_64)
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// copy just the number
|
|
|
|
|
strcpy(frequency, strchr(s, ':') + 2);
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#if defined(__alpha)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// strip " est.\n"
|
|
|
|
|
frequency[strlen(frequency) - 6] = '\0';
|
|
|
|
|
// kernel reports in Hz
|
|
|
|
|
freq = strtod(frequency, NULL) / 1000000;
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#else
|
2008-02-20 20:30:45 +00:00
|
|
|
|
// strip \n
|
|
|
|
|
frequency[strlen(frequency) - 1] = '\0';
|
|
|
|
|
freq = strtod(frequency, NULL);
|
2006-03-06 00:33:32 +00:00
|
|
|
|
#endif
|
2008-02-20 20:30:45 +00:00
|
|
|
|
break;
|
2005-08-05 01:06:17 +00:00
|
|
|
|
}
|
2006-05-22 02:49:22 +00:00
|
|
|
|
if (strncmp(s, "processor", 9) == 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
cpu--;
|
|
|
|
|
continue;
|
2006-05-22 02:49:22 +00:00
|
|
|
|
}
|
2005-08-11 18:13:57 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2005-11-13 03:33:26 +00:00
|
|
|
|
fclose(f);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
|
(float) freq / divisor);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 1;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-24 00:23:47 +00:00
|
|
|
|
#define CPUFREQ_VOLTAGE "cpufreq/scaling_voltages"
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* /sys/devices/system/cpu/cpu0/cpufreq/scaling_voltages looks something
|
|
|
|
|
* like this:
|
2006-05-24 00:23:47 +00:00
|
|
|
|
# frequency voltage
|
|
|
|
|
1800000 1340
|
|
|
|
|
1600000 1292
|
|
|
|
|
1400000 1100
|
|
|
|
|
1200000 988
|
|
|
|
|
1000000 1116
|
|
|
|
|
800000 1004
|
|
|
|
|
600000 988
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* return cpu voltage in mV (use divisor=1) or V (use divisor=1000) */
|
|
|
|
|
char get_voltage(char *p_client_buffer, size_t client_buffer_size,
|
2008-03-29 03:45:36 +00:00
|
|
|
|
const char *p_format, int divisor, unsigned int cpu)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
{
|
2006-05-24 00:23:47 +00:00
|
|
|
|
FILE *f;
|
|
|
|
|
char s[256];
|
|
|
|
|
int freq = 0;
|
|
|
|
|
int voltage = 0;
|
|
|
|
|
char current_freq_file[128];
|
|
|
|
|
int freq_comp = 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* build the voltage file name */
|
2006-05-24 00:23:47 +00:00
|
|
|
|
cpu--;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
|
|
|
|
|
CPUFREQ_POSTFIX);
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0 || !p_format
|
|
|
|
|
|| divisor <= 0) {
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
|
|
|
|
/* read the current cpu frequency from the /sys node */
|
|
|
|
|
f = fopen(current_freq_file, "r");
|
|
|
|
|
if (f) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(s, sizeof(s), f)) {
|
|
|
|
|
s[strlen(s) - 1] = '\0';
|
|
|
|
|
freq = strtod(s, NULL);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
} else {
|
2008-07-12 10:25:05 +00:00
|
|
|
|
fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
perror("get_voltage()");
|
|
|
|
|
if (f) {
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(current_freq_file, 127, "%s/cpu%d/%s", CPUFREQ_PREFIX, cpu,
|
|
|
|
|
CPUFREQ_VOLTAGE);
|
2006-05-24 00:23:47 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* use the current cpu frequency to find the corresponding voltage */
|
2006-05-24 00:23:47 +00:00
|
|
|
|
f = fopen(current_freq_file, "r");
|
|
|
|
|
|
2006-05-24 00:46:50 +00:00
|
|
|
|
if (f) {
|
|
|
|
|
while (!feof(f)) {
|
|
|
|
|
char line[256];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(line, 255, f) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-24 00:46:50 +00:00
|
|
|
|
sscanf(line, "%d %d", &freq_comp, &voltage);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (freq_comp == freq) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-05-24 00:46:50 +00:00
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
} else {
|
2008-07-12 10:25:05 +00:00
|
|
|
|
fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
perror("get_voltage()");
|
|
|
|
|
if (f) {
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
|
(float) voltage / divisor);
|
2006-05-24 00:46:50 +00:00
|
|
|
|
return 1;
|
2006-05-24 00:23:47 +00:00
|
|
|
|
}
|
2005-07-24 23:08:51 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#define ACPI_FAN_DIR "/proc/acpi/fan/"
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char buf[256];
|
|
|
|
|
char buf2[256];
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-12 21:42:00 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* yeah, slow... :/ */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) {
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "no fans?");
|
2005-11-12 21:42:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
fp = open_file(buf2, &rep);
|
|
|
|
|
if (!fp) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size,
|
|
|
|
|
"can't open fan's state file");
|
2005-11-12 21:42:00 +00:00
|
|
|
|
return;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
memset(buf, 0, sizeof(buf));
|
2005-11-12 21:42:00 +00:00
|
|
|
|
fscanf(fp, "%*s %99s", buf);
|
|
|
|
|
fclose(fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%s", buf);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-06-28 19:17:49 +00:00
|
|
|
|
#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply/AC"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
|
2008-06-28 19:17:49 +00:00
|
|
|
|
/* Linux 2.6.25 onwards ac adapter info is in
|
|
|
|
|
/sys/class/power_supply/AC/
|
|
|
|
|
On my system I get the following.
|
|
|
|
|
/sys/class/power_supply/AC/uevent:
|
|
|
|
|
PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A08:00/device:01/PNP0C09:00/ACPI0003:00
|
|
|
|
|
PHYSDEVBUS=acpi
|
|
|
|
|
PHYSDEVDRIVER=ac
|
|
|
|
|
POWER_SUPPLY_NAME=AC
|
|
|
|
|
POWER_SUPPLY_TYPE=Mains
|
|
|
|
|
POWER_SUPPLY_ONLINE=1
|
|
|
|
|
*/
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2008-06-28 19:17:49 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char buf[256];
|
|
|
|
|
char buf2[256];
|
|
|
|
|
FILE *fp;
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-12 21:42:00 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-06-28 19:17:49 +00:00
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s/uevent", SYSFS_AC_ADAPTER_DIR);
|
|
|
|
|
fp = open_file(buf2, &rep);
|
|
|
|
|
if (fp) {
|
|
|
|
|
/* sysfs processing */
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
if (fgets(buf, sizeof(buf), fp) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (strncmp(buf, "POWER_SUPPLY_ONLINE=", 20) == 0) {
|
|
|
|
|
int online = 0;
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_ONLINE=%d", &online);
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size,
|
|
|
|
|
"%s-line", (online ? "on" : "off"));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
} else {
|
|
|
|
|
/* yeah, slow... :/ */
|
|
|
|
|
if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) {
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "no ac_adapters?");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-06-28 19:17:49 +00:00
|
|
|
|
snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-06-28 19:17:49 +00:00
|
|
|
|
fp = open_file(buf2, &rep);
|
|
|
|
|
if (!fp) {
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size,
|
|
|
|
|
"No ac adapter found.... where is it?");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
fscanf(fp, "%*s %99s", buf);
|
|
|
|
|
fclose(fp);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-06-28 19:17:49 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%s", buf);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/cooling_mode
|
|
|
|
|
cooling mode: active
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/polling_frequency
|
|
|
|
|
<polling disabled>
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/state
|
|
|
|
|
state: ok
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/temperature
|
|
|
|
|
temperature: 45 C
|
|
|
|
|
/proc/acpi/thermal_zone/THRM/trip_points
|
|
|
|
|
critical (S5): 73 C
|
|
|
|
|
passive: 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define ACPI_THERMAL_DIR "/proc/acpi/thermal_zone/"
|
|
|
|
|
#define ACPI_THERMAL_FORMAT "/proc/acpi/thermal_zone/%s/temperature"
|
|
|
|
|
|
|
|
|
|
int open_acpi_temperature(const char *name)
|
|
|
|
|
{
|
|
|
|
|
char path[256];
|
2005-10-22 01:13:39 +00:00
|
|
|
|
char buf[256];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
|
|
if (name == NULL || strcmp(name, "*") == 0) {
|
2006-12-23 06:01:16 +00:00
|
|
|
|
static int rep = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (!get_first_file_in_a_directory(ACPI_THERMAL_DIR, buf, &rep)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
return -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
name = buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(path, 255, ACPI_THERMAL_FORMAT, name);
|
|
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fd < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ERR("can't open '%s': %s", path, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double last_acpi_temp;
|
|
|
|
|
static double last_acpi_temp_time;
|
|
|
|
|
|
|
|
|
|
double get_acpi_temperature(int fd)
|
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fd <= 0) {
|
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
|
|
|
|
|
|
|
|
|
/* don't update acpi temperature too often */
|
|
|
|
|
if (current_update_time - last_acpi_temp_time < 11.32) {
|
|
|
|
|
return last_acpi_temp;
|
|
|
|
|
}
|
|
|
|
|
last_acpi_temp_time = current_update_time;
|
|
|
|
|
|
|
|
|
|
/* seek to beginning */
|
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
|
|
|
|
|
|
/* read */
|
|
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
int n;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
n = read(fd, buf, 255);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (n < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
ERR("can't read fd %d: %s", fd, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
buf[n] = '\0';
|
|
|
|
|
sscanf(buf, "temperature: %lf", &last_acpi_temp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return last_acpi_temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2007-08-05 04:47:21 +00:00
|
|
|
|
hipo@lepakko hipo $ cat /proc/acpi/battery/BAT1/info
|
2005-07-20 00:30:40 +00:00
|
|
|
|
present: yes
|
|
|
|
|
design capacity: 4400 mAh
|
|
|
|
|
last full capacity: 4064 mAh
|
|
|
|
|
battery technology: rechargeable
|
|
|
|
|
design voltage: 14800 mV
|
|
|
|
|
design capacity warning: 300 mAh
|
|
|
|
|
design capacity low: 200 mAh
|
|
|
|
|
capacity granularity 1: 32 mAh
|
|
|
|
|
capacity granularity 2: 32 mAh
|
|
|
|
|
model number: 02KT
|
|
|
|
|
serial number: 16922
|
|
|
|
|
battery type: LION
|
|
|
|
|
OEM info: SANYO
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
hipo@lepakko conky $ cat /proc/acpi/battery/BAT1/state
|
|
|
|
|
present: yes
|
|
|
|
|
capacity state: ok
|
|
|
|
|
charging state: unknown
|
|
|
|
|
present rate: 0 mA
|
|
|
|
|
remaining capacity: 4064 mAh
|
|
|
|
|
present voltage: 16608 mV
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> jupet@lagi-unstable:~$ cat /proc/apm
|
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0xff 0x10 -1% -1 ?
|
|
|
|
|
2213<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> (-1 ollee ei akkua kiinni, koska akku on p<EFBFBD>yd<EFBFBD>ll<EFBFBD>)
|
|
|
|
|
2214<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> jupet@lagi-unstable:~$ cat /proc/apm
|
|
|
|
|
2214<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x03 0x09 98% -1 ?
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2238<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x00 0x00 0x01 100% -1 ? ilman verkkovirtaa
|
|
|
|
|
2239<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x00 0x01 99% -1 ? verkkovirralla
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2240<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x01 0x03 0x09 100% -1 ? verkkovirralla ja monitori p<EFBFBD><EFBFBD>ll<EFBFBD>
|
|
|
|
|
2241<@jupet<EFBFBD>kellari<EFBFBD><EFBFBD>> 1.16 1.2 0x03 0x00 0x00 0x01 99% -1 ? monitori p<EFBFBD><EFBFBD>ll<EFBFBD> mutta ilman verkkovirtaa
|
2005-07-20 00:30:40 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* Kapil Hari Paranjape <kapil@imsc.res.in>
|
|
|
|
|
Linux 2.6.24 onwards battery info is in
|
|
|
|
|
/sys/class/power_supply/BAT0/
|
|
|
|
|
On my system I get the following.
|
|
|
|
|
/sys/class/power_supply/BAT0/uevent:
|
|
|
|
|
PHYSDEVPATH=/devices/LNXSYSTM:00/device:00/PNP0A03:00/device:01/PNP0C09:00/PNP0C0A:00
|
|
|
|
|
PHYSDEVBUS=acpi
|
|
|
|
|
PHYSDEVDRIVER=battery
|
|
|
|
|
POWER_SUPPLY_NAME=BAT0
|
|
|
|
|
POWER_SUPPLY_TYPE=Battery
|
|
|
|
|
POWER_SUPPLY_STATUS=Discharging
|
|
|
|
|
POWER_SUPPLY_PRESENT=1
|
|
|
|
|
POWER_SUPPLY_TECHNOLOGY=Li-ion
|
|
|
|
|
POWER_SUPPLY_VOLTAGE_MIN_DESIGN=10800000
|
|
|
|
|
POWER_SUPPLY_VOLTAGE_NOW=10780000
|
|
|
|
|
POWER_SUPPLY_CURRENT_NOW=13970000
|
|
|
|
|
POWER_SUPPLY_ENERGY_FULL_DESIGN=47510000
|
|
|
|
|
POWER_SUPPLY_ENERGY_FULL=27370000
|
|
|
|
|
POWER_SUPPLY_ENERGY_NOW=11810000
|
|
|
|
|
POWER_SUPPLY_MODEL_NAME=IBM-92P1060
|
|
|
|
|
POWER_SUPPLY_MANUFACTURER=Panasonic
|
|
|
|
|
On some systems POWER_SUPPLY_ENERGY_* is replaced by POWER_SUPPLY_CHARGE_*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define SYSFS_BATTERY_BASE_PATH "/sys/class/power_supply"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
#define ACPI_BATTERY_BASE_PATH "/proc/acpi/battery"
|
|
|
|
|
#define APM_PATH "/proc/apm"
|
2007-08-05 04:47:21 +00:00
|
|
|
|
#define MAX_BATTERY_COUNT 4
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-03-24 21:44:50 +00:00
|
|
|
|
static FILE *sysfs_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
|
|
|
|
|
static FILE *acpi_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
|
|
|
|
|
static FILE *apm_bat_fp[MAX_BATTERY_COUNT] = { NULL, NULL, NULL, NULL };
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
static int batteries_initialized = 0;
|
|
|
|
|
static char batteries[MAX_BATTERY_COUNT][32];
|
|
|
|
|
|
|
|
|
|
static int acpi_last_full[MAX_BATTERY_COUNT];
|
|
|
|
|
static int acpi_design_capacity[MAX_BATTERY_COUNT];
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* e.g. "charging 75%" */
|
|
|
|
|
static char last_battery_str[MAX_BATTERY_COUNT][64];
|
|
|
|
|
/* e.g. "3h 15m" */
|
|
|
|
|
static char last_battery_time_str[MAX_BATTERY_COUNT][64];
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
|
|
|
|
static double last_battery_time[MAX_BATTERY_COUNT];
|
|
|
|
|
|
|
|
|
|
static int last_battery_perct[MAX_BATTERY_COUNT];
|
|
|
|
|
static double last_battery_perct_time[MAX_BATTERY_COUNT];
|
|
|
|
|
|
|
|
|
|
void init_batteries(void)
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (batteries_initialized) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
batteries[idx][0] = '\0';
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
batteries_initialized = 1;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
int get_battery_idx(const char *bat)
|
|
|
|
|
{
|
|
|
|
|
int idx;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
for (idx = 0; idx < MAX_BATTERY_COUNT; idx++) {
|
|
|
|
|
if (!strlen(batteries[idx]) || !strcmp(batteries[idx], bat)) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
/* if not found, enter a new entry */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!strlen(batteries[idx])) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
snprintf(batteries[idx], 31, "%s", bat);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
return idx;
|
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2008-04-10 22:45:45 +00:00
|
|
|
|
void set_return_value(char *buffer, unsigned int n, int item, int idx);
|
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
2008-09-01 23:46:48 +00:00
|
|
|
|
static int idx, rep = 0, rep1 = 0, rep2 = 0;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char acpi_path[128];
|
2008-03-29 06:24:04 +00:00
|
|
|
|
char sysfs_path[128];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
|
2008-03-17 22:27:20 +00:00
|
|
|
|
snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
init_batteries();
|
|
|
|
|
|
|
|
|
|
idx = get_battery_idx(bat);
|
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* don't update battery too often */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (current_update_time - last_battery_time[idx] < 29.5) {
|
2008-04-10 22:45:45 +00:00
|
|
|
|
set_return_value(buffer, n, item, idx);
|
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2006-12-12 23:21:14 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
last_battery_time[idx] = current_update_time;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
memset(last_battery_str[idx], 0, sizeof(last_battery_str[idx]));
|
|
|
|
|
memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
|
2006-12-12 23:21:14 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* first try SYSFS if that fails try ACPI */
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-24 21:44:50 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
|
2008-03-17 22:27:20 +00:00
|
|
|
|
sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
|
2008-03-24 21:44:50 +00:00
|
|
|
|
}
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-24 21:44:50 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
|
2008-09-01 23:46:48 +00:00
|
|
|
|
acpi_bat_fp[idx] = open_file(acpi_path, &rep1);
|
2008-03-24 21:44:50 +00:00
|
|
|
|
}
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] != NULL) {
|
|
|
|
|
/* SYSFS */
|
|
|
|
|
int present_rate = -1;
|
|
|
|
|
int remaining_capacity = -1;
|
|
|
|
|
char charging_state[64];
|
|
|
|
|
char present[4];
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-04-01 06:10:58 +00:00
|
|
|
|
strcpy(charging_state, "unknown");
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
while (!feof(sysfs_bat_fp[idx])) {
|
|
|
|
|
char buf[256];
|
|
|
|
|
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
|
|
|
|
|
break;
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* let's just hope units are ok */
|
|
|
|
|
if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
|
2008-04-01 06:10:58 +00:00
|
|
|
|
strcpy(present, "yes");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
else if (strncmp (buf, "POWER_SUPPLY_PRESENT=0", 22) == 0)
|
2008-04-01 06:10:58 +00:00
|
|
|
|
strcpy(present, "no");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
else if (strncmp (buf, "POWER_SUPPLY_STATUS=", 20) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_STATUS=%63s", charging_state);
|
|
|
|
|
/* present_rate is not the same as the
|
|
|
|
|
current flowing now but it is the same value
|
|
|
|
|
which was used in the past. so we continue
|
|
|
|
|
the tradition! */
|
|
|
|
|
else if (strncmp(buf, "POWER_SUPPLY_CURRENT_NOW=", 25) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_CURRENT_NOW=%d", &present_rate);
|
|
|
|
|
else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
|
|
|
|
|
else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=", 25) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_last_full[idx]);
|
|
|
|
|
else if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
|
|
|
|
|
else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
|
|
|
|
|
}
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
fclose(sysfs_bat_fp[idx]);
|
|
|
|
|
sysfs_bat_fp[idx] = NULL;
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
|
|
|
|
|
if (remaining_capacity > acpi_last_full[idx])
|
|
|
|
|
acpi_last_full[idx] = remaining_capacity; /* normalize to 100% */
|
2008-06-15 18:38:33 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* not present */
|
|
|
|
|
if (strcmp(present, "No") == 0) {
|
|
|
|
|
strncpy(last_battery_str[idx], "not present", 64);
|
|
|
|
|
}
|
|
|
|
|
/* charging */
|
|
|
|
|
else if (strcmp(charging_state, "Charging") == 0) {
|
|
|
|
|
if (acpi_last_full[idx] != 0 && present_rate > 0) {
|
|
|
|
|
/* e.g. charging 75% */
|
2008-03-30 03:26:55 +00:00
|
|
|
|
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %i%%",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
(int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
|
|
|
|
|
/* e.g. 2h 37m */
|
|
|
|
|
format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
|
|
|
|
|
(long) (((float)(acpi_last_full[idx] - remaining_capacity) / present_rate) * 3600));
|
|
|
|
|
} else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
|
2008-03-30 03:26:55 +00:00
|
|
|
|
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "charging %d%%",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
(int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
} else {
|
2008-04-01 06:10:58 +00:00
|
|
|
|
strncpy(last_battery_str[idx], "charging", sizeof(last_battery_str[idx])-1);
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* discharging */
|
|
|
|
|
else if (strncmp(charging_state, "Discharging", 64) == 0) {
|
|
|
|
|
if (present_rate > 0) {
|
|
|
|
|
/* e.g. discharging 35% */
|
2008-03-30 03:26:55 +00:00
|
|
|
|
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "discharging %i%%",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
(int) (((float) remaining_capacity / acpi_last_full[idx]) * 100 ));
|
|
|
|
|
/* e.g. 1h 12m */
|
|
|
|
|
format_seconds(last_battery_time_str[idx], sizeof(last_battery_time_str[idx])-1,
|
2008-04-10 22:45:45 +00:00
|
|
|
|
(long) (((float) remaining_capacity / present_rate) * 3600));
|
2008-03-17 22:27:20 +00:00
|
|
|
|
} else if (present_rate == 0) { /* Thanks to Nexox for this one */
|
2008-03-30 03:26:55 +00:00
|
|
|
|
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1, "full");
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
} else {
|
|
|
|
|
snprintf(last_battery_str[idx], sizeof(last_battery_str[idx])-1,
|
2008-03-30 03:26:55 +00:00
|
|
|
|
"discharging %d%%",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
(int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* charged */
|
|
|
|
|
/* thanks to Lukas Zapletal <lzap@seznam.cz> */
|
|
|
|
|
else if (strncmp(charging_state, "Charged", 64) == 0) {
|
|
|
|
|
/* Below happens with the second battery on my X40,
|
|
|
|
|
* when the second one is empty and the first one
|
|
|
|
|
* being charged. */
|
|
|
|
|
if (remaining_capacity == 0)
|
2008-03-30 03:26:55 +00:00
|
|
|
|
strcpy(last_battery_str[idx], "empty");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
else
|
2008-03-30 03:26:55 +00:00
|
|
|
|
strcpy(last_battery_str[idx], "charged");
|
2008-03-17 22:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
/* unknown, probably full / AC */
|
|
|
|
|
else {
|
|
|
|
|
if (acpi_last_full[idx] != 0
|
|
|
|
|
&& remaining_capacity != acpi_last_full[idx])
|
2008-03-30 03:26:55 +00:00
|
|
|
|
snprintf(last_battery_str[idx], 64, "unknown %d%%",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
(int) (((float)remaining_capacity / acpi_last_full[idx]) * 100));
|
|
|
|
|
else
|
|
|
|
|
strncpy(last_battery_str[idx], "AC", 64);
|
|
|
|
|
}
|
|
|
|
|
} else if (acpi_bat_fp[idx] != NULL) {
|
|
|
|
|
/* ACPI */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
int present_rate = -1;
|
|
|
|
|
int remaining_capacity = -1;
|
|
|
|
|
char charging_state[64];
|
2006-06-22 04:08:02 +00:00
|
|
|
|
char present[4];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* read last full capacity if it's zero */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (acpi_last_full[idx] == 0) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
static int rep3 = 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
char path[128];
|
2005-07-20 00:30:40 +00:00
|
|
|
|
FILE *fp;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
|
2008-03-29 09:58:09 +00:00
|
|
|
|
fp = open_file(path, &rep3);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char b[256];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(b, 256, fp) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (sscanf(b, "last full capacity: %d",
|
|
|
|
|
&acpi_last_full[idx]) != 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2006-03-07 01:49:51 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
fseek(acpi_bat_fp[idx], 0, SEEK_SET);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
strcpy(charging_state, "unknown");
|
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
while (!feof(acpi_bat_fp[idx])) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
char buf[256];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
/* let's just hope units are ok */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (strncmp(buf, "present:", 8) == 0) {
|
2006-06-22 04:08:02 +00:00
|
|
|
|
sscanf(buf, "present: %4s", present);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strncmp(buf, "charging state:", 15) == 0) {
|
2006-11-18 23:39:43 +00:00
|
|
|
|
sscanf(buf, "charging state: %63s", charging_state);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strncmp(buf, "present rate:", 13) == 0) {
|
2006-11-18 23:39:43 +00:00
|
|
|
|
sscanf(buf, "present rate: %d", &present_rate);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strncmp(buf, "remaining capacity:", 19) == 0) {
|
2006-11-18 23:39:43 +00:00
|
|
|
|
sscanf(buf, "remaining capacity: %d", &remaining_capacity);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2006-12-12 01:20:10 +00:00
|
|
|
|
/* Hellf[i]re notes that remaining capacity can exceed acpi_last_full */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (remaining_capacity > acpi_last_full[idx]) {
|
|
|
|
|
/* normalize to 100% */
|
|
|
|
|
acpi_last_full[idx] = remaining_capacity;
|
|
|
|
|
}
|
2006-12-12 01:20:10 +00:00
|
|
|
|
|
2006-06-22 04:08:02 +00:00
|
|
|
|
/* not present */
|
|
|
|
|
if (strcmp(present, "no") == 0) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
strncpy(last_battery_str[idx], "not present", 64);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* charging */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strcmp(charging_state, "charging") == 0) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (acpi_last_full[idx] != 0 && present_rate > 0) {
|
2006-12-12 01:20:10 +00:00
|
|
|
|
/* e.g. charging 75% */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(last_battery_str[idx],
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1, "charging %i%%",
|
2007-08-05 04:47:21 +00:00
|
|
|
|
(int) ((remaining_capacity * 100) / acpi_last_full[idx]));
|
2006-12-12 01:20:10 +00:00
|
|
|
|
/* e.g. 2h 37m */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
format_seconds(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1,
|
|
|
|
|
(long) (((acpi_last_full[idx] - remaining_capacity) *
|
|
|
|
|
3600) / present_rate));
|
2007-08-05 04:47:21 +00:00
|
|
|
|
} else if (acpi_last_full[idx] != 0 && present_rate <= 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(last_battery_str[idx],
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1, "charging %d%%",
|
2007-08-05 04:47:21 +00:00
|
|
|
|
(int) ((remaining_capacity * 100) / acpi_last_full[idx]));
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2005-07-28 04:48:27 +00:00
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
strncpy(last_battery_str[idx], "charging",
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1);
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2005-07-28 04:48:27 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* discharging */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strncmp(charging_state, "discharging", 64) == 0) {
|
2006-06-08 04:29:23 +00:00
|
|
|
|
if (present_rate > 0) {
|
2006-12-12 01:20:10 +00:00
|
|
|
|
/* e.g. discharging 35% */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(last_battery_str[idx],
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1, "discharging %i%%",
|
2007-08-05 04:47:21 +00:00
|
|
|
|
(int) ((remaining_capacity * 100) / acpi_last_full[idx]));
|
2006-12-12 01:20:10 +00:00
|
|
|
|
/* e.g. 1h 12m */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
format_seconds(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1,
|
|
|
|
|
(long) ((remaining_capacity * 3600) / present_rate));
|
|
|
|
|
} else if (present_rate == 0) { /* Thanks to Nexox for this one */
|
|
|
|
|
snprintf(last_battery_str[idx],
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1, "full");
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2006-06-08 04:29:23 +00:00
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(last_battery_str[idx],
|
|
|
|
|
sizeof(last_battery_str[idx]) - 1, "discharging %d%%",
|
2007-08-05 04:47:21 +00:00
|
|
|
|
(int) ((remaining_capacity * 100) / acpi_last_full[idx]));
|
2008-03-30 04:02:53 +00:00
|
|
|
|
snprintf(last_battery_time_str[idx],
|
|
|
|
|
sizeof(last_battery_time_str[idx]) - 1, "unknown");
|
2006-06-08 04:29:23 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* charged */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else if (strncmp(charging_state, "charged", 64) == 0) {
|
|
|
|
|
/* thanks to Lukas Zapletal <lzap@seznam.cz> */
|
|
|
|
|
/* Below happens with the second battery on my X40,
|
|
|
|
|
* when the second one is empty and the first one being charged. */
|
|
|
|
|
if (remaining_capacity == 0) {
|
|
|
|
|
strcpy(last_battery_str[idx], "empty");
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(last_battery_str[idx], "charged");
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
/* unknown, probably full / AC */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (acpi_last_full[idx] != 0
|
2008-02-20 20:30:45 +00:00
|
|
|
|
&& remaining_capacity != acpi_last_full[idx]) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
snprintf(last_battery_str[idx], 64, "unknown %d%%",
|
|
|
|
|
(int) ((remaining_capacity * 100) / acpi_last_full[idx]));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
strncpy(last_battery_str[idx], "AC", 64);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* APM */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (apm_bat_fp[idx] == NULL) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
apm_bat_fp[idx] = open_file(APM_PATH, &rep2);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (apm_bat_fp[idx] != NULL) {
|
2008-03-29 03:01:48 +00:00
|
|
|
|
unsigned int ac, status, flag;
|
|
|
|
|
int life;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fscanf(apm_bat_fp[idx], "%*s %*s %*x %x %x %x %d%%",
|
|
|
|
|
&ac, &status, &flag, &life);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
|
|
if (life == -1) {
|
|
|
|
|
/* could check now that there is ac */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
snprintf(last_battery_str[idx], 64, "AC");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
/* could check that status == 3 here? */
|
|
|
|
|
} else if (ac && life != 100) {
|
|
|
|
|
snprintf(last_battery_str[idx], 64, "charging %d%%", life);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(last_battery_str[idx], 64, "%d%%", life);
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* it seemed to buffer it so file must be closed (or could use
|
|
|
|
|
* syscalls directly but I don't feel like coding it now) */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
fclose(apm_bat_fp[idx]);
|
|
|
|
|
apm_bat_fp[idx] = NULL;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-10 22:45:45 +00:00
|
|
|
|
set_return_value(buffer, n, item, idx);
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
2008-04-10 22:45:45 +00:00
|
|
|
|
void set_return_value(char *buffer, unsigned int n, int item, int idx)
|
|
|
|
|
{
|
2006-12-12 23:21:14 +00:00
|
|
|
|
switch (item) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
case BATTERY_STATUS:
|
2008-03-29 09:58:09 +00:00
|
|
|
|
snprintf(buffer, n, "%s", last_battery_str[idx]);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
break;
|
|
|
|
|
case BATTERY_TIME:
|
2008-03-29 09:58:09 +00:00
|
|
|
|
snprintf(buffer, n, "%s", last_battery_time_str[idx]);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-04-06 06:11:53 +00:00
|
|
|
|
int get_battery_perct(const char *bat)
|
|
|
|
|
{
|
2008-03-24 21:44:50 +00:00
|
|
|
|
static int rep = 0;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
int idx;
|
2007-04-06 06:11:53 +00:00
|
|
|
|
char acpi_path[128];
|
2008-03-29 06:24:04 +00:00
|
|
|
|
char sysfs_path[128];
|
|
|
|
|
int remaining_capacity = -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2007-04-06 06:11:53 +00:00
|
|
|
|
snprintf(acpi_path, 127, ACPI_BATTERY_BASE_PATH "/%s/state", bat);
|
2008-03-17 22:27:20 +00:00
|
|
|
|
snprintf(sysfs_path, 127, SYSFS_BATTERY_BASE_PATH "/%s/uevent", bat);
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
init_batteries();
|
|
|
|
|
|
|
|
|
|
idx = get_battery_idx(bat);
|
|
|
|
|
|
2007-04-06 06:11:53 +00:00
|
|
|
|
/* don't update battery too often */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (current_update_time - last_battery_perct_time[idx] < 30) {
|
|
|
|
|
return last_battery_perct[idx];
|
2007-04-06 06:11:53 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
last_battery_perct_time[idx] = current_update_time;
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
/* Only check for SYSFS or ACPI */
|
|
|
|
|
|
2008-03-24 21:44:50 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
|
2008-03-17 22:27:20 +00:00
|
|
|
|
sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
|
2008-03-24 21:44:50 +00:00
|
|
|
|
rep = 0;
|
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2008-03-24 21:44:50 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
|
2007-08-05 04:47:21 +00:00
|
|
|
|
acpi_bat_fp[idx] = open_file(acpi_path, &rep);
|
2008-03-24 21:44:50 +00:00
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2008-03-17 22:27:20 +00:00
|
|
|
|
if (sysfs_bat_fp[idx] != NULL) {
|
|
|
|
|
/* SYSFS */
|
|
|
|
|
while (!feof(sysfs_bat_fp[idx])) {
|
|
|
|
|
char buf[256];
|
|
|
|
|
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
2008-03-24 05:09:09 +00:00
|
|
|
|
if (strncmp(buf, "POWER_SUPPLY_CHARGE_NOW=", 24) == 0) {
|
2008-03-17 22:27:20 +00:00
|
|
|
|
sscanf(buf, "POWER_SUPPLY_CHARGE_NOW=%d", &remaining_capacity);
|
2008-03-24 05:09:09 +00:00
|
|
|
|
} else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=",25) == 0) {
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_design_capacity[idx]);
|
|
|
|
|
} else if (strncmp(buf, "POWER_SUPPLY_ENERGY_NOW=", 24) == 0) {
|
2008-03-17 22:27:20 +00:00
|
|
|
|
sscanf(buf, "POWER_SUPPLY_ENERGY_NOW=%d", &remaining_capacity);
|
2008-03-24 05:09:09 +00:00
|
|
|
|
} else if (strncmp(buf, "POWER_SUPPLY_ENERGY_FULL=",25) == 0) {
|
|
|
|
|
sscanf(buf, "POWER_SUPPLY_ENERGY_FULL=%d", &acpi_design_capacity[idx]);
|
|
|
|
|
}
|
2008-03-17 22:27:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(sysfs_bat_fp[idx]);
|
|
|
|
|
sysfs_bat_fp[idx] = NULL;
|
|
|
|
|
|
|
|
|
|
} else if (acpi_bat_fp[idx] != NULL) {
|
|
|
|
|
/* ACPI */
|
2007-04-06 06:11:53 +00:00
|
|
|
|
/* read last full capacity if it's zero */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
if (acpi_design_capacity[idx] == 0) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
static int rep2;
|
2007-04-06 06:11:53 +00:00
|
|
|
|
char path[128];
|
|
|
|
|
FILE *fp;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
snprintf(path, 127, ACPI_BATTERY_BASE_PATH "/%s/info", bat);
|
2008-03-29 09:58:09 +00:00
|
|
|
|
fp = open_file(path, &rep2);
|
2007-04-06 06:11:53 +00:00
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char b[256];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(b, 256, fp) == NULL) {
|
2007-04-06 06:11:53 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
if (sscanf(b, "last full capacity: %d",
|
2008-03-17 22:27:20 +00:00
|
|
|
|
&acpi_design_capacity[idx]) != 0) {
|
2007-04-06 06:11:53 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
fseek(acpi_bat_fp[idx], 0, SEEK_SET);
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
|
while (!feof(acpi_bat_fp[idx])) {
|
2007-04-06 06:11:53 +00:00
|
|
|
|
char buf[256];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(buf, 256, acpi_bat_fp[idx]) == NULL) {
|
2007-04-06 06:11:53 +00:00
|
|
|
|
break;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (buf[0] == 'r') {
|
|
|
|
|
sscanf(buf, "remaining capacity: %d", &remaining_capacity);
|
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (remaining_capacity < 0) {
|
2007-04-06 06:11:53 +00:00
|
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-04-06 06:11:53 +00:00
|
|
|
|
/* compute the battery percentage */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
last_battery_perct[idx] =
|
2008-02-20 20:30:45 +00:00
|
|
|
|
(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
|
2008-12-07 22:58:40 +00:00
|
|
|
|
if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
return last_battery_perct[idx];
|
2007-04-06 06:11:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int get_battery_perct_bar(const char *bar)
|
|
|
|
|
{
|
2007-08-05 04:47:21 +00:00
|
|
|
|
int idx;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
2007-04-06 06:11:53 +00:00
|
|
|
|
get_battery_perct(bar);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
idx = get_battery_idx(bar);
|
|
|
|
|
return (int) (last_battery_perct[idx] * 2.56 - 1);
|
2007-04-06 06:11:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 21:35:31 +00:00
|
|
|
|
/* On Apple powerbook and ibook:
|
|
|
|
|
$ cat /proc/pmu/battery_0
|
|
|
|
|
flags : 00000013
|
|
|
|
|
charge : 3623
|
|
|
|
|
max_charge : 3720
|
|
|
|
|
current : 388
|
|
|
|
|
voltage : 16787
|
|
|
|
|
time rem. : 900
|
|
|
|
|
$ cat /proc/pmu/info
|
|
|
|
|
PMU driver version : 2
|
|
|
|
|
PMU firmware version : 0c
|
|
|
|
|
AC Power : 1
|
|
|
|
|
Battery count : 1
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* defines as in <linux/pmu.h> */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
#define PMU_BATT_PRESENT 0x00000001
|
|
|
|
|
#define PMU_BATT_CHARGING 0x00000002
|
2006-04-23 21:35:31 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
static FILE *pmu_battery_fp;
|
|
|
|
|
static FILE *pmu_info_fp;
|
2006-04-23 21:35:31 +00:00
|
|
|
|
static char pb_battery_info[3][32];
|
|
|
|
|
static double pb_battery_info_update;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2006-04-23 21:35:31 +00:00
|
|
|
|
#define PMU_PATH "/proc/pmu"
|
2008-03-29 09:58:09 +00:00
|
|
|
|
void get_powerbook_batt_info(char *buffer, size_t n, int i)
|
2006-04-23 21:35:31 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
static int rep = 0;
|
|
|
|
|
const char *batt_path = PMU_PATH "/battery_0";
|
|
|
|
|
const char *info_path = PMU_PATH "/info";
|
2008-03-29 03:01:48 +00:00
|
|
|
|
unsigned int flags;
|
|
|
|
|
int charge, max_charge, ac = -1;
|
2008-03-29 09:58:09 +00:00
|
|
|
|
long timeval = -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
/* don't update battery too often */
|
|
|
|
|
if (current_update_time - pb_battery_info_update < 29.5) {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
snprintf(buffer, n, "%s", pb_battery_info[i]);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pb_battery_info_update = current_update_time;
|
|
|
|
|
|
|
|
|
|
if (pmu_battery_fp == NULL) {
|
|
|
|
|
pmu_battery_fp = open_file(batt_path, &rep);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pmu_battery_fp != NULL) {
|
|
|
|
|
rewind(pmu_battery_fp);
|
|
|
|
|
while (!feof(pmu_battery_fp)) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
|
|
if (fgets(buf, sizeof(buf), pmu_battery_fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (buf[0] == 'f') {
|
|
|
|
|
sscanf(buf, "flags : %8x", &flags);
|
|
|
|
|
} else if (buf[0] == 'c' && buf[1] == 'h') {
|
|
|
|
|
sscanf(buf, "charge : %d", &charge);
|
|
|
|
|
} else if (buf[0] == 'm') {
|
|
|
|
|
sscanf(buf, "max_charge : %d", &max_charge);
|
|
|
|
|
} else if (buf[0] == 't') {
|
2008-03-29 09:58:09 +00:00
|
|
|
|
sscanf(buf, "time rem. : %ld", &timeval);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pmu_info_fp == NULL) {
|
|
|
|
|
pmu_info_fp = open_file(info_path, &rep);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pmu_info_fp != NULL) {
|
|
|
|
|
rewind(pmu_info_fp);
|
|
|
|
|
while (!feof(pmu_info_fp)) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
|
|
|
|
|
if (fgets(buf, sizeof(buf), pmu_info_fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (buf[0] == 'A') {
|
|
|
|
|
sscanf(buf, "AC Power : %d", &ac);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* update status string */
|
|
|
|
|
if ((ac && !(flags & PMU_BATT_PRESENT))) {
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "AC");
|
|
|
|
|
} else if (ac && (flags & PMU_BATT_PRESENT)
|
|
|
|
|
&& !(flags & PMU_BATT_CHARGING)) {
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "charged");
|
|
|
|
|
} else if ((flags & PMU_BATT_PRESENT) && (flags & PMU_BATT_CHARGING)) {
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "charging");
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(pb_battery_info[PB_BATT_STATUS], "discharging");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* update percentage string */
|
2008-03-29 09:58:09 +00:00
|
|
|
|
if (timeval == 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
pb_battery_info[PB_BATT_PERCENT][0] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(pb_battery_info[PB_BATT_PERCENT],
|
|
|
|
|
sizeof(pb_battery_info[PB_BATT_PERCENT]), "%d%%",
|
|
|
|
|
(charge * 100) / max_charge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* update time string */
|
2008-03-29 09:58:09 +00:00
|
|
|
|
if (timeval == 0) { /* fully charged or battery not present */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
pb_battery_info[PB_BATT_TIME][0] = 0;
|
2008-03-29 09:58:09 +00:00
|
|
|
|
} else if (timeval < 60 * 60) { /* don't show secs */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
format_seconds_short(pb_battery_info[PB_BATT_TIME],
|
2008-03-29 09:58:09 +00:00
|
|
|
|
sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
format_seconds(pb_battery_info[PB_BATT_TIME],
|
2008-03-29 09:58:09 +00:00
|
|
|
|
sizeof(pb_battery_info[PB_BATT_TIME]), timeval);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 09:58:09 +00:00
|
|
|
|
snprintf(buffer, n, "%s", pb_battery_info[i]);
|
2006-04-23 21:35:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void update_top(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
|
{
|
|
|
|
|
show_nice_processes = 1;
|
2005-07-25 00:22:16 +00:00
|
|
|
|
process_find_top(info.cpu, info.memu);
|
2005-11-10 04:19:43 +00:00
|
|
|
|
info.first_process = get_first_process();
|
2005-07-20 00:30:40 +00:00
|
|
|
|
}
|
2005-08-26 02:16:35 +00:00
|
|
|
|
|
2006-04-15 18:00:39 +00:00
|
|
|
|
/* Here come the IBM ACPI-specific things. For reference, see
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* http://ibm-acpi.sourceforge.net/README
|
|
|
|
|
* If IBM ACPI is installed, /proc/acpi/ibm contains the following files:
|
2006-04-15 18:00:39 +00:00
|
|
|
|
bay
|
|
|
|
|
beep
|
|
|
|
|
bluetooth
|
|
|
|
|
brightness
|
|
|
|
|
cmos
|
|
|
|
|
dock
|
|
|
|
|
driver
|
|
|
|
|
ecdump
|
|
|
|
|
fan
|
|
|
|
|
hotkey
|
|
|
|
|
led
|
|
|
|
|
light
|
|
|
|
|
thermal
|
|
|
|
|
video
|
|
|
|
|
volume
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* The content of these files is described in detail in the aforementioned
|
|
|
|
|
* README - some of them also in the following functions accessing them.
|
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
|
|
|
|
#define IBM_ACPI_DIR "/proc/acpi/ibm"
|
|
|
|
|
|
|
|
|
|
/* get fan speed on IBM/Lenovo laptops running the ibm acpi.
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* /proc/acpi/ibm/fan looks like this (3 lines):
|
2006-04-15 18:00:39 +00:00
|
|
|
|
status: disabled
|
|
|
|
|
speed: 2944
|
|
|
|
|
commands: enable, disable
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_ibm_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
unsigned int speed = 0;
|
|
|
|
|
char fan[128];
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-03-29 06:24:04 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(fan, 127, "%s/fan", IBM_ACPI_DIR);
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fp = fopen(fan, "r");
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char line[256];
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-03-29 03:01:48 +00:00
|
|
|
|
if (sscanf(line, "speed: %u", &speed)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
|
2008-07-12 10:25:05 +00:00
|
|
|
|
"ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%d", speed);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
}
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* get the measured temperatures from the temperature sensors
|
|
|
|
|
* on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
* There are 8 values in /proc/acpi/ibm/thermal, and according to
|
|
|
|
|
* http://ibm-acpi.sourceforge.net/README
|
|
|
|
|
* these mean the following (at least on an IBM R51...)
|
|
|
|
|
* 0: CPU (also on the T series laptops)
|
|
|
|
|
* 1: Mini PCI Module (?)
|
|
|
|
|
* 2: HDD (?)
|
|
|
|
|
* 3: GPU (also on the T series laptops)
|
|
|
|
|
* 4: Battery (?)
|
|
|
|
|
* 5: N/A
|
|
|
|
|
* 6: Battery (?)
|
|
|
|
|
* 7: N/A
|
|
|
|
|
* I'm not too sure about those with the question mark, but the values I'm
|
|
|
|
|
* reading from *my* thermal file (on a T42p) look realistic for the
|
|
|
|
|
* hdd and the battery.
|
|
|
|
|
* #5 and #7 are always -128.
|
|
|
|
|
* /proc/acpi/ibm/thermal looks like this (1 line):
|
|
|
|
|
temperatures: 41 43 31 46 33 -128 29 -128
|
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
|
|
|
|
|
2006-04-15 18:00:39 +00:00
|
|
|
|
static double last_ibm_acpi_temp_time;
|
2008-03-29 02:01:03 +00:00
|
|
|
|
void get_ibm_acpi_temps(void)
|
2006-04-15 18:00:39 +00:00
|
|
|
|
{
|
|
|
|
|
|
2008-03-29 06:24:04 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
char thermal[128];
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* don't update too often */
|
|
|
|
|
if (current_update_time - last_ibm_acpi_temp_time < 10.00) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
last_ibm_acpi_temp_time = current_update_time;
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* if (!p_client_buffer || client_buffer_size <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
} */
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(thermal, 127, "%s/thermal", IBM_ACPI_DIR);
|
|
|
|
|
fp = fopen(thermal, "r");
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char line[256];
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (sscanf(line, "temperatures: %d %d %d %d %d %d %d %d",
|
|
|
|
|
&ibm_acpi.temps[0], &ibm_acpi.temps[1], &ibm_acpi.temps[2],
|
|
|
|
|
&ibm_acpi.temps[3], &ibm_acpi.temps[4], &ibm_acpi.temps[5],
|
|
|
|
|
&ibm_acpi.temps[6], &ibm_acpi.temps[7])) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
|
2008-07-12 10:25:05 +00:00
|
|
|
|
"ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* get volume (0-14) on IBM/Lenovo laptops running the ibm acpi.
|
|
|
|
|
* "Volume" here is none of the mixer volumes, but a "master of masters"
|
|
|
|
|
* volume adjusted by the IBM volume keys.
|
|
|
|
|
* /proc/acpi/ibm/fan looks like this (4 lines):
|
2006-04-15 18:00:39 +00:00
|
|
|
|
level: 4
|
|
|
|
|
mute: off
|
|
|
|
|
commands: up, down, mute
|
|
|
|
|
commands: level <level> (<level> is 0-15)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_ibm_acpi_volume(char *p_client_buffer, size_t client_buffer_size)
|
|
|
|
|
{
|
2008-03-29 06:24:04 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
char volume[128];
|
|
|
|
|
unsigned int vol = -1;
|
|
|
|
|
char mute[3] = "";
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2007-09-28 20:16:16 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(volume, 127, "%s/volume", IBM_ACPI_DIR);
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2007-09-28 20:16:16 +00:00
|
|
|
|
fp = fopen(volume, "r");
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char line[256];
|
|
|
|
|
unsigned int read_vol = -1;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-03-29 03:01:48 +00:00
|
|
|
|
if (sscanf(line, "level: %u", &read_vol)) {
|
2007-09-28 20:16:16 +00:00
|
|
|
|
vol = read_vol;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (sscanf(line, "mute: %s", mute)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-09-28 20:16:16 +00:00
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
|
2008-07-12 10:25:05 +00:00
|
|
|
|
"ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
|
2006-04-15 18:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-28 20:16:16 +00:00
|
|
|
|
fclose(fp);
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (strcmp(mute, "on") == 0) {
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%s", "mute");
|
2007-09-28 20:16:16 +00:00
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%d", vol);
|
2007-09-28 20:16:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2006-04-15 18:00:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
/* static FILE *fp = NULL; */
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
|
|
|
|
/* get LCD brightness on IBM/Lenovo laptops running the ibm acpi.
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* /proc/acpi/ibm/brightness looks like this (3 lines):
|
2006-04-15 18:00:39 +00:00
|
|
|
|
level: 7
|
|
|
|
|
commands: up, down
|
|
|
|
|
commands: level <level> (<level> is 0-7)
|
2008-02-20 20:30:45 +00:00
|
|
|
|
* Peter Tarjan (ptarjan@citromail.hu) */
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void get_ibm_acpi_brightness(char *p_client_buffer, size_t client_buffer_size)
|
|
|
|
|
{
|
|
|
|
|
FILE *fp;
|
|
|
|
|
unsigned int brightness = 0;
|
|
|
|
|
char filename[128];
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-03-29 06:24:04 +00:00
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(filename, 127, "%s/brightness", IBM_ACPI_DIR);
|
|
|
|
|
|
|
|
|
|
fp = fopen(filename, "r");
|
|
|
|
|
if (fp != NULL) {
|
|
|
|
|
while (!feof(fp)) {
|
|
|
|
|
char line[256];
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if (fgets(line, 255, fp) == NULL) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-03-29 03:01:48 +00:00
|
|
|
|
if (sscanf(line, "level: %u", &brightness)) {
|
2008-02-20 20:30:45 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
|
2008-07-12 10:25:05 +00:00
|
|
|
|
"ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
|
}
|
2006-04-15 18:00:39 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(fp);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, "%d", brightness);
|
2007-08-05 04:47:21 +00:00
|
|
|
|
}
|
2006-11-30 20:46:34 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
void update_entropy(void)
|
2006-11-30 20:46:34 +00:00
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
|
static int rep = 0;
|
|
|
|
|
const char *entropy_avail = "/proc/sys/kernel/random/entropy_avail";
|
|
|
|
|
const char *entropy_poolsize = "/proc/sys/kernel/random/poolsize";
|
|
|
|
|
FILE *fp1, *fp2;
|
2006-11-30 20:46:34 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
info.entropy.entropy_avail = 0;
|
|
|
|
|
info.entropy.poolsize = 0;
|
2006-11-30 20:46:34 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if ((fp1 = open_file(entropy_avail, &rep)) == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
if ((fp2 = open_file(entropy_poolsize, &rep)) == NULL) {
|
|
|
|
|
fclose(fp1);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fscanf(fp1, "%u", &info.entropy.entropy_avail);
|
|
|
|
|
fscanf(fp2, "%u", &info.entropy.poolsize);
|
2006-12-23 06:01:16 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
fclose(fp1);
|
|
|
|
|
fclose(fp2);
|
2006-11-30 20:46:34 +00:00
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
|
info.mask |= (1 << INFO_ENTROPY);
|
2006-12-23 06:01:16 +00:00
|
|
|
|
}
|
2008-03-24 20:08:16 +00:00
|
|
|
|
|
2008-03-29 03:45:36 +00:00
|
|
|
|
const char *get_disk_protect_queue(const char *disk)
|
2008-03-24 20:08:16 +00:00
|
|
|
|
{
|
2008-03-24 20:43:55 +00:00
|
|
|
|
FILE *fp;
|
|
|
|
|
char path[128];
|
|
|
|
|
int state;
|
|
|
|
|
|
|
|
|
|
snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
|
|
|
|
|
if ((fp = fopen(path, "r")) == NULL)
|
|
|
|
|
return "n/a ";
|
|
|
|
|
if (fscanf(fp, "%d\n", &state) != 1) {
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return "failed";
|
|
|
|
|
}
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return state ? "frozen" : "free ";
|
2008-03-24 20:08:16 +00:00
|
|
|
|
}
|
|
|
|
|
|