]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/reboot.c
src/tests: Fix container creation errors
[mirror_lxc.git] / src / tests / reboot.c
CommitLineData
f7f1ba77 1/*
8cd80b50
SG
2 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
3 * Copyright © 2012 Canonical Ltd.
2aa12318
SH
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
e49c56d6
CB
19
20#include "config.h"
21
2aa12318
SH
22#include <alloca.h>
23#include <stdio.h>
24#include <sched.h>
25#include <unistd.h>
26#include <signal.h>
13277ec4 27#include <errno.h>
28#include <string.h>
2aa12318
SH
29#include <sys/reboot.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
44c22b8a 33#include "namespace.h"
4012c891 34
2aa12318
SH
35#include <linux/reboot.h>
36
16c6ff22
MG
37/*
38 * glibc clone(2) wrapper function prototypes as defined in that manpage. Most
39 * architectures use clone(...), but ia64 uses __clone2(...).
40 */
f3cef1cb 41int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...);
16c6ff22
MG
42int __clone2(int (*fn)(void *), void *stack_base, size_t stack_size, \
43 int flags, void *arg, ...);
2aa12318
SH
44
45static int do_reboot(void *arg)
46{
d028235d 47 int *cmd = arg;
2aa12318 48
d028235d 49 if (reboot(*cmd))
13277ec4 50 printf("failed to reboot(%d): %s\n", *cmd, strerror(errno));
733666f8 51
2aa12318
SH
52 return 0;
53}
54
0b98289e 55static int test_reboot(int cmd, int sig)
2aa12318 56{
d028235d
SG
57 long stack_size = 4096;
58 void *stack = alloca(stack_size) + stack_size;
59 int status;
60 pid_t ret;
61
16c6ff22
MG
62#if defined(__ia64__)
63 ret = __clone2(do_reboot, stack, stack_size, \
64 CLONE_NEWPID | SIGCHLD, &cmd);
65#else
d028235d 66 ret = clone(do_reboot, stack, CLONE_NEWPID | SIGCHLD, &cmd);
16c6ff22 67#endif
d028235d 68 if (ret < 0) {
13277ec4 69 printf("failed to clone: %s\n", strerror(errno));
d028235d
SG
70 return -1;
71 }
72
73 if (wait(&status) < 0) {
13277ec4 74 printf("unexpected wait error: %s\n", strerror(errno));
d028235d
SG
75 return -1;
76 }
77
78 if (!WIFSIGNALED(status)) {
2aa12318
SH
79 if (sig != -1)
80 printf("child process exited but was not signaled\n");
733666f8 81
d028235d
SG
82 return -1;
83 }
2aa12318 84
d028235d
SG
85 if (WTERMSIG(status) != sig) {
86 printf("signal termination is not the one expected\n");
87 return -1;
88 }
2aa12318 89
d028235d 90 return 0;
2aa12318
SH
91}
92
93static int have_reboot_patch(void)
94{
95 FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
96 int ret;
97 int v;
98
99 if (!f)
100 return 0;
101
102 ret = fscanf(f, "%d", &v);
103 fclose(f);
104 if (ret != 1)
105 return 0;
733666f8 106
2aa12318
SH
107 ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
108 if (ret != -1)
109 return 0;
733666f8 110
2aa12318
SH
111 return 1;
112}
113
114int main(int argc, char *argv[])
115{
d028235d 116 int status;
2aa12318
SH
117
118 if (getuid() != 0) {
d028235d
SG
119 printf("Must run as root.\n");
120 return 1;
2aa12318
SH
121 }
122
123 status = have_reboot_patch();
124 if (status != 0) {
d028235d
SG
125 printf("Your kernel does not have the container reboot patch\n");
126 return 1;
2aa12318
SH
127 }
128
d028235d
SG
129 status = test_reboot(LINUX_REBOOT_CMD_CAD_ON, -1);
130 if (status >= 0) {
131 printf("reboot(LINUX_REBOOT_CMD_CAD_ON) should have failed\n");
132 return 1;
133 }
134 printf("reboot(LINUX_REBOOT_CMD_CAD_ON) has failed as expected\n");
135
136 status = test_reboot(LINUX_REBOOT_CMD_RESTART, SIGHUP);
137 if (status < 0)
138 return 1;
139 printf("reboot(LINUX_REBOOT_CMD_RESTART) succeed\n");
140
141 status = test_reboot(LINUX_REBOOT_CMD_RESTART2, SIGHUP);
142 if (status < 0)
143 return 1;
144 printf("reboot(LINUX_REBOOT_CMD_RESTART2) succeed\n");
145
146 status = test_reboot(LINUX_REBOOT_CMD_HALT, SIGINT);
147 if (status < 0)
148 return 1;
149 printf("reboot(LINUX_REBOOT_CMD_HALT) succeed\n");
150
151 status = test_reboot(LINUX_REBOOT_CMD_POWER_OFF, SIGINT);
152 if (status < 0)
153 return 1;
154 printf("reboot(LINUX_REBOOT_CMD_POWERR_OFF) succeed\n");
155
156 printf("All tests passed\n");
157 return 0;
2aa12318 158}