~serge-hallyn/ubuntu/natty/lxc/lxc-fix-3bugs

« back to all changes in this revision

Viewing changes to src/lxc/lxc_cgroup.c

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2009-07-25 12:24:30 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090725122430-dxv1wb7ds07fc0sk
Tags: 0.6.3-1
* New Upstream Version
* Remove duplicate build-dependency on autotools-dev
* Build depend on linux-libc-dev
* Disable checking of netlink headers from configure
  (currently fails under sid)
* Upgrade standards-version to 3.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <sys/types.h>
28
28
 
29
29
#include <lxc/lxc.h>
30
 
 
31
 
void usage(char *cmd)
 
30
#include "arguments.h"
 
31
 
 
32
lxc_log_define(lxc_cgroup, lxc);
 
33
 
 
34
static int my_checker(const struct lxc_arguments* args)
32
35
{
33
 
        fprintf(stderr, "%s <subsystem> [value]\n", basename(cmd));
34
 
        fprintf(stderr, "\t -n <name>   : name of the container\n");
35
 
        _exit(1);
 
36
        if (!args->argc) {
 
37
                lxc_error(args, "missing cgroup subsystem");
 
38
                return -1;
 
39
        }
 
40
        return 0;
36
41
}
37
42
 
 
43
static const struct option my_longopts[] = {
 
44
        LXC_COMMON_OPTIONS
 
45
};
 
46
 
 
47
static struct lxc_arguments my_args = {
 
48
        .progname = "lxc-cgroup",
 
49
        .help     = "\
 
50
--name=NAME subsystem [value]\n\
 
51
\n\
 
52
lxc-cgroup get or set subsystem value of cgroup\n\
 
53
associated with the NAME container\n\
 
54
\n\
 
55
Options :\n\
 
56
  -n, --name=NAME      NAME for name of the container",
 
57
        .options  = my_longopts,
 
58
        .parser   = NULL,
 
59
        .checker  = my_checker,
 
60
};
 
61
 
38
62
int main(int argc, char *argv[])
39
63
{
40
 
        int opt;
41
 
        char *name = NULL, *subsystem = NULL, *value = NULL;
42
 
        int nbargs = 0;
43
 
 
44
 
        while ((opt = getopt(argc, argv, "n:")) != -1) {
45
 
                switch (opt) {
46
 
                case 'n':
47
 
                        name = optarg;
48
 
                        break;
49
 
                }
50
 
 
51
 
                nbargs++;
52
 
        }
53
 
 
54
 
        if (!name || argc < 4)
55
 
                usage(argv[0]);
56
 
 
57
 
        if (argc >= 5)
58
 
                value = argv[4];
59
 
 
60
 
        subsystem = argv[3];
 
64
        char *subsystem = NULL, *value = NULL;
 
65
 
 
66
        if (lxc_arguments_parse(&my_args, argc, argv))
 
67
                return -1;
 
68
 
 
69
        if (lxc_log_init(my_args.log_file, my_args.log_priority,
 
70
                         my_args.progname, my_args.quiet))
 
71
                return -1;
 
72
 
 
73
        subsystem = my_args.argv[0];
 
74
 
 
75
        if ((argc) > 1)
 
76
                value = my_args.argv[1];
61
77
 
62
78
        if (value) {
63
 
                if (lxc_cgroup_set(name, subsystem, value)) {
64
 
                        fprintf(stderr, "failed to assign '%s' value to '%s' for '%s'\n",
65
 
                                value, subsystem, name);
66
 
                        return 1;
 
79
                if (lxc_cgroup_set(my_args.name, subsystem, value)) {
 
80
                        ERROR("failed to assign '%s' value to '%s' for '%s'",
 
81
                                value, subsystem, my_args.name);
 
82
                        return -1;
67
83
                }
68
84
        } else {
69
85
                const unsigned long len = 4096;
70
86
                char buffer[len];
71
 
                if (lxc_cgroup_get(name, subsystem, buffer, len)) {
72
 
                        fprintf(stderr, "failed to retrieve value of '%s' for '%s'\n",
73
 
                                subsystem, name);
74
 
                        return 1;
 
87
                if (lxc_cgroup_get(my_args.name, subsystem, buffer, len)) {
 
88
                        ERROR("failed to retrieve value of '%s' for '%s'",
 
89
                                subsystem, my_args.name);
 
90
                        return -1;
75
91
                }
76
92
 
77
93
                printf("%s", buffer);