1272343Sngie/* $NetBSD: h_spawnattr.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 <errno.h>
34272343Sngie#include <stdio.h>
35272343Sngie#include <stdlib.h>
36272343Sngie#include <signal.h>
37272343Sngie#include <unistd.h>
38272343Sngie
39272343Sngie/*
40272343Sngie * Helper to test the hardcoded assumptions from t_spawnattr.c
41272343Sngie * Exit with apropriate exit status and print diagnostics to
42272343Sngie * stderr explaining what is wrong.
43272343Sngie */
44272343Sngieint
45272343Sngiemain(int argc, char **argv)
46272343Sngie{
47272343Sngie	int parent_pipe, res = EXIT_SUCCESS;
48272343Sngie	sigset_t sig;
49272343Sngie	struct sigaction act;
50272343Sngie	ssize_t rd;
51272343Sngie	char tmp;
52272343Sngie
53272343Sngie	sigemptyset(&sig);
54272343Sngie	if (sigprocmask(0, NULL, &sig) < 0) {
55272343Sngie		fprintf(stderr, "%s: sigprocmask error\n", getprogname());
56272343Sngie		res = EXIT_FAILURE;
57272343Sngie	}
58272343Sngie	if (!sigismember(&sig, SIGUSR1)) {
59272343Sngie		fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname());
60272343Sngie		res = EXIT_FAILURE;
61272343Sngie	}
62272343Sngie	if (sigaction(SIGUSR1, NULL, &act) < 0) {
63272343Sngie		fprintf(stderr, "%s: sigaction error\n", getprogname());
64272343Sngie		res = EXIT_FAILURE;
65272343Sngie	}
66272343Sngie	if (act.sa_sigaction != (void *)SIG_DFL) {
67272343Sngie		fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n",
68272343Sngie		    getprogname());
69272343Sngie		res = EXIT_FAILURE;
70272343Sngie	}
71272343Sngie
72272343Sngie	if (argc >= 2) {
73272343Sngie		parent_pipe = atoi(argv[1]);
74272343Sngie		if (parent_pipe > 2) {
75272343Sngie			printf("%s: waiting for command from parent on pipe "
76272343Sngie			    "%d\n", getprogname(), parent_pipe);
77272343Sngie			rd = read(parent_pipe, &tmp, 1);
78272343Sngie			if (rd == 1) {
79272343Sngie				printf("%s: got command %c from parent\n",
80272343Sngie				    getprogname(), tmp);
81272343Sngie			} else if (rd == -1) {
82272343Sngie				printf("%s: %d is no pipe, errno %d\n",
83272343Sngie				    getprogname(), parent_pipe, errno);
84272343Sngie				res = EXIT_FAILURE;
85272343Sngie			}
86272343Sngie		}
87272343Sngie	}
88272343Sngie
89272343Sngie	return res;
90272343Sngie}
91