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