d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek/***
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek This file is part of systemd.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek Copyright 2013 Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek (at your option) any later version.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek***/
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering#include <sys/socket.h>
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#include "fd-util.h"
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek#include "namespace.h"
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier#include "process-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "util.h"
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmekstatic void test_tmpdir(const char *id, const char *A, const char *B) {
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *a, *b;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering struct stat x, y;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering char *c, *d;
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(setup_tmp_dirs(id, &a, &b) == 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(startswith(a, A));
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(startswith(b, B));
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(stat(a, &x) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(stat(b, &y) >= 0);
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(S_ISDIR(x.st_mode));
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(S_ISDIR(y.st_mode));
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se((x.st_mode & 01777) == 0700);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se((y.st_mode & 01777) == 0700);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering c = strjoina(a, "/tmp");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering d = strjoina(b, "/tmp");
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(stat(c, &x) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(stat(d, &y) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(S_ISDIR(x.st_mode));
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(S_ISDIR(y.st_mode));
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se((x.st_mode & 01777) == 01777);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se((y.st_mode & 01777) == 01777);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(rmdir(c) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(rmdir(d) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(rmdir(a) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(rmdir(b) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering}
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poetteringstatic void test_netns(void) {
3d94f76c99da13e5603831d0b278f8c8c21bcb02Lennart Poettering _cleanup_close_pair_ int s[2] = { -1, -1 };
613b411c947635136637f8cdd66b94512f761eabLennart Poettering pid_t pid1, pid2, pid3;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering int r, n = 0;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering siginfo_t si;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering if (geteuid() > 0)
613b411c947635136637f8cdd66b94512f761eabLennart Poettering return;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering pid1 = fork();
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(pid1 >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering if (pid1 == 0) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = setup_netns(s);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering _exit(r);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering }
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering pid2 = fork();
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(pid2 >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering if (pid2 == 0) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = setup_netns(s);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering exit(r);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering }
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering pid3 = fork();
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(pid3 >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering if (pid3 == 0) {
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = setup_netns(s);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering exit(r);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering }
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = wait_for_terminate(pid1, &si);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(si.si_code == CLD_EXITED);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering n += si.si_status;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = wait_for_terminate(pid2, &si);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(si.si_code == CLD_EXITED);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering n += si.si_status;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering r = wait_for_terminate(pid3, &si);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(r >= 0);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(si.si_code == CLD_EXITED);
613b411c947635136637f8cdd66b94512f761eabLennart Poettering n += si.si_status;
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
613b411c947635136637f8cdd66b94512f761eabLennart Poettering assert_se(n == 1);
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek}
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering sd_id128_t bid;
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering char boot_id[SD_ID128_STRING_MAX];
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering assert_se(sd_id128_get_boot(&bid) >= 0);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering sd_id128_to_string(bid, boot_id);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering assert_se(x && y);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering test_tmpdir("abcd.service", x, y);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering z = strjoin("/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-", NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering zz = strjoin("/var/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-", NULL);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering assert_se(z && zz);
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering
6b46ea73e3b1d8a1e65f58ac04772821bd4a72fbLennart Poettering test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek
613b411c947635136637f8cdd66b94512f761eabLennart Poettering test_netns();
613b411c947635136637f8cdd66b94512f761eabLennart Poettering
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek return 0;
d8c9d3a468e61ee2a2b2c3454e662398b0885411Zbigniew Jędrzejewski-Szmek}