]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_cgroup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
14 #include "arguments.h"
17 lxc_log_define(lxc_cgroup
, lxc
);
19 static int my_checker(const struct lxc_arguments
*args
);
21 static const struct option my_longopts
[] = {
25 static struct lxc_arguments my_args
= {
26 .progname
= "lxc-cgroup",
28 --name=NAME state-object [value]\n\
30 Get or set the value of a state object (for example, 'cpuset.cpus')\n\
31 in the container's cgroup for the corresponding subsystem.\n\
34 -n, --name=NAME NAME of the container\n\
35 --rcfile=FILE Load configuration file FILE\n",
36 .options
= my_longopts
,
38 .checker
= my_checker
,
39 .log_priority
= "ERROR",
43 static int my_checker(const struct lxc_arguments
*args
)
46 ERROR("Missing state object");
53 int __attribute__((weak
, alias("lxc_cgroup_main"))) main(int argc
, char *argv
[]);
54 int lxc_cgroup_main(int argc
, char *argv
[])
56 char *state_object
= NULL
, *value
= NULL
;
57 struct lxc_container
*c
;
60 if (lxc_arguments_parse(&my_args
, argc
, argv
))
63 log
.name
= my_args
.name
;
64 log
.file
= my_args
.log_file
;
65 log
.level
= my_args
.log_priority
;
66 log
.prefix
= my_args
.progname
;
67 log
.quiet
= my_args
.quiet
;
68 log
.lxcpath
= my_args
.lxcpath
[0];
70 if (lxc_log_init(&log
))
73 state_object
= my_args
.argv
[0];
75 c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
82 if (!c
->load_config(c
, my_args
.rcfile
)) {
83 ERROR("Failed to load rcfile");
88 c
->configfile
= strdup(my_args
.rcfile
);
90 ERROR("Out of memory setting new config filename");
96 if (!c
->may_control(c
)) {
97 ERROR("Insufficent privileges to control %s:%s", my_args
.lxcpath
[0], my_args
.name
);
102 if (!c
->is_running(c
)) {
103 ERROR("'%s:%s' is not running", my_args
.lxcpath
[0], my_args
.name
);
104 lxc_container_put(c
);
108 if ((my_args
.argc
) > 1) {
109 value
= my_args
.argv
[1];
111 if (!c
->set_cgroup_item(c
, state_object
, value
)) {
112 ERROR("Failed to assign '%s' value to '%s' for '%s'",
113 value
, state_object
, my_args
.name
);
114 lxc_container_put(c
);
118 char buffer
[PATH_MAX
];
121 ret
= c
->get_cgroup_item(c
, state_object
, buffer
, PATH_MAX
);
123 ERROR("Failed to retrieve value of '%s' for '%s:%s'",
124 state_object
, my_args
.lxcpath
[0], my_args
.name
);
125 lxc_container_put(c
);
129 printf("%*s\n", ret
, buffer
);
132 lxc_container_put(c
);