1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2014-2020
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #define NS_MAX 5
10*49cdfc7eSAndroid Build Coastguard Worker static int ns_types[NS_MAX];
11*49cdfc7eSAndroid Build Coastguard Worker static int ns_fds[NS_MAX];
12*49cdfc7eSAndroid Build Coastguard Worker static int ns_total;
13*49cdfc7eSAndroid Build Coastguard Worker
get_ns_fd(int pid,const char * ns)14*49cdfc7eSAndroid Build Coastguard Worker static int get_ns_fd(int pid, const char *ns)
15*49cdfc7eSAndroid Build Coastguard Worker {
16*49cdfc7eSAndroid Build Coastguard Worker char tmp[PATH_MAX];
17*49cdfc7eSAndroid Build Coastguard Worker struct stat st;
18*49cdfc7eSAndroid Build Coastguard Worker int fd = -1;
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker sprintf(tmp, "/proc/%d/ns/%s", pid, ns);
21*49cdfc7eSAndroid Build Coastguard Worker if (stat(tmp, &st) == 0) {
22*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(tmp, O_RDONLY);
23*49cdfc7eSAndroid Build Coastguard Worker } else {
24*49cdfc7eSAndroid Build Coastguard Worker if (errno != ENOENT)
25*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK|TERRNO, "failed to stat %s", tmp);
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker return fd;
28*49cdfc7eSAndroid Build Coastguard Worker }
29*49cdfc7eSAndroid Build Coastguard Worker
init_ns_type(int clone_type,const char * proc_name)30*49cdfc7eSAndroid Build Coastguard Worker static void init_ns_type(int clone_type, const char *proc_name)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker int fd;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker fd = get_ns_fd(getpid(), proc_name);
35*49cdfc7eSAndroid Build Coastguard Worker if (fd != -1) {
36*49cdfc7eSAndroid Build Coastguard Worker ns_types[ns_total] = clone_type;
37*49cdfc7eSAndroid Build Coastguard Worker ns_fds[ns_total] = fd;
38*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "ns_name=%s, ns_fds[%d]=%d, ns_types[%d]=0x%x",
39*49cdfc7eSAndroid Build Coastguard Worker proc_name, ns_total, fd, ns_total, clone_type);
40*49cdfc7eSAndroid Build Coastguard Worker ns_total++;
41*49cdfc7eSAndroid Build Coastguard Worker }
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker
init_available_ns(void)44*49cdfc7eSAndroid Build Coastguard Worker static void init_available_ns(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker init_ns_type(CLONE_NEWIPC, "ipc");
47*49cdfc7eSAndroid Build Coastguard Worker init_ns_type(CLONE_NEWNS, "mnt");
48*49cdfc7eSAndroid Build Coastguard Worker init_ns_type(CLONE_NEWNET, "net");
49*49cdfc7eSAndroid Build Coastguard Worker init_ns_type(CLONE_NEWPID, "pid");
50*49cdfc7eSAndroid Build Coastguard Worker init_ns_type(CLONE_NEWUTS, "uts");
51*49cdfc7eSAndroid Build Coastguard Worker }
52*49cdfc7eSAndroid Build Coastguard Worker
close_ns_fds(void)53*49cdfc7eSAndroid Build Coastguard Worker static void close_ns_fds(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker int i;
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < ns_total; i++)
58*49cdfc7eSAndroid Build Coastguard Worker if (ns_fds[i] != -1)
59*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(ns_fds[i]);
60*49cdfc7eSAndroid Build Coastguard Worker }
61