rawdev: add private data size to info query
[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 #include <libgen.h> /* basename et al */
11 #include <stdlib.h> /* NULL */
12 #include <string.h> /* strerror */
13 #include <unistd.h> /* readlink */
14 #include <dirent.h>
15 #include <sys/wait.h>
16
17 #include <rte_string_fns.h> /* strlcpy */
18
19 #ifdef RTE_EXEC_ENV_FREEBSD
20 #define self "curproc"
21 #define exe "file"
22 #else
23 #define self "self"
24 #define exe "exe"
25 #endif
26
27 #ifdef RTE_LIBRTE_PDUMP
28 #ifdef RTE_LIBRTE_RING_PMD
29 #include <pthread.h>
30 extern void *send_pkts(void *empty);
31 extern uint16_t flag_for_send_pkts;
32 #endif
33 #endif
34
35 /*
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
40  */
41 static inline int
42 process_dup(const char *const argv[], int numargs, const char *env_value)
43 {
44         int num;
45         char *argv_cpy[numargs + 1];
46         int i, status;
47         char path[32];
48 #ifdef RTE_LIBRTE_PDUMP
49 #ifdef RTE_LIBRTE_RING_PMD
50         pthread_t thread;
51 #endif
52 #endif
53
54         pid_t pid = fork();
55         if (pid < 0)
56                 return -1;
57         else if (pid == 0) {
58                 /* make a copy of the arguments to be passed to exec */
59                 for (i = 0; i < numargs; i++)
60                         argv_cpy[i] = strdup(argv[i]);
61                 argv_cpy[i] = NULL;
62                 num = numargs;
63
64 #ifdef RTE_EXEC_ENV_LINUX
65                 {
66                         const char *procdir = "/proc/" self "/fd/";
67                         struct dirent *dirent;
68                         char *endptr;
69                         int fd, fdir;
70                         DIR *dir;
71
72                         /* close all open file descriptors, check /proc/self/fd
73                          * to only call close on open fds. Exclude fds 0, 1 and
74                          * 2
75                          */
76                         dir = opendir(procdir);
77                         if (dir == NULL) {
78                                 rte_panic("Error opening %s: %s\n", procdir,
79                                                 strerror(errno));
80                         }
81
82                         fdir = dirfd(dir);
83                         if (fdir < 0) {
84                                 status = errno;
85                                 closedir(dir);
86                                 rte_panic("Error %d obtaining fd for dir %s: %s\n",
87                                                 fdir, procdir,
88                                                 strerror(status));
89                         }
90
91                         while ((dirent = readdir(dir)) != NULL) {
92                                 errno = 0;
93                                 fd = strtol(dirent->d_name, &endptr, 10);
94                                 if (errno != 0 || endptr[0] != '\0') {
95                                         printf("Error converting name fd %d %s:\n",
96                                                 fd, dirent->d_name);
97                                         continue;
98                                 }
99
100                                 if (fd == fdir || fd <= 2)
101                                         continue;
102
103                                 close(fd);
104                         }
105                         closedir(dir);
106                 }
107 #endif
108                 printf("Running binary with argv[]:");
109                 for (i = 0; i < num; i++)
110                         printf("'%s' ", argv_cpy[i]);
111                 printf("\n");
112
113                 /* set the environment variable */
114                 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
115                         rte_panic("Cannot export environment variable\n");
116
117                 strlcpy(path, "/proc/" self "/" exe, sizeof(path));
118                 if (execv(path, argv_cpy) < 0) {
119                         if (errno == ENOENT) {
120                                 printf("Could not find '%s', is procfs mounted?\n",
121                                                 path);
122                         }
123                         rte_panic("Cannot exec: %s\n", strerror(errno));
124                 }
125         }
126         /* parent process does a wait */
127 #ifdef RTE_LIBRTE_PDUMP
128 #ifdef RTE_LIBRTE_RING_PMD
129         if ((strcmp(env_value, "run_pdump_server_tests") == 0))
130                 pthread_create(&thread, NULL, &send_pkts, NULL);
131 #endif
132 #endif
133
134         while (wait(&status) != pid)
135                 ;
136 #ifdef RTE_LIBRTE_PDUMP
137 #ifdef RTE_LIBRTE_RING_PMD
138         if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
139                 flag_for_send_pkts = 0;
140                 pthread_join(thread, NULL);
141         }
142 #endif
143 #endif
144         return status;
145 }
146
147 /* FreeBSD doesn't support file prefixes, so force compile failures for any
148  * tests attempting to use this function on FreeBSD.
149  */
150 #ifdef RTE_EXEC_ENV_LINUX
151 static char *
152 get_current_prefix(char *prefix, int size)
153 {
154         char path[PATH_MAX] = {0};
155         char buf[PATH_MAX] = {0};
156
157         /* get file for config (fd is always 3) */
158         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
159
160         /* return NULL on error */
161         if (readlink(path, buf, sizeof(buf)) == -1)
162                 return NULL;
163
164         /* get the prefix */
165         snprintf(prefix, size, "%s", basename(dirname(buf)));
166
167         return prefix;
168 }
169 #endif
170
171 #endif /* _PROCESS_H_ */