1*57718be8SEnji Cooper /* $NetBSD: h_spawnattr.c,v 1.1 2012/02/13 21:03:08 martin Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2012 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper * by Charles Zhang <charles@NetBSD.org> and
9*57718be8SEnji Cooper * Martin Husemann <martin@NetBSD.org>.
10*57718be8SEnji Cooper *
11*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
12*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
13*57718be8SEnji Cooper * are met:
14*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
15*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
16*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
17*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
18*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
19*57718be8SEnji Cooper *
20*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
31*57718be8SEnji Cooper */
32*57718be8SEnji Cooper
33*57718be8SEnji Cooper #include <errno.h>
34*57718be8SEnji Cooper #include <stdio.h>
35*57718be8SEnji Cooper #include <stdlib.h>
36*57718be8SEnji Cooper #include <signal.h>
37*57718be8SEnji Cooper #include <unistd.h>
38*57718be8SEnji Cooper
39*57718be8SEnji Cooper /*
40*57718be8SEnji Cooper * Helper to test the hardcoded assumptions from t_spawnattr.c
41*57718be8SEnji Cooper * Exit with apropriate exit status and print diagnostics to
42*57718be8SEnji Cooper * stderr explaining what is wrong.
43*57718be8SEnji Cooper */
44*57718be8SEnji Cooper int
main(int argc,char ** argv)45*57718be8SEnji Cooper main(int argc, char **argv)
46*57718be8SEnji Cooper {
47*57718be8SEnji Cooper int parent_pipe, res = EXIT_SUCCESS;
48*57718be8SEnji Cooper sigset_t sig;
49*57718be8SEnji Cooper struct sigaction act;
50*57718be8SEnji Cooper ssize_t rd;
51*57718be8SEnji Cooper char tmp;
52*57718be8SEnji Cooper
53*57718be8SEnji Cooper sigemptyset(&sig);
54*57718be8SEnji Cooper if (sigprocmask(0, NULL, &sig) < 0) {
55*57718be8SEnji Cooper fprintf(stderr, "%s: sigprocmask error\n", getprogname());
56*57718be8SEnji Cooper res = EXIT_FAILURE;
57*57718be8SEnji Cooper }
58*57718be8SEnji Cooper if (!sigismember(&sig, SIGUSR1)) {
59*57718be8SEnji Cooper fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname());
60*57718be8SEnji Cooper res = EXIT_FAILURE;
61*57718be8SEnji Cooper }
62*57718be8SEnji Cooper if (sigaction(SIGUSR1, NULL, &act) < 0) {
63*57718be8SEnji Cooper fprintf(stderr, "%s: sigaction error\n", getprogname());
64*57718be8SEnji Cooper res = EXIT_FAILURE;
65*57718be8SEnji Cooper }
66*57718be8SEnji Cooper if (act.sa_sigaction != (void *)SIG_DFL) {
67*57718be8SEnji Cooper fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n",
68*57718be8SEnji Cooper getprogname());
69*57718be8SEnji Cooper res = EXIT_FAILURE;
70*57718be8SEnji Cooper }
71*57718be8SEnji Cooper
72*57718be8SEnji Cooper if (argc >= 2) {
73*57718be8SEnji Cooper parent_pipe = atoi(argv[1]);
74*57718be8SEnji Cooper if (parent_pipe > 2) {
75*57718be8SEnji Cooper printf("%s: waiting for command from parent on pipe "
76*57718be8SEnji Cooper "%d\n", getprogname(), parent_pipe);
77*57718be8SEnji Cooper rd = read(parent_pipe, &tmp, 1);
78*57718be8SEnji Cooper if (rd == 1) {
79*57718be8SEnji Cooper printf("%s: got command %c from parent\n",
80*57718be8SEnji Cooper getprogname(), tmp);
81*57718be8SEnji Cooper } else if (rd == -1) {
82*57718be8SEnji Cooper printf("%s: %d is no pipe, errno %d\n",
83*57718be8SEnji Cooper getprogname(), parent_pipe, errno);
84*57718be8SEnji Cooper res = EXIT_FAILURE;
85*57718be8SEnji Cooper }
86*57718be8SEnji Cooper }
87*57718be8SEnji Cooper }
88*57718be8SEnji Cooper
89*57718be8SEnji Cooper return res;
90*57718be8SEnji Cooper }
91