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 #ifndef RTE_EXEC_ENV_WINDOWS
11 #include <libgen.h> /* basename et al */
14 #include <stdlib.h> /* NULL */
15 #include <string.h> /* strerror */
16 #include <unistd.h> /* readlink */
19 #include <rte_string_fns.h> /* strlcpy */
21 #ifdef RTE_EXEC_ENV_FREEBSD
22 #define self "curproc"
32 extern void *send_pkts(void *empty);
33 extern uint16_t flag_for_send_pkts;
38 * launches a second copy of the test process using the given argv parameters,
39 * which should include argv[0] as the process name. To identify in the
40 * subprocess the source of the call, the env_value parameter is set in the
41 * environment as $RTE_TEST
44 process_dup(const char *const argv[], int numargs, const char *env_value)
47 char *argv_cpy[numargs + 1];
61 /* make a copy of the arguments to be passed to exec */
62 for (i = 0; i < numargs; i++)
63 argv_cpy[i] = strdup(argv[i]);
67 #ifdef RTE_EXEC_ENV_LINUX
69 const char *procdir = "/proc/" self "/fd/";
70 struct dirent *dirent;
75 /* close all open file descriptors, check /proc/self/fd
76 * to only call close on open fds. Exclude fds 0, 1 and
79 dir = opendir(procdir);
81 rte_panic("Error opening %s: %s\n", procdir,
89 rte_panic("Error %d obtaining fd for dir %s: %s\n",
94 while ((dirent = readdir(dir)) != NULL) {
96 if (strcmp(dirent->d_name, ".") == 0 ||
97 strcmp(dirent->d_name, "..") == 0)
101 fd = strtol(dirent->d_name, &endptr, 10);
102 if (errno != 0 || endptr[0] != '\0') {
103 printf("Error converting name fd %d %s:\n",
108 if (fd == fdir || fd <= 2)
116 printf("Running binary with argv[]:");
117 for (i = 0; i < num; i++)
118 printf("'%s' ", argv_cpy[i]);
122 /* set the environment variable */
123 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
124 rte_panic("Cannot export environment variable\n");
126 strlcpy(path, "/proc/" self "/" exe, sizeof(path));
127 if (execv(path, argv_cpy) < 0) {
128 if (errno == ENOENT) {
129 printf("Could not find '%s', is procfs mounted?\n",
132 rte_panic("Cannot exec: %s\n", strerror(errno));
135 /* parent process does a wait */
138 if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
139 rc = pthread_create(&thread, NULL, &send_pkts, NULL);
141 rte_panic("Cannot start send pkts thread: %s\n",
148 while (wait(&status) != pid)
152 if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
153 flag_for_send_pkts = 0;
154 pthread_join(thread, NULL);
161 /* FreeBSD doesn't support file prefixes, so force compile failures for any
162 * tests attempting to use this function on FreeBSD.
164 #ifdef RTE_EXEC_ENV_LINUX
166 get_current_prefix(char *prefix, int size)
168 char path[PATH_MAX] = {0};
169 char buf[PATH_MAX] = {0};
171 /* get file for config (fd is always 3) */
172 snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
174 /* return NULL on error */
175 if (readlink(path, buf, sizeof(buf)) == -1)
179 snprintf(prefix, size, "%s", basename(dirname(buf)));
185 #endif /* _PROCESS_H_ */