xref: /aosp_15_r20/external/ltp/testcases/lib/tst_ns_exec.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2015 Red Hat, Inc.
4  *               Matus Marhefka <[email protected]>
5  * Copyright (c) Linux Test Project, 2015-2023
6  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Enters the namespace(s) of a process specified by a PID and then executes
13  * the indicated program inside that namespace(s).
14  */
15 
16 #define TST_NO_DEFAULT_MAIN
17 
18 #include <stdio.h>
19 #include <sys/wait.h>
20 #include "tst_test.h"
21 #include "tst_ns_common.h"
22 
23 extern struct tst_test *tst_test;
24 
25 static struct tst_test test = {
26 	.forks_child = 1, /* Needed by SAFE_CLONE */
27 };
28 
29 static int ns_fd[NS_TOTAL];
30 static int ns_fds;
31 
print_help(void)32 static void print_help(void)
33 {
34 	int i;
35 
36 	printf("usage: tst_ns_exec <NS_PID> <%s", params[0].name);
37 
38 	for (i = 1; params[i].name; i++)
39 		printf("|,%s", params[i].name);
40 
41 	printf("> <PROGRAM> [ARGS]\nSecond argument indicates the types"
42 		" of a namespaces maintained by NS_PID\nand is specified"
43 		" as a comma separated list.\n"
44 		"Example: tst_ns_exec 1234 net,ipc ip a\n");
45 }
46 
open_ns_fd(const char * pid,const char * ns)47 static void open_ns_fd(const char *pid, const char *ns)
48 {
49 	int fd;
50 	char file_buf[64];
51 
52 	sprintf(file_buf, "%s/%s/ns/%s", PROC_PATH, pid, ns);
53 
54 	fd = SAFE_OPEN(file_buf, O_RDONLY);
55 	ns_fd[ns_fds] = fd;
56 
57 	++ns_fds;
58 }
59 
close_ns_fd(void)60 static void close_ns_fd(void)
61 {
62 	int i;
63 
64 	for (i = 0; i < ns_fds; i++)
65 		SAFE_CLOSE(ns_fd[i]);
66 }
67 
main(int argc,char * argv[])68 int main(int argc, char *argv[])
69 {
70 	struct tst_clone_args args = { .exit_signal = SIGCHLD };
71 	int i, status, pid;
72 	char *token;
73 
74 	tst_test = &test;
75 
76 	if (argc < 4) {
77 		print_help();
78 		return 1;
79 	}
80 
81 	memset(ns_fd, 0, sizeof(ns_fd));
82 
83 	while ((token = strsep(&argv[2], ","))) {
84 		struct param *p = get_param(token);
85 
86 		if (!p) {
87 			printf("Unknown namespace: %s\n", token);
88 			print_help();
89 			return 1;
90 		}
91 
92 		open_ns_fd(argv[1], token);
93 	}
94 
95 	if (!ns_fds) {
96 		printf("no namespace entries in /proc/%s/ns/\n", argv[1]);
97 		return 1;
98 	}
99 
100 	for (i = 0; i < ns_fds; i++)
101 		SAFE_SETNS(ns_fd[i], 0);
102 
103 	pid = SAFE_CLONE(&args);
104 	if (!pid)
105 		SAFE_EXECVP(argv[3], argv+3);
106 
107 	SAFE_WAITPID(pid, &status, 0);
108 
109 	close_ns_fd();
110 
111 	if (WIFEXITED(status))
112 		return WEXITSTATUS(status);
113 
114 	return 0;
115 }
116