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