xref: /dpdk/app/test/process.h (revision 2d0c29a37a9c080c1cccb1ad7941aba2ccf5437e)
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 <limits.h> /* PATH_MAX */
9 #include <libgen.h> /* basename et al */
10 #include <stdlib.h> /* NULL */
11 #include <unistd.h> /* readlink */
12 #include <sys/wait.h>
13 
14 #ifdef RTE_EXEC_ENV_FREEBSD
15 #define self "curproc"
16 #define exe "file"
17 #else
18 #define self "self"
19 #define exe "exe"
20 #endif
21 
22 #ifdef RTE_LIBRTE_PDUMP
23 #include <pthread.h>
24 extern void *send_pkts(void *empty);
25 extern uint16_t flag_for_send_pkts;
26 #endif
27 
28 /*
29  * launches a second copy of the test process using the given argv parameters,
30  * which should include argv[0] as the process name. To identify in the
31  * subprocess the source of the call, the env_value parameter is set in the
32  * environment as $RTE_TEST
33  */
34 static inline int
35 process_dup(const char *const argv[], int numargs, const char *env_value)
36 {
37 	int num;
38 	char *argv_cpy[numargs + 1];
39 	int i, fd, status;
40 	char path[32];
41 #ifdef RTE_LIBRTE_PDUMP
42 	pthread_t thread;
43 #endif
44 
45 	pid_t pid = fork();
46 	if (pid < 0)
47 		return -1;
48 	else if (pid == 0) {
49 		/* make a copy of the arguments to be passed to exec */
50 		for (i = 0; i < numargs; i++)
51 			argv_cpy[i] = strdup(argv[i]);
52 		argv_cpy[i] = NULL;
53 		num = numargs;
54 
55 		/* close all open file descriptors, check /proc/self/fd to only
56 		 * call close on open fds. Exclude fds 0, 1 and 2*/
57 		for (fd = getdtablesize(); fd > 2; fd-- ) {
58 			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
59 			if (access(path, F_OK) == 0)
60 				close(fd);
61 		}
62 		printf("Running binary with argv[]:");
63 		for (i = 0; i < num; i++)
64 			printf("'%s' ", argv_cpy[i]);
65 		printf("\n");
66 
67 		/* set the environment variable */
68 		if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
69 			rte_panic("Cannot export environment variable\n");
70 		if (execv("/proc/" self "/" exe, argv_cpy) < 0)
71 			rte_panic("Cannot exec\n");
72 	}
73 	/* parent process does a wait */
74 #ifdef RTE_LIBRTE_PDUMP
75 	if ((strcmp(env_value, "run_pdump_server_tests") == 0))
76 		pthread_create(&thread, NULL, &send_pkts, NULL);
77 #endif
78 
79 	while (wait(&status) != pid)
80 		;
81 #ifdef RTE_LIBRTE_PDUMP
82 	if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
83 		flag_for_send_pkts = 0;
84 		pthread_join(thread, NULL);
85 	}
86 #endif
87 	return status;
88 }
89 
90 /* FreeBSD doesn't support file prefixes, so force compile failures for any
91  * tests attempting to use this function on FreeBSD.
92  */
93 #ifdef RTE_EXEC_ENV_LINUX
94 static char *
95 get_current_prefix(char *prefix, int size)
96 {
97 	char path[PATH_MAX] = {0};
98 	char buf[PATH_MAX] = {0};
99 
100 	/* get file for config (fd is always 3) */
101 	snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
102 
103 	/* return NULL on error */
104 	if (readlink(path, buf, sizeof(buf)) == -1)
105 		return NULL;
106 
107 	/* get the prefix */
108 	snprintf(prefix, size, "%s", basename(dirname(buf)));
109 
110 	return prefix;
111 }
112 #endif
113 
114 #endif /* _PROCESS_H_ */
115