* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
- * $Id$ */
+ */
#include "conky.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "logging.h"
+#include "common.h"
+#include "linux.h"
#include <dirent.h>
#include <ctype.h>
#include <errno.h>
char buf[256];
info.mem = info.memmax = info.swap = info.swapmax = info.bufmem =
- info.buffers = info.cached = 0;
+ info.buffers = info.cached = info.memfree = info.memeasyfree = 0;
if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) {
return;
if (strncmp(buf, "MemTotal:", 9) == 0) {
sscanf(buf, "%*s %llu", &info.memmax);
} else if (strncmp(buf, "MemFree:", 8) == 0) {
- sscanf(buf, "%*s %llu", &info.mem);
+ sscanf(buf, "%*s %llu", &info.memfree);
} else if (strncmp(buf, "SwapTotal:", 10) == 0) {
sscanf(buf, "%*s %llu", &info.swapmax);
} else if (strncmp(buf, "SwapFree:", 9) == 0) {
}
}
- info.mem = info.memmax - info.mem;
+ info.mem = info.memmax - info.memfree;
+ info.memeasyfree = info.memfree;
info.swap = info.swapmax - info.swap;
info.bufmem = info.cached + info.buffers;
x = strndup(y, text_buffer_size); \
}
-void update_gateway_info_failure(char *reason)
+void update_gateway_info_failure(const char *reason)
{
if(reason != NULL) {
perror(reason);
return;
}
-inline void update_net_stats(void)
+void update_net_stats(void)
{
FILE *net_dev_fp;
static int rep = 0;
for (i2 = 0; i2 < 16; i2++) {
struct net_stat *ns;
char *s, *p;
- char temp_addr[17];
+ char temp_addr[18];
long long r, t, last_recv, last_trans;
if (fgets(buf, 255, net_dev_fp) == NULL) {
memset(&(ns->addr.sa_data), 0, 14);
if(NULL == ns->addrs)
- ns->addrs = (char*) malloc(17 * 16);
+ ns->addrs = (char*) malloc(17 * 16 + 1);
if(NULL != ns->addrs)
- memset(ns->addrs, 0, 17 * 16); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
+ memset(ns->addrs, 0, 17 * 16 + 1); /* Up to 17 chars per ip, max 16 interfaces. Nasty memory usage... */
last_recv = ns->recv;
last_trans = ns->trans;
info.mask |= (1 << INFO_RUN_PROCS);
} else if (strncmp(buf, "cpu", 3) == 0) {
double delta;
- idx = isdigit(buf[3]) ? ((int) buf[3]) - 0x2F : 0;
+ if (isdigit(buf[3])) {
+ idx = atoi(&buf[3]) + 1;
+ } else {
+ idx = 0;
+ }
sscanf(buf, stat_template, &(cpu[idx].cpu_user),
&(cpu[idx].cpu_nice), &(cpu[idx].cpu_system),
&(cpu[idx].cpu_idle), &(cpu[idx].cpu_iowait),
fd = open(path, O_RDONLY);
if (fd < 0) {
CRIT_ERR("can't open '%s': %s\nplease check your device or remove this "
- "var from Conky", path, strerror(errno));
+ "var from "PACKAGE_NAME, path, strerror(errno));
}
if (strcmp(type, "in") == 0 || strcmp(type, "temp") == 0
snprintf(p_client_buffer, client_buffer_size, "%s", adt746x_cpu_state);
}
-/* Thanks to "Walt Nelson" <wnelsonjr@comcast.net> */
-
-/***********************************************************************/
-/* This file is part of x86info.
- * (C) 2001 Dave Jones.
- *
- * Licensed under the terms of the GNU GPL License version 2.
- *
- * Estimate CPU MHz routine by Andrea Arcangeli <andrea@suse.de>
- * Small changes by David Sterba <sterd9am@ss1000.ms.mff.cuni.cz> */
-
-#if defined(__i386) || defined(__x86_64)
-__inline__ unsigned long long int rdtsc(void)
-{
- unsigned long long int x;
-
- __asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
- return x;
-}
-#endif
-
-/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
-void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
- const char *p_format, int divisor)
-{
-#if defined(__i386) || defined(__x86_64)
- struct timezone tz;
- struct timeval tvstart, tvstop;
- unsigned long long cycles[2]; /* gotta be 64 bit */
- unsigned int microseconds; /* total time taken */
-
- if (!p_client_buffer || client_buffer_size <= 0 || !p_format
- || divisor <= 0) {
- return;
- }
-
- memset(&tz, 0, sizeof(tz));
-
- /* get this function in cached memory */
- gettimeofday(&tvstart, &tz);
- cycles[0] = rdtsc();
- gettimeofday(&tvstart, &tz);
-
- /* we don't trust that this is any specific length of time */
- usleep(100);
- cycles[1] = rdtsc();
- gettimeofday(&tvstop, &tz);
- microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
- (tvstop.tv_usec - tvstart.tv_usec);
-
- snprintf(p_client_buffer, client_buffer_size, p_format,
- (float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
- return;
-#else
- /* FIXME: hardwired: get freq for first cpu!
- * this whole function needs to be rethought and redone for
- * multi-cpu/multi-core/multi-threaded environments and
- * arbitrary combinations thereof */
- get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
- return;
-#endif
-}
-
#define CPUFREQ_PREFIX "/sys/devices/system/cpu"
#define CPUFREQ_POSTFIX "cpufreq/scaling_cur_freq"
// open the CPU information file
f = open_file("/proc/cpuinfo", &rep);
if (!f) {
- perror("Conky: Failed to access '/proc/cpuinfo' at get_freq()");
+ perror(PACKAGE_NAME": Failed to access '/proc/cpuinfo' at get_freq()");
return 0;
}
}
fclose(f);
} else {
- fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
+ fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
perror("get_voltage()");
if (f) {
fclose(f);
}
fclose(f);
} else {
- fprintf(stderr, "Conky: Failed to access '%s' at ", current_freq_file);
+ fprintf(stderr, PACKAGE_NAME": Failed to access '%s' at ", current_freq_file);
perror("get_voltage()");
if (f) {
fclose(f);
snprintf(p_client_buffer, client_buffer_size, "%s", buf);
}
+#define SYSFS_AC_ADAPTER_DIR "/sys/class/power_supply/AC"
#define ACPI_AC_ADAPTER_DIR "/proc/acpi/ac_adapter/"
+/* 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
+*/
void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
{
static int rep = 0;
+
char buf[256];
char buf2[256];
FILE *fp;
return;
}
- /* 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;
- }
+ 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;
- snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
+ 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;
+ }
- 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);
+ snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf);
- snprintf(p_client_buffer, client_buffer_size, "%s", buf);
+ 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);
+
+ snprintf(p_client_buffer, client_buffer_size, "%s", buf);
+ }
}
/*
void get_battery_stuff(char *buffer, unsigned int n, const char *bat, int item)
{
- static int idx, rep = 0, rep2 = 0;
+ static int idx, rep = 0, rep1 = 0, rep2 = 0;
char acpi_path[128];
char sysfs_path[128];
memset(last_battery_time_str[idx], 0, sizeof(last_battery_time_str[idx]));
/* first try SYSFS if that fails try ACPI */
-
+
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
sysfs_bat_fp[idx] = open_file(sysfs_path, &rep);
- rep = 0;
}
-
+
if (sysfs_bat_fp[idx] == NULL && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == NULL) {
- acpi_bat_fp[idx] = open_file(acpi_path, &rep);
+ acpi_bat_fp[idx] = open_file(acpi_path, &rep1);
}
-
+
if (sysfs_bat_fp[idx] != NULL) {
/* SYSFS */
int present_rate = -1;
int remaining_capacity = -1;
char charging_state[64];
char present[4];
-
+
strcpy(charging_state, "unknown");
-
+
while (!feof(sysfs_bat_fp[idx])) {
char buf[256];
if (fgets(buf, 256, sysfs_bat_fp[idx]) == NULL)
break;
-
+
/* let's just hope units are ok */
if (strncmp (buf, "POWER_SUPPLY_PRESENT=1", 22) == 0)
strcpy(present, "yes");
else if (strncmp(buf, "POWER_SUPPLY_CHARGE_FULL=", 25) == 0)
sscanf(buf, "POWER_SUPPLY_CHARGE_FULL=%d", &acpi_last_full[idx]);
}
-
+
fclose(sysfs_bat_fp[idx]);
sysfs_bat_fp[idx] = NULL;
-
+
/* 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% */
-
+
/* not present */
if (strcmp(present, "No") == 0) {
strncpy(last_battery_str[idx], "not present", 64);
/* compute the battery percentage */
last_battery_perct[idx] =
(int) (((float) remaining_capacity / acpi_design_capacity[idx]) * 100);
+ if (last_battery_perct[idx] > 100) last_battery_perct[idx] = 100;
return last_battery_perct[idx];
}
}
} else {
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
- "ibm* from your Conky config file.", fan, strerror(errno));
+ "ibm* from your "PACKAGE_NAME" config file.", fan, strerror(errno));
}
fclose(fp);
}
} else {
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
- "ibm* from your Conky config file.", thermal, strerror(errno));
+ "ibm* from your "PACKAGE_NAME" config file.", thermal, strerror(errno));
}
fclose(fp);
}
} else {
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
- "ibm* from your Conky config file.", volume, strerror(errno));
+ "ibm* from your "PACKAGE_NAME" config file.", volume, strerror(errno));
}
fclose(fp);
}
} else {
CRIT_ERR("can't open '%s': %s\nYou are not using the IBM ACPI. Remove "
- "ibm* from your Conky config file.", filename, strerror(errno));
+ "ibm* from your "PACKAGE_NAME" config file.", filename, strerror(errno));
}
fclose(fp);
char path[128];
int state;
- snprintf(path, 127, "/sys/block/%s/queue/protect", disk);
+ snprintf(path, 127, "/sys/block/%s/device/unload_heads", disk);
+ if (access(path, F_OK)) {
+ 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) {
return "failed";
}
fclose(fp);
- return state ? "frozen" : "free ";
+ return (state > 0) ? "frozen" : "free ";
}