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