]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/api_reboot.c
github: Update for main branch
[mirror_lxc.git] / src / tests / api_reboot.c
CommitLineData
c02c49ee
CB
1/* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 *
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.
8 *
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.
13 *
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.
17 */
18
e49c56d6
CB
19#include "config.h"
20
c02c49ee
CB
21#include <alloca.h>
22#include <stdio.h>
23#include <sched.h>
24#include <unistd.h>
25#include <signal.h>
26#include <errno.h>
27#include <string.h>
28#include <sys/reboot.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31
32#include "lxc/lxccontainer.h"
33#include "lxctest.h"
3919322d
CB
34#include "utils.h"
35
34498dea 36#if !HAVE_STRLCPY
58db1a61 37#include "strlcpy.h"
3919322d
CB
38#endif
39
40#define TSTNAME "lxc-api-reboot"
c02c49ee
CB
41
42int main(int argc, char *argv[])
43{
44 int i;
45 struct lxc_container *c;
46 int ret = EXIT_FAILURE;
3919322d
CB
47 struct lxc_log log;
48 char template[sizeof(P_tmpdir"/reboot_XXXXXX")];
49
50 (void)strlcpy(template, P_tmpdir"/reboot_XXXXXX", sizeof(template));
51
52 i = lxc_make_tmpfile(template, false);
53 if (i < 0) {
54 lxc_error("Failed to create temporary log file for container %s\n", TSTNAME);
55 exit(EXIT_FAILURE);
56 } else {
57 lxc_debug("Using \"%s\" as temporary log file for container %s\n", template, TSTNAME);
58 close(i);
59 }
60
61 log.name = TSTNAME;
62 log.file = template;
63 log.level = "TRACE";
64 log.prefix = "reboot";
65 log.quiet = false;
66 log.lxcpath = NULL;
67 if (lxc_log_init(&log))
68 exit(ret);
c02c49ee
CB
69
70 /* Test that the reboot() API function properly waits for containers to
71 * restart.
72 */
3919322d 73 c = lxc_container_new(TSTNAME, NULL);
c02c49ee
CB
74 if (!c) {
75 lxc_error("%s", "Failed to create container \"reboot\"");
76 exit(ret);
77 }
78
79 if (c->is_defined(c)) {
80 lxc_error("%s\n", "Container \"reboot\" is defined");
81 goto on_error_put;
82 }
83
84 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
85 lxc_error("%s\n", "Failed to create busybox container \"reboot\"");
86 goto on_error_put;
87 }
88
89 if (!c->is_defined(c)) {
90 lxc_error("%s\n", "Container \"reboot\" is not defined");
91 goto on_error_put;
92 }
2e338007 93
c02c49ee
CB
94 c->clear_config(c);
95
96 if (!c->load_config(c, NULL)) {
97 lxc_error("%s\n", "Failed to load config for container \"reboot\"");
98 goto on_error_stop;
99 }
100
101 if (!c->want_daemonize(c, true)) {
102 lxc_error("%s\n", "Failed to mark container \"reboot\" daemonized");
103 goto on_error_stop;
104 }
105
106 if (!c->startl(c, 0, NULL)) {
107 lxc_error("%s\n", "Failed to start container \"reboot\" daemonized");
108 goto on_error_stop;
109 }
110
111 /* reboot 10 times */
112 for (i = 0; i < 10; i++) {
113 /* Give the init system some time to setup it's signal handlers
4f6c7312 114 * otherwise we will wait indefinitely.
c02c49ee
CB
115 */
116 sleep(5);
117
fb2ac067 118 if (!c->reboot2(c, 60 * 5)) {
c02c49ee
CB
119 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
120 goto on_error_stop;
121 }
122
123 if (!c->is_running(c)) {
124 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
125 goto on_error_stop;
126 }
127 lxc_debug("%s\n", "Container \"reboot\" rebooted successfully");
128 }
129
130 /* Give the init system some time to setup it's signal handlers
4f6c7312 131 * otherwise we will wait indefinitely.
c02c49ee
CB
132 */
133 sleep(5);
134
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\"");
138 goto on_error_stop;
139 }
140 lxc_debug("%s\n", "Non-blocking reboot of container \"reboot\" succeeded");
141
142 ret = EXIT_SUCCESS;
143
144on_error_stop:
145 if (c->is_running(c) && !c->stop(c))
146 lxc_error("%s\n", "Failed to stop container \"reboot\"");
147
148 if (!c->destroy(c))
149 lxc_error("%s\n", "Failed to destroy container \"reboot\"");
150
151on_error_put:
152 lxc_container_put(c);
2e338007 153
3919322d 154 if (ret == EXIT_SUCCESS) {
c02c49ee 155 lxc_debug("%s\n", "All reboot tests passed");
3919322d
CB
156 } else {
157 int fd;
158
159 fd = open(template, O_RDONLY);
160 if (fd >= 0) {
161 char buf[4096];
162 ssize_t buflen;
163 while ((buflen = read(fd, buf, 1024)) > 0) {
164 buflen = write(STDERR_FILENO, buf, buflen);
165 if (buflen <= 0)
166 break;
167 }
168 close(fd);
169 }
170 }
171 (void)unlink(template);
2e338007 172
c02c49ee
CB
173 exit(ret);
174}