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_BSDAPP
15 #define self "curproc"
23 extern void *send_pkts(void *empty);
24 extern uint16_t flag_for_send_pkts;
27 * launches a second copy of the test process using the given argv parameters,
28 * which should include argv[0] as the process name. To identify in the
29 * subprocess the source of the call, the env_value parameter is set in the
30 * environment as $RTE_TEST
33 process_dup(const char *const argv[], int numargs, const char *env_value)
36 char *argv_cpy[numargs + 1];
45 /* make a copy of the arguments to be passed to exec */
46 for (i = 0; i < numargs; i++)
47 argv_cpy[i] = strdup(argv[i]);
51 /* close all open file descriptors, check /proc/self/fd to only
52 * call close on open fds. Exclude fds 0, 1 and 2*/
53 for (fd = getdtablesize(); fd > 2; fd-- ) {
54 snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
55 if (access(path, F_OK) == 0)
58 printf("Running binary with argv[]:");
59 for (i = 0; i < num; i++)
60 printf("'%s' ", argv_cpy[i]);
63 /* set the environment variable */
64 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
65 rte_panic("Cannot export environment variable\n");
66 if (execv("/proc/" self "/" exe, argv_cpy) < 0)
67 rte_panic("Cannot exec\n");
69 /* parent process does a wait */
70 if ((strcmp(env_value, "run_pdump_server_tests") == 0))
71 pthread_create(&thread, NULL, &send_pkts, NULL);
73 while (wait(&status) != pid)
75 if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
76 flag_for_send_pkts = 0;
77 pthread_join(thread, NULL);
82 /* FreeBSD doesn't support file prefixes, so force compile failures for any
83 * tests attempting to use this function on FreeBSD.
85 #ifdef RTE_EXEC_ENV_LINUXAPP
87 get_current_prefix(char *prefix, int size)
89 char path[PATH_MAX] = {0};
90 char buf[PATH_MAX] = {0};
92 /* get file for config (fd is always 3) */
93 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
95 /* return NULL on error */
96 if (readlink(path, buf, sizeof(buf)) == -1)
100 snprintf(prefix, size, "%s", basename(dirname(buf)));
106 #endif /* _PROCESS_H_ */