]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/tools/lxc_wait.c
tree-wide: fix lxc header inclusion
[mirror_lxc.git] / src / lxc / tools / lxc_wait.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <libgen.h>
7 #include <signal.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13
14 #include "lxc.h"
15
16 #include "arguments.h"
17 #include "config.h"
18 #include "log.h"
19
20 lxc_log_define(lxc_wait, lxc);
21
22 static int my_parser(struct lxc_arguments *args, int c, char *arg);
23 static int my_checker(const struct lxc_arguments *args);
24
25 static const struct option my_longopts[] = {
26 {"state", required_argument, 0, 's'},
27 {"timeout", required_argument, 0, 't'},
28 LXC_COMMON_OPTIONS
29 };
30
31 static struct lxc_arguments my_args = {
32 .progname = "lxc-wait",
33 .help = "\
34 --name=NAME --state=STATE\n\
35 \n\
36 lxc-wait waits for NAME container state to reach STATE\n\
37 \n\
38 Options :\n\
39 -n, --name=NAME NAME of the container\n\
40 -s, --state=STATE ORed states to wait for\n\
41 STOPPED, STARTING, RUNNING, STOPPING,\n\
42 ABORTING, FREEZING, FROZEN, THAWED\n\
43 -t, --timeout=TMO Seconds to wait for state changes\n\
44 --rcfile=FILE Load configuration file FILE\n",
45 .options = my_longopts,
46 .parser = my_parser,
47 .checker = my_checker,
48 .log_priority = "ERROR",
49 .log_file = "none",
50 .timeout = -1,
51 };
52
53 static 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
67 static 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
77 int main(int argc, char *argv[])
78 {
79 struct lxc_container *c;
80 struct lxc_log log;
81
82 if (lxc_arguments_parse(&my_args, argc, argv))
83 exit(EXIT_FAILURE);
84
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];
91
92 if (lxc_log_init(&log))
93 exit(EXIT_FAILURE);
94
95 c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
96 if (!c)
97 exit(EXIT_FAILURE);
98
99 if (!c->may_control(c)) {
100 ERROR("Insufficent privileges to control %s", c->name);
101 lxc_container_put(c);
102 exit(EXIT_FAILURE);
103 }
104
105 if (my_args.rcfile) {
106 c->clear_config(c);
107
108 if (!c->load_config(c, my_args.rcfile)) {
109 ERROR("Failed to load rcfile");
110 lxc_container_put(c);
111 exit(EXIT_FAILURE);
112 }
113
114 c->configfile = strdup(my_args.rcfile);
115 if (!c->configfile) {
116 ERROR("Out of memory setting new config filename");
117 lxc_container_put(c);
118 exit(EXIT_FAILURE);
119 }
120 }
121
122 if (!c->wait(c, my_args.states, my_args.timeout)) {
123 lxc_container_put(c);
124 exit(EXIT_FAILURE);
125 }
126
127 exit(EXIT_SUCCESS);
128 }