]> granicus.if.org Git - sysstat/blobdiff - count.c
Make sure that datafiles with unknown activities can be read by sar
[sysstat] / count.c
diff --git a/count.c b/count.c
index a7799da23d7647dac4be0d15c9dd066fd3eaedf1..c1a976476fa91c72f7dfa7751cb64bdb649f1f14 100644 (file)
--- a/count.c
+++ b/count.c
@@ -1,6 +1,6 @@
 /*
  * count.c: Count items for which statistics will be collected.
- * (C) 1999-2015 by Sebastien GODARD (sysstat <at> orange.fr)
+ * (C) 1999-2021 by Sebastien GODARD (sysstat <at> orange.fr)
  *
  ***************************************************************************
  * This program is free software; you can redistribute it and/or modify it *
@@ -15,7 +15,7 @@
  *                                                                         *
  * You should have received a copy of the GNU General Public License along *
  * with this program; if not, write to the Free Software Foundation, Inc., *
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                   *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA              *
  ***************************************************************************
  */
 
@@ -73,8 +73,8 @@ int get_sys_cpu_nr(int highest)
        while ((drd = readdir(dir)) != NULL) {
 
                if (!strncmp(drd->d_name, "cpu", 3) && isdigit(drd->d_name[3])) {
-                       snprintf(line, MAX_PF_NAME, "%s/%s", SYSFS_DEVCPU, drd->d_name);
-                       line[MAX_PF_NAME - 1] = '\0';
+                       snprintf(line, sizeof(line), "%s/%s", SYSFS_DEVCPU, drd->d_name);
+                       line[sizeof(line) - 1] = '\0';
                        if (stat(line, &buf) < 0)
                                continue;
                        if (S_ISDIR(buf.st_mode)) {
@@ -156,9 +156,9 @@ int get_proc_cpu_nr(void)
  * 2: two proc...
  ***************************************************************************
  */
-int get_cpu_nr(unsigned int max_nr_cpus, int highest)
+__nr_t get_cpu_nr(unsigned int max_nr_cpus, int highest)
 {
-       int cpu_nr;
+       __nr_t cpu_nr;
 
        if ((cpu_nr = get_sys_cpu_nr(highest)) == 0) {
                /* /sys may be not mounted. Use /proc/stat instead */
@@ -188,11 +188,11 @@ int get_cpu_nr(unsigned int max_nr_cpus, int highest)
  * Number of interrupts per processor.
  ***************************************************************************
  */
-int get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
+__nr_t get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
 {
        FILE *fp;
        char *line = NULL;
-       unsigned int irq = 0;
+       __nr_t irq = 0;
        int p;
 
        if ((fp = fopen(file, "r")) == NULL)
@@ -229,12 +229,13 @@ int get_irqcpu_nr(char *file, int max_nr_irqcpu, int cpu_nr)
  * Number of devices (and partitions).
  ***************************************************************************
  */
-int get_diskstats_dev_nr(int count_part, int only_used_dev)
+__nr_t get_diskstats_dev_nr(int count_part, int only_used_dev)
 {
        FILE *fp;
        char line[256];
        char dev_name[MAX_NAME_LEN];
-       int dev = 0, i;
+       __nr_t dev = 0;
+       int i;
        unsigned long rd_ios, wr_ios;
 
        if ((fp = fopen(DISKSTATS, "r")) == NULL)
@@ -249,7 +250,7 @@ int get_diskstats_dev_nr(int count_part, int only_used_dev)
                if (!count_part) {
                        i = sscanf(line, "%*d %*d %s %lu %*u %*u %*u %lu",
                                   dev_name, &rd_ios, &wr_ios);
-                       if ((i == 2) || !is_device(dev_name, ACCEPT_VIRTUAL_DEVICES))
+                       if ((i == 2) || !is_device(SLASH_SYS, dev_name, ACCEPT_VIRTUAL_DEVICES))
                                /* It was a partition and not a device */
                                continue;
                        if (only_used_dev && !rd_ios && !wr_ios)
@@ -275,11 +276,11 @@ int get_diskstats_dev_nr(int count_part, int only_used_dev)
  * Number of interrupts, including total number of interrupts.
  ***************************************************************************
  */
-int get_irq_nr(void)
+__nr_t get_irq_nr(void)
 {
        FILE *fp;
        char line[8192];
-       int in = 0;
+       __nr_t in = 0;
        int pos = 4;
 
        if ((fp = fopen(STAT, "r")) == NULL)
@@ -310,11 +311,11 @@ int get_irq_nr(void)
  * Number of serial lines supporting tx/rx accouting.
  ***************************************************************************
  */
-int get_serial_nr(void)
+__nr_t get_serial_nr(void)
 {
        FILE *fp;
        char line[256];
-       int sl = 0;
+       __nr_t sl = 0;
 
        if ((fp = fopen(SERIAL, "r")) == NULL)
                return 0;       /* No SERIAL file */
@@ -343,11 +344,11 @@ int get_serial_nr(void)
  * Number of network interfaces.
  ***************************************************************************
  */
-int get_iface_nr(void)
+__nr_t get_iface_nr(void)
 {
        FILE *fp;
        char line[128];
-       int iface = 0;
+       __nr_t iface = 0;
 
        if ((fp = fopen(NET_DEV, "r")) == NULL)
                return 0;       /* No network device file */
@@ -375,9 +376,9 @@ int get_iface_nr(void)
  * Number of devices.
  ***************************************************************************
  */
-int get_disk_nr(unsigned int f)
+__nr_t get_disk_nr(unsigned int f)
 {
-       int disk_nr;
+       __nr_t disk_nr;
 
        /*
         * Partitions are taken into account by sar -d only with
@@ -396,12 +397,12 @@ int get_disk_nr(unsigned int f)
  * Number of frequencies.
  ***************************************************************************
  */
-int get_freq_nr(void)
+__nr_t get_freq_nr(void)
 {
        FILE *fp;
        char filename[MAX_PF_NAME];
        char line[128];
-       int freq = 0;
+       __nr_t freq = 0;
 
        snprintf(filename, MAX_PF_NAME, "%s/cpu0/%s",
                 SYSFS_DEVCPU, SYSFS_TIME_IN_STATE);
@@ -427,11 +428,11 @@ int get_freq_nr(void)
  * Return -1 if directory doesn't exist in sysfs.
  ***************************************************************************
  */
-int get_usb_nr(void)
+__nr_t get_usb_nr(void)
 {
        DIR *dir;
        struct dirent *drd;
-       int usb = 0;
+       __nr_t usb = 0;
 
        /* Open relevant /sys directory */
        if ((dir = opendir(SYSFS_USBDEV)) == NULL)
@@ -459,11 +460,13 @@ int get_usb_nr(void)
  * Number of filesystems.
  ***************************************************************************
  */
-int get_filesystem_nr(void)
+__nr_t get_filesystem_nr(void)
 {
        FILE *fp;
-       char line[512], fs_name[MAX_FS_LEN], mountp[256];
-       int fs = 0;
+       char line[512], fs_name[MAX_FS_LEN], mountp[256], type[128];
+       char *pos = 0, *pos2 = 0;
+       __nr_t fs = 0;
+       int skip = 0, skip_next = 0;
        struct statvfs buf;
 
        if ((fp = fopen(MTAB, "r")) == NULL)
@@ -472,17 +475,44 @@ int get_filesystem_nr(void)
 
        /* Get current filesystem */
        while (fgets(line, sizeof(line), fp) != NULL) {
+               /*
+                * Ignore line if the preceding line did not contain '\n'.
+                * (Some very long lines may be found for instance when
+                * overlay2 filesystem with docker is used).
+                */
+               skip = skip_next;
+               skip_next = (strchr(line, '\n') == NULL);
+               if (skip)
+                       continue;
+
                if (line[0] == '/') {
+                       /* Find field separator position */
+                       pos = strchr(line, ' ');
+                       if (pos == NULL)
+                               continue;
+
+                       /*
+                        * Find second field separator position,
+                        * read filesystem type,
+                        * if filesystem type is autofs, skip it
+                       */
+                       pos2 = strchr(pos + 1, ' ');
+                       if (pos2 == NULL)
+                               continue;
+
+                       sscanf(pos2 + 1, "%127s", type);
+                       if(strcmp(type, "autofs") == 0)
+                               continue;
 
                        /* Read filesystem name and mount point */
                        sscanf(line, "%127s", fs_name);
-                       sscanf(strchr(line, ' ') + 1, "%255s", mountp);
+                       sscanf(pos + 1, "%255s", mountp);
 
                        /* Replace octal codes */
                        oct2chr(mountp);
 
-                       /* Check that total size is not null */
-                       if (statvfs(mountp, &buf) < 0)
+                       /* Check that total size is not zero */
+                       if (__statvfs(mountp, &buf) < 0)
                                continue;
 
                        if (buf.f_blocks) {
@@ -505,11 +535,11 @@ int get_filesystem_nr(void)
  * Return -1 if directory doesn't exist in sysfs.
  ***************************************************************************
  */
-int get_fchost_nr(void)
+__nr_t get_fchost_nr(void)
 {
        DIR *dir;
        struct dirent *drd;
-       int fc = 0;
+       __nr_t fc = 0;
 
        if ((dir = opendir(SYSFS_FCHOST)) == NULL) {
                /* Directory non-existent */