xref: /netbsd-src/tests/lib/libc/gen/posix_spawn/h_fileactions.c (revision c0746c1e9a05705192ac7c39a7590c9a43d72db6)
1 /* $NetBSD: h_fileactions.c,v 1.2 2021/11/07 15:46:20 christos 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 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: h_fileactions.c,v 1.2 2021/11/07 15:46:20 christos Exp $");
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <sys/stat.h>
41 
42 #define BUFSIZE	16
43 
44 /*
45  * This checks (hardcoded) the assumptions that are setup from the
46  * main test program via posix spawn file actions.
47  * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly
48  * (and does some stderr diagnostics in case of errors).
49  */
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 	int res = EXIT_SUCCESS;
54 	char buf[BUFSIZE];
55 	struct stat sb0, sb1;
56 
57 	strcpy(buf, "test...");
58 	/* file desc 3 should be closed via addclose */
59 	if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) {
60 		fprintf(stderr, "%s: filedesc 3 is not closed\n",
61 		    getprogname());
62 		res = EXIT_FAILURE;
63 	}
64 	/* file desc 4 should be closed via closeonexec */
65 	if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) {
66 		fprintf(stderr, "%s: filedesc 4 is not closed\n",
67 		    getprogname());
68 		res = EXIT_FAILURE;
69 	}
70 	/* file desc 5 remains open */
71 	if (write(5, buf, BUFSIZE) <= 0) {
72 		fprintf(stderr, "%s: could not write to filedesc 5\n",
73 		    getprogname());
74 		res = EXIT_FAILURE;
75 	}
76 	/* file desc 6 should be open (via addopen) */
77 	if (write(6, buf, BUFSIZE) <= 0) {
78 		fprintf(stderr, "%s: could not write to filedesc 6\n",
79 		    getprogname());
80 		res = EXIT_FAILURE;
81 	}
82 	/* file desc 7 should refer to stdout */
83 	fflush(stdout);
84 	if (fstat(fileno(stdout), &sb0) != 0) {
85 		fprintf(stderr, "%s: could not fstat stdout\n",
86 		    getprogname());
87 		res = EXIT_FAILURE;
88 	}
89 	if (fstat(7, &sb1) != 0) {
90 		fprintf(stderr, "%s: could not fstat filedesc 7\n",
91 		    getprogname());
92 		res = EXIT_FAILURE;
93 	}
94 	if (write(7, buf, strlen(buf)) <= 0) {
95 		fprintf(stderr, "%s: could not write to filedesc 7\n",
96 		    getprogname());
97 		res = EXIT_FAILURE;
98 	}
99 	if (memcmp(&sb0, &sb1, sizeof sb0) != 0) {
100 		fprintf(stderr, "%s: stat results differ\n", getprogname());
101 		res = EXIT_FAILURE;
102 	}
103 
104 	return res;
105 }
106 
107