Mercurial > archived > louis > perso > liblxcstats
comparison container.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 | 2cb8a6cbe468 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6ce4443e7545 |
---|---|
1 #include <sys/types.h> | |
2 #include <sys/stat.h> | |
3 | |
4 #include <assert.h> | |
5 #include <dirent.h> | |
6 #include <err.h> | |
7 #include <errno.h> | |
8 #include <stdio.h> | |
9 #include <stdlib.h> | |
10 #include <string.h> | |
11 | |
12 #include "lxcstats.h" | |
13 #include "_lxcstats.h" | |
14 | |
15 static int | |
16 _lxcst_unselect_current_and_parent(const struct dirent* d) | |
17 { | |
18 return (strcmp(d->d_name, ".") && strcmp(d->d_name, "..")); | |
19 } | |
20 | |
21 struct lxcst * | |
22 _lxcst_container_new(struct _lxcst_controller *ct, const char *name) | |
23 { | |
24 char buf[256]; | |
25 struct lxcst *c; | |
26 | |
27 assert(ct); | |
28 assert(name); | |
29 | |
30 printf("new container %s\n", name); | |
31 | |
32 c = calloc(1, sizeof(*c)); | |
33 if (!c) | |
34 return (NULL); | |
35 | |
36 _lxcst_join_path(buf, sizeof(buf), ct->cgroup_dir, name); | |
37 | |
38 c->name = strdup(name); | |
39 c->cgroup_dir = strdup(buf); | |
40 if (!c->name || !c->cgroup_dir) | |
41 goto free_container; | |
42 | |
43 return (c); | |
44 | |
45 free_container: | |
46 free(c->name); | |
47 free(c); | |
48 return (NULL); | |
49 } | |
50 | |
51 void | |
52 _lxcst_container_delete(struct lxcst *c) | |
53 { | |
54 if (c) { | |
55 free(c->name); | |
56 free(c->cgroup_dir); | |
57 free(c->cpuacct.percpu); | |
58 free(c); | |
59 } | |
60 } | |
61 | |
62 int | |
63 _lxcst_container_read_infos(struct lxcst *c) | |
64 { | |
65 int i; | |
66 | |
67 assert(c); | |
68 | |
69 for (i = 0; _lxcst_probes[i].fn; ++i) | |
70 if (_lxcst_probes[i].fn((c))) | |
71 warn("%s probe failed for container %s", _lxcst_probes[i].name, c->name); | |
72 | |
73 return (0); | |
74 } | |
75 | |
76 int | |
77 lxcst_span_containers(lxcst_handle *hdl, int (*cb)(void *, const struct lxcst *), void *ctx) | |
78 { | |
79 int n; | |
80 struct dirent **c_vec; | |
81 struct lxcst *c; | |
82 | |
83 assert(hdl); | |
84 assert(hdl->cgroup_dir); | |
85 assert(cb); | |
86 | |
87 n = scandir(hdl->cgroup_dir, &c_vec, &_lxcst_unselect_current_and_parent, NULL); | |
88 if (n > 0) { | |
89 while (n--) { | |
90 if (_lxcst_isdir(hdl, c_vec[n])) { | |
91 c = _lxcst_container_new(hdl, c_vec[n]->d_name); | |
92 if (c && _lxcst_container_read_infos(c) == 0 | |
93 && cb(ctx, c)) { | |
94 errno = EINTR; | |
95 goto abort_by_cb; | |
96 } | |
97 } | |
98 free(c_vec[n]); | |
99 } | |
100 free(c_vec); | |
101 } else if (n < 0) { | |
102 return (-1); | |
103 } | |
104 | |
105 return (0); | |
106 | |
107 abort_by_cb: | |
108 free(c_vec[n + 1]); | |
109 while (n--) | |
110 free(c_vec[n]); | |
111 free(c_vec); | |
112 return (-1); | |
113 } |