1219820Sjeff/* $NetBSD: h_spawnattr.c,v 1.1 2012/02/11 23:31:25 martin Exp $ */
2219820Sjeff
3219820Sjeff/*-
4219820Sjeff * Copyright (c) 2012 The NetBSD Foundation, Inc.
5219820Sjeff * All rights reserved.
6219820Sjeff *
7219820Sjeff * This code is derived from software contributed to The NetBSD Foundation
8219820Sjeff * by Charles Zhang <charles@NetBSD.org> and
9219820Sjeff * Martin Husemann <martin@NetBSD.org>.
10219820Sjeff *
11219820Sjeff * Redistribution and use in source and binary forms, with or without
12219820Sjeff * modification, are permitted provided that the following conditions
13219820Sjeff * are met:
14219820Sjeff * 1. Redistributions of source code must retain the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer.
16219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
17219820Sjeff *    notice, this list of conditions and the following disclaimer in the
18219820Sjeff *    documentation and/or other materials provided with the distribution.
19219820Sjeff *
20219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21219820Sjeff * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22219820Sjeff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23219820Sjeff * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24219820Sjeff * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25219820Sjeff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26219820Sjeff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27219820Sjeff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28219820Sjeff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29219820Sjeff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30219820Sjeff * POSSIBILITY OF SUCH DAMAGE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#include <errno.h>
34219820Sjeff#include <stdio.h>
35219820Sjeff#include <stdlib.h>
36219820Sjeff#include <signal.h>
37219820Sjeff#include <unistd.h>
38219820Sjeff
39219820Sjeff/*
40219820Sjeff * Helper to test the hardcoded assumptions from t_spawnattr.c
41219820Sjeff * Exit with apropriate exit status and print diagnostics to
42219820Sjeff * stderr explaining what is wrong.
43219820Sjeff */
44219820Sjeffint
45219820Sjeffmain(int argc, char **argv)
46219820Sjeff{
47219820Sjeff	int parent_pipe, res = EXIT_SUCCESS;
48219820Sjeff	sigset_t sig;
49219820Sjeff	struct sigaction act;
50219820Sjeff	ssize_t rd;
51219820Sjeff	char tmp;
52219820Sjeff
53219820Sjeff	sigemptyset(&sig);
54219820Sjeff	if (sigprocmask(0, NULL, &sig) < 0) {
55219820Sjeff		fprintf(stderr, "%s: sigprocmask error\n", getprogname());
56219820Sjeff		res = EXIT_FAILURE;
57219820Sjeff	}
58219820Sjeff	if (!sigismember(&sig, SIGUSR1)) {
59219820Sjeff		fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname());
60219820Sjeff		res = EXIT_FAILURE;
61219820Sjeff	}
62219820Sjeff	if (sigaction(SIGUSR1, NULL, &act) < 0) {
63219820Sjeff		fprintf(stderr, "%s: sigaction error\n", getprogname());
64219820Sjeff		res = EXIT_FAILURE;
65219820Sjeff	}
66219820Sjeff	if (act.sa_sigaction != (void *)SIG_DFL) {
67219820Sjeff		fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n",
68219820Sjeff		    getprogname());
69219820Sjeff		res = EXIT_FAILURE;
70219820Sjeff	}
71219820Sjeff
72219820Sjeff	if (argc >= 2) {
73219820Sjeff		parent_pipe = atoi(argv[1]);
74219820Sjeff		if (parent_pipe > 2) {
75219820Sjeff			printf("%s: waiting for command from parent on pipe "
76219820Sjeff			    "%d\n", getprogname(), parent_pipe);
77219820Sjeff			rd = read(parent_pipe, &tmp, 1);
78219820Sjeff			if (rd == 1) {
79219820Sjeff				printf("%s: got command %c from parent\n",
80219820Sjeff				    getprogname(), tmp);
81219820Sjeff			} else if (rd == -1) {
82219820Sjeff				printf("%s: %d is no pipe, errno %d\n",
83219820Sjeff				    getprogname(), parent_pipe, errno);
84219820Sjeff				res = EXIT_FAILURE;
85219820Sjeff			}
86219820Sjeff		}
87219820Sjeff	}
88219820Sjeff
89219820Sjeff	return res;
90219820Sjeff}
91219820Sjeff