]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/sys_mixed.c
meson: Remove non-existent tests
[mirror_lxc.git] / src / tests / sys_mixed.c
1 /* liblxcapi
2 *
3 * Copyright © 2021 Christian Brauner <christian.brauner@ubuntu.com>.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "config.h"
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <signal.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32
33 #include "lxccontainer.h"
34 #include "attach_options.h"
35
36 #ifdef HAVE_STATVFS
37 #include <sys/statvfs.h>
38 #endif
39
40 #include "lxctest.h"
41 #include "utils.h"
42
43 static int is_read_only(const char *path)
44 {
45 #ifdef HAVE_STATVFS
46 int ret;
47 struct statvfs sb;
48
49 ret = statvfs(path, &sb);
50 if (ret < 0)
51 return -errno;
52
53 return (sb.f_flag & MS_RDONLY) > 0;
54 #else
55 return -EOPNOTSUPP;
56 #endif
57 }
58
59 static int sys_mixed(void *payload)
60 {
61 int ret;
62
63 ret = is_read_only("/sys");
64 if (ret == -EOPNOTSUPP)
65 return 0;
66
67 if (ret <= 0)
68 return -1;
69
70 if (is_read_only("/sys/devices/virtual/net"))
71 return -1;
72
73 return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78 int fret = EXIT_FAILURE;
79 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
80 int ret;
81 pid_t pid;
82 struct lxc_container *c;
83
84 c = lxc_container_new("sys-mixed", NULL);
85 if (!c) {
86 lxc_error("%s", "Failed to create container \"sys-mixed\"");
87 exit(fret);
88 }
89
90 if (c->is_defined(c)) {
91 lxc_error("%s\n", "Container \"sys-mixed\" is defined");
92 goto on_error_put;
93 }
94
95 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
96 lxc_error("%s\n", "Failed to create busybox container \"sys-mixed\"");
97 goto on_error_put;
98 }
99
100 if (!c->is_defined(c)) {
101 lxc_error("%s\n", "Container \"sys-mixed\" is not defined");
102 goto on_error_put;
103 }
104
105 c->clear_config(c);
106
107 if (!c->load_config(c, NULL)) {
108 lxc_error("%s\n", "Failed to load config for container \"sys-mixed\"");
109 goto on_error_stop;
110 }
111
112 if (!c->set_config_item(c, "lxc.mount.auto", "sys:mixed")) {
113 lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=sys:mixed\"");
114 goto on_error_put;
115 }
116
117 if (!c->want_daemonize(c, true)) {
118 lxc_error("%s\n", "Failed to mark container \"sys-mixed\" daemonized");
119 goto on_error_stop;
120 }
121
122 if (!c->startl(c, 0, NULL)) {
123 lxc_error("%s\n", "Failed to start container \"sys-mixed\" daemonized");
124 goto on_error_stop;
125 }
126
127 /* Leave some time for the container to write something to the log. */
128 sleep(2);
129
130 ret = c->attach(c, sys_mixed, NULL, &attach_options, &pid);
131 if (ret < 0) {
132 lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
133 goto on_error_stop;
134 }
135
136 ret = wait_for_pid(pid);
137 if (ret < 0) {
138 lxc_error("%s\n", "Failed to run function in container \"sys-mixed\"");
139 goto on_error_stop;
140 }
141
142 fret = 0;
143
144 on_error_stop:
145 if (c->is_running(c) && !c->stop(c))
146 lxc_error("%s\n", "Failed to stop container \"sys-mixed\"");
147
148 if (!c->destroy(c))
149 lxc_error("%s\n", "Failed to destroy container \"sys-mixed\"");
150
151 on_error_put:
152 lxc_container_put(c);
153 exit(fret);
154 }