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