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