diff open.c @ 0:6ce4443e7545

Add the draft of an API to collect statistics on LXC
author Louis Opter <kalessin@kalessin.fr>
date Wed, 29 Dec 2010 23:28:14 +0100
parents
children 8339ab15527d
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/open.c	Wed Dec 29 23:28:14 2010 +0100
@@ -0,0 +1,56 @@
+#include <errno.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <mntent.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "_lxcstats.h"
+#include "lxcstats.h"
+
+static char *
+lxcst_cgroup_mount_point(void)
+{
+    FILE            *mtab;
+    struct mntent   mntbuf;
+    char            strbuf[1024];
+    bool            found = false;
+
+    mtab = fopen("/etc/mtab", "r");
+    if (!mtab)
+        return (NULL);
+
+    while (getmntent_r(mtab, &mntbuf, strbuf, sizeof(strbuf))) {
+        if (!strcmp(mntbuf.mnt_type, "cgroup")) {
+            found = true;
+            break ;
+        }
+    }
+
+    fclose(mtab);
+
+    if (found)
+        return (strdup(mntbuf.mnt_dir));
+
+    errno = ENOENT;
+    return (NULL);
+}
+
+lxcst_handle    *lxcst_open(void)
+{
+    struct _lxcst_controller    *c;
+
+    c = calloc(1, sizeof(*c));
+    if (!c)
+        return (NULL);
+
+    c->cgroup_dir = lxcst_cgroup_mount_point();
+    if (!c->cgroup_dir)
+        goto free_controller;
+
+    return (c);
+
+free_controller:
+    free(c);
+    return (NULL);
+}