]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/api_reboot.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/reboot.h>
29 #include <sys/types.h>
32 #include "lxc/lxccontainer.h"
40 #define TSTNAME "lxc-api-reboot"
42 int main(int argc
, char *argv
[])
45 struct lxc_container
*c
;
46 int ret
= EXIT_FAILURE
;
48 char template[sizeof(P_tmpdir
"/reboot_XXXXXX")];
50 (void)strlcpy(template, P_tmpdir
"/reboot_XXXXXX", sizeof(template));
52 i
= lxc_make_tmpfile(template, false);
54 lxc_error("Failed to create temporary log file for container %s\n", TSTNAME
);
57 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, TSTNAME
);
64 log
.prefix
= "reboot";
67 if (lxc_log_init(&log
))
70 /* Test that the reboot() API function properly waits for containers to
73 c
= lxc_container_new(TSTNAME
, NULL
);
75 lxc_error("%s", "Failed to create container \"reboot\"");
79 if (c
->is_defined(c
)) {
80 lxc_error("%s\n", "Container \"reboot\" is defined");
84 if (!c
->createl(c
, "busybox", NULL
, NULL
, 0, NULL
)) {
85 lxc_error("%s\n", "Failed to create busybox container \"reboot\"");
89 if (!c
->is_defined(c
)) {
90 lxc_error("%s\n", "Container \"reboot\" is not defined");
96 if (!c
->load_config(c
, NULL
)) {
97 lxc_error("%s\n", "Failed to load config for container \"reboot\"");
101 if (!c
->want_daemonize(c
, true)) {
102 lxc_error("%s\n", "Failed to mark container \"reboot\" daemonized");
106 if (!c
->startl(c
, 0, NULL
)) {
107 lxc_error("%s\n", "Failed to start container \"reboot\" daemonized");
111 /* reboot 10 times */
112 for (i
= 0; i
< 10; i
++) {
113 /* Give the init system some time to setup it's signal handlers
114 * otherwise we will wait indefinitely.
118 if (!c
->reboot2(c
, 60 * 5)) {
119 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
123 if (!c
->is_running(c
)) {
124 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
127 lxc_debug("%s\n", "Container \"reboot\" rebooted successfully");
130 /* Give the init system some time to setup it's signal handlers
131 * otherwise we will wait indefinitely.
135 /* Test non-blocking reboot2() */
136 if (!c
->reboot2(c
, 0)) {
137 lxc_error("%s\n", "Failed to request non-blocking reboot of container \"reboot\"");
140 lxc_debug("%s\n", "Non-blocking reboot of container \"reboot\" succeeded");
145 if (c
->is_running(c
) && !c
->stop(c
))
146 lxc_error("%s\n", "Failed to stop container \"reboot\"");
149 lxc_error("%s\n", "Failed to destroy container \"reboot\"");
152 lxc_container_put(c
);
154 if (ret
== EXIT_SUCCESS
) {
155 lxc_debug("%s\n", "All reboot tests passed");
159 fd
= open(template, O_RDONLY
);
163 while ((buflen
= read(fd
, buf
, 1024)) > 0) {
164 buflen
= write(STDERR_FILENO
, buf
, buflen
);
171 (void)unlink(template);