2017-12-19 15:49:05 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* Copyright(c) 2010-2014 Intel Corporation
|
2012-09-04 12:54:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _PROCESS_H_
|
|
|
|
#define _PROCESS_H_
|
|
|
|
|
2018-07-25 13:44:26 +00:00
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#include <libgen.h> /* basename et al */
|
|
|
|
#include <stdlib.h> /* NULL */
|
|
|
|
#include <unistd.h> /* readlink */
|
2018-10-08 12:12:47 +00:00
|
|
|
#include <sys/wait.h>
|
2018-07-25 13:44:26 +00:00
|
|
|
|
2019-03-06 16:22:40 +00:00
|
|
|
#ifdef RTE_EXEC_ENV_FREEBSD
|
2014-02-10 11:49:10 +00:00
|
|
|
#define self "curproc"
|
|
|
|
#define exe "file"
|
|
|
|
#else
|
|
|
|
#define self "self"
|
|
|
|
#define exe "exe"
|
|
|
|
#endif
|
|
|
|
|
2019-03-20 11:33:19 +00:00
|
|
|
#ifdef RTE_LIBRTE_PDUMP
|
2018-10-08 12:12:47 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
extern void *send_pkts(void *empty);
|
|
|
|
extern uint16_t flag_for_send_pkts;
|
2019-03-20 11:33:19 +00:00
|
|
|
#endif
|
2018-10-08 12:12:47 +00:00
|
|
|
|
2012-09-04 12:54:00 +00:00
|
|
|
/*
|
|
|
|
* launches a second copy of the test process using the given argv parameters,
|
|
|
|
* which should include argv[0] as the process name. To identify in the
|
|
|
|
* subprocess the source of the call, the env_value parameter is set in the
|
|
|
|
* environment as $RTE_TEST
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
process_dup(const char *const argv[], int numargs, const char *env_value)
|
|
|
|
{
|
2014-02-12 15:32:25 +00:00
|
|
|
int num;
|
2012-09-04 12:54:00 +00:00
|
|
|
char *argv_cpy[numargs + 1];
|
|
|
|
int i, fd, status;
|
|
|
|
char path[32];
|
2019-03-20 11:33:19 +00:00
|
|
|
#ifdef RTE_LIBRTE_PDUMP
|
2018-10-08 12:12:47 +00:00
|
|
|
pthread_t thread;
|
2019-03-20 11:33:19 +00:00
|
|
|
#endif
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
else if (pid == 0) {
|
|
|
|
/* make a copy of the arguments to be passed to exec */
|
|
|
|
for (i = 0; i < numargs; i++)
|
|
|
|
argv_cpy[i] = strdup(argv[i]);
|
|
|
|
argv_cpy[i] = NULL;
|
2014-02-12 15:32:25 +00:00
|
|
|
num = numargs;
|
2012-09-04 12:54:00 +00:00
|
|
|
|
|
|
|
/* close all open file descriptors, check /proc/self/fd to only
|
|
|
|
* call close on open fds. Exclude fds 0, 1 and 2*/
|
|
|
|
for (fd = getdtablesize(); fd > 2; fd-- ) {
|
2014-06-24 18:15:28 +00:00
|
|
|
snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
|
2012-09-04 12:54:00 +00:00
|
|
|
if (access(path, F_OK) == 0)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
printf("Running binary with argv[]:");
|
2014-02-12 15:32:25 +00:00
|
|
|
for (i = 0; i < num; i++)
|
2012-09-04 12:54:00 +00:00
|
|
|
printf("'%s' ", argv_cpy[i]);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
/* set the environment variable */
|
|
|
|
if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
|
|
|
|
rte_panic("Cannot export environment variable\n");
|
2014-02-10 11:49:10 +00:00
|
|
|
if (execv("/proc/" self "/" exe, argv_cpy) < 0)
|
2012-09-04 12:54:00 +00:00
|
|
|
rte_panic("Cannot exec\n");
|
|
|
|
}
|
|
|
|
/* parent process does a wait */
|
2019-03-20 11:33:19 +00:00
|
|
|
#ifdef RTE_LIBRTE_PDUMP
|
2018-10-08 12:12:47 +00:00
|
|
|
if ((strcmp(env_value, "run_pdump_server_tests") == 0))
|
|
|
|
pthread_create(&thread, NULL, &send_pkts, NULL);
|
2019-03-20 11:33:19 +00:00
|
|
|
#endif
|
2018-10-08 12:12:47 +00:00
|
|
|
|
2012-09-04 12:54:00 +00:00
|
|
|
while (wait(&status) != pid)
|
2015-06-04 14:43:24 +00:00
|
|
|
;
|
2019-03-20 11:33:19 +00:00
|
|
|
#ifdef RTE_LIBRTE_PDUMP
|
2018-10-08 12:12:47 +00:00
|
|
|
if ((strcmp(env_value, "run_pdump_server_tests") == 0)) {
|
|
|
|
flag_for_send_pkts = 0;
|
|
|
|
pthread_join(thread, NULL);
|
|
|
|
}
|
2019-03-20 11:33:19 +00:00
|
|
|
#endif
|
2012-09-04 12:54:00 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2018-07-25 13:44:26 +00:00
|
|
|
/* FreeBSD doesn't support file prefixes, so force compile failures for any
|
|
|
|
* tests attempting to use this function on FreeBSD.
|
|
|
|
*/
|
2019-03-06 16:22:39 +00:00
|
|
|
#ifdef RTE_EXEC_ENV_LINUX
|
2018-07-25 13:44:26 +00:00
|
|
|
static char *
|
|
|
|
get_current_prefix(char *prefix, int size)
|
|
|
|
{
|
|
|
|
char path[PATH_MAX] = {0};
|
|
|
|
char buf[PATH_MAX] = {0};
|
|
|
|
|
|
|
|
/* get file for config (fd is always 3) */
|
|
|
|
snprintf(path, sizeof(path), "/proc/self/fd/%d", 3);
|
|
|
|
|
|
|
|
/* return NULL on error */
|
|
|
|
if (readlink(path, buf, sizeof(buf)) == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* get the prefix */
|
|
|
|
snprintf(prefix, size, "%s", basename(dirname(buf)));
|
|
|
|
|
|
|
|
return prefix;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-04 12:54:00 +00:00
|
|
|
#endif /* _PROCESS_H_ */
|