Home | History | Annotate | Line # | Download | only in posix_spawn
h_fileactions.c revision 1.1
      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 (at) NetBSD.org> and
      9  * Martin Husemann <martin (at) 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 	char buf[BUFSIZE];
     53 	struct stat sb0, sb1;
     54 
     55 	strcpy(buf, "test...");
     56 	/* file desc 3 should be closed via addclose */
     57 	if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) {
     58 		fprintf(stderr, "%s: filedesc 3 is not closed\n",
     59 		    getprogname());
     60 		res = EXIT_FAILURE;
     61 	}
     62 	/* file desc 4 should be closed via closeonexec */
     63 	if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) {
     64 		fprintf(stderr, "%s: filedesc 4 is not closed\n",
     65 		    getprogname());
     66 		res = EXIT_FAILURE;
     67 	}
     68 	/* file desc 5 remains open */
     69 	if (write(5, buf, BUFSIZE) <= 0) {
     70 		fprintf(stderr, "%s: could not write to filedesc 5\n",
     71 		    getprogname());
     72 		res = EXIT_FAILURE;
     73 	}
     74 	/* file desc 6 should be open (via addopen) */
     75 	if (write(6, buf, BUFSIZE) <= 0) {
     76 		fprintf(stderr, "%s: could not write to filedesc 6\n",
     77 		    getprogname());
     78 		res = EXIT_FAILURE;
     79 	}
     80 	/* file desc 7 should refer to stdout */
     81 	fflush(stdout);
     82 	if (fstat(fileno(stdout), &sb0) != 0) {
     83 		fprintf(stderr, "%s: could not fstat stdout\n",
     84 		    getprogname());
     85 		res = EXIT_FAILURE;
     86 	}
     87 	if (fstat(7, &sb1) != 0) {
     88 		fprintf(stderr, "%s: could not fstat filedesc 7\n",
     89 		    getprogname());
     90 		res = EXIT_FAILURE;
     91 	}
     92 	if (write(7, buf, strlen(buf)) <= 0) {
     93 		fprintf(stderr, "%s: could not write to filedesc 7\n",
     94 		    getprogname());
     95 		res = EXIT_FAILURE;
     96 	}
     97 	if (memcmp(&sb0, &sb1, sizeof sb0) != 0) {
     98 		fprintf(stderr, "%s: stat results differ\n", getprogname());
     99 		res = EXIT_FAILURE;
    100 	}
    101 
    102 	return res;
    103 }
    104 
    105