]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/reboot.c
tests: include config.h
[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 "lxc/namespace.h"
34
35 #include <sched.h>
36 #include <linux/sched.h>
37 #include <linux/reboot.h>
38
39 int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...);
40
41 static int do_reboot(void *arg)
42 {
43 int *cmd = arg;
44
45 if (reboot(*cmd))
46 printf("failed to reboot(%d): %s\n", *cmd, strerror(errno));
47
48 return 0;
49 }
50
51 static int test_reboot(int cmd, int sig)
52 {
53 long stack_size = 4096;
54 void *stack = alloca(stack_size) + stack_size;
55 int status;
56 pid_t ret;
57
58 ret = clone(do_reboot, stack, CLONE_NEWPID | SIGCHLD, &cmd);
59 if (ret < 0) {
60 printf("failed to clone: %s\n", strerror(errno));
61 return -1;
62 }
63
64 if (wait(&status) < 0) {
65 printf("unexpected wait error: %s\n", strerror(errno));
66 return -1;
67 }
68
69 if (!WIFSIGNALED(status)) {
70 if (sig != -1)
71 printf("child process exited but was not signaled\n");
72
73 return -1;
74 }
75
76 if (WTERMSIG(status) != sig) {
77 printf("signal termination is not the one expected\n");
78 return -1;
79 }
80
81 return 0;
82 }
83
84 static int have_reboot_patch(void)
85 {
86 FILE *f = fopen("/proc/sys/kernel/ctrl-alt-del", "r");
87 int ret;
88 int v;
89
90 if (!f)
91 return 0;
92
93 ret = fscanf(f, "%d", &v);
94 fclose(f);
95 if (ret != 1)
96 return 0;
97
98 ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
99 if (ret != -1)
100 return 0;
101
102 return 1;
103 }
104
105 int main(int argc, char *argv[])
106 {
107 int status;
108
109 if (getuid() != 0) {
110 printf("Must run as root.\n");
111 return 1;
112 }
113
114 status = have_reboot_patch();
115 if (status != 0) {
116 printf("Your kernel does not have the container reboot patch\n");
117 return 1;
118 }
119
120 status = test_reboot(LINUX_REBOOT_CMD_CAD_ON, -1);
121 if (status >= 0) {
122 printf("reboot(LINUX_REBOOT_CMD_CAD_ON) should have failed\n");
123 return 1;
124 }
125 printf("reboot(LINUX_REBOOT_CMD_CAD_ON) has failed as expected\n");
126
127 status = test_reboot(LINUX_REBOOT_CMD_RESTART, SIGHUP);
128 if (status < 0)
129 return 1;
130 printf("reboot(LINUX_REBOOT_CMD_RESTART) succeed\n");
131
132 status = test_reboot(LINUX_REBOOT_CMD_RESTART2, SIGHUP);
133 if (status < 0)
134 return 1;
135 printf("reboot(LINUX_REBOOT_CMD_RESTART2) succeed\n");
136
137 status = test_reboot(LINUX_REBOOT_CMD_HALT, SIGINT);
138 if (status < 0)
139 return 1;
140 printf("reboot(LINUX_REBOOT_CMD_HALT) succeed\n");
141
142 status = test_reboot(LINUX_REBOOT_CMD_POWER_OFF, SIGINT);
143 if (status < 0)
144 return 1;
145 printf("reboot(LINUX_REBOOT_CMD_POWERR_OFF) succeed\n");
146
147 printf("All tests passed\n");
148 return 0;
149 }