]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 | */ | |
23 | #include <stdio.h> | |
5d42011a | 24 | #include <stdbool.h> |
0ad19a3f | 25 | #include <unistd.h> |
26 | #include <libgen.h> | |
27 | #include <sys/types.h> | |
28 | ||
b113348e | 29 | #include <lxc/lxc.h> |
00b3c2e2 | 30 | #include <lxc/log.h> |
f549edcc GK |
31 | |
32 | #include "commands.h" | |
4237c6ca | 33 | #include "arguments.h" |
0ad19a3f | 34 | |
5d42011a DL |
35 | static bool state; |
36 | static bool pid; | |
b917ef75 | 37 | static char *test_state = NULL; |
5d42011a DL |
38 | |
39 | static int my_parser(struct lxc_arguments* args, int c, char* arg) | |
40 | { | |
41 | switch (c) { | |
42 | case 's': state = true; break; | |
43 | case 'p': pid = true; break; | |
b917ef75 | 44 | case 't': test_state = arg; break; |
5d42011a DL |
45 | } |
46 | return 0; | |
47 | } | |
48 | ||
4237c6ca | 49 | static const struct option my_longopts[] = { |
5d42011a DL |
50 | {"state", no_argument, 0, 's'}, |
51 | {"pid", no_argument, 0, 'p'}, | |
b917ef75 | 52 | {"state-is", required_argument, 0, 't'}, |
5d42011a | 53 | LXC_COMMON_OPTIONS, |
4237c6ca MN |
54 | }; |
55 | ||
56 | static struct lxc_arguments my_args = { | |
57 | .progname = "lxc-info", | |
58 | .help = "\ | |
59 | --name=NAME\n\ | |
60 | \n\ | |
5d42011a | 61 | lxc-info display some information about a container with the identifier NAME\n\ |
4237c6ca MN |
62 | \n\ |
63 | Options :\n\ | |
b917ef75 NC |
64 | -n, --name=NAME NAME for name of the container\n\ |
65 | -s, --state shows the state of the container\n\ | |
66 | -p, --pid shows the process id of the init container\n\ | |
67 | -t, --state-is=STATE test if current state is STATE\n\ | |
68 | returns success if it matches, false otherwise\n", | |
4237c6ca | 69 | .options = my_longopts, |
5d42011a | 70 | .parser = my_parser, |
4237c6ca MN |
71 | .checker = NULL, |
72 | }; | |
0ad19a3f | 73 | |
74 | int main(int argc, char *argv[]) | |
75 | { | |
5d42011a | 76 | int ret; |
13f5be62 SH |
77 | /* TODO: add lxcpath cmdline arg */ |
78 | const char *lxcpath = NULL; | |
0ad19a3f | 79 | |
4237c6ca MN |
80 | ret = lxc_arguments_parse(&my_args, argc, argv); |
81 | if (ret) | |
82 | return 1; | |
0ad19a3f | 83 | |
5e1e7aaf | 84 | if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority, |
4237c6ca | 85 | my_args.progname, my_args.quiet)) |
51cab631 MN |
86 | return 1; |
87 | ||
5d42011a DL |
88 | if (!state && !pid) |
89 | state = pid = true; | |
90 | ||
b917ef75 | 91 | if (state || test_state) { |
13f5be62 | 92 | ret = lxc_getstate(my_args.name, lxcpath); |
5d42011a DL |
93 | if (ret < 0) |
94 | return 1; | |
b917ef75 NC |
95 | if (test_state) |
96 | return strcmp(lxc_state2str(ret), test_state) != 0; | |
5d42011a DL |
97 | |
98 | printf("state:%10s\n", lxc_state2str(ret)); | |
99 | } | |
0ad19a3f | 100 | |
5d42011a | 101 | if (pid) |
13f5be62 | 102 | printf("pid:%10d\n", get_init_pid(my_args.name, lxcpath)); |
0ad19a3f | 103 | |
104 | return 0; | |
105 | } |