15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#ifndef _PROCESS_H_
355129044dSC.J. Collier#define _PROCESS_H_
365129044dSC.J. Collier
375129044dSC.J. Collier#ifdef RTE_EXEC_ENV_BSDAPP
385129044dSC.J. Collier#define self "curproc"
395129044dSC.J. Collier#define exe "file"
405129044dSC.J. Collier#else
415129044dSC.J. Collier#define self "self"
425129044dSC.J. Collier#define exe "exe"
435129044dSC.J. Collier#endif
445129044dSC.J. Collier
455129044dSC.J. Collier/*
465129044dSC.J. Collier * launches a second copy of the test process using the given argv parameters,
475129044dSC.J. Collier * which should include argv[0] as the process name. To identify in the
485129044dSC.J. Collier * subprocess the source of the call, the env_value parameter is set in the
495129044dSC.J. Collier * environment as $RTE_TEST
505129044dSC.J. Collier */
515129044dSC.J. Collierstatic inline int
525129044dSC.J. Collierprocess_dup(const char *const argv[], int numargs, const char *env_value)
535129044dSC.J. Collier{
545129044dSC.J. Collier	int num;
555129044dSC.J. Collier#ifdef RTE_LIBRTE_XEN_DOM0
565129044dSC.J. Collier	char *argv_cpy[numargs + 2];
575129044dSC.J. Collier#else
585129044dSC.J. Collier	char *argv_cpy[numargs + 1];
595129044dSC.J. Collier#endif
605129044dSC.J. Collier	int i, fd, status;
615129044dSC.J. Collier	char path[32];
625129044dSC.J. Collier
635129044dSC.J. Collier	pid_t pid = fork();
645129044dSC.J. Collier	if (pid < 0)
655129044dSC.J. Collier		return -1;
665129044dSC.J. Collier	else if (pid == 0) {
675129044dSC.J. Collier		/* make a copy of the arguments to be passed to exec */
685129044dSC.J. Collier		for (i = 0; i < numargs; i++)
695129044dSC.J. Collier			argv_cpy[i] = strdup(argv[i]);
705129044dSC.J. Collier#ifdef RTE_LIBRTE_XEN_DOM0
715129044dSC.J. Collier		argv_cpy[i] = strdup("--xen-dom0");
725129044dSC.J. Collier		argv_cpy[i + 1] = NULL;
735129044dSC.J. Collier		num = numargs + 1;
745129044dSC.J. Collier#else
755129044dSC.J. Collier		argv_cpy[i] = NULL;
765129044dSC.J. Collier		num = numargs;
775129044dSC.J. Collier#endif
785129044dSC.J. Collier
795129044dSC.J. Collier		/* close all open file descriptors, check /proc/self/fd to only
805129044dSC.J. Collier		 * call close on open fds. Exclude fds 0, 1 and 2*/
815129044dSC.J. Collier		for (fd = getdtablesize(); fd > 2; fd-- ) {
825129044dSC.J. Collier			snprintf(path, sizeof(path), "/proc/" exe "/fd/%d", fd);
835129044dSC.J. Collier			if (access(path, F_OK) == 0)
845129044dSC.J. Collier				close(fd);
855129044dSC.J. Collier		}
865129044dSC.J. Collier		printf("Running binary with argv[]:");
875129044dSC.J. Collier		for (i = 0; i < num; i++)
885129044dSC.J. Collier			printf("'%s' ", argv_cpy[i]);
895129044dSC.J. Collier		printf("\n");
905129044dSC.J. Collier
915129044dSC.J. Collier		/* set the environment variable */
925129044dSC.J. Collier		if (setenv(RECURSIVE_ENV_VAR, env_value, 1) != 0)
935129044dSC.J. Collier			rte_panic("Cannot export environment variable\n");
945129044dSC.J. Collier		if (execv("/proc/" self "/" exe, argv_cpy) < 0)
955129044dSC.J. Collier			rte_panic("Cannot exec\n");
965129044dSC.J. Collier	}
975129044dSC.J. Collier	/* parent process does a wait */
985129044dSC.J. Collier	while (wait(&status) != pid)
995129044dSC.J. Collier		;
1005129044dSC.J. Collier	return status;
1015129044dSC.J. Collier}
1025129044dSC.J. Collier
1035129044dSC.J. Collier#endif /* _PROCESS_H_ */
104