]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/reboot.c
oss-fuzz: adapt options to oss-fuzz build
[mirror_lxc.git] / src / tests / reboot.c
CommitLineData
f7f1ba77 1/*
8cd80b50
SG
2 * Copyright © 2012 Serge Hallyn <serge.hallyn@ubuntu.com>.
3 * Copyright © 2012 Canonical Ltd.
2aa12318
SH
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 */
e49c56d6
CB
19
20#include "config.h"
21
2aa12318
SH
22#include <alloca.h>
23#include <stdio.h>
24#include <sched.h>
25#include <unistd.h>
26#include <signal.h>
13277ec4 27#include <errno.h>
28#include <string.h>
2aa12318
SH
29#include <sys/reboot.h>
30#include <sys/types.h>
31#include <sys/wait.h>
32
44c22b8a 33#include "namespace.h"
4012c891 34
2aa12318
SH
35#include <sched.h>
36#include <linux/sched.h>
37#include <linux/reboot.h>
38
f3cef1cb 39int clone(int (*fn)(void *), void *child_stack, int flags, void *arg, ...);
2aa12318
SH
40
41static int do_reboot(void *arg)
42{
d028235d 43 int *cmd = arg;
2aa12318 44
d028235d 45 if (reboot(*cmd))
13277ec4 46 printf("failed to reboot(%d): %s\n", *cmd, strerror(errno));
733666f8 47
2aa12318
SH
48 return 0;
49}
50
0b98289e 51static int test_reboot(int cmd, int sig)
2aa12318 52{
d028235d
SG
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) {
13277ec4 60 printf("failed to clone: %s\n", strerror(errno));
d028235d
SG
61 return -1;
62 }
63
64 if (wait(&status) < 0) {
13277ec4 65 printf("unexpected wait error: %s\n", strerror(errno));
d028235d
SG
66 return -1;
67 }
68
69 if (!WIFSIGNALED(status)) {
2aa12318
SH
70 if (sig != -1)
71 printf("child process exited but was not signaled\n");
733666f8 72
d028235d
SG
73 return -1;
74 }
2aa12318 75
d028235d
SG
76 if (WTERMSIG(status) != sig) {
77 printf("signal termination is not the one expected\n");
78 return -1;
79 }
2aa12318 80
d028235d 81 return 0;
2aa12318
SH
82}
83
84static 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;
733666f8 97
2aa12318
SH
98 ret = reboot(v ? LINUX_REBOOT_CMD_CAD_ON : LINUX_REBOOT_CMD_CAD_OFF);
99 if (ret != -1)
100 return 0;
733666f8 101
2aa12318
SH
102 return 1;
103}
104
105int main(int argc, char *argv[])
106{
d028235d 107 int status;
2aa12318
SH
108
109 if (getuid() != 0) {
d028235d
SG
110 printf("Must run as root.\n");
111 return 1;
2aa12318
SH
112 }
113
114 status = have_reboot_patch();
115 if (status != 0) {
d028235d
SG
116 printf("Your kernel does not have the container reboot patch\n");
117 return 1;
2aa12318
SH
118 }
119
d028235d
SG
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;
2aa12318 149}