lxc-test-utils.c revision 8b79a8fec4127c754509d65faae1fe7f09515365
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/*
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * lxc: linux Container library
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Copyright © 2016 Canonical Ltd.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Authors:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Christian Brauner <christian.brauner@mailbox.org>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This program is free software; you can redistribute it and/or
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher * modify it under the terms of the GNU Lesser General Public
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * License as published by the Free Software Foundation; either
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * version 2.1 of the License, or (at your option) any later version.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * This program is distributed in the hope that it will be useful,
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * but WITHOUT ANY WARRANTY; without even the implied warranty of
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Lesser General Public License for more details.
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher *
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * You should have received a copy of the GNU Lesser General Public
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * License along with this program; if not, write to the Free Software
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define _GNU_SOURCE
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define __STDC_FORMAT_MACROS
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <errno.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <fcntl.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <inttypes.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <limits.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sched.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <stdio.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <stdlib.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <string.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <unistd.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/mount.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/stat.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include <sys/types.h>
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "lxctest.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#include "utils.h"
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_deslashify(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *s = strdup("/A///B//C/D/E/");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!s)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(EXIT_FAILURE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_deslashify(&s));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "/A/B/C/D/E") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = strdup("/A");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!s)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(EXIT_FAILURE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_deslashify(&s));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "/A") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = strdup("");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!s)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(EXIT_FAILURE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_deslashify(&s));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = strdup("//");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!s)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(EXIT_FAILURE);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_deslashify(&s));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "/") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher/* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher#define __MNTNS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_detect_ramfs_rootfs(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher size_t i;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int ret;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int fret = EXIT_FAILURE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char path[__MNTNS_LEN];
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int init_ns = -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char tmpf1[] = "lxc-test-utils-XXXXXX";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char tmpf2[] = "lxc-test-utils-XXXXXX";
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher int fd1 = -1, fd2 = -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher FILE *fp1 = NULL, *fp2 = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *mountinfo[] = {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "18 24 0:17 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "19 24 0:4 / /proc rw,nosuid,nodev,noexec,relatime shared:13 - proc proc rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "20 24 0:6 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=4019884k,nr_inodes=1004971,mode=755",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "21 20 0:14 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "22 24 0:18 / /run rw,nosuid,noexec,relatime shared:5 - tmpfs tmpfs rw,size=807912k,mode=755",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* This is what we care about. */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "25 18 0:12 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "26 20 0:20 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "27 22 0:21 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "28 18 0:22 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "29 28 0:23 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "30 18 0:24 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:11 - pstore pstore rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "31 18 0:25 / /sys/firmware/efi/efivars rw,nosuid,nodev,noexec,relatime shared:12 - efivarfs efivarfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "32 28 0:26 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:14 - cgroup cgroup rw,cpu,cpuacct",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "33 28 0:27 / /sys/fs/cgroup/net_cls,net_prio rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,net_cls,net_prio",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "34 28 0:28 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,blkio",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "35 28 0:29 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,freezer",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "36 28 0:30 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,memory",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "37 28 0:31 / /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,hugetlb",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "38 28 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,cpuset",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "39 28 0:33 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:21 - cgroup cgroup rw,devices",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "40 28 0:34 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,pids",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "41 28 0:35 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:23 - cgroup cgroup rw,perf_event",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "42 19 0:36 / /proc/sys/fs/binfmt_misc rw,relatime shared:24 - autofs systemd-1 rw,fd=32,pgrp=1,timeout=0,minproto=5,maxproto=5,direct",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "43 18 0:7 / /sys/kernel/debug rw,relatime shared:25 - debugfs debugfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "44 20 0:37 / /dev/hugepages rw,relatime shared:26 - hugetlbfs hugetlbfs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "45 20 0:16 / /dev/mqueue rw,relatime shared:27 - mqueue mqueue rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "46 43 0:9 / /sys/kernel/debug/tracing rw,relatime shared:28 - tracefs tracefs rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "76 18 0:38 / /sys/fs/fuse/connections rw,relatime shared:29 - fusectl fusectl rw",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher "78 24 8:1 / /boot/efi rw,relatime shared:30 - vfat /dev/sda1 rw,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro",
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher };
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher ret = snprintf(path, __MNTNS_LEN, "/proc/self/ns/mnt");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (ret < 0 || (size_t)ret >= __MNTNS_LEN) {
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher lxc_error("%s\n", "Failed to create path with snprintf().");
3a8abe04137d028b8ebd1cb33152aefa55893efbStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher init_ns = open(path, O_RDONLY | O_CLOEXEC);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (init_ns < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Failed to open initial mount namespace.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (unshare(CLONE_NEWNS) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not unshare mount namespace.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(init_ns);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher init_ns = -1;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("Failed to remount / private: %s.\n", strerror(errno));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fd1 = mkstemp(tmpf1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fd1 < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not create temporary file.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fd2 = mkstemp(tmpf2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fd2 < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not create temporary file.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fp1 = fdopen(fd1, "r+");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!fp1) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not fdopen() temporary file.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fp2 = fdopen(fd2, "r+");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (!fp2) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not fdopen() temporary file.");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Test if it correctly detects - rootfs rootfs */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fprintf(fp1, "%s\n", mountinfo[i]) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fclose(fp1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fp1 = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher /* Test if it correctly fails to detect when no - rootfs rootfs */
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (strcmp(mountinfo[i], "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099") == 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher continue;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fprintf(fp2, "%s\n", mountinfo[i]) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fclose(fp2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fp2 = NULL;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (mount(tmpf1, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(detect_ramfs_rootfs());
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (mount(tmpf2, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher goto non_test_error;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(!detect_ramfs_rootfs());
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fret = EXIT_SUCCESS;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghernon_test_error:
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fp1)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fclose(fp1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (fd1 > 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(fd1);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fp2)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fclose(fp2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher else if (fd2 > 0)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(fd2);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (init_ns > 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (setns(init_ns, 0) < 0) {
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher fret = EXIT_FAILURE;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher close(init_ns);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher }
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher if (fret == EXIT_SUCCESS)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher return;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(fret);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_safe_uint(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher unsigned int n;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_uint("1234345", &n)) && n == 1234345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_uint(" 345", &n)) && n == 345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_safe_int(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher signed int n;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_int(" 345", &n)) && n == 345);
bdd205037059e56484de3174951b22ff8f0f79f8Stephen Gallagher lxc_test_assert_abort((0 == lxc_safe_int("-1234345", &n)) && n == -1234345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_int(" -345", &n)) && n == -345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int("g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_safe_long(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher signed long int n;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_long("1234345", &n)) && n == 1234345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_long(" 345", &n)) && n == 345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_long("-1234345", &n)) && n == -1234345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((0 == lxc_safe_long(" -345", &n)) && n == -345);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45 ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g ", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long("g", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_string_replace(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher char *s;
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("A", "A", "A");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "A") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("A", "AA", "A");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "AA") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("A", "AA", "BA");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "BAA") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("A", "AA", "BAB");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "BAAB") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("AA", "A", "AA");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "A") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("AA", "A", "BAA");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "BA") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("AA", "A", "BAAB");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "BAB") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher s = lxc_string_replace("\"A\"A", "\"A\"", "B\"A\"AB");
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(strcmp(s, "B\"A\"B") == 0);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher free(s);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallaghervoid test_lxc_string_in_array(void)
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("", (const char *[]){"", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(!lxc_string_in_array("A", (const char *[]){"", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(!lxc_string_in_array("AAA", (const char *[]){"", "3472", "jshH", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", "B", "C", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"B", "A", "C", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("ABC", (const char *[]){"ASD", "ATR", "ABC", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("GHJ", (const char *[]){"AZIU", "WRT567B", "879C", "GHJ", "IUZ89", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher lxc_test_assert_abort(lxc_string_in_array("XYZ", (const char *[]){"BERTA", "ARQWE(9", "C8Zhkd", "7U", "XYZ", "UOIZ9", "=)()", NULL}));
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagherint main(int argc, char *argv[])
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher{
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_string_replace();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_string_in_array();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_deslashify();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_detect_ramfs_rootfs();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_safe_uint();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_safe_int();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher test_lxc_safe_long();
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher exit(EXIT_SUCCESS);
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher}
52261fe16203dec6e6f69177c6d0a810b47d073fStephen Gallagher