]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/lxc_wait.c
ovl_rsync: make sure to umount
[mirror_lxc.git] / src / lxc / lxc_wait.c
CommitLineData
afdbaabd 1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
9afe19d6 7 * Daniel Lezcano <daniel.lezcano at free.fr>
afdbaabd 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
250b1eec 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
afdbaabd 22 */
23#include <stdio.h>
24#include <string.h>
25#include <libgen.h>
26#include <unistd.h>
b486346a
JK
27#include <stdlib.h>
28#include <signal.h>
afdbaabd 29#include <sys/types.h>
30
92b0b5ba 31#include <lxc/lxccontainer.h>
f2363e38
ÇO
32
33#include "lxc.h"
34#include "log.h"
41cfbac9 35#include "arguments.h"
afdbaabd 36
6ea518f6 37lxc_log_define(lxc_wait_ui, lxc);
3ab87b66 38
41cfbac9 39static int my_checker(const struct lxc_arguments* args)
afdbaabd 40{
41cfbac9
MN
41 if (!args->states) {
42 lxc_error(args, "missing state option to wait for.");
43 return -1;
44 }
45 return 0;
46}
47
48static int my_parser(struct lxc_arguments* args, int c, char* arg)
49{
50 switch (c) {
51 case 's': args->states = optarg; break;
b486346a 52 case 't': args->timeout = atol(optarg); break;
41cfbac9
MN
53 }
54 return 0;
afdbaabd 55}
56
41cfbac9
MN
57static const struct option my_longopts[] = {
58 {"state", required_argument, 0, 's'},
b486346a 59 {"timeout", required_argument, 0, 't'},
41cfbac9
MN
60 LXC_COMMON_OPTIONS
61};
62
63static struct lxc_arguments my_args = {
64 .progname = "lxc-wait",
65 .help = "\
66--name=NAME --state=STATE\n\
67\n\
68lxc-wait waits for NAME container state to reach STATE\n\
69\n\
70Options :\n\
5e8757ed 71 -n, --name=NAME NAME of the container\n\
41cfbac9
MN
72 -s, --state=STATE ORed states to wait for\n\
73 STOPPED, STARTING, RUNNING, STOPPING,\n\
d27b0806 74 ABORTING, FREEZING, FROZEN, THAWED\n\
b486346a 75 -t, --timeout=TMO Seconds to wait for state changes\n",
41cfbac9
MN
76 .options = my_longopts,
77 .parser = my_parser,
78 .checker = my_checker,
06200a37 79 .timeout = -1,
41cfbac9
MN
80};
81
afdbaabd 82int main(int argc, char *argv[])
83{
92b0b5ba
SH
84 struct lxc_container *c;
85
75b1e198 86 if (lxc_arguments_parse(&my_args, argc, argv))
13bc2fd2 87 return 1;
afdbaabd 88
f5abd74d
SG
89 if (!my_args.log_file)
90 my_args.log_file = "none";
91
5e1e7aaf 92 if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
8d06bd13 93 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
13bc2fd2 94 return 1;
6edbfc86 95 lxc_log_options_no_override();
51cab631 96
92b0b5ba
SH
97 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
98 if (!c)
13bc2fd2 99 return 1;
92b0b5ba 100
f5abd74d
SG
101 if (!c->may_control(c)) {
102 fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
f3e52710 103 lxc_container_put(c);
13bc2fd2 104 return 1;
f5abd74d
SG
105 }
106
92b0b5ba
SH
107 if (!c->wait(c, my_args.states, my_args.timeout)) {
108 lxc_container_put(c);
13bc2fd2 109 return 1;
92b0b5ba
SH
110 }
111 return 0;
afdbaabd 112}