]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/tools/lxc_cgroup.c
tree-wide: fix lxc header inclusion
[mirror_lxc.git] / src / lxc / tools / lxc_cgroup.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
0ad19a3f 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
70952c01 6#include <libgen.h>
576f946d 7#include <stdio.h>
70952c01 8#include <stdlib.h>
744b1eec 9#include <string.h>
2ab8e9ba 10#include <sys/types.h>
d38dd64a 11#include <unistd.h>
2ab8e9ba 12
12ae2a33 13#include "lxc.h"
f2363e38 14
4237c6ca 15#include "arguments.h"
d38dd64a 16#include "config.h"
93f81bc7 17#include "log.h"
18
19lxc_log_define(lxc_cgroup, lxc);
0ad19a3f 20
62ebeb04 21static int my_checker(const struct lxc_arguments *args);
2ab8e9ba 22
4237c6ca
MN
23static const struct option my_longopts[] = {
24 LXC_COMMON_OPTIONS
25};
2ab8e9ba 26
4237c6ca 27static struct lxc_arguments my_args = {
62ebeb04 28 .progname = "lxc-cgroup",
29 .help = "\
f10e7166 30--name=NAME state-object [value]\n\
4237c6ca 31\n\
f10e7166
DW
32Get or set the value of a state object (for example, 'cpuset.cpus')\n\
33in the container's cgroup for the corresponding subsystem.\n\
4237c6ca
MN
34\n\
35Options :\n\
50b737a3
WB
36 -n, --name=NAME NAME of the container\n\
37 --rcfile=FILE Load configuration file FILE\n",
62ebeb04 38 .options = my_longopts,
39 .parser = NULL,
40 .checker = my_checker,
41 .log_priority = "ERROR",
42 .log_file = "none",
4237c6ca 43};
2ab8e9ba 44
62ebeb04 45static int my_checker(const struct lxc_arguments *args)
46{
47 if (!args->argc) {
48 ERROR("Missing state object");
49 return -1;
50 }
51
52 return 0;
53}
54
4237c6ca
MN
55int main(int argc, char *argv[])
56{
f10e7166 57 char *state_object = NULL, *value = NULL;
9069513c 58 struct lxc_container *c;
73b910a3 59 struct lxc_log log;
2ab8e9ba 60
272bc5af 61 if (lxc_arguments_parse(&my_args, argc, argv))
b52b0595 62 exit(EXIT_FAILURE);
2ab8e9ba 63
62ebeb04 64 log.name = my_args.name;
65 log.file = my_args.log_file;
66 log.level = my_args.log_priority;
67 log.prefix = my_args.progname;
68 log.quiet = my_args.quiet;
69 log.lxcpath = my_args.lxcpath[0];
f5abd74d 70
62ebeb04 71 if (lxc_log_init(&log))
72 exit(EXIT_FAILURE);
51cab631 73
f10e7166 74 state_object = my_args.argv[0];
2ab8e9ba 75
9069513c
SH
76 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
77 if (!c)
b52b0595 78 exit(EXIT_FAILURE);
f5abd74d 79
50b737a3
WB
80 if (my_args.rcfile) {
81 c->clear_config(c);
93f81bc7 82
50b737a3 83 if (!c->load_config(c, my_args.rcfile)) {
93f81bc7 84 ERROR("Failed to load rcfile");
50b737a3 85 lxc_container_put(c);
b52b0595 86 exit(EXIT_FAILURE);
50b737a3 87 }
097268e1 88
6118210e
WB
89 c->configfile = strdup(my_args.rcfile);
90 if (!c->configfile) {
93f81bc7 91 ERROR("Out of memory setting new config filename");
6118210e 92 lxc_container_put(c);
b52b0595 93 exit(EXIT_FAILURE);
6118210e 94 }
50b737a3
WB
95 }
96
f5abd74d 97 if (!c->may_control(c)) {
93f81bc7 98 ERROR("Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
f3e52710 99 lxc_container_put(c);
b52b0595 100 exit(EXIT_FAILURE);
f5abd74d
SG
101 }
102
9069513c 103 if (!c->is_running(c)) {
93f81bc7 104 ERROR("'%s:%s' is not running", my_args.lxcpath[0], my_args.name);
9069513c 105 lxc_container_put(c);
b52b0595 106 exit(EXIT_FAILURE);
9069513c 107 }
2ab8e9ba 108
9069513c
SH
109 if ((my_args.argc) > 1) {
110 value = my_args.argv[1];
097268e1 111
9069513c 112 if (!c->set_cgroup_item(c, state_object, value)) {
93f81bc7 113 ERROR("Failed to assign '%s' value to '%s' for '%s'",
114 value, state_object, my_args.name);
9069513c 115 lxc_container_put(c);
b52b0595 116 exit(EXIT_FAILURE);
2ab8e9ba 117 }
118 } else {
3a5996ff 119 char buffer[PATH_MAX];
097268e1 120 int ret;
121
3a5996ff 122 ret = c->get_cgroup_item(c, state_object, buffer, PATH_MAX);
70f7755e 123 if (ret < 0) {
93f81bc7 124 ERROR("Failed to retrieve value of '%s' for '%s:%s'",
125 state_object, my_args.lxcpath[0], my_args.name);
9069513c 126 lxc_container_put(c);
b52b0595 127 exit(EXIT_FAILURE);
2ab8e9ba 128 }
62ebeb04 129
6a5cc560 130 printf("%*s\n", ret, buffer);
2ab8e9ba 131 }
132
9069513c 133 lxc_container_put(c);
93f81bc7 134
b52b0595 135 exit(EXIT_SUCCESS);
0ad19a3f 136}