Merge lp:~serge-hallyn/upstart/cgroup-test-base1 into lp:~jamesodhunt/upstart/cgroups

Proposed by Dimitri John Ledkov
Status: Merged
Merged at revision: 1611
Proposed branch: lp:~serge-hallyn/upstart/cgroup-test-base1
Merge into: lp:~jamesodhunt/upstart/cgroups
Diff against target: 362 lines (+257/-5)
6 files modified
init/Makefile.am (+2/-0)
init/event.c (+0/-3)
test/Makefile.am (+3/-1)
test/test_util_common.c (+171/-1)
test/test_util_common.h (+4/-0)
test/tests/test_util_check_env.c (+77/-0)
To merge this branch: bzr merge lp:~serge-hallyn/upstart/cgroup-test-base1
Reviewer Review Type Date Requested Status
James Hunt Approve
Review via email: mp+214782@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

i proposed this to see the diff generated for me =)

Revision history for this message
James Hunt (jamesodhunt) wrote :

Thanks Serge - pulled into lp:~jamesodhunt/upstart/cgroups.

review: Approve
Revision history for this message
Serge Hallyn (serge-hallyn) wrote :

Quoting James Hunt (<email address hidden>):
> Review: Approve
>
> Thanks Serge - pulled into lp:~jamesodhunt/upstart/cgroups.

Oops. Hi James, thanks, however the main reason for this patch was
in support of upcoming native upstart-cgroup support which now is not
going to happen.

So I'm not saying it necessarily should be reverted, as it still
may be useful, but I'm not sure what should be done.

Revision history for this message
James Hunt (jamesodhunt) wrote :

... actually, we *may* be adding native cgroup support after all :-)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'init/Makefile.am'
2--- init/Makefile.am 2014-03-26 14:38:30 +0000
3+++ init/Makefile.am 2014-04-08 14:58:40 +0000
4@@ -10,6 +10,7 @@
5 $(DBUS_CFLAGS) \
6 $(JSON_CFLAGS) \
7 $(CGMANAGER_CFLAGS)
8+AM_LIBADD = -lcgmanager $(NIH_LIBS) $(NIH_DBUS_LIBS)
9
10 AM_CPPFLAGS = \
11 -DLOCALEDIR="\"$(localedir)\"" \
12@@ -465,6 +466,7 @@
13 $(NIH_LIBS) \
14 $(NIH_DBUS_LIBS) \
15 $(DBUS_LIBS) \
16+ -lcgmanager \
17 -lrt
18
19 test_cgroup_SOURCES = tests/test_cgroup.c
20
21=== modified file 'init/event.c'
22--- init/event.c 2014-04-04 16:21:38 +0000
23+++ init/event.c 2014-04-08 14:58:40 +0000
24@@ -304,10 +304,7 @@
25 event_pending_handle_jobs (Event *event)
26 {
27 int empty = TRUE;
28-
29-#ifdef ENABLE_CGROUPS
30 int warn = FALSE;
31-#endif /* ENABLE_CGROUPS */
32
33 nih_assert (event != NULL);
34
35
36=== modified file 'test/Makefile.am'
37--- test/Makefile.am 2013-11-16 12:42:57 +0000
38+++ test/Makefile.am 2014-04-08 14:58:40 +0000
39@@ -8,6 +8,7 @@
40 $(NIH_DBUS_CFLAGS) \
41 $(DBUS_CFLAGS)
42
43+AM_LIBADD = -lcgmanager $(NIH_LIBS) $(NIH_DBUS_LIBS)
44 AM_CPPFLAGS = \
45 -DLOCALEDIR="\"$(localedir)\"" \
46 -DSBINDIR="\"$(sbindir)\"" \
47@@ -36,4 +37,5 @@
48 test_util_check_env_SOURCES = tests/test_util_check_env.c
49 test_util_check_env_LDADD = \
50 libtest_util_common.a \
51- $(NIH_LIBS)
52+ -lcgmanager \
53+ $(NIH_LIBS) $(NIH_DBUS_LIBS)
54
55=== modified file 'test/test_util_common.c'
56--- test/test_util_common.c 2014-04-03 14:58:47 +0000
57+++ test/test_util_common.c 2014-04-08 14:58:40 +0000
58@@ -48,6 +48,9 @@
59
60 #include "dbus/upstart.h"
61
62+#include <nih-dbus/dbus_connection.h>
63+#include <cgmanager/cgmanager-client.h>
64+
65 #include "test_util_common.h"
66
67 #ifndef UPSTART_BINARY
68@@ -1087,7 +1090,7 @@
69 }
70
71 NihList *
72-test_list_get_index (NihList *list, size_t count)
73+test_list_get_strchr (NihList *list, size_t count)
74 {
75 size_t i = 0;
76
77@@ -1225,3 +1228,170 @@
78
79 return (ret == -1 ? 0 : len);
80 }
81+
82+static NihDBusProxy *cgroup_manager;
83+#define CGMANAGER_DBUS_SOCK "unix:path=/sys/fs/cgroup/cgmanager/sock"
84+/*
85+ * connect_to_cgmanager:
86+ *
87+ * Returns -2 if cgmanager is not running, -1 on other error,
88+ * and 0 on success
89+ */
90+int
91+connect_to_cgmanager(void)
92+{
93+ DBusError dbus_error;
94+ static DBusConnection *connection;
95+
96+ dbus_error_init(&dbus_error);
97+
98+ connection = dbus_connection_open_private(CGMANAGER_DBUS_SOCK, &dbus_error);
99+ if (!connection) {
100+ nih_error("Failed opening dbus connection: %s: %s",
101+ dbus_error.name, dbus_error.message);
102+ dbus_error_free(&dbus_error);
103+ return -2;
104+ }
105+ if (nih_dbus_setup(connection, NULL) < 0) {
106+ NihError *nerr;
107+ nerr = nih_error_get();
108+ nih_error("Unable to open cgmanager connection at %s: %s", CGMANAGER_DBUS_SOCK,
109+ nerr->message);
110+ nih_free(nerr);
111+ dbus_error_free(&dbus_error);
112+ dbus_connection_unref(connection);
113+ return -1;
114+ }
115+ dbus_connection_set_exit_on_disconnect(connection, FALSE);
116+ dbus_error_free(&dbus_error);
117+ cgroup_manager = nih_dbus_proxy_new(NULL, connection,
118+ NULL /* p2p */,
119+ "/org/linuxcontainers/cgmanager", NULL, NULL);
120+ dbus_connection_unref(connection);
121+ if (!cgroup_manager) {
122+ NihError *nerr;
123+ nerr = nih_error_get();
124+ nih_error("Error opening cgmanager proxy: %s", nerr->message);
125+ nih_free(nerr);
126+ return -1;
127+ }
128+
129+ if (cgmanager_ping_sync(NULL, cgroup_manager, 0) != 0) {
130+ NihError *nerr;
131+ nerr = nih_error_get();
132+ nih_error("Error pinging cgroup manager: %s", nerr->message);
133+ nih_free(nerr);
134+ disconnect_cgmanager();
135+ return -1;
136+ }
137+ return 0;
138+}
139+
140+void
141+disconnect_cgmanager(void)
142+{
143+ if (cgroup_manager) {
144+ dbus_connection_flush(cgroup_manager->connection);
145+ dbus_connection_close(cgroup_manager->connection);
146+ nih_free(cgroup_manager);
147+ }
148+ cgroup_manager = NULL;
149+}
150+
151+/*
152+ * get_pid_cgroup:
153+ *
154+ * @controller: cgroup controller to query
155+ * @pid: pid whose cgroup to return
156+ *
157+ * The result must be nih_freed
158+ */
159+char *
160+get_pid_cgroup(const char *controller, pid_t pid)
161+{
162+ char *str;
163+ if (cgmanager_get_pid_cgroup_sync(NULL, cgroup_manager,
164+ controller, pid, &str) != 0) {
165+
166+ NihError *nerr;
167+ nerr = nih_error_get();
168+ nih_error("call to cgmanager_set_value_sync failed: %s", nerr->message);
169+ nih_free(nerr);
170+ return NULL;
171+ }
172+ return str;
173+}
174+
175+/*
176+ * setup_cgroup_sandbox:
177+ *
178+ * Creates a unique cgroup, sets it to remove on empty, and enters the
179+ * current task into the new cgroup.
180+ */
181+int
182+setup_cgroup_sandbox(void)
183+{
184+ char tmpnam[32], line[1024], *cg;
185+ int32_t e;
186+ int ret = -1, fd = -1;
187+ FILE *cgf = NULL;
188+ pid_t mypid = getpid();
189+
190+ if (!cgroup_manager) {
191+ nih_error("%s: connect_to_cgmanager must be called first",
192+ __func__);
193+ goto out;
194+ }
195+ memset(tmpnam, 0, 32);
196+ strcpy(tmpnam, "/tmp/upstart-test-XXXXXX");
197+ if ((fd = mkstemp(tmpnam)) < 0) {
198+ nih_error("%s: failed to create a tempfile", __func__);
199+ goto out;
200+ }
201+ unlink(tmpnam);
202+ cg = tmpnam+5;
203+
204+ if ((cgf = fopen("/proc/cgroups", "r")) == NULL) {
205+ nih_error("%s: failed to read my cgroups", __func__);
206+ goto out;
207+ }
208+ while (fgets(line, 1024, cgf)) {
209+ char *p;
210+ if (line[0] == '#')
211+ continue;
212+
213+ p = strchr(line, '\t');
214+ if (!p) {
215+ nih_error("failed to find a \t");
216+ continue;
217+ }
218+ *p = '\0';
219+ if (cgmanager_create_sync(NULL, cgroup_manager, line, cg,
220+ &e) != 0) {
221+ nih_error("%s: failed to create cgroup %s:%s",
222+ __func__, line, cg);
223+ goto out;
224+ }
225+ if (e == 1)
226+ nih_warn("%s: boggle: cgroup %s:%s already existed",
227+ __func__, line, cg);
228+ if (cgmanager_remove_on_empty_sync(NULL, cgroup_manager, line,
229+ cg) != 0)
230+ nih_warn("%s: failed to mark %s:%s remove-on-empty",
231+ __func__, line, cg);
232+ if (cgmanager_move_pid_sync(NULL, cgroup_manager, line, cg,
233+ mypid) != 0) {
234+ nih_error("%s: failed to move myself to cgroup %s:%s",
235+ __func__, line, cg);
236+ goto out;
237+ }
238+ }
239+ ret = 0;
240+
241+out:
242+ if (fd != -1)
243+ close(fd);
244+ if (cgf)
245+ fclose(cgf);
246+ return ret;
247+}
248
249=== modified file 'test/test_util_common.h'
250--- test/test_util_common.h 2014-03-26 14:38:30 +0000
251+++ test/test_util_common.h 2014-04-08 14:58:40 +0000
252@@ -775,4 +775,8 @@
253 size_t test_tree_count (NihTree *tree)
254 __attribute__((warn_unused_result, unused));
255
256+int connect_to_cgmanager(void);
257+void disconnect_cgmanager(void);
258+char *get_pid_cgroup(const char *controller, pid_t pid);
259+int setup_cgroup_sandbox(void);
260 #endif /* TEST_UTIL_COMMON_H */
261
262=== modified file 'test/tests/test_util_check_env.c'
263--- test/tests/test_util_check_env.c 2013-11-06 17:59:04 +0000
264+++ test/tests/test_util_check_env.c 2014-04-08 14:58:40 +0000
265@@ -72,6 +72,68 @@
266 return found;
267 }
268
269+void print_my_cgroup(void)
270+{
271+ char *str;
272+ str = get_pid_cgroup("freezer", getpid());
273+ if (str) {
274+ nih_warn("I am in freezer cgroup: %s", str);
275+ TEST_EQ_STR(str, "/");
276+ nih_free(str);
277+ } else {
278+ TEST_FAILED("Failed to get my freezer cgroup");
279+ }
280+}
281+
282+char *get_my_cgroup()
283+{
284+ char line[1024], *ret = NULL;
285+ FILE *f = fopen("/proc/self/cgroup", "r");
286+
287+ while (fgets(line, 1024, f)) {
288+ char *p, *p2;
289+ if ((p = strchr(line, ':')) == NULL)
290+ continue;
291+ p++;
292+ if ((p2 = strchr(p, ':')) == NULL)
293+ continue;
294+ if (strncmp(p, "name=", 5) == 0)
295+ continue;
296+ ret = NIH_MUST( nih_strdup(NULL, p2+1) );
297+ break;
298+ }
299+ fclose(f);
300+ return ret;
301+}
302+
303+int check_cgroup_sandbox(void)
304+{
305+ char *cg_prev = NULL, *cg_post = NULL;
306+ int ret = -1;
307+
308+ cg_prev = get_my_cgroup();
309+ if (!cg_prev)
310+ return -1;
311+ if (setup_cgroup_sandbox() < 0) {
312+ nih_free(cg_prev);
313+ return -1;
314+ }
315+ cg_post = get_my_cgroup();
316+ if (!cg_post) {
317+ nih_free(cg_prev);
318+ return -1;
319+ }
320+ /* we should have moved cgroups, so the two should be different */
321+ if (strcmp(cg_prev, cg_post) != 0) {
322+ nih_warn("setup_cgroup_sandbox moved me from %s to %s",
323+ cg_prev, cg_post);
324+ ret = 0;
325+ }
326+ nih_free(cg_prev);
327+ nih_free(cg_post);
328+ return ret;
329+}
330+
331 /**
332 * test_checks:
333 *
334@@ -80,6 +142,8 @@
335 void
336 test_checks (void)
337 {
338+ int ret;
339+
340 TEST_GROUP ("test environment");
341
342 /*
343@@ -94,6 +158,19 @@
344 nih_warn ("This environment will probably cause tests to fail mysteriously!!");
345 nih_warn ("See bug LP:#882147 for further details.");
346 }
347+
348+ TEST_FEATURE ("checking for cgmanager");
349+ ret = connect_to_cgmanager();
350+ switch(ret) {
351+ case -2: TEST_FAILED("Found no cgroup manager"); break;
352+ case -1: TEST_FAILED("Error connecting to cgmanager"); break;
353+ case 0: print_my_cgroup(); break;
354+ default: TEST_FAILED("Unknown error from connect_to_cgmanager: %d", ret);
355+ }
356+
357+ TEST_FEATURE("cgroup sandbox");
358+ TEST_EQ(check_cgroup_sandbox(), 0);
359+ disconnect_cgmanager();
360 }
361
362 int

Subscribers

People subscribed via source and target branches

to all changes: