diff container.c @ 7:8339ab15527d

Add running/stopped containers global statistics and refactor some things
author Louis Opter <louis@dotcloud.com>
date Sat, 01 Jan 2011 17:46:40 +0100
parents 2cb8a6cbe468
children fe254ba0818d
line wrap: on
line diff
--- a/container.c	Sat Jan 01 16:18:17 2011 +0100
+++ b/container.c	Sat Jan 01 17:46:40 2011 +0100
@@ -18,7 +18,7 @@
 }
 
 struct lxcst *
-_lxcst_container_new(struct _lxcst_controller *ct, const char *name)
+_lxcst_container_new(const struct _lxcst_controller *ct, const char *name)
 {
     char            buf[256];
     struct lxcst    *c;
@@ -56,6 +56,32 @@
     }
 }
 
+/*
+ * Should be equivalent to the number of directories found under /var/lib/lxc/
+ */
+int
+_lxcst_container_count(const struct _lxcst_controller *ct)
+{
+    int             n;
+    struct dirent   **c_vec;
+    int             count;
+    char            buf[PATH_MAX];
+
+    n = scandir(ct->lxc_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL);
+    if (n != -1) {
+        count = n;
+        while (n--) {
+            if (!_lxcst_isdir(_lxcst_join_path(buf, sizeof(buf), ct->lxc_dir, c_vec[n]->d_name)))
+                --count;
+            free(c_vec[n]);
+        }
+        free(c_vec);
+        return (count);
+    }
+
+    return (-1);
+}
+
 int
 _lxcst_container_read_infos(struct lxcst *c)
 {
@@ -76,16 +102,20 @@
     int             n;
     struct dirent   **c_vec;
     struct lxcst    *c;
+    char            buf[PATH_MAX];
 
     assert(hdl);
     assert(hdl->cgroup_dir);
     assert(cb);
 
+    _lxcst_globals_reset();
+
     n = scandir(hdl->cgroup_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL);
     if (n > 0) {
         while (n--) {
-            if (_lxcst_isdir(hdl, c_vec[n])) {
+            if (_lxcst_isdir(_lxcst_join_path(buf, sizeof(buf), hdl->cgroup_dir, c_vec[n]->d_name))) {
                 c = _lxcst_container_new(hdl, c_vec[n]->d_name);
+                _lxcst_globals.running_containers++;
                 if (c && _lxcst_container_read_infos(c) == 0
                     && cb(ctx, c)) {
                     errno = EINTR;
@@ -99,6 +129,14 @@
         return (-1);
     }
 
+    n = _lxcst_container_count(hdl);
+    if (n == -1) {
+        warn("can't get the total number of containers");
+        return (-1);
+    }
+
+    _lxcst_globals.stopped_containers = n - _lxcst_globals.running_containers;
+
     return (0);
 
 abort_by_cb: