]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_info.c
licensing: Add missing headers and FSF address
[mirror_lxc.git] / src / lxc / lxc_info.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <libgen.h>
28 #include <sys/types.h>
29
30 #include <lxc/lxc.h>
31 #include <lxc/log.h>
32
33 #include "commands.h"
34 #include "arguments.h"
35
36 static bool state;
37 static bool pid;
38 static char *test_state = NULL;
39 static char **key = NULL;
40 static int keys = 0;
41
42 static int my_parser(struct lxc_arguments* args, int c, char* arg)
43 {
44 switch (c) {
45 case 'c':
46 key = realloc(key, keys+1 * sizeof(key[0]));
47 key[keys] = arg;
48 keys++;
49 break;
50 case 's': state = true; break;
51 case 'p': pid = true; break;
52 case 't': test_state = arg; break;
53 }
54 return 0;
55 }
56
57 static const struct option my_longopts[] = {
58 {"config", required_argument, 0, 'c'},
59 {"state", no_argument, 0, 's'},
60 {"pid", no_argument, 0, 'p'},
61 {"state-is", required_argument, 0, 't'},
62 LXC_COMMON_OPTIONS,
63 };
64
65 static struct lxc_arguments my_args = {
66 .progname = "lxc-info",
67 .help = "\
68 --name=NAME\n\
69 \n\
70 lxc-info display some information about a container with the identifier NAME\n\
71 \n\
72 Options :\n\
73 -n, --name=NAME NAME for name of the container\n\
74 -c, --config=KEY show configuration variable KEY from running container\n\
75 -p, --pid shows the process id of the init container\n\
76 -s, --state shows the state of the container\n\
77 -t, --state-is=STATE test if current state is STATE\n\
78 returns success if it matches, false otherwise\n",
79 .options = my_longopts,
80 .parser = my_parser,
81 .checker = NULL,
82 };
83
84 int main(int argc, char *argv[])
85 {
86 int ret,i;
87
88 ret = lxc_arguments_parse(&my_args, argc, argv);
89 if (ret)
90 return 1;
91
92 if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
93 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
94 return 1;
95
96 if (!state && !pid && keys <= 0)
97 state = pid = true;
98
99 if (state || test_state) {
100 ret = lxc_getstate(my_args.name, my_args.lxcpath[0]);
101 if (ret < 0)
102 return 1;
103 if (test_state)
104 return strcmp(lxc_state2str(ret), test_state) != 0;
105
106 printf("state:%10s\n", lxc_state2str(ret));
107 }
108
109 if (pid) {
110 pid_t initpid;
111
112 initpid = lxc_cmd_get_init_pid(my_args.name, my_args.lxcpath[0]);
113 if (initpid >= 0)
114 printf("pid:%10d\n", initpid);
115 }
116
117 for(i = 0; i < keys; i++) {
118 char *val;
119
120 val = lxc_cmd_get_config_item(my_args.name, key[i], my_args.lxcpath[0]);
121 if (val) {
122 printf("%s = %s\n", key[i], val);
123 free(val);
124 } else {
125 fprintf(stderr, "%s unset or invalid\n", key[i]);
126 }
127 }
128
129 return 0;
130 }