]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/tools/lxc_wait.c
API: expose function lxc_log_init
[mirror_lxc.git] / src / lxc / tools / 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\
50b737a3
WB
75 -t, --timeout=TMO Seconds to wait for state changes\n\
76 --rcfile=FILE Load configuration file FILE\n",
41cfbac9
MN
77 .options = my_longopts,
78 .parser = my_parser,
79 .checker = my_checker,
06200a37 80 .timeout = -1,
41cfbac9
MN
81};
82
afdbaabd 83int main(int argc, char *argv[])
84{
92b0b5ba 85 struct lxc_container *c;
73b910a3 86 struct lxc_log log;
92b0b5ba 87
75b1e198 88 if (lxc_arguments_parse(&my_args, argc, argv))
b52b0595 89 exit(EXIT_FAILURE);
afdbaabd 90
f5abd74d
SG
91 if (!my_args.log_file)
92 my_args.log_file = "none";
93
73b910a3 94 log.name = my_args.name;
95 log.file = my_args.log_file;
96 log.priority = my_args.log_priority;
97 log.prefix = my_args.progname;
98 log.quiet = my_args.quiet;
99 log.lxcpath = my_args.lxcpath[0];
100
101 if (lxc_log_init(&log))
b52b0595 102 exit(EXIT_FAILURE);
6edbfc86 103 lxc_log_options_no_override();
51cab631 104
92b0b5ba
SH
105 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
106 if (!c)
b52b0595 107 exit(EXIT_FAILURE);
92b0b5ba 108
f5abd74d
SG
109 if (!c->may_control(c)) {
110 fprintf(stderr, "Insufficent privileges to control %s\n", c->name);
f3e52710 111 lxc_container_put(c);
b52b0595 112 exit(EXIT_FAILURE);
f5abd74d
SG
113 }
114
50b737a3
WB
115 if (my_args.rcfile) {
116 c->clear_config(c);
117 if (!c->load_config(c, my_args.rcfile)) {
118 fprintf(stderr, "Failed to load rcfile\n");
119 lxc_container_put(c);
b52b0595 120 exit(EXIT_FAILURE);
50b737a3 121 }
6118210e
WB
122 c->configfile = strdup(my_args.rcfile);
123 if (!c->configfile) {
124 fprintf(stderr, "Out of memory setting new config filename\n");
125 lxc_container_put(c);
b52b0595 126 exit(EXIT_FAILURE);
6118210e 127 }
50b737a3
WB
128 }
129
92b0b5ba
SH
130 if (!c->wait(c, my_args.states, my_args.timeout)) {
131 lxc_container_put(c);
b52b0595 132 exit(EXIT_FAILURE);
92b0b5ba 133 }
b52b0595 134 exit(EXIT_SUCCESS);
afdbaabd 135}