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