]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_cgroup.c
ovl_rsync: make sure to umount
[mirror_lxc.git] / src / lxc / lxc_cgroup.c
CommitLineData
0ad19a3f 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
0ad19a3f 8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0ad19a3f 22 */
0ad19a3f 23
576f946d 24#include <stdio.h>
2ab8e9ba 25#include <unistd.h>
26#include <libgen.h>
27#include <sys/types.h>
28
9069513c 29#include <lxc/lxccontainer.h>
f2363e38
ÇO
30
31#include "lxc.h"
32#include "log.h"
4237c6ca 33#include "arguments.h"
0ad19a3f 34
6ea518f6 35lxc_log_define(lxc_cgroup_ui, lxc);
3ab87b66 36
4237c6ca 37static int my_checker(const struct lxc_arguments* args)
2ab8e9ba 38{
4237c6ca 39 if (!args->argc) {
f10e7166 40 lxc_error(args, "missing state object");
4237c6ca
MN
41 return -1;
42 }
43 return 0;
2ab8e9ba 44}
45
4237c6ca
MN
46static const struct option my_longopts[] = {
47 LXC_COMMON_OPTIONS
48};
2ab8e9ba 49
4237c6ca
MN
50static struct lxc_arguments my_args = {
51 .progname = "lxc-cgroup",
52 .help = "\
f10e7166 53--name=NAME state-object [value]\n\
4237c6ca 54\n\
f10e7166
DW
55Get or set the value of a state object (for example, 'cpuset.cpus')\n\
56in the container's cgroup for the corresponding subsystem.\n\
4237c6ca
MN
57\n\
58Options :\n\
5e8757ed 59 -n, --name=NAME NAME of the container",
4237c6ca
MN
60 .options = my_longopts,
61 .parser = NULL,
62 .checker = my_checker,
63};
2ab8e9ba 64
4237c6ca
MN
65int main(int argc, char *argv[])
66{
f10e7166 67 char *state_object = NULL, *value = NULL;
9069513c 68 struct lxc_container *c;
2ab8e9ba 69
272bc5af 70 if (lxc_arguments_parse(&my_args, argc, argv))
13bc2fd2 71 return 1;
2ab8e9ba 72
f5abd74d
SG
73 if (!my_args.log_file)
74 my_args.log_file = "none";
75
5e1e7aaf 76 if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
8d06bd13 77 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
13bc2fd2 78 return 1;
6edbfc86 79 lxc_log_options_no_override();
51cab631 80
f10e7166 81 state_object = my_args.argv[0];
2ab8e9ba 82
9069513c
SH
83 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
84 if (!c)
13bc2fd2 85 return 1;
f5abd74d
SG
86
87 if (!c->may_control(c)) {
88 ERROR("Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
f3e52710 89 lxc_container_put(c);
13bc2fd2 90 return 1;
f5abd74d
SG
91 }
92
9069513c
SH
93 if (!c->is_running(c)) {
94 ERROR("'%s:%s' is not running", my_args.lxcpath[0], my_args.name);
95 lxc_container_put(c);
13bc2fd2 96 return 1;
9069513c 97 }
2ab8e9ba 98
9069513c
SH
99 if ((my_args.argc) > 1) {
100 value = my_args.argv[1];
101 if (!c->set_cgroup_item(c, state_object, value)) {
4237c6ca 102 ERROR("failed to assign '%s' value to '%s' for '%s'",
f10e7166 103 value, state_object, my_args.name);
9069513c 104 lxc_container_put(c);
13bc2fd2 105 return 1;
2ab8e9ba 106 }
107 } else {
9069513c 108 int len = 4096;
2ab8e9ba 109 char buffer[len];
9069513c 110 int ret = c->get_cgroup_item(c, state_object, buffer, len);
70f7755e 111 if (ret < 0) {
9069513c
SH
112 ERROR("failed to retrieve value of '%s' for '%s:%s'",
113 state_object, my_args.lxcpath[0], my_args.name);
114 lxc_container_put(c);
13bc2fd2 115 return 1;
2ab8e9ba 116 }
70f7755e 117 printf("%*s", ret, buffer);
2ab8e9ba 118 }
119
9069513c 120 lxc_container_put(c);
0ad19a3f 121 return 0;
122}