1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Red Hat, Inc. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Clone a process with CLONE_NEWPID flag and check if procfs mounted folder
11*49cdfc7eSAndroid Build Coastguard Worker * belongs to the new pid namespace by looking at /proc/self .
12*49cdfc7eSAndroid Build Coastguard Worker */
13*49cdfc7eSAndroid Build Coastguard Worker
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mount.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker #define PROCDIR "proc"
19*49cdfc7eSAndroid Build Coastguard Worker
child_func(void)20*49cdfc7eSAndroid Build Coastguard Worker static void child_func(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker char proc_self[10];
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker SAFE_MOUNT("none", PROCDIR, "proc", MS_RDONLY, NULL);
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker SAFE_READLINK(PROCDIR"/self", proc_self, sizeof(proc_self) - 1);
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker SAFE_UMOUNT(PROCDIR);
29*49cdfc7eSAndroid Build Coastguard Worker
30*49cdfc7eSAndroid Build Coastguard Worker TST_EXP_PASS(strcmp(proc_self, "1"), PROCDIR"/self contains 1:");
31*49cdfc7eSAndroid Build Coastguard Worker }
32*49cdfc7eSAndroid Build Coastguard Worker
setup(void)33*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR(PROCDIR, 0555);
36*49cdfc7eSAndroid Build Coastguard Worker }
37*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)38*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker if (tst_is_mounted_at_tmpdir(PROCDIR))
41*49cdfc7eSAndroid Build Coastguard Worker SAFE_UMOUNT(PROCDIR);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
run(void)44*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker const struct tst_clone_args args = {
47*49cdfc7eSAndroid Build Coastguard Worker .flags = CLONE_NEWPID,
48*49cdfc7eSAndroid Build Coastguard Worker .exit_signal = SIGCHLD,
49*49cdfc7eSAndroid Build Coastguard Worker };
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if (!SAFE_CLONE(&args)) {
52*49cdfc7eSAndroid Build Coastguard Worker child_func();
53*49cdfc7eSAndroid Build Coastguard Worker return;
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
58*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
59*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
60*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
61*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
62*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
63*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
64*49cdfc7eSAndroid Build Coastguard Worker .needs_kconfigs = (const char *[]) {
65*49cdfc7eSAndroid Build Coastguard Worker "CONFIG_PID_NS",
66*49cdfc7eSAndroid Build Coastguard Worker NULL,
67*49cdfc7eSAndroid Build Coastguard Worker },
68*49cdfc7eSAndroid Build Coastguard Worker };
69