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