]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/sysctls.c
oss-fuzz: adapt options to oss-fuzz build
[mirror_lxc.git] / src / tests / sysctls.c
CommitLineData
b8eb6ca7
CB
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "config.h"
4
5#include <stdio.h>
6#include <stdlib.h>
7
8#include "lxccontainer.h"
9#include "attach_options.h"
10
11#include "lxctest.h"
12#include "utils.h"
13
14#define CONTAINER_NAME "test-proc-sys"
15#define SYSCTL_PATH "/proc/sys/net/ipv4/ip_forward"
16#define SYSCTL_CONFIG_KEY "lxc.sysctl.net.ipv4.ip_forward"
17#define SYSCTL_CONFIG_VALUE "1"
18
19static int check_sysctls(void *payload)
20{
21 __do_close int fd = -EBADF;
22 char buf[INTTYPE_TO_STRLEN(__u64)];
23 ssize_t ret;
24
25 fd = open(SYSCTL_PATH, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
26 if (fd < 0) {
27 lxc_error("Failed to open " SYSCTL_PATH);
28 return EXIT_FAILURE;
29 }
30
31 ret = lxc_read_nointr(fd, buf, sizeof(buf));
32 if (ret < 0 || (size_t)ret >= sizeof(buf)) {
33 lxc_error("Failed to read " SYSCTL_PATH);
34 return EXIT_FAILURE;
35 }
36
37 buf[ret] = '\0';
38 remove_trailing_newlines(buf);
39
40 if (!strequal(buf, SYSCTL_CONFIG_VALUE)) {
41 lxc_error("Unexpected value %s for " SYSCTL_PATH, buf);
42 return EXIT_FAILURE;
43 }
44
45 return EXIT_SUCCESS;
46}
47
48int main(int argc, char *argv[])
49{
50 int fd_log = -EBADF, fret = EXIT_FAILURE;
51 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
52 int ret;
53 pid_t pid;
54 struct lxc_container *c;
55 struct lxc_log log;
56 char template[sizeof(P_tmpdir "/" CONTAINER_NAME "_XXXXXX")];
57
58 if (!file_exists(SYSCTL_PATH)) {
59 lxc_debug("The sysctl path \"" SYSCTL_PATH "\" needed for this test does not exist. Skipping");
60 exit(EXIT_SUCCESS);
61 }
62
63 (void)strlcpy(template, P_tmpdir "/" CONTAINER_NAME "_XXXXXX", sizeof(template));
64
65 fd_log = lxc_make_tmpfile(template, false);
66 if (fd_log < 0) {
67 lxc_error("%s", "Failed to create temporary log file for container \"capabilities\"");
68 return fret;
69 }
70
71 log.name = CONTAINER_NAME;
72 log.file = template;
73 log.level = "TRACE";
74 log.prefix = CONTAINER_NAME;
75 log.quiet = false;
76 log.lxcpath = NULL;
77
78 if (lxc_log_init(&log))
79 exit(fret);
80
81 c = lxc_container_new(CONTAINER_NAME, NULL);
82 if (!c) {
83 lxc_error("%s", "Failed to create container " CONTAINER_NAME);
84 exit(fret);
85 }
86
87 if (c->is_defined(c)) {
88 lxc_error("%s\n", "Container " CONTAINER_NAME " is defined");
89 goto on_error_put;
90 }
91
92 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
93 lxc_error("%s\n", "Failed to create busybox container " CONTAINER_NAME);
94 goto on_error_put;
95 }
96
97 if (!c->is_defined(c)) {
98 lxc_error("%s\n", "Container " CONTAINER_NAME " is not defined");
99 goto on_error_destroy;
100 }
101
102 if (!c->set_config_item(c, "lxc.mount.auto", "proc:rw")) {
103 lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=proc:rw\"");
104 goto on_error_destroy;
105 }
106
107 if (!c->clear_config_item(c, SYSCTL_CONFIG_KEY)) {
108 lxc_error("%s\n", "Failed to clear config item \"" SYSCTL_CONFIG_KEY "\"");
109 goto on_error_destroy;
110 }
111
112 if (!c->set_config_item(c, SYSCTL_CONFIG_KEY, SYSCTL_CONFIG_VALUE)) {
113 lxc_error("%s\n", "Failed to set config item \"" SYSCTL_CONFIG_KEY "\"");
114 goto on_error_destroy;
115 }
116
117 if (!c->want_daemonize(c, true)) {
118 lxc_error("%s\n", "Failed to mark container " CONTAINER_NAME " daemonized");
119 goto on_error_destroy;
120 }
121
122 if (!c->startl(c, 0, NULL)) {
123 lxc_error("%s\n", "Failed to start container " CONTAINER_NAME " daemonized");
124 goto on_error_destroy;
125 }
126
127 /* Leave some time for the container to write something to the log. */
128 sleep(2);
129
130 ret = c->attach(c, check_sysctls, NULL, &attach_options, &pid);
131 if (ret < 0) {
132 lxc_error("%s\n", "Failed to run function in container " CONTAINER_NAME);
133 goto on_error_stop;
134 }
135
136 ret = wait_for_pid(pid);
137 if (ret < 0) {
138 lxc_error("%s\n", "Function "CONTAINER_NAME" failed");
139 goto on_error_stop;
140 }
141
142 fret = 0;
143
144on_error_stop:
145 if (c->is_running(c) && !c->stop(c))
146 lxc_error("%s\n", "Failed to stop container " CONTAINER_NAME);
147
148on_error_destroy:
149 if (!c->destroy(c))
150 lxc_error("%s\n", "Failed to destroy container " CONTAINER_NAME);
151
152on_error_put:
153 lxc_container_put(c);
154
155 if (fret == EXIT_SUCCESS) {
156 lxc_debug("All sysctl tests passed\n");
157 } else {
158 char buf[4096];
159 ssize_t buflen;
160
161 while ((buflen = read(fd_log, buf, 1024)) > 0) {
162 buflen = write(STDERR_FILENO, buf, buflen);
163 if (buflen <= 0)
164 break;
165 }
166 }
167 close_prot_errno_disarm(fd_log);
168 (void)unlink(template);
169
170 exit(fret);
171}