1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
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 */
16 #include <rte_string_fns.h> /* strlcpy */
18 #ifdef RTE_EXEC_ENV_FREEBSD
19 #define self "curproc"
26 #ifdef RTE_LIBRTE_PDUMP
28 extern void *send_pkts(void *empty);
29 extern uint16_t flag_for_send_pkts;
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
39 process_dup(const char *const argv[], int numargs, const char *env_value)
42 char *argv_cpy[numargs + 1];
45 #ifdef RTE_LIBRTE_PDUMP
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]);
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)
66 printf("Running binary with argv[]:");
67 for (i = 0; i < num; i++)
68 printf("'%s' ", argv_cpy[i]);
71 /* set the environment variable */
72 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
73 rte_panic("Cannot export environment variable\n");
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",
81 rte_panic("Cannot exec: %s\n", strerror(errno));
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);
90 while (wait(&status) != pid)
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);
101 /* FreeBSD doesn't support file prefixes, so force compile failures for any
102 * tests attempting to use this function on FreeBSD.
104 #ifdef RTE_EXEC_ENV_LINUX
106 get_current_prefix(char *prefix, int size)
108 char path[PATH_MAX] = {0};
109 char buf[PATH_MAX] = {0};
111 /* get file for config (fd is always 3) */
112 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
114 /* return NULL on error */
115 if (readlink(path, buf, sizeof(buf)) == -1)
119 snprintf(prefix, size, "%s", basename(dirname(buf)));
125 #endif /* _PROCESS_H_ */