Skip to content

Commit

Permalink
tests: add lxc.proc.* test
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Nov 4, 2021
1 parent b8eb6ca commit 20acdbb
Show file tree
Hide file tree
Showing 3 changed files with 254 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -97,6 +97,7 @@ src/tests/lxc-test-utils*
src/tests/lxc-usernic-test
src/tests/lxc-test-config-jump-table
src/tests/lxc-test-parse-config-file
src/tests/lxc-test-proc-pid
src/tests/lxc-test-shortlived
src/tests/lxc-test-api-reboot
src/tests/lxc-test-criu-check-feature
Expand Down
83 changes: 83 additions & 0 deletions src/tests/Makefile.am
Expand Up @@ -1438,6 +1438,87 @@ lxc_test_sysctls_SOURCES += ../include/prlimit.c ../include/prlimit.h
endif
endif

lxc_test_proc_pid_SOURCES = proc_pid.c \
../lxc/af_unix.c ../lxc/af_unix.h \
../lxc/caps.c ../lxc/caps.h \
../lxc/cgroups/cgfsng.c \
../lxc/cgroups/cgroup.c ../lxc/cgroups/cgroup.h \
../lxc/cgroups/cgroup2_devices.c ../lxc/cgroups/cgroup2_devices.h \
../lxc/cgroups/cgroup_utils.c ../lxc/cgroups/cgroup_utils.h \
../lxc/commands.c ../lxc/commands.h \
../lxc/commands_utils.c ../lxc/commands_utils.h \
../lxc/conf.c ../lxc/conf.h \
../lxc/confile.c ../lxc/confile.h \
../lxc/confile_utils.c ../lxc/confile_utils.h \
../lxc/error.c ../lxc/error.h \
../lxc/file_utils.c ../lxc/file_utils.h \
../include/netns_ifaddrs.c ../include/netns_ifaddrs.h \
../lxc/initutils.c ../lxc/initutils.h \
../lxc/log.c ../lxc/log.h \
../lxc/lxclock.c ../lxc/lxclock.h \
../lxc/mainloop.c ../lxc/mainloop.h \
../lxc/monitor.c ../lxc/monitor.h \
../lxc/mount_utils.c ../lxc/mount_utils.h \
../lxc/namespace.c ../lxc/namespace.h \
../lxc/network.c ../lxc/network.h \
../lxc/nl.c ../lxc/nl.h \
../lxc/parse.c ../lxc/parse.h \
../lxc/process_utils.c ../lxc/process_utils.h \
../lxc/ringbuf.c ../lxc/ringbuf.h \
../lxc/start.c ../lxc/start.h \
../lxc/state.c ../lxc/state.h \
../lxc/storage/btrfs.c ../lxc/storage/btrfs.h \
../lxc/storage/dir.c ../lxc/storage/dir.h \
../lxc/storage/loop.c ../lxc/storage/loop.h \
../lxc/storage/lvm.c ../lxc/storage/lvm.h \
../lxc/storage/nbd.c ../lxc/storage/nbd.h \
../lxc/storage/overlay.c ../lxc/storage/overlay.h \
../lxc/storage/rbd.c ../lxc/storage/rbd.h \
../lxc/storage/rsync.c ../lxc/storage/rsync.h \
../lxc/storage/storage.c ../lxc/storage/storage.h \
../lxc/storage/storage_utils.c ../lxc/storage/storage_utils.h \
../lxc/storage/zfs.c ../lxc/storage/zfs.h \
../lxc/sync.c ../lxc/sync.h \
../lxc/string_utils.c ../lxc/string_utils.h \
../lxc/terminal.c ../lxc/terminal.h \
../lxc/utils.c ../lxc/utils.h \
../lxc/uuid.c ../lxc/uuid.h \
$(LSM_SOURCES)
if ENABLE_SECCOMP
lxc_test_proc_pid_SOURCES += ../lxc/seccomp.c ../lxc/lxcseccomp.h
endif

if !HAVE_STRCHRNUL
lxc_test_proc_pid_SOURCES += ../include/strchrnul.c ../include/strchrnul.h
endif

if !HAVE_STRLCPY
lxc_test_proc_pid_SOURCES += ../include/strlcpy.c ../include/strlcpy.h
endif

if !HAVE_STRLCAT
lxc_test_proc_pid_SOURCES += ../include/strlcat.c ../include/strlcat.h
endif

if !HAVE_OPENPTY
lxc_test_proc_pid_SOURCES += ../include/openpty.c ../include/openpty.h
endif

if IS_BIONIC
lxc_test_proc_pid_SOURCES += ../include/fexecve.c ../include/fexecve.h \
../include/lxcmntent.c ../include/lxcmntent.h
endif

if !HAVE_GETGRGID_R
lxc_test_proc_pid_SOURCES += ../include/getgrgid_r.c ../include/getgrgid_r.h
endif

if !HAVE_PRLIMIT
if HAVE_PRLIMIT64
lxc_test_proc_pid_SOURCES += ../include/prlimit.c ../include/prlimit.h
endif
endif

AM_CFLAGS += -DLXCROOTFSMOUNT=\"$(LXCROOTFSMOUNT)\" \
-DLXCPATH=\"$(LXCPATH)\" \
-DLXC_GLOBAL_CONF=\"$(LXC_GLOBAL_CONF)\" \
Expand Down Expand Up @@ -1497,6 +1578,7 @@ bin_PROGRAMS = lxc-test-api-reboot \
lxc-test-may-control \
lxc-test-mount-injection \
lxc-test-parse-config-file \
lxc-test-proc-pid \
lxc-test-raw-clone \
lxc-test-reboot \
lxc-test-rootfs-options \
Expand Down Expand Up @@ -1609,6 +1691,7 @@ EXTRA_DIST = arch_parse.c \
may_control.c \
mount_injection.c \
parse_config_file.c \
proc_pid.c \
rootfs_options.c \
saveconfig.c \
shortlived.c \
Expand Down
170 changes: 170 additions & 0 deletions src/tests/proc_pid.c
@@ -0,0 +1,170 @@
/* SPDX-License-Identifier: LGPL-2.1+ */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include "lxccontainer.h"
#include "attach_options.h"

#include "lxctest.h"
#include "utils.h"

#define CONTAINER_NAME "test-proc-pid"
#define PROC_INIT_PATH "/proc/1/oom_score_adj"
#define PROC_SELF_PATH "/proc/self/oom_score_adj"

static int check_oom_score_adj(void *payload)
{
__do_close int fd = -EBADF;
char buf[INTTYPE_TO_STRLEN(__s64)];
ssize_t ret;

fd = open(PROC_INIT_PATH, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (fd < 0) {
lxc_error("Failed to open " PROC_INIT_PATH);
return EXIT_FAILURE;
}

ret = lxc_read_nointr(fd, buf, sizeof(buf));
if (ret < 0 || (size_t)ret >= sizeof(buf)) {
lxc_error("Failed to read " PROC_INIT_PATH);
return EXIT_FAILURE;
}

buf[ret] = '\0';
remove_trailing_newlines(buf);

if (!strequal(buf, "-1000")) {
lxc_error("Unexpected value %s for " PROC_INIT_PATH, buf);
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
int fd_log = -EBADF, fret = EXIT_FAILURE;
lxc_attach_options_t attach_options = LXC_ATTACH_OPTIONS_DEFAULT;
int ret;
pid_t pid;
struct lxc_container *c;
struct lxc_log log;
char template[sizeof(P_tmpdir "/" CONTAINER_NAME "_XXXXXX")];

if (!file_exists(PROC_SELF_PATH)) {
lxc_debug("The sysctl path \"" PROC_SELF_PATH "\" needed for this test does not exist. Skipping");
exit(EXIT_SUCCESS);
}

(void)strlcpy(template, P_tmpdir "/" CONTAINER_NAME "_XXXXXX", sizeof(template));

fd_log = lxc_make_tmpfile(template, false);
if (fd_log < 0) {
lxc_error("%s", "Failed to create temporary log file for container \"capabilities\"");
return fret;
}

log.name = CONTAINER_NAME;
log.file = template;
log.level = "TRACE";
log.prefix = CONTAINER_NAME;
log.quiet = false;
log.lxcpath = NULL;

if (lxc_log_init(&log))
exit(fret);

c = lxc_container_new(CONTAINER_NAME, NULL);
if (!c) {
lxc_error("%s", "Failed to create container " CONTAINER_NAME);
exit(fret);
}

if (c->is_defined(c)) {
lxc_error("%s\n", "Container " CONTAINER_NAME " is defined");
goto on_error_put;
}

if (!c->createl(c, "busybox", NULL, NULL, 0, NULL)) {
lxc_error("%s\n", "Failed to create busybox container " CONTAINER_NAME);
goto on_error_put;
}

if (!c->is_defined(c)) {
lxc_error("%s\n", "Container " CONTAINER_NAME " is not defined");
goto on_error_destroy;
}

if (!c->set_config_item(c, "lxc.mount.auto", "proc:rw")) {
lxc_error("%s\n", "Failed to set config item \"lxc.mount.auto=proc:rw\"");
goto on_error_destroy;
}

if (!c->clear_config_item(c, "lxc.proc.oom_score_adj")) {
lxc_error("%s\n", "Failed to clear config item \"lxc.proc.oom_score_adj\"");
goto on_error_destroy;
}

if (!c->set_config_item(c, "lxc.proc.oom_score_adj", "-1000")) {
lxc_error("%s\n", "Failed to set config item \"lxc.proc.oom_score_adj=-1000\"");
goto on_error_destroy;
}

if (!c->want_daemonize(c, true)) {
lxc_error("%s\n", "Failed to mark container " CONTAINER_NAME " daemonized");
goto on_error_destroy;
}

if (!c->startl(c, 0, NULL)) {
lxc_error("%s\n", "Failed to start container " CONTAINER_NAME " daemonized");
goto on_error_destroy;
}

/* Leave some time for the container to write something to the log. */
sleep(2);

ret = c->attach(c, check_oom_score_adj, NULL, &attach_options, &pid);
if (ret < 0) {
lxc_error("%s\n", "Failed to run function in container " CONTAINER_NAME);
goto on_error_stop;
}

ret = wait_for_pid(pid);
if (ret < 0) {
lxc_error("%s\n", "Function "CONTAINER_NAME" failed");
goto on_error_stop;
}

fret = 0;

on_error_stop:
if (c->is_running(c) && !c->stop(c))
lxc_error("%s\n", "Failed to stop container " CONTAINER_NAME);

on_error_destroy:
if (!c->destroy(c))
lxc_error("%s\n", "Failed to destroy container " CONTAINER_NAME);

on_error_put:
lxc_container_put(c);

if (fret == EXIT_SUCCESS) {
lxc_debug("All \"/proc/<pid>\" tests passed\n");
} else {
char buf[4096];
ssize_t buflen;

while ((buflen = read(fd_log, buf, 1024)) > 0) {
buflen = write(STDERR_FILENO, buf, buflen);
if (buflen <= 0)
break;
}
}
close_prot_errno_disarm(fd_log);
(void)unlink(template);

exit(fret);
}

0 comments on commit 20acdbb

Please sign in to comment.