157718be8SEnji Cooper /* $NetBSD: h_fileactions.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*-
457718be8SEnji Cooper * Copyright (c) 2012 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper * by Charles Zhang <charles@NetBSD.org> and
957718be8SEnji Cooper * Martin Husemann <martin@NetBSD.org>.
1057718be8SEnji Cooper *
1157718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
1257718be8SEnji Cooper * modification, are permitted provided that the following conditions
1357718be8SEnji Cooper * are met:
1457718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1557718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1657718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1757718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1857718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1957718be8SEnji Cooper *
2057718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2157718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2257718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2357718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2457718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2557718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2657718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2757718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2857718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2957718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3057718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
3157718be8SEnji Cooper */
3257718be8SEnji Cooper
3357718be8SEnji Cooper #include <stdio.h>
3457718be8SEnji Cooper #include <stdlib.h>
3557718be8SEnji Cooper #include <string.h>
3657718be8SEnji Cooper #include <unistd.h>
3757718be8SEnji Cooper #include <errno.h>
3857718be8SEnji Cooper #include <sys/stat.h>
3957718be8SEnji Cooper
4057718be8SEnji Cooper #define BUFSIZE 16
4157718be8SEnji Cooper
4257718be8SEnji Cooper /*
4357718be8SEnji Cooper * This checks (hardcoded) the assumptions that are setup from the
4457718be8SEnji Cooper * main test program via posix spawn file actions.
4557718be8SEnji Cooper * Program exits with EXIT_SUCCESS or EXIT_FAILURE accordingly
4657718be8SEnji Cooper * (and does some stderr diagnostics in case of errors).
4757718be8SEnji Cooper */
4857718be8SEnji Cooper int
main(int argc,char ** argv)4957718be8SEnji Cooper main(int argc, char **argv)
5057718be8SEnji Cooper {
5157718be8SEnji Cooper int res = EXIT_SUCCESS;
52*2aa3ef28SAlex Richardson long lowfd;
5357718be8SEnji Cooper char buf[BUFSIZE];
5457718be8SEnji Cooper struct stat sb0, sb1;
5557718be8SEnji Cooper
56*2aa3ef28SAlex Richardson if (argc < 2) {
57*2aa3ef28SAlex Richardson fprintf(stderr, "%s: Not enough arguments: %d\n", getprogname(),
58*2aa3ef28SAlex Richardson argc);
59*2aa3ef28SAlex Richardson return EXIT_FAILURE;
60*2aa3ef28SAlex Richardson }
61*2aa3ef28SAlex Richardson lowfd = strtol(argv[1], NULL, 10);
62*2aa3ef28SAlex Richardson if (lowfd < 3) {
63*2aa3ef28SAlex Richardson fprintf(stderr, "%s: Invalid lowfd %d (as str: %s) \n",
64*2aa3ef28SAlex Richardson getprogname(), argc, argv[1]);
65*2aa3ef28SAlex Richardson return EXIT_FAILURE;
66*2aa3ef28SAlex Richardson }
67*2aa3ef28SAlex Richardson
6857718be8SEnji Cooper strcpy(buf, "test...");
69*2aa3ef28SAlex Richardson /* First fd should be closed via addclose */
70*2aa3ef28SAlex Richardson if (read(lowfd, buf, BUFSIZE) != -1 || errno != EBADF) {
71*2aa3ef28SAlex Richardson fprintf(stderr, "%s: first filedesc is not closed\n",
7257718be8SEnji Cooper getprogname());
7357718be8SEnji Cooper res = EXIT_FAILURE;
7457718be8SEnji Cooper }
75*2aa3ef28SAlex Richardson /* Next file desc should be closed via closeonexec */
76*2aa3ef28SAlex Richardson if (read(lowfd + 1, buf, BUFSIZE) != -1 || errno != EBADF) {
77*2aa3ef28SAlex Richardson fprintf(stderr, "%s: filedesc +1 is not closed\n",
7857718be8SEnji Cooper getprogname());
7957718be8SEnji Cooper res = EXIT_FAILURE;
8057718be8SEnji Cooper }
81*2aa3ef28SAlex Richardson /* file desc + 2 remains open */
82*2aa3ef28SAlex Richardson if (write(lowfd + 2, buf, BUFSIZE) <= 0) {
83*2aa3ef28SAlex Richardson fprintf(stderr, "%s: could not write to filedesc +2\n",
8457718be8SEnji Cooper getprogname());
8557718be8SEnji Cooper res = EXIT_FAILURE;
8657718be8SEnji Cooper }
87*2aa3ef28SAlex Richardson /* file desc + 3 should be open (via addopen) */
88*2aa3ef28SAlex Richardson if (write(lowfd + 3, buf, BUFSIZE) <= 0) {
89*2aa3ef28SAlex Richardson fprintf(stderr, "%s: could not write to filedesc +3\n",
9057718be8SEnji Cooper getprogname());
9157718be8SEnji Cooper res = EXIT_FAILURE;
9257718be8SEnji Cooper }
93*2aa3ef28SAlex Richardson /* file desc + 4 should refer to stdout */
9457718be8SEnji Cooper fflush(stdout);
9557718be8SEnji Cooper if (fstat(fileno(stdout), &sb0) != 0) {
9657718be8SEnji Cooper fprintf(stderr, "%s: could not fstat stdout\n",
9757718be8SEnji Cooper getprogname());
9857718be8SEnji Cooper res = EXIT_FAILURE;
9957718be8SEnji Cooper }
100*2aa3ef28SAlex Richardson if (fstat(lowfd + 4, &sb1) != 0) {
101*2aa3ef28SAlex Richardson fprintf(stderr, "%s: could not fstat filedesc +4\n",
10257718be8SEnji Cooper getprogname());
10357718be8SEnji Cooper res = EXIT_FAILURE;
10457718be8SEnji Cooper }
105*2aa3ef28SAlex Richardson if (write(lowfd + 4, buf, strlen(buf)) <= 0) {
106*2aa3ef28SAlex Richardson fprintf(stderr, "%s: could not write to filedesc +4\n",
10757718be8SEnji Cooper getprogname());
10857718be8SEnji Cooper res = EXIT_FAILURE;
10957718be8SEnji Cooper }
11057718be8SEnji Cooper if (memcmp(&sb0, &sb1, sizeof sb0) != 0) {
11157718be8SEnji Cooper fprintf(stderr, "%s: stat results differ\n", getprogname());
11257718be8SEnji Cooper res = EXIT_FAILURE;
11357718be8SEnji Cooper }
11457718be8SEnji Cooper
11557718be8SEnji Cooper return res;
11657718be8SEnji Cooper }
11757718be8SEnji Cooper
118