xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /* $NetBSD: h_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles Zhang <charles@NetBSD.org> and
9  * Martin Husemann <martin@NetBSD.org>.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <sys/stat.h>
39 
40 #define BUFSIZE	16
41 
42 /*
43  * This checks (hardcoded) the assumptions that are setup from the
44  * main test program via posix spawn file actions.
45  * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly
46  * (and does some stderr diagnostics in case of errors).
47  */
48 int
49 main(int argc, char **argv)
50 {
51 	int res = EXIT_SUCCESS;
52 	long lowfd;
53 	char buf[BUFSIZE];
54 	struct stat sb0, sb1;
55 
56 	if (argc < 2) {
57 		fprintf(stderr, "%s: Not enough arguments: %d\n", getprogname(),
58 		    argc);
59 		return EXIT_FAILURE;
60 	}
61 	lowfd = strtol(argv[1], NULL, 10);
62 	if (lowfd < 3) {
63 		fprintf(stderr, "%s: Invalid lowfd %d (as str: %s) \n",
64 		    getprogname(), argc, argv[1]);
65 		return EXIT_FAILURE;
66 	}
67 
68 	strcpy(buf, "test...");
69 	/* First fd should be closed via addclose */
70 	if (read(lowfd, buf, BUFSIZE) != -1 || errno != EBADF) {
71 		fprintf(stderr, "%s: first filedesc is not closed\n",
72 		    getprogname());
73 		res = EXIT_FAILURE;
74 	}
75 	/* Next file desc should be closed via closeonexec */
76 	if (read(lowfd + 1, buf, BUFSIZE) != -1 || errno != EBADF) {
77 		fprintf(stderr, "%s: filedesc +1 is not closed\n",
78 		    getprogname());
79 		res = EXIT_FAILURE;
80 	}
81 	/* file desc + 2 remains open */
82 	if (write(lowfd + 2, buf, BUFSIZE) <= 0) {
83 		fprintf(stderr, "%s: could not write to filedesc +2\n",
84 		    getprogname());
85 		res = EXIT_FAILURE;
86 	}
87 	/* file desc + 3 should be open (via addopen) */
88 	if (write(lowfd + 3, buf, BUFSIZE) <= 0) {
89 		fprintf(stderr, "%s: could not write to filedesc +3\n",
90 		    getprogname());
91 		res = EXIT_FAILURE;
92 	}
93 	/* file desc + 4 should refer to stdout */
94 	fflush(stdout);
95 	if (fstat(fileno(stdout), &sb0) != 0) {
96 		fprintf(stderr, "%s: could not fstat stdout\n",
97 		    getprogname());
98 		res = EXIT_FAILURE;
99 	}
100 	if (fstat(lowfd + 4, &sb1) != 0) {
101 		fprintf(stderr, "%s: could not fstat filedesc +4\n",
102 		    getprogname());
103 		res = EXIT_FAILURE;
104 	}
105 	if (write(lowfd + 4, buf, strlen(buf)) <= 0) {
106 		fprintf(stderr, "%s: could not write to filedesc +4\n",
107 		    getprogname());
108 		res = EXIT_FAILURE;
109 	}
110 	if (memcmp(&sb0, &sb1, sizeof sb0) != 0) {
111 		fprintf(stderr, "%s: stat results differ\n", getprogname());
112 		res = EXIT_FAILURE;
113 	}
114 
115 	return res;
116 }
117 
118