]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/reboot.c
tests: lxc-test-reboot: Fix build on ia64
[mirror_lxc.git] / src / tests / reboot.c
1 /*
2 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
3 * Copyright © 2012 Canonical Ltd.
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 */
19
20 #include "config.h"
21
22 #include <alloca.h>
23 #include <stdio.h>
24 #include <sched.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <sys/reboot.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32
33 #include "namespace.h"
34
35 #include <linux/reboot.h>
36
37 /*
38 * glibc clone(2) wrapper function prototypes as defined in that manpage. Most
39 * architectures use clone(...), but ia64 uses __clone2(...).
40 */
41 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...);
42 int __clone2(int (*fn)(void *), void *stack_base, size_t stack_size, \
43 int flags, void *arg, ...);
44
45 static int do_reboot(void *arg)
46 {
47 int *cmd = arg;
48
49 if (reboot(*cmd))
50 printf("failed to reboot(%d): %s\n", *cmd, strerror(errno));
51
52 return 0;
53 }
54
55 static int test_reboot(int cmd, int sig)
56 {
57 long stack_size = 4096;
58 void *stack = alloca(stack_size) + stack_size;
59 int status;
60 pid_t ret;
61
62 #if defined(__ia64__)
63 ret = __clone2(do_reboot, stack, stack_size, \
64 CLONE_NEWPID | SIGCHLD, &cmd);
65 #else
66 ret = clone(do_reboot, stack, CLONE_NEWPID | SIGCHLD, &cmd);
67 #endif
68 if (ret < 0) {
69 printf("failed to clone: %s\n", strerror(errno));
70 return -1;
71 }
72
73 if (wait(&status) < 0) {
74 printf("unexpected wait error: %s\n", strerror(errno));
75 return -1;
76 }
77
78 if (!WIFSIGNALED(status)) {
79 if (sig != -1)
80 printf("child process exited but was not signaled\n");
81
82 return -1;
83 }
84
85 if (WTERMSIG(status) != sig) {
86 printf("signal termination is not the one expected\n");
87 return -1;
88 }
89
90 return 0;
91 }
92
93 static 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;
106
107 ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
108 if (ret != -1)
109 return 0;
110
111 return 1;
112 }
113
114 int main(int argc, char *argv[])
115 {
116 int status;
117
118 if (getuid() != 0) {
119 printf("Must run as root.\n");
120 return 1;
121 }
122
123 status = have_reboot_patch();
124 if (status != 0) {
125 printf("Your kernel does not have the container reboot patch\n");
126 return 1;
127 }
128
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;
158 }