test-namespace.c revision 613b411c947635136637f8cdd66b94512f761eab
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering/***
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering This file is part of systemd.
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering systemd is free software; you can redistribute it and/or modify it
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering under the terms of the GNU Lesser General Public License as published by
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering (at your option) any later version.
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering systemd is distributed in the hope that it will be useful, but
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering Lesser General Public License for more details.
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering You should have received a copy of the GNU Lesser General Public License
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering***/
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <libgen.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/socket.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering#include "namespace.h"
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering#include "util.h"
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poetteringstatic void test_tmpdir(const char *id, const char *A, const char *B) {
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering _cleanup_free_ char *a, *b;
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering struct stat x, y;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering char *c, *d;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek
4f5dd3943bef8a04be7e3b838b822bb9a7ad6cb3Lennart Poettering assert_se(setup_tmp_dirs(id, &a, &b) == 0);
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering assert_se(startswith(a, A));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(startswith(b, B));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(stat(a, &x) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(stat(b, &y) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(S_ISDIR(x.st_mode));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(S_ISDIR(y.st_mode));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se((x.st_mode & 01777) == 0700);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se((y.st_mode & 01777) == 0700);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering c = strappenda(a, "/tmp");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering d = strappenda(b, "/tmp");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(stat(c, &x) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(stat(d, &y) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(S_ISDIR(x.st_mode));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(S_ISDIR(y.st_mode));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se((x.st_mode & 01777) == 01777);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se((y.st_mode & 01777) == 01777);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(rmdir(c) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(rmdir(d) >= 0);
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(rmdir(a) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(rmdir(b) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering}
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poetteringstatic void test_netns(void) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering _cleanup_close_pipe_ int s[2] = { -1, -1 };
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering pid_t pid1, pid2, pid3;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering int r, n = 0;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering siginfo_t si;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (geteuid() > 0)
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
39883f622f392d8579f4428fc5a789a102efbb10Lennart Poettering assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering pid1 = fork();
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek assert_se(pid1 >= 0);
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (pid1 == 0) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = setup_netns(s);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering _exit(r);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering }
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering pid2 = fork();
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(pid2 >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (pid2 == 0) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = setup_netns(s);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering exit(r);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering }
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering pid3 = fork();
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(pid3 >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (pid3 == 0) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = setup_netns(s);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering exit(r);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering }
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = wait_for_terminate(pid1, &si);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(si.si_code == CLD_EXITED);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering n += si.si_status;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = wait_for_terminate(pid2, &si);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(si.si_code == CLD_EXITED);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering n += si.si_status;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = wait_for_terminate(pid3, &si);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(r >= 0);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(si.si_code == CLD_EXITED);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering n += si.si_status;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering assert_se(n == 1);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering}
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poetteringint main(int argc, char *argv[]) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering test_tmpdir("abcd.service",
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "/tmp/systemd-abcd.service-",
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "/var/tmp/systemd-abcd.service-");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device",
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "/tmp/systemd-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-",
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "/var/tmp/systemd-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering test_netns();
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return 0;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering}
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering