xref: /openbsd-src/regress/lib/libpthread/execve/execve.c (revision 9de9e20a682542e3a9bc96f10bf96555361cdd38)
1*9de9e20aSguenther /*	$OpenBSD: execve.c,v 1.5 2011/11/25 00:31:43 guenther Exp $	*/
2b2ea75c1Sfgsch /*
3b2ea75c1Sfgsch  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4b2ea75c1Sfgsch  * proven@mit.edu All rights reserved.
5b2ea75c1Sfgsch  *
6b2ea75c1Sfgsch  * Redistribution and use in source and binary forms, with or without
7b2ea75c1Sfgsch  * modification, are permitted provided that the following conditions
8b2ea75c1Sfgsch  * are met:
9b2ea75c1Sfgsch  * 1. Redistributions of source code must retain the above copyright
10b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer.
11b2ea75c1Sfgsch  * 2. Redistributions in binary form must reproduce the above copyright
12b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer in the
13b2ea75c1Sfgsch  *    documentation and/or other materials provided with the distribution.
14b2ea75c1Sfgsch  * 3. All advertising materials mentioning features or use of this software
15b2ea75c1Sfgsch  *    must display the following acknowledgement:
16b2ea75c1Sfgsch  *	This product includes software developed by Chris Provenzano,
17b2ea75c1Sfgsch  *	the University of California, Berkeley, and contributors.
18b2ea75c1Sfgsch  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19b2ea75c1Sfgsch  *   contributors may be used to endorse or promote products derived
20b2ea75c1Sfgsch  *   from this software without specific prior written permission.
21b2ea75c1Sfgsch  *
22b2ea75c1Sfgsch  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23b2ea75c1Sfgsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b2ea75c1Sfgsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b2ea75c1Sfgsch  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26b2ea75c1Sfgsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27b2ea75c1Sfgsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28b2ea75c1Sfgsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29b2ea75c1Sfgsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30b2ea75c1Sfgsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31b2ea75c1Sfgsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32b2ea75c1Sfgsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33b2ea75c1Sfgsch  */
34b2ea75c1Sfgsch 
35b2ea75c1Sfgsch /*
36b2ea75c1Sfgsch  * Copyright (c) 1994 by Chris Provenzano, proven@athena.mit.edu
37b2ea75c1Sfgsch  *
38b2ea75c1Sfgsch  * Test execve() and dup2() calls.
39b2ea75c1Sfgsch  */
40b2ea75c1Sfgsch 
41b2ea75c1Sfgsch #include <pthread.h>
42b2ea75c1Sfgsch #include <string.h>
43b2ea75c1Sfgsch #include <unistd.h>
44b2ea75c1Sfgsch #include <fcntl.h>
45b2ea75c1Sfgsch #include <stdio.h>
46b2ea75c1Sfgsch #include "test.h"
47b2ea75c1Sfgsch 
48b2ea75c1Sfgsch extern char **environ;
49*9de9e20aSguenther char *bad_argv[] = {
50*9de9e20aSguenther 	"/NO SUCH FILE",
51*9de9e20aSguenther 	NULL
52*9de9e20aSguenther };
53560ac243Shenning char *new_argv[] = {
54*9de9e20aSguenther 	"/bin/sh",
55*9de9e20aSguenther 	"-c",
56*9de9e20aSguenther 	"sleep 3; echo 'This line should appear after the execve'",
57b2ea75c1Sfgsch 	NULL
58b2ea75c1Sfgsch };
59b2ea75c1Sfgsch 
60b2ea75c1Sfgsch char * should_succeed = "This line should be displayed\n";
61b2ea75c1Sfgsch 
62*9de9e20aSguenther void *
other(void * arg)63*9de9e20aSguenther other(void *arg)
64*9de9e20aSguenther {
65*9de9e20aSguenther 	sleep(2);
66*9de9e20aSguenther 	printf("%s\n", (char *)arg);
67*9de9e20aSguenther 	return NULL;
68*9de9e20aSguenther }
69*9de9e20aSguenther 
70b2ea75c1Sfgsch int
main(int argc,char * argv[])71db3296cfSderaadt main(int argc, char *argv[])
72b2ea75c1Sfgsch {
73b2ea75c1Sfgsch 	int fd;
74*9de9e20aSguenther 	pthread_t t1;
75b2ea75c1Sfgsch 
76b2ea75c1Sfgsch 	printf("This is the first message\n");
77b2ea75c1Sfgsch 	if (isatty(STDOUT_FILENO)) {
78b2ea75c1Sfgsch 		char *ttynm;
79b2ea75c1Sfgsch 
80b2ea75c1Sfgsch 		CHECKn(ttynm = ttyname(STDOUT_FILENO));
81b2ea75c1Sfgsch 		printf("tty is %s\n", ttynm);
82b2ea75c1Sfgsch 		CHECKe(fd = open(ttynm, O_RDWR));
836c93af35Smarc 	} else {
846c93af35Smarc 		printf("IGNORED: stdout is not a tty: this test needs a tty\n");
856c93af35Smarc 		SUCCEED;
866c93af35Smarc 	}
87b2ea75c1Sfgsch 
88b2ea75c1Sfgsch 	CHECKn(printf("This output is necessary to set the stdout fd to NONBLOCKING\n"));
89b2ea75c1Sfgsch 
90*9de9e20aSguenther 	/* create another thread to make things interesting */
91*9de9e20aSguenther 	CHECKr(pthread_create(&t1, NULL, other, "Should see this too"));
92*9de9e20aSguenther 
93b2ea75c1Sfgsch 	/* do a dup2 */
94b2ea75c1Sfgsch 	CHECKe(dup2(fd, STDOUT_FILENO));
95b2ea75c1Sfgsch 	CHECKe(write(STDOUT_FILENO, should_succeed,
96b2ea75c1Sfgsch 	    (size_t)strlen(should_succeed)));
97*9de9e20aSguenther 	CHECKn(execve(bad_argv[0], bad_argv, environ));
98*9de9e20aSguenther 	pthread_join(t1, NULL);
99*9de9e20aSguenther 	CHECKr(pthread_create(&t1, NULL, other, "failed!"));
100*9de9e20aSguenther 	sleep(1);
101560ac243Shenning 	CHECKe(execve(new_argv[0], new_argv, environ));
102560ac243Shenning 	DIE(errno, "execve %s", new_argv[0]);
103b2ea75c1Sfgsch }
104*9de9e20aSguenther 
105