xref: /dpdk/app/test/process.h (revision e9fd1ebf981f361844aea9ec94e17f4bda5e1479)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #ifndef _PROCESS_H_
6 #define _PROCESS_H_
7 
8 #include <errno.h>  /* errno */
9 #include <limits.h> /* PATH_MAX */
10 #ifndef RTE_EXEC_ENV_WINDOWS
11 #include <libgen.h> /* basename et al */
12 #include <sys/wait.h>
13 #endif
14 #include <stdlib.h> /* NULL */
15 #include <string.h> /* strerror */
16 #include <unistd.h> /* readlink */
17 #include <dirent.h>
18 
19 #include <rte_string_fns.h> /* strlcpy */
20 
21 #ifdef RTE_EXEC_ENV_FREEBSD
22 #define self "curproc"
23 #define exe "file"
24 #else
25 #define self "self"
26 #define exe "exe"
27 #endif
28 
29 #ifdef RTE_LIB_PDUMP
30 #ifdef RTE_NET_RING
31 #include <rte_thread.h>
32 extern uint32_t send_pkts(void *empty);
33 extern uint16_t flag_for_send_pkts;
34 #endif
35 #endif
36 
37 /*
38  * launches a second copy of the test process using the given argv parameters,
39  * which should include argv[0] as the process name. To identify in the
40  * subprocess the source of the call, the env_value parameter is set in the
41  * environment as $RTE_TEST
42  */
43 static inline int
44 process_dup(const char *const argv[], int numargs, const char *env_value)
45 {
46 	int num;
47 	char *argv_cpy[numargs + 1];
48 	int i, status;
49 	char path[32];
50 #ifdef RTE_LIB_PDUMP
51 #ifdef RTE_NET_RING
52 	rte_thread_t thread;
53 	int rc;
54 #endif
55 #endif
56 
57 	pid_t pid = fork();
58 	if (pid < 0)
59 		return -1;
60 	else if (pid == 0) {
61 		/* make a copy of the arguments to be passed to exec */
62 		for (i = 0; i < numargs; i++) {
63 			argv_cpy[i] = strdup(argv[i]);
64 			if (argv_cpy[i] == NULL)
65 				rte_panic("Error dup args\n");
66 		}
67 		argv_cpy[i] = NULL;
68 		num = numargs;
69 
70 #ifdef RTE_EXEC_ENV_LINUX
71 		{
72 			const char *procdir = "/proc/" self "/fd/";
73 			struct dirent *dirent;
74 			char *endptr;
75 			int fd, fdir;
76 			DIR *dir;
77 
78 			/* close all open file descriptors, check /proc/self/fd
79 			 * to only call close on open fds. Exclude fds 0, 1 and
80 			 * 2
81 			 */
82 			dir = opendir(procdir);
83 			if (dir == NULL) {
84 				rte_panic("Error opening %s: %s\n", procdir,
85 						strerror(errno));
86 			}
87 
88 			fdir = dirfd(dir);
89 			if (fdir < 0) {
90 				status = errno;
91 				closedir(dir);
92 				rte_panic("Error %d obtaining fd for dir %s: %s\n",
93 						fdir, procdir,
94 						strerror(status));
95 			}
96 
97 			while ((dirent = readdir(dir)) != NULL) {
98 
99 				if (strcmp(dirent->d_name, ".") == 0 ||
100 					strcmp(dirent->d_name, "..") == 0)
101 					continue;
102 
103 				errno = 0;
104 				fd = strtol(dirent->d_name, &endptr, 10);
105 				if (errno != 0 || endptr[0] != '\0') {
106 					printf("Error converting name fd %d %s:\n",
107 						fd, dirent->d_name);
108 					continue;
109 				}
110 
111 				if (fd == fdir || fd <= 2)
112 					continue;
113 
114 				close(fd);
115 			}
116 			closedir(dir);
117 		}
118 #endif
119 		printf("Running binary with argv[]:");
120 		for (i = 0; i < num; i++)
121 			printf("'%s' ", argv_cpy[i]);
122 		printf("\n");
123 		fflush(stdout);
124 
125 		/* set the environment variable */
126 		if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
127 			rte_panic("Cannot export environment variable\n");
128 
129 		strlcpy(path, "/proc/" self "/" exe, sizeof(path));
130 		if (execv(path, argv_cpy) < 0) {
131 			if (errno == ENOENT) {
132 				printf("Could not find '%s', is procfs mounted?\n",
133 						path);
134 			}
135 			rte_panic("Cannot exec: %s\n", strerror(errno));
136 		}
137 	}
138 	/* parent process does a wait */
139 #ifdef RTE_LIB_PDUMP
140 #ifdef RTE_NET_RING
141 	if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
142 		rc = rte_thread_create(&thread, NULL, send_pkts, NULL);
143 		if (rc != 0) {
144 			rte_panic("Cannot start send pkts thread: %s\n",
145 				  strerror(rc));
146 		}
147 	}
148 #endif
149 #endif
150 
151 	while (wait(&status) != pid)
152 		;
153 #ifdef RTE_LIB_PDUMP
154 #ifdef RTE_NET_RING
155 	if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
156 		flag_for_send_pkts = 0;
157 		rte_thread_join(thread, NULL);
158 	}
159 #endif
160 #endif
161 	return status;
162 }
163 
164 /* FreeBSD doesn't support file prefixes, so force compile failures for any
165  * tests attempting to use this function on FreeBSD.
166  */
167 #ifdef RTE_EXEC_ENV_LINUX
168 static char *
169 get_current_prefix(char *prefix, int size)
170 {
171 	char path[PATH_MAX] = {0};
172 	char buf[PATH_MAX] = {0};
173 
174 	/* get file for config (fd is always 3) */
175 	snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
176 
177 	/* return NULL on error */
178 	if (readlink(path, buf, sizeof(buf)) == -1)
179 		return NULL;
180 
181 	/* get the prefix */
182 	snprintf(prefix, size, "%s", basename(dirname(buf)));
183 
184 	return prefix;
185 }
186 #endif
187 
188 #endif /* _PROCESS_H_ */
189