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