]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_checkpoint.c
conditional use of new capabilities
[mirror_lxc.git] / src / lxc / lxc_checkpoint.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 <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <fcntl.h>
32
33 #include <lxc/lxc.h>
34 #include <lxc/log.h>
35 #include <lxc/utils.h>
36
37 #include "arguments.h"
38 #include "config.h"
39
40 lxc_log_define(lxc_checkpoint_ui, lxc_checkpoint);
41
42 static int my_checker(const struct lxc_arguments* args)
43 {
44 if ((!args->statefile) && (args->statefd == -1)) {
45 lxc_error(args, "no statefile specified");
46 return -1;
47 }
48
49 if ((args->statefile) && (args->statefd != -1)) {
50 lxc_error(args, "--statefile AND --statefd abnormally set");
51 return -1;
52 }
53
54 return 0;
55 }
56
57 static int my_parser(struct lxc_arguments* args, int c, char* arg)
58 {
59 switch (c) {
60 case 'k': args->flags = LXC_FLAG_HALT; break;
61 case 'p': args->flags = LXC_FLAG_PAUSE; break;
62 case 'S': args->statefile = arg; break;
63 case 'd': {
64 int fd;
65 fd = lxc_arguments_str_to_int(args, arg);
66 if (fd < 0)
67 return -1;
68
69 args->statefd = fd;
70 break;
71 }
72 }
73 return 0;
74 }
75
76 static const struct option my_longopts[] = {
77 {"kill", no_argument, 0, 'k'},
78 {"pause", no_argument, 0, 'p'},
79 {"statefile", required_argument, 0, 'S'},
80 {"statefd", required_argument, 0, 'd'},
81 LXC_COMMON_OPTIONS
82 };
83
84 static struct lxc_arguments my_args = {
85 .progname = "lxc-checkpoint",
86 .help = "\
87 --name=NAME --statefile FILE\n\
88 \n\
89 lxc-checkpoint checkpoints in FILE the NAME container\n\
90 \n\
91 Options :\n\
92 -n, --name=NAME NAME for name of the container\n\
93 -k, --kill stop the container after checkpoint\n\
94 -p, --pause don't unfreeze the container after the checkpoint\n\
95 -S, --statefile=FILE write the container state into this file, or\n\
96 -d, --statefd=FD write the container state into this file descriptor\n",
97
98 .options = my_longopts,
99 .parser = my_parser,
100 .checker = my_checker,
101
102 .statefd = -1,
103 };
104
105 int main(int argc, char *argv[])
106 {
107 int ret;
108 int sfd = -1;
109
110 ret = lxc_arguments_parse(&my_args, argc, argv);
111 if (ret)
112 return ret;
113
114 ret = lxc_log_init(my_args.log_file, my_args.log_priority,
115 my_args.progname, my_args.quiet);
116 if (ret)
117 return ret;
118
119 if (my_args.statefd != -1)
120 sfd = my_args.statefd;
121
122 #define OPEN_WRITE_MODE O_CREAT | O_RDWR | O_EXCL | O_CLOEXEC | O_LARGEFILE
123 if (my_args.statefile) {
124 sfd = open(my_args.statefile, OPEN_WRITE_MODE, 0600);
125 if (sfd < 0) {
126 ERROR("'%s' open failure : %m", my_args.statefile);
127 return sfd;
128 }
129 }
130
131 ret = lxc_checkpoint(my_args.name, sfd, my_args.flags);
132
133 assert(ret == 0 || ret == -1);
134
135 if (ret)
136 ERROR("failed to checkpoint '%s'", my_args.name);
137 else
138 INFO("'%s' checkpointed", my_args.name);
139
140 if (my_args.statefile)
141 close(sfd);
142 return ret;
143 }