]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/rootfs_options.c
src/tests: Fix container creation errors
[mirror_lxc.git] / src / tests / rootfs_options.c
CommitLineData
587b2dff
CB
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
587b2dff
CB
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
cdb4f412
CB
33#include "lxccontainer.h"
34#include "attach_options.h"
587b2dff
CB
35
36#ifdef HAVE_STATVFS
37#include <sys/statvfs.h>
38#endif
39
40#include "lxctest.h"
41#include "utils.h"
42
43static int has_mount_properties(const char *path, unsigned int flags)
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 if ((sb.f_flag & flags) == flags)
54 return 0;
55
56 return -EINVAL;
57
58#else
59 return -EOPNOTSUPP;
60#endif
61}
62
63static int rootfs_options(void *payload)
64{
65 int ret;
66
67 ret = has_mount_properties("/",
68 MS_NODEV |
69 MS_NOSUID |
70 MS_RDONLY);
71 if (ret != 0) {
72 if (ret == -EOPNOTSUPP)
73 return EXIT_SUCCESS;
74
75 return EXIT_FAILURE;
76 }
77
78 return EXIT_SUCCESS;
79}
80
81int main(int argc, char *argv[])
82{
83 int fret = EXIT_FAILURE;
84 lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
85 int ret;
86 pid_t pid;
87 struct lxc_container *c;
88
89 c = lxc_container_new("rootfs-options", NULL);
90 if (!c) {
91 lxc_error("%s", "Failed to create container \"rootfs-options\"");
92 exit(fret);
93 }
94
95 if (c->is_defined(c)) {
96 lxc_error("%s\n", "Container \"rootfs-options\" is defined");
97 goto on_error_put;
98 }
99
100 if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
101 lxc_error("%s\n", "Failed to create busybox container \"rootfs-options\"");
102 goto on_error_put;
103 }
104
105 if (!c->is_defined(c)) {
106 lxc_error("%s\n", "Container \"rootfs-options\" is not defined");
107 goto on_error_put;
108 }
109
110 c->clear_config(c);
111
112 if (!c->set_config_item(c, "lxc.rootfs.options", "nodev,nosuid,ro")) {
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->load_config(c, NULL)) {
118 lxc_error("%s\n", "Failed to load config for container \"rootfs-options\"");
119 goto on_error_stop;
120 }
121
122 if (!c->want_daemonize(c, true)) {
123 lxc_error("%s\n", "Failed to mark container \"rootfs-options\" daemonized");
124 goto on_error_stop;
125 }
126
127 if (!c->startl(c, 0, NULL)) {
128 lxc_error("%s\n", "Failed to start container \"rootfs-options\" daemonized");
129 goto on_error_stop;
130 }
131
132 /* Leave some time for the container to write something to the log. */
133 sleep(2);
134
135 ret = c->attach(c, rootfs_options, NULL, &attach_options, &pid);
136 if (ret < 0) {
137 lxc_error("%s\n", "Failed to run function in container \"rootfs-options\"");
138 goto on_error_stop;
139 }
140
141 ret = wait_for_pid(pid);
142 if (ret < 0) {
143 lxc_error("%s\n", "Function \"rootfs-options\" failed");
144 goto on_error_stop;
145 }
146
147 fret = 0;
148
149on_error_stop:
150 if (c->is_running(c) && !c->stop(c))
151 lxc_error("%s\n", "Failed to stop container \"rootfs-options\"");
152
153 if (!c->destroy(c))
154 lxc_error("%s\n", "Failed to destroy container \"rootfs-options\"");
155
156on_error_put:
157 lxc_container_put(c);
158 exit(fret);
159}