New upstream version 18.08
[deb_dpdk.git] / test / 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 <limits.h> /* PATH_MAX */
9 #include <libgen.h> /* basename et al */
10 #include <stdlib.h> /* NULL */
11 #include <unistd.h> /* readlink */
12
13 #ifdef RTE_EXEC_ENV_BSDAPP
14 #define self "curproc"
15 #define exe "file"
16 #else
17 #define self "self"
18 #define exe "exe"
19 #endif
20
21 /*
22  * launches a second copy of the test process using the given argv parameters,
23  * which should include argv[0] as the process name. To identify in the
24  * subprocess the source of the call, the env_value parameter is set in the
25  * environment as $RTE_TEST
26  */
27 static inline int
28 process_dup(const char *const argv[], int numargs, const char *env_value)
29 {
30         int num;
31         char *argv_cpy[numargs + 1];
32         int i, fd, status;
33         char path[32];
34
35         pid_t pid = fork();
36         if (pid < 0)
37                 return -1;
38         else if (pid == 0) {
39                 /* make a copy of the arguments to be passed to exec */
40                 for (i = 0; i < numargs; i++)
41                         argv_cpy[i] = strdup(argv[i]);
42                 argv_cpy[i] = NULL;
43                 num = numargs;
44
45                 /* close all open file descriptors, check /proc/self/fd to only
46                  * call close on open fds. Exclude fds 0, 1 and 2*/
47                 for (fd = getdtablesize(); fd > 2; fd-- ) {
48                         snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
49                         if (access(path, F_OK) == 0)
50                                 close(fd);
51                 }
52                 printf("Running binary with argv[]:");
53                 for (i = 0; i < num; i++)
54                         printf("'%s' ", argv_cpy[i]);
55                 printf("\n");
56
57                 /* set the environment variable */
58                 if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
59                         rte_panic("Cannot export environment variable\n");
60                 if (execv("/proc/" self "/" exe, argv_cpy) < 0)
61                         rte_panic("Cannot exec\n");
62         }
63         /* parent process does a wait */
64         while (wait(&status) != pid)
65                 ;
66         return status;
67 }
68
69 /* FreeBSD doesn't support file prefixes, so force compile failures for any
70  * tests attempting to use this function on FreeBSD.
71  */
72 #ifdef RTE_EXEC_ENV_LINUXAPP
73 static char *
74 get_current_prefix(char *prefix, int size)
75 {
76         char path[PATH_MAX] = {0};
77         char buf[PATH_MAX] = {0};
78
79         /* get file for config (fd is always 3) */
80         snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
81
82         /* return NULL on error */
83         if (readlink(path, buf, sizeof(buf)) == -1)
84                 return NULL;
85
86         /* get the prefix */
87         snprintf(prefix, size, "%s", basename(dirname(buf)));
88
89         return prefix;
90 }
91 #endif
92
93 #endif /* _PROCESS_H_ */