lxc_cgroup.c revision 6edbfc86508fb07d2b8377dce40f36f3032c32a5
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * lxc: linux Container library
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * (C) Copyright IBM Corp. 2007, 2008
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
2cb6f28b3a12bb714bf14494d31eb6b6fff64b8bJakub Hrozek * Authors:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Daniel Lezcano <daniel.lezcano at free.fr>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
531661c7bb54eb71853977a64cb30f80c20b963eJakub Hrozek * This library is free software; you can redistribute it and/or
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * modify it under the terms of the GNU Lesser General Public
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek * License as published by the Free Software Foundation; either
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek * version 2.1 of the License, or (at your option) any later version.
ad805face83ba7d67b1cf2067a1982c7e63d1060Jakub Hrozek *
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * This library is distributed in the hope that it will be useful,
a7797068c4deb6ce2bdbcda27c45ff1bbb4a8e78Jakub Hrozek * but WITHOUT ANY WARRANTY; without even the implied warranty of
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Lesser General Public License for more details.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * You should have received a copy of the GNU Lesser General Public
7a14e8f66c0e932fe2954d792614a3b61d444bd1Jakub Hrozek * License along with this library; if not, write to the Free Software
36b56482ca1e53d832accef0354124fd79711172Jakub Hrozek * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <stdio.h>
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek#include <unistd.h>
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek#include <libgen.h>
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek#include <sys/types.h>
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek#include <lxc/lxccontainer.h>
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek#include "lxc.h"
d25fa6f2608d5fe0617ada47f9d426f45deb96ffJakub Hrozek#include "log.h"
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek#include "arguments.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherlxc_log_define(lxc_cgroup_ui, lxc_cgroup);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherstatic int my_checker(const struct lxc_arguments* args)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!args->argc) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher lxc_error(args, "missing state object");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
f10ebaa51ecdcbbd10f171d19fe8e680e5bc74aaJakub Hrozek return 0;
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek}
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozekstatic const struct option my_longopts[] = {
261cdde02b40aa8dabb3d69e43586a5a220647e9Jakub Hrozek LXC_COMMON_OPTIONS
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek};
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherstatic struct lxc_arguments my_args = {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher .progname = "lxc-cgroup",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher .help = "\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher--name=NAME state-object [value]\n\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher\n\
b355dcb54194f498921743ca33304eac35d89718Stephen GallagherGet or set the value of a state object (for example, 'cpuset.cpus')\n\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherin the container's cgroup for the corresponding subsystem.\n\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher\n\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen GallagherOptions :\n\
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher -n, --name=NAME container name",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher .options = my_longopts,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher .parser = NULL,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher .checker = my_checker,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher};
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagherint main(int argc, char *argv[])
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *state_object = NULL, *value = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher struct lxc_container *c;
f10ebaa51ecdcbbd10f171d19fe8e680e5bc74aaJakub Hrozek
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek if (lxc_arguments_parse(&my_args, argc, argv))
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek return -1;
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek if (!my_args.log_file)
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek my_args.log_file = "none";
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek my_args.progname, my_args.quiet, my_args.lxcpath[0]))
b47fd11a259c50e63cd674c7cba0da3f2549cae0Jakub Hrozek return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_log_options_no_override();
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher state_object = my_args.argv[0];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!c)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!c->may_control(c)) {
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher ERROR("Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher lxc_container_put(c);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
f10ebaa51ecdcbbd10f171d19fe8e680e5bc74aaJakub Hrozek
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!c->is_running(c)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ERROR("'%s:%s' is not running", my_args.lxcpath[0], my_args.name);
0142e7e2558a887992b1c5d4dc3051178e377687Jakub Hrozek lxc_container_put(c);
f45a20d6ba9e8d695ec3ab707f0cc082999aa4a3Jakub Hrozek return -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if ((my_args.argc) > 1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher value = my_args.argv[1];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!c->set_cgroup_item(c, state_object, value)) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ERROR("failed to assign '%s' value to '%s' for '%s'",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher value, state_object, my_args.name);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_container_put(c);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher } else {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int len = 4096;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char buffer[len];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret = c->get_cgroup_item(c, state_object, buffer, len);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher ERROR("failed to retrieve value of '%s' for '%s:%s'",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher state_object, my_args.lxcpath[0], my_args.name);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_container_put(c);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return -1;
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher printf("%*s", ret, buffer);
b355dcb54194f498921743ca33304eac35d89718Stephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_container_put(c);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return 0;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher