]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_restart.c
lxc: child failing before container rename
[mirror_lxc.git] / src / lxc / lxc_restart.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2010
5 *
6 * Authors:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #undef _GNU_SOURCE
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <fcntl.h>
31
32 #include "log.h"
33 #include "lxc.h"
34 #include "conf.h"
35 #include "config.h"
36 #include "confile.h"
37 #include "arguments.h"
38
39 lxc_log_define(lxc_restart_ui, lxc_restart);
40
41 static struct lxc_list defines;
42
43 static int my_checker(const struct lxc_arguments* args)
44 {
45 if ((!args->statefile) && (args->statefd == -1)) {
46 lxc_error(args, "no statefile specified");
47 return -1;
48 }
49
50 if ((args->statefile) && (args->statefd != -1)) {
51 lxc_error(args, "--statefile AND --statefd abnormally set");
52 return -1;
53 }
54
55 return 0;
56 }
57
58 static int my_parser(struct lxc_arguments* args, int c, char* arg)
59 {
60 switch (c) {
61 case 'S': args->statefile = arg; break;
62 case 'f': args->rcfile = arg; break;
63 case 'p': args->flags = LXC_FLAG_PAUSE; break;
64 case 's': return lxc_config_define_add(&defines, arg);
65 case 'd': {
66 int fd;
67 fd = lxc_arguments_str_to_int(args, arg);
68 if (fd < 0)
69 return -1;
70
71 args->statefd = fd;
72 break;
73 }
74 }
75
76 return 0;
77 }
78
79 static const struct option my_longopts[] = {
80 {"statefile", required_argument, 0, 'S'},
81 {"statefd", required_argument, 0, 'd'},
82 {"rcfile", required_argument, 0, 'f'},
83 {"pause", no_argument, 0, 'p'},
84 {"define", required_argument, 0, 's'},
85 LXC_COMMON_OPTIONS
86 };
87
88 static struct lxc_arguments my_args = {
89 .progname = "lxc-restart",
90 .help = "\
91 --name=NAME --statefile FILE\n\
92 \n\
93 lxc-restart restarts from FILE the NAME container\n\
94 \n\
95 Options :\n\
96 -n, --name=NAME NAME for name of the container\n\
97 -p, --pause do not unfreeze the container after the restart\n\
98 -S, --statefile=FILE read the container state from this file, or\n\
99 -d, --statefd=FD read the container state from this file descriptor\n\
100 -f, --rcfile=FILE Load configuration file FILE\n\
101 -s, --define KEY=VAL Assign VAL to configuration variable KEY\n",
102 .options = my_longopts,
103 .parser = my_parser,
104 .checker = my_checker,
105
106 .statefd = -1,
107 };
108
109 int main(int argc, char *argv[])
110 {
111 int sfd = -1;
112 int ret;
113 char *rcfile = NULL;
114 struct lxc_conf *conf;
115
116 lxc_list_init(&defines);
117
118 if (lxc_arguments_parse(&my_args, argc, argv))
119 return -1;
120
121 if (lxc_log_init(my_args.log_file, my_args.log_priority,
122 my_args.progname, my_args.quiet))
123 return -1;
124
125 /* rcfile is specified in the cli option */
126 if (my_args.rcfile)
127 rcfile = (char *)my_args.rcfile;
128 else {
129 if (!asprintf(&rcfile, LXCPATH "/%s/config", my_args.name)) {
130 SYSERROR("failed to allocate memory");
131 return -1;
132 }
133
134 /* container configuration does not exist */
135 if (access(rcfile, F_OK)) {
136 free(rcfile);
137 rcfile = NULL;
138 }
139 }
140
141 conf = lxc_conf_init();
142 if (!conf) {
143 ERROR("failed to initialize configuration");
144 return -1;
145 }
146
147 if (rcfile && lxc_config_read(rcfile, conf)) {
148 ERROR("failed to read configuration file");
149 return -1;
150 }
151
152 if (lxc_config_define_load(&defines, conf))
153 return -1;
154
155 if (my_args.statefd != -1)
156 sfd = my_args.statefd;
157
158 #define OPEN_READ_MODE O_RDONLY | O_CLOEXEC | O_LARGEFILE
159 if (my_args.statefile) {
160 sfd = open(my_args.statefile, OPEN_READ_MODE, 0);
161 if (sfd < 0) {
162 ERROR("'%s' open failure : %m", my_args.statefile);
163 return sfd;
164 }
165 }
166
167 ret = lxc_restart(my_args.name, sfd, conf, my_args.flags);
168
169 if (my_args.statefile)
170 close(sfd);
171 return ret;
172 }