]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_wait.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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.
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.
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
27 #include <sys/types.h>
30 #include "arguments.h"
32 lxc_log_define(lxc_wait
, lxc
);
34 static int my_checker(const struct lxc_arguments
* args
)
37 lxc_error(args
, "missing state option to wait for.");
43 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
46 case 's': args
->states
= optarg
; break;
51 static const struct option my_longopts
[] = {
52 {"state", required_argument
, 0, 's'},
56 static struct lxc_arguments my_args
= {
57 .progname
= "lxc-wait",
59 --name=NAME --state=STATE\n\
61 lxc-wait waits for NAME container state to reach STATE\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
,
70 .checker
= my_checker
,
73 static int fillwaitedstates(char *strstates
, int *states
)
75 char *token
, *saveptr
= NULL
;
78 token
= strtok_r(strstates
, "|", &saveptr
);
81 state
= lxc_str2state(token
);
83 ERROR("invalid state %s", token
);
88 token
= strtok_r(NULL
, "|", &saveptr
);
93 int main(int argc
, char *argv
[])
96 int s
[MAX_STATE
] = { }, fd
;
99 if (lxc_arguments_parse(&my_args
, argc
, argv
))
102 if (lxc_log_init(my_args
.log_file
, my_args
.log_priority
,
103 my_args
.progname
, my_args
.quiet
))
106 if (fillwaitedstates(my_args
.states
, s
))
109 fd
= lxc_monitor_open();
114 * if container present,
115 * then check if already in requested state
118 state
= lxc_getstate(my_args
.name
);
121 } else if ((state
>= 0) && (s
[state
])) {
127 if (lxc_monitor_read(fd
, &msg
) < 0)
130 if (strcmp(my_args
.name
, msg
.name
))
135 if (msg
.value
< 0 || msg
.value
>= MAX_STATE
) {
136 ERROR("Receive an invalid state number '%d'",
147 /* just ignore garbage */
153 lxc_monitor_close(fd
);