1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <limits.h> /* PATH_MAX */
9 #include <libgen.h> /* basename et al */
10 #include <stdlib.h> /* NULL */
11 #include <unistd.h> /* readlink */
14 #ifdef RTE_EXEC_ENV_FREEBSD
15 #define self "curproc"
22 #ifdef RTE_LIBRTE_PDUMP
24 extern void *send_pkts(void *empty);
25 extern uint16_t flag_for_send_pkts;
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
35 process_dup(const char *const argv[], int numargs, const char *env_value)
38 char *argv_cpy[numargs + 1];
41 #ifdef RTE_LIBRTE_PDUMP
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]);
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)
62 printf("Running binary with argv[]:");
63 for (i = 0; i < num; i++)
64 printf("'%s' ", argv_cpy[i]);
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");
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);
79 while (wait(&status) != pid)
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);
90 /* FreeBSD doesn't support file prefixes, so force compile failures for any
91 * tests attempting to use this function on FreeBSD.
93 #ifdef RTE_EXEC_ENV_LINUX
95 get_current_prefix(char *prefix, int size)
97 char path[PATH_MAX] = {0};
98 char buf[PATH_MAX] = {0};
100 /* get file for config (fd is always 3) */
101 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
103 /* return NULL on error */
104 if (readlink(path, buf, sizeof(buf)) == -1)
108 snprintf(prefix, size, "%s", basename(dirname(buf)));
114 #endif /* _PROCESS_H_ */