net/bnxt: fix null dereference in session cleanup
[dpdk.git] / app / test / process.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _PROCESS_H_
6 #define _PROCESS_H_
7
8 #include <errno.h>  /* errno */
9 #include <limits.h> /* PATH_MAX */
10 #ifndef RTE_EXEC_ENV_WINDOWS
11 #include <libgen.h> /* basename et al */
12 #include <sys/wait.h>
13 #endif
14 #include <stdlib.h> /* NULL */
15 #include <string.h> /* strerror */
16 #include <unistd.h> /* readlink */
17 #include <dirent.h>
18
19 #include <rte_string_fns.h> /* strlcpy */
20
21 #ifdef RTE_EXEC_ENV_FREEBSD
22 #define self "curproc"
23 #define exe "file"
24 #else
25 #define self "self"
26 #define exe "exe"
27 #endif
28
29 #ifdef RTE_LIB_PDUMP
30 #ifdef RTE_NET_RING
31 #include <pthread.h>
32 extern void *send_pkts(void *empty);
33 extern uint16_t flag_for_send_pkts;
34 #endif
35 #endif
36
37 /*
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
42  */
43 static inline int
44 process_dup(const char *const argv[], int numargs, const char *env_value)
45 {
46         int num;
47         char *argv_cpy[numargs + 1];
48         int i, status;
49         char path[32];
50 #ifdef RTE_LIB_PDUMP
51 #ifdef RTE_NET_RING
52         pthread_t thread;
53         int rc;
54 #endif
55 #endif
56
57         pid_t pid = fork();
58         if (pid < 0)
59                 return -1;
60         else if (pid == 0) {
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]);
64                 argv_cpy[i] = NULL;
65                 num = numargs;
66
67 #ifdef RTE_EXEC_ENV_LINUX
68                 {
69                         const char *procdir = "/proc/" self "/fd/";
70                         struct dirent *dirent;
71                         char *endptr;
72                         int fd, fdir;
73                         DIR *dir;
74
75                         /* close all open file descriptors, check /proc/self/fd
76                          * to only call close on open fds. Exclude fds 0, 1 and
77                          * 2
78                          */
79                         dir = opendir(procdir);
80                         if (dir == NULL) {
81                                 rte_panic("Error opening %s: %s\n", procdir,
82                                                 strerror(errno));
83                         }
84
85                         fdir = dirfd(dir);
86                         if (fdir < 0) {
87                                 status = errno;
88                                 closedir(dir);
89                                 rte_panic("Error %d obtaining fd for dir %s: %s\n",
90                                                 fdir, procdir,
91                                                 strerror(status));
92                         }
93
94                         while ((dirent = readdir(dir)) != NULL) {
95
96                                 if (strcmp(dirent->d_name, ".") == 0 ||
97                                         strcmp(dirent->d_name, "..") == 0)
98                                         continue;
99
100                                 errno = 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",
104                                                 fd, dirent->d_name);
105                                         continue;
106                                 }
107
108                                 if (fd == fdir || fd <= 2)
109                                         continue;
110
111                                 close(fd);
112                         }
113                         closedir(dir);
114                 }
115 #endif
116                 printf("Running binary with argv[]:");
117                 for (i = 0; i < num; i++)
118                         printf("'%s' ", argv_cpy[i]);
119                 printf("\n");
120                 fflush(stdout);
121
122                 /* set the environment variable */
123                 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
124                         rte_panic("Cannot export environment variable\n");
125
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",
130                                                 path);
131                         }
132                         rte_panic("Cannot exec: %s\n", strerror(errno));
133                 }
134         }
135         /* parent process does a wait */
136 #ifdef RTE_LIB_PDUMP
137 #ifdef RTE_NET_RING
138         if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
139                 rc = pthread_create(&thread, NULL, &send_pkts, NULL);
140                 if (rc != 0) {
141                         rte_panic("Cannot start send pkts thread: %s\n",
142                                   strerror(rc));
143                 }
144         }
145 #endif
146 #endif
147
148         while (wait(&status) != pid)
149                 ;
150 #ifdef RTE_LIB_PDUMP
151 #ifdef RTE_NET_RING
152         if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
153                 flag_for_send_pkts = 0;
154                 pthread_join(thread, NULL);
155         }
156 #endif
157 #endif
158         return status;
159 }
160
161 /* FreeBSD doesn't support file prefixes, so force compile failures for any
162  * tests attempting to use this function on FreeBSD.
163  */
164 #ifdef RTE_EXEC_ENV_LINUX
165 static char *
166 get_current_prefix(char *prefix, int size)
167 {
168         char path[PATH_MAX] = {0};
169         char buf[PATH_MAX] = {0};
170
171         /* get file for config (fd is always 3) */
172         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
173
174         /* return NULL on error */
175         if (readlink(path, buf, sizeof(buf)) == -1)
176                 return NULL;
177
178         /* get the prefix */
179         snprintf(prefix, size, "%s", basename(dirname(buf)));
180
181         return prefix;
182 }
183 #endif
184
185 #endif /* _PROCESS_H_ */