cgpath.c revision 4fb3cba5bc6b256b774e780f2bbf47b1dbcc0ce9
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen * Copyright © 2012 Canonical Ltd.
2e705e574eb916181ce91f920d4d6f66bebce5baTimo Sirainen * This program is free software; you can redistribute it and/or modify
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen * it under the terms of the GNU General Public License version 2, as
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger * published by the Free Software Foundation.
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen * This program is distributed in the hope that it will be useful,
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen * but WITHOUT ANY WARRANTY; without even the implied warranty of
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen * GNU General Public License for more details.
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen * You should have received a copy of the GNU General Public License along
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen * with this program; if not, write to the Free Software Foundation, Inc.,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5e4d905f3b8e58839fe8aa44fc5ae1280031a114Timo Sirainen fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__); \
d756ebcfa96bd7cff02097c8f26df9df368b81b1Timo Sirainen * test_running_container: test cgroup functions against a running container
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen * @group : name of the container group or NULL for default "lxc"
6b96cd73c9b106f3fc78df263c0ec3e1ab6488b8Timo Sirainen * @name : name of the container
6b96cd73c9b106f3fc78df263c0ec3e1ab6488b8Timo Sirainenstatic int test_running_container(const char *lxcpath,
6d3ebefb87dfddc6a9edabcec841b72c1cd7ae64Timo Sirainen sprintf(relpath, "%s/%s", group ? group : "lxc", name);
6d3ebefb87dfddc6a9edabcec841b72c1cd7ae64Timo Sirainen if ((c = lxc_container_new(name, lxcpath)) == NULL) {
6d3ebefb87dfddc6a9edabcec841b72c1cd7ae64Timo Sirainen TSTERR("container %s couldn't instantiate", name);
6d3ebefb87dfddc6a9edabcec841b72c1cd7ae64Timo Sirainen cgrelpath = lxc_cmd_get_cgroup_path(c->name, c->config_path, "freezer");
6d3ebefb87dfddc6a9edabcec841b72c1cd7ae64Timo Sirainen TSTERR("lxc_cmd_get_cgroup_path returned NULL");
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen TSTERR("lxc_cmd_get_cgroup_path %s not in %s", relpath, cgrelpath);
9e7de2ced0b9ece9d7ae454d06e4f96cbe27668bTimo Sirainen /* test get/set value using memory.soft_limit_in_bytes file */
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
b26b9ec937b0d067bff9d408101798e4f93cb919Timo Sirainen ret = lxc_cgroup_set("memory.soft_limit_in_bytes", "512M", c->name, c->config_path);
d1bcee48225783610f0f6f639973677dd72b884aTimo Sirainen TSTERR("lxc_cgroup_set failed %d %d", ret, errno);
5e4d905f3b8e58839fe8aa44fc5ae1280031a114Timo Sirainen ret = lxc_cgroup_get("memory.soft_limit_in_bytes", value, sizeof(value),
9691078006cfe7af5847116b519a0201c197a947Timo Sirainen TSTERR("lxc_cgroup_set_bypath failed to set value >%s<", value);
9691078006cfe7af5847116b519a0201c197a947Timo Sirainen /* restore original value */
9691078006cfe7af5847116b519a0201c197a947Timo Sirainen ret = lxc_cgroup_set("memory.soft_limit_in_bytes", value_save,
43b4b4c1e9e468bfcb8b2434e26718279cbbc0d5Timo Sirainen TSTERR("failed to mkdir %s %s", lxcpath, strerror(errno));
c7801f830c7d2e7d340065cdd5a5c795b1726223Timo Sirainen if ((c = lxc_container_new(name, lxcpath)) == NULL) {
14ee885f15104cdc0937773ea7bfbf84a2326fdbTimo Sirainen c->set_config_item(c, "lxc.network.type", "empty");
c7801f830c7d2e7d340065cdd5a5c795b1726223Timo Sirainen if (!c->createl(c, template, NULL, NULL, 0, NULL)) {
c7801f830c7d2e7d340065cdd5a5c795b1726223Timo Sirainen ret = test_running_container(lxcpath, group, name);
2ded32b165ce2f510f063c7da04809891c0e8cc2Stephan Bosch /* won't require privilege necessarily once users are classified by
9805f5093a0e0845f028fb8fd52b0eec27d8f7abTimo Sirainen * pam_cgroup */
2e705e574eb916181ce91f920d4d6f66bebce5baTimo Sirainen if (geteuid() != 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen * This is useful for running with valgrind to test for memory
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen * leaks. The container should already be running, we can't start
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen * the container ourselves because valgrind gets confused by lxc's
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen * internal calls to clone.
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (test_running_container(NULL, NULL, "bb01") < 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen printf("Running container cgroup tests...Passed\n");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (test_container(NULL, NULL, MYNAME, "busybox") < 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen printf("Container creation tests...Passed\n");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (test_container("/var/lib/lxctest2", NULL, MYNAME, "busybox") < 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen printf("Container creation with LXCPATH tests...Passed\n");