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 */
17 #include <rte_string_fns.h> /* strlcpy */
19 #ifdef RTE_EXEC_ENV_FREEBSD
20 #define self "curproc"
30 extern void *send_pkts(void *empty);
31 extern uint16_t flag_for_send_pkts;
36 * launches a second copy of the test process using the given argv parameters,
37 * which should include argv[0] as the process name. To identify in the
38 * subprocess the source of the call, the env_value parameter is set in the
39 * environment as $RTE_TEST
42 process_dup(const char *const argv[], int numargs, const char *env_value)
45 char *argv_cpy[numargs + 1];
59 /* make a copy of the arguments to be passed to exec */
60 for (i = 0; i < numargs; i++)
61 argv_cpy[i] = strdup(argv[i]);
65 #ifdef RTE_EXEC_ENV_LINUX
67 const char *procdir = "/proc/" self "/fd/";
68 struct dirent *dirent;
73 /* close all open file descriptors, check /proc/self/fd
74 * to only call close on open fds. Exclude fds 0, 1 and
77 dir = opendir(procdir);
79 rte_panic("Error opening %s: %s\n", procdir,
87 rte_panic("Error %d obtaining fd for dir %s: %s\n",
92 while ((dirent = readdir(dir)) != NULL) {
94 if (strcmp(dirent->d_name, ".") == 0 ||
95 strcmp(dirent->d_name, "..") == 0)
99 fd = strtol(dirent->d_name, &endptr, 10);
100 if (errno != 0 || endptr[0] != '\0') {
101 printf("Error converting name fd %d %s:\n",
106 if (fd == fdir || fd <= 2)
114 printf("Running binary with argv[]:");
115 for (i = 0; i < num; i++)
116 printf("'%s' ", argv_cpy[i]);
120 /* set the environment variable */
121 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
122 rte_panic("Cannot export environment variable\n");
124 strlcpy(path, "/proc/" self "/" exe, sizeof(path));
125 if (execv(path, argv_cpy) < 0) {
126 if (errno == ENOENT) {
127 printf("Could not find '%s', is procfs mounted?\n",
130 rte_panic("Cannot exec: %s\n", strerror(errno));
133 /* parent process does a wait */
136 if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
137 rc = pthread_create(&thread, NULL, &send_pkts, NULL);
139 rte_panic("Cannot start send pkts thread: %s\n",
146 while (wait(&status) != pid)
150 if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
151 flag_for_send_pkts = 0;
152 pthread_join(thread, NULL);
159 /* FreeBSD doesn't support file prefixes, so force compile failures for any
160 * tests attempting to use this function on FreeBSD.
162 #ifdef RTE_EXEC_ENV_LINUX
164 get_current_prefix(char *prefix, int size)
166 char path[PATH_MAX] = {0};
167 char buf[PATH_MAX] = {0};
169 /* get file for config (fd is always 3) */
170 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
172 /* return NULL on error */
173 if (readlink(path, buf, sizeof(buf)) == -1)
177 snprintf(prefix, size, "%s", basename(dirname(buf)));
183 #endif /* _PROCESS_H_ */