]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_wait.c
Remove annoying warnings and fix tty for restart
[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
31 void usage(char *cmd)
32 {
33 fprintf(stderr, "%s <command>\n", basename(cmd));
34 fprintf(stderr, "\t -n <name> : name of the container\n");
35 fprintf(stderr, "\t -s <states> : ORed states to wait for STOPPED, " \
36 "STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN\n");
37 _exit(1);
38 }
39
40 static int fillwaitedstates(char *strstates, int *states)
41 {
42 char *token, *saveptr = NULL;
43 int state;
44
45 token = strtok_r(strstates, "|", &saveptr);
46 while (token) {
47
48 state = lxc_str2state(token);
49 if (state < 0)
50 return -1;
51 states[state] = 1;
52
53 token = strtok_r(NULL, "|", &saveptr);
54
55 }
56 return 0;
57 }
58
59 int main(int argc, char *argv[])
60 {
61 char opt, *name = NULL, *states = NULL;
62 struct lxc_msg msg;
63 int s[MAX_STATE] = { }, fd;
64
65 while ((opt = getopt(argc, argv, "s:n:")) != -1) {
66 switch (opt) {
67 case 'n':
68 name = optarg;
69 break;
70 case 's':
71 states = optarg;
72 break;
73 }
74 }
75
76 if (!name || !states)
77 usage(argv[0]);
78
79 if (fillwaitedstates(states, s)) {
80 fprintf(stderr, "invalid states specified\n");
81 usage(argv[0]);
82 }
83
84
85 fd = lxc_monitor_open();
86 if (fd < 0) {
87 fprintf(stderr, "failed to open monitor for '%s'\n", name);
88 return -1;
89 }
90
91 for (;;) {
92 if (lxc_monitor_read(fd, &msg) < 0) {
93 fprintf(stderr,
94 "failed to read monitor's message for '%s'\n",
95 name);
96 return -1;
97 }
98
99 if (strcmp(name, msg.name))
100 continue;
101
102 switch (msg.type) {
103 case lxc_msg_state:
104 if (msg.value < 0 || msg.value >= MAX_STATE) {
105 fprintf(stderr, "Receive an invalid state number '%d'\n",
106 msg.value);
107 return -1;
108 }
109
110 if (s[msg.value])
111 return 0;
112 break;
113 default:
114 /* just ignore garbage */
115 break;
116 }
117 }
118
119 return 0;
120 }