]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc_stop.c
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
28 #include <lxc/lxccontainer.h>
32 #include "arguments.h"
36 #define OPT_NO_LOCK OPT_USAGE+1
38 static int my_parser(struct lxc_arguments
* args
, int c
, char* arg
)
41 case 'r': args
->reboot
= 1; break;
42 case 'W': args
->nowait
= 1; break;
43 case 't': args
->timeout
= atoi(arg
); break;
44 case 'k': args
->hardstop
= 1; break;
45 case 's': args
->shutdown
= 1; break;
46 case OPT_NO_LOCK
: args
->nolock
= 1; break;
51 static const struct option my_longopts
[] = {
52 {"reboot", no_argument
, 0, 'r'},
53 {"nowait", no_argument
, 0, 'W'},
54 {"timeout", required_argument
, 0, 't'},
55 {"kill", no_argument
, 0, 'k'},
56 {"shutdown", no_argument
, 0, 's'},
57 {"no-lock", no_argument
, 0, OPT_NO_LOCK
},
61 static struct lxc_arguments my_args
= {
62 .progname
= "lxc-stop",
66 lxc-stop stops a container with the identifier NAME\n\
69 -n, --name=NAME NAME for name of the container\n\
70 -r, --reboot reboot the container\n\
71 -W, --nowait don't wait for shutdown or reboot to complete\n\
72 -t, --timeout=T wait T seconds before hard-stopping\n\
73 -k, --kill kill container rather than request clean shutdown\n\
74 --nolock Avoid using API locks\n\
75 -s, --shutdown Only request clean shutdown, don't later force kill\n",
76 .options
= my_longopts
,
82 /* returns -1 on failure, 0 on success */
83 static int do_reboot_and_check(struct lxc_arguments
*a
, struct lxc_container
*c
)
88 int timeout
= a
->timeout
;
101 /* can we use c-> wait for this, assuming it will
102 * re-enter RUNNING? For now just sleep */
103 int elapsed_time
, curtime
= 0;
106 newpid
= c
->init_pid(c
);
107 if (newpid
!= -1 && newpid
!= pid
)
110 ret
= gettimeofday(&tv
, NULL
);
116 ret
= gettimeofday(&tv
, NULL
);
119 elapsed_time
= tv
.tv_sec
- curtime
;
120 if (timeout
- elapsed_time
<= 0)
122 timeout
-= elapsed_time
;
126 newpid
= c
->init_pid(c
);
127 if (newpid
== -1 || newpid
== pid
) {
128 printf("Reboot did not complete before timeout\n");
134 int main(int argc
, char *argv
[])
136 struct lxc_container
*c
;
140 if (lxc_arguments_parse(&my_args
, argc
, argv
))
143 if (lxc_log_init(my_args
.name
, my_args
.log_file
, my_args
.log_priority
,
144 my_args
.progname
, my_args
.quiet
, my_args
.lxcpath
[0]))
147 /* shortcut - if locking is bogus, we should be able to kill
148 * containers at least */
150 return lxc_cmd_stop(my_args
.name
, my_args
.lxcpath
[0]);
152 c
= lxc_container_new(my_args
.name
, my_args
.lxcpath
[0]);
154 fprintf(stderr
, "Error opening container\n");
158 if (!c
->may_control(c
)) {
159 fprintf(stderr
, "Insufficent privileges to control %s\n", c
->name
);
163 if (!c
->is_running(c
)) {
164 fprintf(stderr
, "%s is not running\n", c
->name
);
169 if (my_args
.hardstop
) {
170 ret
= c
->stop(c
) ? 0 : 1;
173 if (my_args
.reboot
) {
174 ret
= do_reboot_and_check(&my_args
, c
);
178 if (my_args
.shutdown
)
181 s
= c
->shutdown(c
, my_args
.timeout
);
183 if (!my_args
.shutdown
)
184 ret
= c
->wait(c
, "STOPPED", -1) ? 0 : 1;
191 lxc_container_put(c
);