]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_wait.c
lxc-wait to check if container already in requested state
[mirror_lxc.git] / src / lxc / lxc_wait.c
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>
24 #include <string.h>
25 #include <libgen.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28
29 #include <lxc/lxc.h>
30 #include "arguments.h"
31
32 lxc_log_define(lxc_wait, lxc);
33
34 static int my_checker(const struct lxc_arguments* args)
35 {
36 if (!args->states) {
37 lxc_error(args, "missing state option to wait for.");
38 return -1;
39 }
40 return 0;
41 }
42
43 static int my_parser(struct lxc_arguments* args, int c, char* arg)
44 {
45 switch (c) {
46 case 's': args->states = optarg; break;
47 }
48 return 0;
49 }
50
51 static const struct option my_longopts[] = {
52 {"state", required_argument, 0, 's'},
53 LXC_COMMON_OPTIONS
54 };
55
56 static struct lxc_arguments my_args = {
57 .progname = "lxc-wait",
58 .help = "\
59 --name=NAME --state=STATE\n\
60 \n\
61 lxc-wait waits for NAME container state to reach STATE\n\
62 \n\
63 Options :\n\
64 -n, --name=NAME NAME for name of the container\n\
65 -s, --state=STATE ORed states to wait for\n\
66 STOPPED, STARTING, RUNNING, STOPPING,\n\
67 ABORTING, FREEZING, FROZEN\n",
68 .options = my_longopts,
69 .parser = my_parser,
70 .checker = my_checker,
71 };
72
73 static int fillwaitedstates(char *strstates, int *states)
74 {
75 char *token, *saveptr = NULL;
76 int state;
77
78 token = strtok_r(strstates, "|", &saveptr);
79 while (token) {
80
81 state = lxc_str2state(token);
82 if (state < 0) {
83 ERROR("invalid state %s", token);
84 return -1;
85 }
86 states[state] = 1;
87
88 token = strtok_r(NULL, "|", &saveptr);
89 }
90 return 0;
91 }
92
93 int main(int argc, char *argv[])
94 {
95 struct lxc_msg msg;
96 int s[MAX_STATE] = { }, fd;
97 int state, ret;
98
99 if (lxc_arguments_parse(&my_args, argc, argv))
100 return -1;
101
102 if (lxc_log_init(my_args.log_file, my_args.log_priority,
103 my_args.progname, my_args.quiet))
104 return -1;
105
106 if (fillwaitedstates(my_args.states, s))
107 return -1;
108
109 fd = lxc_monitor_open();
110 if (fd < 0)
111 return -1;
112
113 /*
114 * if container present,
115 * then check if already in requested state
116 */
117 ret = -1;
118 state = lxc_getstate(my_args.name);
119 if (state < 0) {
120 goto out_close;
121 } else if ((state >= 0) && (s[state])) {
122 ret = 0;
123 goto out_close;
124 }
125
126 for (;;) {
127 if (lxc_monitor_read(fd, &msg) < 0)
128 goto out_close;
129
130 if (strcmp(my_args.name, msg.name))
131 continue;
132
133 switch (msg.type) {
134 case lxc_msg_state:
135 if (msg.value < 0 || msg.value >= MAX_STATE) {
136 ERROR("Receive an invalid state number '%d'",
137 msg.value);
138 goto out_close;
139 }
140
141 if (s[msg.value]) {
142 ret = 0;
143 goto out_close;
144 }
145 break;
146 default:
147 /* just ignore garbage */
148 break;
149 }
150 }
151
152 out_close:
153 lxc_monitor_close(fd);
154 return ret;
155 }