]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/api_reboot.c
spelling: userns
[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
19#include <alloca.h>
20#include <stdio.h>
21#include <sched.h>
22#include <unistd.h>
23#include <signal.h>
24#include <errno.h>
25#include <string.h>
26#include <sys/reboot.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29
30#include "lxc/lxccontainer.h"
31#include "lxctest.h"
32
33int main(int argc, char *argv[])
34{
35 int i;
36 struct lxc_container *c;
37 int ret = EXIT_FAILURE;
38
39 /* Test that the reboot() API function properly waits for containers to
40 * restart.
41 */
42 c = lxc_container_new("reboot", NULL);
43 if (!c) {
44 lxc_error("%s", "Failed to create container \"reboot\"");
45 exit(ret);
46 }
47
48 if (c->is_defined(c)) {
49 lxc_error("%s\n", "Container \"reboot\" is defined");
50 goto on_error_put;
51 }
52
53 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
54 lxc_error("%s\n", "Failed to create busybox container \"reboot\"");
55 goto on_error_put;
56 }
57
58 if (!c->is_defined(c)) {
59 lxc_error("%s\n", "Container \"reboot\" is not defined");
60 goto on_error_put;
61 }
2e338007 62
c02c49ee
CB
63 c->clear_config(c);
64
65 if (!c->load_config(c, NULL)) {
66 lxc_error("%s\n", "Failed to load config for container \"reboot\"");
67 goto on_error_stop;
68 }
69
70 if (!c->want_daemonize(c, true)) {
71 lxc_error("%s\n", "Failed to mark container \"reboot\" daemonized");
72 goto on_error_stop;
73 }
74
75 if (!c->startl(c, 0, NULL)) {
76 lxc_error("%s\n", "Failed to start container \"reboot\" daemonized");
77 goto on_error_stop;
78 }
79
80 /* reboot 10 times */
81 for (i = 0; i < 10; i++) {
82 /* Give the init system some time to setup it's signal handlers
83 * otherwise we will hang indefinitely.
84 */
85 sleep(5);
86
87 if (!c->reboot2(c, -1)) {
88 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
89 goto on_error_stop;
90 }
91
92 if (!c->is_running(c)) {
93 lxc_error("%s\n", "Failed to reboot container \"reboot\"");
94 goto on_error_stop;
95 }
96 lxc_debug("%s\n", "Container \"reboot\" rebooted successfully");
97 }
98
99 /* Give the init system some time to setup it's signal handlers
100 * otherwise we will hang indefinitely.
101 */
102 sleep(5);
103
104 /* Test non-blocking reboot2() */
105 if (!c->reboot2(c, 0)) {
106 lxc_error("%s\n", "Failed to request non-blocking reboot of container \"reboot\"");
107 goto on_error_stop;
108 }
109 lxc_debug("%s\n", "Non-blocking reboot of container \"reboot\" succeeded");
110
111 ret = EXIT_SUCCESS;
112
113on_error_stop:
114 if (c->is_running(c) && !c->stop(c))
115 lxc_error("%s\n", "Failed to stop container \"reboot\"");
116
117 if (!c->destroy(c))
118 lxc_error("%s\n", "Failed to destroy container \"reboot\"");
119
120on_error_put:
121 lxc_container_put(c);
2e338007 122
c02c49ee
CB
123 if (ret == EXIT_SUCCESS)
124 lxc_debug("%s\n", "All reboot tests passed");
2e338007 125
c02c49ee
CB
126 exit(ret);
127}