changeset 6:e2b88f50e136

cpuacct: replace strtoll by strtoul and replace sscanf by strsplit + strtoul
author Louis Opter <louis@dotcloud.com>
date Sat, 01 Jan 2011 16:18:17 +0100
parents 8d8c49b066d4
children 8339ab15527d
files _lxcstats.h probes/cpuacct.c
diffstat 2 files changed, 14 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/_lxcstats.h	Sat Jan 01 16:05:08 2011 +0100
+++ b/_lxcstats.h	Sat Jan 01 16:18:17 2011 +0100
@@ -4,7 +4,9 @@
 # include "compat/compat.h"
 # include "probes/probes.h"
 
-# define NSEC_PER_SEC 1000000000L
+# define NSEC_PER_SEC   1000000000L
+
+# define ARRAY_SIZE(a)  (sizeof((a)) / sizeof((a)[0]))
 
 struct      dirent;
 
--- a/probes/cpuacct.c	Sat Jan 01 16:05:08 2011 +0100
+++ b/probes/cpuacct.c	Sat Jan 01 16:18:17 2011 +0100
@@ -4,7 +4,6 @@
 #include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
-#include <stdio.h>
 #include <string.h>
 #include <unistd.h>
 
@@ -25,16 +24,23 @@
 {
     char    path[PATH_MAX];
     char    *values;
+    char    *fields[4];
     int     ret;
 
     _lxcst_join_path(path, sizeof(path), c->cgroup_dir, "cpuacct.stat");
     ret = _lxcst_read_file(path, &values);
     if (ret > 0) {
         values[ret - 1] = '\0'; /* Replace the last \n by a \0 */
-        ret = sscanf(values, "user %u\nsystem %u", &c->cpuacct.user, &c->cpuacct.system);
+        if (_lxcst_strsplit(values, fields, ARRAY_SIZE(fields)) == ARRAY_SIZE(fields)) {
+            errno = 0;
+            c->cpuacct.user = strtoul(fields[1], NULL, 10);
+            c->cpuacct.system = strtoul(fields[3], NULL, 10);
+            if (!errno) {
+                free(values);
+                return (0);
+            }
+        }
         free(values);
-        if (ret == 2)
-            return (0);
     }
 
     c->cpuacct.user = 0;
@@ -71,7 +77,7 @@
             errno = 0;
             p = values;
             for (i = 0; i != ncpus && *p; ++i) {
-                c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoll(p, &p, 10));
+                c->cpuacct.percpu[i] = nsecs_to_user_hz(strtoul(p, &p, 10));
                 if (errno)
                     goto err;
                 p += strcspn(p, "0123456789");