1272343Sngie/* $NetBSD: h_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */ 2272343Sngie 3272343Sngie/*- 4272343Sngie * Copyright (c) 2012 The NetBSD Foundation, Inc. 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * This code is derived from software contributed to The NetBSD Foundation 8272343Sngie * by Charles Zhang <charles@NetBSD.org> and 9272343Sngie * Martin Husemann <martin@NetBSD.org>. 10272343Sngie * 11272343Sngie * Redistribution and use in source and binary forms, with or without 12272343Sngie * modification, are permitted provided that the following conditions 13272343Sngie * are met: 14272343Sngie * 1. Redistributions of source code must retain the above copyright 15272343Sngie * notice, this list of conditions and the following disclaimer. 16272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 17272343Sngie * notice, this list of conditions and the following disclaimer in the 18272343Sngie * documentation and/or other materials provided with the distribution. 19272343Sngie * 20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30272343Sngie * POSSIBILITY OF SUCH DAMAGE. 31272343Sngie */ 32272343Sngie 33272343Sngie#include <stdio.h> 34272343Sngie#include <stdlib.h> 35272343Sngie#include <string.h> 36272343Sngie#include <unistd.h> 37272343Sngie#include <errno.h> 38272343Sngie#include <sys/stat.h> 39272343Sngie 40272343Sngie#define BUFSIZE 16 41272343Sngie 42272343Sngie/* 43272343Sngie * This checks (hardcoded) the assumptions that are setup from the 44272343Sngie * main test program via posix spawn file actions. 45272343Sngie * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly 46272343Sngie * (and does some stderr diagnostics in case of errors). 47272343Sngie */ 48272343Sngieint 49272343Sngiemain(int argc, char **argv) 50272343Sngie{ 51272343Sngie int res = EXIT_SUCCESS; 52272343Sngie char buf[BUFSIZE]; 53272343Sngie struct stat sb0, sb1; 54272343Sngie 55272343Sngie strcpy(buf, "test..."); 56272343Sngie /* file desc 3 should be closed via addclose */ 57272343Sngie if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) { 58272343Sngie fprintf(stderr, "%s: filedesc 3 is not closed\n", 59272343Sngie getprogname()); 60272343Sngie res = EXIT_FAILURE; 61272343Sngie } 62272343Sngie /* file desc 4 should be closed via closeonexec */ 63272343Sngie if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) { 64272343Sngie fprintf(stderr, "%s: filedesc 4 is not closed\n", 65272343Sngie getprogname()); 66272343Sngie res = EXIT_FAILURE; 67272343Sngie } 68272343Sngie /* file desc 5 remains open */ 69272343Sngie if (write(5, buf, BUFSIZE) <= 0) { 70272343Sngie fprintf(stderr, "%s: could not write to filedesc 5\n", 71272343Sngie getprogname()); 72272343Sngie res = EXIT_FAILURE; 73272343Sngie } 74272343Sngie /* file desc 6 should be open (via addopen) */ 75272343Sngie if (write(6, buf, BUFSIZE) <= 0) { 76272343Sngie fprintf(stderr, "%s: could not write to filedesc 6\n", 77272343Sngie getprogname()); 78272343Sngie res = EXIT_FAILURE; 79272343Sngie } 80272343Sngie /* file desc 7 should refer to stdout */ 81272343Sngie fflush(stdout); 82272343Sngie if (fstat(fileno(stdout), &sb0) != 0) { 83272343Sngie fprintf(stderr, "%s: could not fstat stdout\n", 84272343Sngie getprogname()); 85272343Sngie res = EXIT_FAILURE; 86272343Sngie } 87272343Sngie if (fstat(7, &sb1) != 0) { 88272343Sngie fprintf(stderr, "%s: could not fstat filedesc 7\n", 89272343Sngie getprogname()); 90272343Sngie res = EXIT_FAILURE; 91272343Sngie } 92272343Sngie if (write(7, buf, strlen(buf)) <= 0) { 93272343Sngie fprintf(stderr, "%s: could not write to filedesc 7\n", 94272343Sngie getprogname()); 95272343Sngie res = EXIT_FAILURE; 96272343Sngie } 97272343Sngie if (memcmp(&sb0, &sb1, sizeof sb0) != 0) { 98272343Sngie fprintf(stderr, "%s: stat results differ\n", getprogname()); 99272343Sngie res = EXIT_FAILURE; 100272343Sngie } 101272343Sngie 102272343Sngie return res; 103272343Sngie} 104272343Sngie 105