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 */
13 #ifdef RTE_EXEC_ENV_BSDAPP
14 #define self "curproc"
22 * launches a second copy of the test process using the given argv parameters,
23 * which should include argv[0] as the process name. To identify in the
24 * subprocess the source of the call, the env_value parameter is set in the
25 * environment as $RTE_TEST
28 process_dup(const char *const argv[], int numargs, const char *env_value)
31 char *argv_cpy[numargs + 1];
39 /* make a copy of the arguments to be passed to exec */
40 for (i = 0; i < numargs; i++)
41 argv_cpy[i] = strdup(argv[i]);
45 /* close all open file descriptors, check /proc/self/fd to only
46 * call close on open fds. Exclude fds 0, 1 and 2*/
47 for (fd = getdtablesize(); fd > 2; fd-- ) {
48 snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
49 if (access(path, F_OK) == 0)
52 printf("Running binary with argv[]:");
53 for (i = 0; i < num; i++)
54 printf("'%s' ", argv_cpy[i]);
57 /* set the environment variable */
58 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
59 rte_panic("Cannot export environment variable\n");
60 if (execv("/proc/" self "/" exe, argv_cpy) < 0)
61 rte_panic("Cannot exec\n");
63 /* parent process does a wait */
64 while (wait(&status) != pid)
69 /* FreeBSD doesn't support file prefixes, so force compile failures for any
70 * tests attempting to use this function on FreeBSD.
72 #ifdef RTE_EXEC_ENV_LINUXAPP
74 get_current_prefix(char *prefix, int size)
76 char path[PATH_MAX] = {0};
77 char buf[PATH_MAX] = {0};
79 /* get file for config (fd is always 3) */
80 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
82 /* return NULL on error */
83 if (readlink(path, buf, sizeof(buf)) == -1)
87 snprintf(prefix, size, "%s", basename(dirname(buf)));
93 #endif /* _PROCESS_H_ */