view open.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 6ce4443e7545
children
line wrap: on
line source

#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;

    c->lxc_dir = LXC_DIR;

    return (c);

free_controller:
    free(c);
    return (NULL);
}