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