diff --git a/src/linux.cc b/src/linux.cc index 09d6f77a..accc913b 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -133,10 +133,10 @@ int update_uptime(void) { } else #endif { - static int rep = 0; + static int reported = 0; FILE *fp; - if (!(fp = open_file("/proc/uptime", &rep))) { + if (!(fp = open_file("/proc/uptime", &reported))) { info.uptime = 0.0; return 0; } @@ -174,7 +174,7 @@ int check_mount(struct text_object *obj) { int update_meminfo(void) { FILE *meminfo_fp; - static int rep = 0; + static int reported = 0; /* unsigned int a; */ char buf[256]; @@ -185,7 +185,7 @@ int update_meminfo(void) { info.swapfree = info.swapmax = info.bufmem = info.buffers = info.cached = info.memfree = info.memeasyfree = 0; - if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) { return 0; } + if (!(meminfo_fp = open_file("/proc/meminfo", &reported))) { return 0; } while (!feof(meminfo_fp)) { if (fgets(buf, 255, meminfo_fp) == nullptr) { break; } @@ -462,38 +462,25 @@ int update_net_stats(void) { update_gateway_info(); update_gateway_info2(); FILE *net_dev_fp; - static int rep = 0; - /* variably to notify the parts averaging the download speed, that this + static int reported = 0; + /* variable to notify the parts averaging the download speed, that this * is the first call ever to this function. This variable can't be used * to decide if this is the first time an interface was parsed as there * are many interfaces, which can be activated and deactivated at arbitrary * times */ - static char first = 1; + static bool is_first_update = true; // FIXME: arbitrary size chosen to keep code simple. - int i; - int i2; - unsigned int curtmp1; - unsigned int curtmp2; - unsigned int k; - struct ifconf conf; char buf[256]; - double delta; - -#ifdef BUILD_WLAN - // wireless info variables - int skfd, has_bitrate = 0; - struct wireless_info *winfo; - struct iwreq wrq; -#endif + double time_between_updates; /* get delta */ - delta = current_update_time - last_update_time; - if (delta <= 0.0001) { return 0; } + time_between_updates = current_update_time - last_update_time; + if (time_between_updates <= 0.0001) { return 0; } /* open file /proc/net/dev. If not something went wrong, clear all * network statistics */ - if (!(net_dev_fp = open_file("/proc/net/dev", &rep))) { + if (!(net_dev_fp = open_file("/proc/net/dev", &reported))) { clear_net_stats(); return 0; } @@ -509,7 +496,14 @@ int update_net_stats(void) { } /* read each interface */ - for (i2 = 0; i2 < MAX_NET_INTERFACES; i2++) { +#ifdef BUILD_WLAN + // wireless info variables + int skfd, has_bitrate = 0; + struct wireless_info *winfo; + struct iwreq wrq; +#endif + + for (int i2 = 0; i2 < MAX_NET_INTERFACES; i2++) { struct net_stat *ns; char *s, *p; char temp_addr[18]; @@ -549,12 +543,12 @@ int update_net_stats(void) { * to currently received, meaning the change in network traffic is 0 */ if (ns->last_read_recv == -1) { ns->recv = r; - first = 1; + is_first_update = true; ns->last_read_recv = r; } if (ns->last_read_trans == -1) { ns->trans = t; - first = 1; + is_first_update = true; ns->last_read_trans = t; } /* move current traffic statistic to last thereby obsoleting the @@ -580,15 +574,16 @@ int update_net_stats(void) { ns->last_read_trans = t; /*** ip addr patch ***/ - i = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + int file_descriptor = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + struct ifconf conf; conf.ifc_buf = (char *)malloc(sizeof(struct ifreq) * MAX_NET_INTERFACES); conf.ifc_len = sizeof(struct ifreq) * MAX_NET_INTERFACES; memset(conf.ifc_buf, 0, conf.ifc_len); - ioctl((long)i, SIOCGIFCONF, &conf); + ioctl(file_descriptor, SIOCGIFCONF, &conf); - for (k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) { + for (unsigned int k = 0; k < conf.ifc_len / sizeof(struct ifreq); k++) { struct net_stat *ns2; if (!(((struct ifreq *)conf.ifc_buf) + k)) break; @@ -603,26 +598,26 @@ int update_net_stats(void) { strncpy(ns2->addrs + strlen(ns2->addrs), temp_addr, 17); } - close((long)i); + close(file_descriptor); free(conf.ifc_buf); /*** end ip addr patch ***/ - if (!first) { - /* calculate instantenous speeds */ - ns->net_rec[0] = (ns->recv - last_recv) / delta; - ns->net_trans[0] = (ns->trans - last_trans) / delta; + if (!is_first_update) { + /* calculate instantaneous speeds */ + ns->net_rec[0] = (ns->recv - last_recv) / time_between_updates; + ns->net_trans[0] = (ns->trans - last_trans) / time_between_updates; } - curtmp1 = 0; - curtmp2 = 0; + unsigned int curtmp1 = 0; + unsigned int curtmp2 = 0; /* get an average over the last speed samples */ int samples = net_avg_samples.get(*state); /* is OpenMP actually useful here? How large is samples? > 1000 ? */ #ifdef HAVE_OPENMP #pragma omp parallel for reduction(+ : curtmp1, curtmp2) schedule(dynamic, 10) #endif /* HAVE_OPENMP */ - for (i = 0; i < samples; i++) { + for (int i = 0; i < samples; i++) { curtmp1 = curtmp1 + ns->net_rec[i]; curtmp2 = curtmp2 + ns->net_trans[i]; } @@ -632,7 +627,7 @@ int update_net_stats(void) { #ifdef HAVE_OPENMP #pragma omp parallel for schedule(dynamic, 10) #endif /* HAVE_OPENMP */ - for (i = samples; i > 1; i--) { + for (int i = samples; i > 1; i--) { ns->net_rec[i - 1] = ns->net_rec[i - 2]; ns->net_trans[i - 1] = ns->net_trans[i - 2]; } @@ -691,6 +686,7 @@ int update_net_stats(void) { snprintf(ns->essid, 34, "%s", "off/any"); } } + // get channel and freq if (winfo->b.has_freq) { if (winfo->has_range == 1) { @@ -704,6 +700,7 @@ int update_net_stats(void) { snprintf(ns->mode, 16, "%s", iw_operation_mode[winfo->b.mode]); } + iw_sockets_close(skfd); free(winfo); #endif @@ -716,6 +713,7 @@ int update_net_stats(void) { unsigned int netmask, scope; struct net_stat *ns; struct v6addr *lastv6; + // remove the old v6 addresses otherwise they are listed multiple times for (unsigned int i = 0; i < MAX_NET_INTERFACES; i++) { ns = &netstats[i]; @@ -725,10 +723,12 @@ int update_net_stats(void) { free(lastv6); } } + if ((file = fopen(PROCDIR "/net/if_inet6", "r")) != nullptr) { while (fscanf(file, "%32s %*02x %02x %02x %*02x %20s\n", v6addr, &netmask, &scope, devname) != EOF) { ns = get_net_stat(devname, nullptr, NULL); + if (ns->v6addrs == nullptr) { lastv6 = (struct v6addr *)malloc(sizeof(struct v6addr)); ns->v6addrs = lastv6; @@ -738,9 +738,12 @@ int update_net_stats(void) { lastv6->next = (struct v6addr *)malloc(sizeof(struct v6addr)); lastv6 = lastv6->next; } + for (int i = 0; i < 16; i++) sscanf(v6addr + 2 * i, "%2hhx", &(lastv6->addr.s6_addr[i])); + lastv6->netmask = netmask; + switch (scope) { case 0: // global lastv6->scope = 'G'; @@ -760,13 +763,15 @@ int update_net_stats(void) { default: lastv6->scope = '?'; } + lastv6->next = nullptr; } + fclose(file); } #endif /* BUILD_IPV6 */ - first = 0; + is_first_update = false; fclose(net_dev_fp); return 0; @@ -803,10 +808,10 @@ int update_threads(void) { } else #endif { - static int rep = 0; + static int reported = 0; FILE *fp; - if (!(fp = open_file("/proc/loadavg", &rep))) { + if (!(fp = open_file("/proc/loadavg", &reported))) { info.threads = 0; return 0; } @@ -853,10 +858,10 @@ void determine_longstat(char *buf) { void determine_longstat_file(void) { #define MAX_PROCSTAT_LINELEN 255 FILE *stat_fp; - static int rep = 0; + static int reported = 0; char buf[MAX_PROCSTAT_LINELEN + 1]; - if (!(stat_fp = open_file("/proc/stat", &rep))) return; + if (!(stat_fp = open_file("/proc/stat", &reported))) return; while (!feof(stat_fp) && fgets(buf, MAX_PROCSTAT_LINELEN, stat_fp) != nullptr) { if (strncmp(buf, "cpu", 3) == 0) { @@ -869,7 +874,7 @@ void determine_longstat_file(void) { void get_cpu_count(void) { FILE *stat_fp; - static int rep = 0; + static int reported = 0; char buf[256]; char *str1, *str2, *token, *subtoken; char *saveptr1, *saveptr2; @@ -878,7 +883,7 @@ void get_cpu_count(void) { if (info.cpu_usage) { return; } - if (!(stat_fp = open_file("/sys/devices/system/cpu/present", &rep))) { + if (!(stat_fp = open_file("/sys/devices/system/cpu/present", &reported))) { return; } @@ -920,7 +925,7 @@ void get_cpu_count(void) { int update_stat(void) { FILE *stat_fp; - static int rep = 0; + static int reported = 0; struct cpu_info *cpu = nullptr; char buf[256]; int i; @@ -965,7 +970,7 @@ int update_stat(void) { global_cpu = cpu; } - if (!(stat_fp = open_file("/proc/stat", &rep))) { + if (!(stat_fp = open_file("/proc/stat", &reported))) { info.run_threads = 0; if (info.cpu_usage) { memset(info.cpu_usage, 0, info.cpu_count * sizeof(float)); @@ -1080,10 +1085,10 @@ int update_load_average(void) { } else #endif { - static int rep = 0; + static int reported = 0; FILE *fp; - if (!(fp = open_file("/proc/loadavg", &rep))) { + if (!(fp = open_file("/proc/loadavg", &reported))) { info.loadavg[0] = info.loadavg[1] = info.loadavg[2] = 0.0; return 0; } @@ -1104,15 +1109,16 @@ static int no_dots(const struct dirent *d) { return 1; } -static int get_first_file_in_a_directory(const char *dir, char *s, int *rep) { +static int get_first_file_in_a_directory(const char *dir, char *s, + int *reported) { struct dirent **namelist; int i, n; n = scandir(dir, &namelist, no_dots, alphasort); if (n < 0) { - if (!rep || !*rep) { + if (!reported || !*reported) { NORM_ERR("scandir for %s: %s", dir, strerror(errno)); - if (rep) { *rep = 1; } + if (reported) { *reported = 1; } } return 0; } else { @@ -1198,9 +1204,9 @@ static int open_sysfs_sensor(const char *dir, const char *dev, const char *type, /* if device is nullptr or *, get first */ if (dev == nullptr || strcmp(dev, "*") == 0) { - static int rep = 0; + static int reported = 0; - if (!get_first_file_in_a_directory(dir, buf, &rep)) { return -1; } + if (!get_first_file_in_a_directory(dir, buf, &reported)) { return -1; } dev = buf; } @@ -1450,7 +1456,7 @@ void free_sysfs_sensor(struct text_object *obj) { char get_freq(char *p_client_buffer, size_t client_buffer_size, const char *p_format, int divisor, unsigned int cpu) { FILE *f; - static int rep = 0; + static int reported = 0; char frequency[32]; char s[256]; double freq = 0; @@ -1481,7 +1487,7 @@ char get_freq(char *p_client_buffer, size_t client_buffer_size, } // open the CPU information file - f = open_file("/proc/cpuinfo", &rep); + f = open_file("/proc/cpuinfo", &reported); if (!f) { perror(PACKAGE_NAME ": Failed to access '/proc/cpuinfo' at get_freq()"); return 0; @@ -1618,7 +1624,7 @@ void print_voltage_v(struct text_object *obj, char *p, #define ACPI_FAN_DIR "/proc/acpi/fan/" void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) { - static int rep = 0; + static int reported = 0; char buf[256]; char buf2[256]; FILE *fp; @@ -1626,14 +1632,14 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) { if (!p_client_buffer || client_buffer_size <= 0) { return; } /* yeah, slow... :/ */ - if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &rep)) { + if (!get_first_file_in_a_directory(ACPI_FAN_DIR, buf, &reported)) { snprintf(p_client_buffer, client_buffer_size, "%s", "no fans?"); return; } snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_FAN_DIR, buf); - fp = open_file(buf2, &rep); + fp = open_file(buf2, &reported); if (!fp) { snprintf(p_client_buffer, client_buffer_size, "%s", "can't open fan's state file"); @@ -1668,7 +1674,7 @@ void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size) { void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, const char *adapter) { - static int rep = 0; + static int reported = 0; char buf[256]; char buf2[256]; @@ -1685,7 +1691,7 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, snprintf(buf2, sizeof(buf2), "%s/ADP1/uevent", SYSFS_AC_ADAPTER_DIR); } if (stat(buf2, &sb) == 0) - fp = open_file(buf2, &rep); + fp = open_file(buf2, &reported); else fp = 0; if (fp) { @@ -1704,14 +1710,14 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, fclose(fp); } else { /* yeah, slow... :/ */ - if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &rep)) { + if (!get_first_file_in_a_directory(ACPI_AC_ADAPTER_DIR, buf, &reported)) { snprintf(p_client_buffer, client_buffer_size, "%s", "no ac_adapters?"); return; } snprintf(buf2, sizeof(buf2), "%s%s/state", ACPI_AC_ADAPTER_DIR, buf); - fp = open_file(buf2, &rep); + fp = open_file(buf2, &reported); if (!fp) { snprintf(p_client_buffer, client_buffer_size, "%s", "No ac adapter found.... where is it?"); @@ -2279,7 +2285,7 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) { } int _get_battery_perct(const char *bat) { - static int rep = 0; + static int reported = 0; int idx; char acpi_path[128]; char sysfs_path[128]; @@ -2300,13 +2306,13 @@ int _get_battery_perct(const char *bat) { if (sysfs_bat_fp[idx] == nullptr && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == nullptr) { - sysfs_bat_fp[idx] = open_file(sysfs_path, &rep); - rep = 0; + sysfs_bat_fp[idx] = open_file(sysfs_path, &reported); + reported = 0; } if (sysfs_bat_fp[idx] == nullptr && acpi_bat_fp[idx] == NULL && apm_bat_fp[idx] == nullptr) { - acpi_bat_fp[idx] = open_file(acpi_path, &rep); + acpi_bat_fp[idx] = open_file(acpi_path, &reported); } if (sysfs_bat_fp[idx] != nullptr) { @@ -2442,7 +2448,7 @@ void powerbook_update_time(long timeval); #define PMU_PATH "/proc/pmu" void get_powerbook_batt_info(struct text_object *obj, char *buffer, unsigned int n) { - static int rep = 0; + static int reported = 0; const char *batt_path = PMU_PATH "/battery_0"; const char *info_path = PMU_PATH "/info"; unsigned int flags = 0; @@ -2459,7 +2465,7 @@ void get_powerbook_batt_info(struct text_object *obj, char *buffer, pb_battery_info_update = current_update_time; if (pmu_battery_fp == nullptr) { - pmu_battery_fp = open_file(batt_path, &rep); + pmu_battery_fp = open_file(batt_path, &reported); if (pmu_battery_fp == nullptr) { return; } } @@ -2479,7 +2485,7 @@ void get_powerbook_batt_info(struct text_object *obj, char *buffer, sscanf(buf, "time rem. : %ld", &timeval); } } - pmu_info_fp = open_file(info_path, &rep); + pmu_info_fp = open_file(info_path, &reported); if (pmu_info_fp == nullptr) { return; } rewind(pmu_info_fp); @@ -2548,10 +2554,10 @@ void powerbook_update_time(long timeval) { #define ENTROPY_AVAIL_PATH "/proc/sys/kernel/random/entropy_avail" int get_entropy_avail(unsigned int *val) { - static int rep = 0; + static int reported = 0; FILE *fp; - if (!(fp = open_file(ENTROPY_AVAIL_PATH, &rep))) return 1; + if (!(fp = open_file(ENTROPY_AVAIL_PATH, &reported))) return 1; if (fscanf(fp, "%u", val) != 1) return 1; @@ -2562,10 +2568,10 @@ int get_entropy_avail(unsigned int *val) { #define ENTROPY_POOLSIZE_PATH "/proc/sys/kernel/random/poolsize" int get_entropy_poolsize(unsigned int *val) { - static int rep = 0; + static int reported = 0; FILE *fp; - if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &rep))) return 1; + if (!(fp = open_file(ENTROPY_POOLSIZE_PATH, &reported))) return 1; if (fscanf(fp, "%u", val) != 1) return 1; @@ -2615,7 +2621,7 @@ int is_disk(char *dev) { int update_diskio(void) { FILE *fp; - static int rep = 0; + static int reported = 0; char buf[512], devbuf[64]; unsigned int major, minor; int col_count = 0; @@ -2627,7 +2633,7 @@ int update_diskio(void) { stats.current_read = 0; stats.current_write = 0; - if (!(fp = open_file("/proc/diskstats", &rep))) { return 0; } + if (!(fp = open_file("/proc/diskstats", &reported))) { return 0; } /* read reads and writes from all disks (minor = 0), including cd-roms * and floppies, and sum them up */