]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/tools/lxc_wait.c
tree-wide: fix lxc header inclusion
[mirror_lxc.git] / src / lxc / tools / lxc_wait.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
b678c6d8 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
afdbaabd 6#include <libgen.h>
b486346a 7#include <signal.h>
d38dd64a
CB
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
afdbaabd 11#include <sys/types.h>
d38dd64a 12#include <unistd.h>
afdbaabd 13
12ae2a33 14#include "lxc.h"
f2363e38 15
41cfbac9 16#include "arguments.h"
d38dd64a 17#include "config.h"
df3ab009 18#include "log.h"
19
20lxc_log_define(lxc_wait, lxc);
afdbaabd 21
75457df5 22static int my_parser(struct lxc_arguments *args, int c, char *arg);
23static int my_checker(const struct lxc_arguments *args);
afdbaabd 24
41cfbac9
MN
25static const struct option my_longopts[] = {
26 {"state", required_argument, 0, 's'},
b486346a 27 {"timeout", required_argument, 0, 't'},
41cfbac9
MN
28 LXC_COMMON_OPTIONS
29};
30
31static struct lxc_arguments my_args = {
75457df5 32 .progname = "lxc-wait",
33 .help = "\
41cfbac9
MN
34--name=NAME --state=STATE\n\
35\n\
36lxc-wait waits for NAME container state to reach STATE\n\
37\n\
38Options :\n\
5e8757ed 39 -n, --name=NAME NAME of the container\n\
41cfbac9
MN
40 -s, --state=STATE ORed states to wait for\n\
41 STOPPED, STARTING, RUNNING, STOPPING,\n\
d27b0806 42 ABORTING, FREEZING, FROZEN, THAWED\n\
50b737a3
WB
43 -t, --timeout=TMO Seconds to wait for state changes\n\
44 --rcfile=FILE Load configuration file FILE\n",
75457df5 45 .options = my_longopts,
46 .parser = my_parser,
47 .checker = my_checker,
48 .log_priority = "ERROR",
49 .log_file = "none",
50 .timeout = -1,
41cfbac9
MN
51};
52
75457df5 53static int my_parser(struct lxc_arguments *args, int c, char *arg)
54{
55 switch (c) {
56 case 's':
57 args->states = optarg;
58 break;
59 case 't':
60 args->timeout = atol(optarg);
61 break;
62 }
63
64 return 0;
65}
66
67static int my_checker(const struct lxc_arguments *args)
68{
69 if (!args->states) {
70 ERROR("Missing state option to wait for");
71 return -1;
72 }
73
74 return 0;
75}
76
afdbaabd 77int main(int argc, char *argv[])
78{
92b0b5ba 79 struct lxc_container *c;
73b910a3 80 struct lxc_log log;
92b0b5ba 81
75b1e198 82 if (lxc_arguments_parse(&my_args, argc, argv))
b52b0595 83 exit(EXIT_FAILURE);
afdbaabd 84
75457df5 85 log.name = my_args.name;
86 log.file = my_args.log_file;
87 log.level = my_args.log_priority;
88 log.prefix = my_args.progname;
89 log.quiet = my_args.quiet;
90 log.lxcpath = my_args.lxcpath[0];
f6d79ec1 91
75457df5 92 if (lxc_log_init(&log))
93 exit(EXIT_FAILURE);
51cab631 94
92b0b5ba
SH
95 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
96 if (!c)
b52b0595 97 exit(EXIT_FAILURE);
92b0b5ba 98
f5abd74d 99 if (!c->may_control(c)) {
df3ab009 100 ERROR("Insufficent privileges to control %s", c->name);
f3e52710 101 lxc_container_put(c);
b52b0595 102 exit(EXIT_FAILURE);
f5abd74d
SG
103 }
104
50b737a3
WB
105 if (my_args.rcfile) {
106 c->clear_config(c);
097268e1 107
50b737a3 108 if (!c->load_config(c, my_args.rcfile)) {
df3ab009 109 ERROR("Failed to load rcfile");
50b737a3 110 lxc_container_put(c);
b52b0595 111 exit(EXIT_FAILURE);
50b737a3 112 }
097268e1 113
6118210e
WB
114 c->configfile = strdup(my_args.rcfile);
115 if (!c->configfile) {
df3ab009 116 ERROR("Out of memory setting new config filename");
6118210e 117 lxc_container_put(c);
b52b0595 118 exit(EXIT_FAILURE);
6118210e 119 }
50b737a3
WB
120 }
121
92b0b5ba
SH
122 if (!c->wait(c, my_args.states, my_args.timeout)) {
123 lxc_container_put(c);
b52b0595 124 exit(EXIT_FAILURE);
92b0b5ba 125 }
097268e1 126
b52b0595 127 exit(EXIT_SUCCESS);
afdbaabd 128}