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