xref: /netbsd-src/tests/lib/libc/gen/posix_spawn/h_fileactions.c (revision c0746c1e9a05705192ac7c39a7590c9a43d72db6)
1*c0746c1eSchristos /* $NetBSD: h_fileactions.c,v 1.2 2021/11/07 15:46:20 christos Exp $ */
20ce98f42Smartin 
30ce98f42Smartin /*-
40ce98f42Smartin  * Copyright (c) 2012 The NetBSD Foundation, Inc.
50ce98f42Smartin  * All rights reserved.
60ce98f42Smartin  *
70ce98f42Smartin  * This code is derived from software contributed to The NetBSD Foundation
80ce98f42Smartin  * by Charles Zhang <charles@NetBSD.org> and
90ce98f42Smartin  * Martin Husemann <martin@NetBSD.org>.
100ce98f42Smartin  *
110ce98f42Smartin  * Redistribution and use in source and binary forms, with or without
120ce98f42Smartin  * modification, are permitted provided that the following conditions
130ce98f42Smartin  * are met:
140ce98f42Smartin  * 1. Redistributions of source code must retain the above copyright
150ce98f42Smartin  *    notice, this list of conditions and the following disclaimer.
160ce98f42Smartin  * 2. Redistributions in binary form must reproduce the above copyright
170ce98f42Smartin  *    notice, this list of conditions and the following disclaimer in the
180ce98f42Smartin  *    documentation and/or other materials provided with the distribution.
190ce98f42Smartin  *
200ce98f42Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
210ce98f42Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
220ce98f42Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
230ce98f42Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
240ce98f42Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
250ce98f42Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
260ce98f42Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
270ce98f42Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
280ce98f42Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
290ce98f42Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
300ce98f42Smartin  * POSSIBILITY OF SUCH DAMAGE.
310ce98f42Smartin  */
32*c0746c1eSchristos #include <sys/cdefs.h>
33*c0746c1eSchristos __RCSID("$NetBSD: h_fileactions.c,v 1.2 2021/11/07 15:46:20 christos Exp $");
340ce98f42Smartin 
350ce98f42Smartin #include <stdio.h>
360ce98f42Smartin #include <stdlib.h>
370ce98f42Smartin #include <string.h>
380ce98f42Smartin #include <unistd.h>
390ce98f42Smartin #include <errno.h>
400ce98f42Smartin #include <sys/stat.h>
410ce98f42Smartin 
420ce98f42Smartin #define BUFSIZE	16
430ce98f42Smartin 
440ce98f42Smartin /*
450ce98f42Smartin  * This checks (hardcoded) the assumptions that are setup from the
460ce98f42Smartin  * main test program via posix spawn file actions.
470ce98f42Smartin  * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly
480ce98f42Smartin  * (and does some stderr diagnostics in case of errors).
490ce98f42Smartin  */
500ce98f42Smartin int
main(int argc,char ** argv)510ce98f42Smartin main(int argc, char **argv)
520ce98f42Smartin {
530ce98f42Smartin 	int res = EXIT_SUCCESS;
540ce98f42Smartin 	char buf[BUFSIZE];
550ce98f42Smartin 	struct stat sb0, sb1;
560ce98f42Smartin 
570ce98f42Smartin 	strcpy(buf, "test...");
580ce98f42Smartin 	/* file desc 3 should be closed via addclose */
590ce98f42Smartin 	if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) {
600ce98f42Smartin 		fprintf(stderr, "%s: filedesc 3 is not closed\n",
610ce98f42Smartin 		    getprogname());
620ce98f42Smartin 		res = EXIT_FAILURE;
630ce98f42Smartin 	}
640ce98f42Smartin 	/* file desc 4 should be closed via closeonexec */
650ce98f42Smartin 	if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) {
660ce98f42Smartin 		fprintf(stderr, "%s: filedesc 4 is not closed\n",
670ce98f42Smartin 		    getprogname());
680ce98f42Smartin 		res = EXIT_FAILURE;
690ce98f42Smartin 	}
700ce98f42Smartin 	/* file desc 5 remains open */
710ce98f42Smartin 	if (write(5, buf, BUFSIZE) <= 0) {
720ce98f42Smartin 		fprintf(stderr, "%s: could not write to filedesc 5\n",
730ce98f42Smartin 		    getprogname());
740ce98f42Smartin 		res = EXIT_FAILURE;
750ce98f42Smartin 	}
760ce98f42Smartin 	/* file desc 6 should be open (via addopen) */
770ce98f42Smartin 	if (write(6, buf, BUFSIZE) <= 0) {
780ce98f42Smartin 		fprintf(stderr, "%s: could not write to filedesc 6\n",
790ce98f42Smartin 		    getprogname());
800ce98f42Smartin 		res = EXIT_FAILURE;
810ce98f42Smartin 	}
820ce98f42Smartin 	/* file desc 7 should refer to stdout */
830ce98f42Smartin 	fflush(stdout);
840ce98f42Smartin 	if (fstat(fileno(stdout), &sb0) != 0) {
850ce98f42Smartin 		fprintf(stderr, "%s: could not fstat stdout\n",
860ce98f42Smartin 		    getprogname());
870ce98f42Smartin 		res = EXIT_FAILURE;
880ce98f42Smartin 	}
890ce98f42Smartin 	if (fstat(7, &sb1) != 0) {
900ce98f42Smartin 		fprintf(stderr, "%s: could not fstat filedesc 7\n",
910ce98f42Smartin 		    getprogname());
920ce98f42Smartin 		res = EXIT_FAILURE;
930ce98f42Smartin 	}
940ce98f42Smartin 	if (write(7, buf, strlen(buf)) <= 0) {
950ce98f42Smartin 		fprintf(stderr, "%s: could not write to filedesc 7\n",
960ce98f42Smartin 		    getprogname());
970ce98f42Smartin 		res = EXIT_FAILURE;
980ce98f42Smartin 	}
990ce98f42Smartin 	if (memcmp(&sb0, &sb1, sizeof sb0) != 0) {
1000ce98f42Smartin 		fprintf(stderr, "%s: stat results differ\n", getprogname());
1010ce98f42Smartin 		res = EXIT_FAILURE;
1020ce98f42Smartin 	}
1030ce98f42Smartin 
1040ce98f42Smartin 	return res;
1050ce98f42Smartin }
1060ce98f42Smartin 
107