1*90540020Schristos /* $NetBSD: h_spawnattr.c,v 1.3 2021/11/07 15:46:20 christos Exp $ */
29633fa7dSmartin
39633fa7dSmartin /*-
49633fa7dSmartin * Copyright (c) 2012 The NetBSD Foundation, Inc.
59633fa7dSmartin * All rights reserved.
69633fa7dSmartin *
79633fa7dSmartin * This code is derived from software contributed to The NetBSD Foundation
89633fa7dSmartin * by Charles Zhang <charles@NetBSD.org> and
99633fa7dSmartin * Martin Husemann <martin@NetBSD.org>.
109633fa7dSmartin *
119633fa7dSmartin * Redistribution and use in source and binary forms, with or without
129633fa7dSmartin * modification, are permitted provided that the following conditions
139633fa7dSmartin * are met:
149633fa7dSmartin * 1. Redistributions of source code must retain the above copyright
159633fa7dSmartin * notice, this list of conditions and the following disclaimer.
169633fa7dSmartin * 2. Redistributions in binary form must reproduce the above copyright
179633fa7dSmartin * notice, this list of conditions and the following disclaimer in the
189633fa7dSmartin * documentation and/or other materials provided with the distribution.
199633fa7dSmartin *
209633fa7dSmartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219633fa7dSmartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229633fa7dSmartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239633fa7dSmartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249633fa7dSmartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259633fa7dSmartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269633fa7dSmartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279633fa7dSmartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289633fa7dSmartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299633fa7dSmartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309633fa7dSmartin * POSSIBILITY OF SUCH DAMAGE.
319633fa7dSmartin */
32*90540020Schristos #include <sys/cdefs.h>
33*90540020Schristos __RCSID("$NetBSD: h_spawnattr.c,v 1.3 2021/11/07 15:46:20 christos Exp $");
349633fa7dSmartin
359633fa7dSmartin #include <errno.h>
369633fa7dSmartin #include <stdio.h>
379633fa7dSmartin #include <stdlib.h>
389633fa7dSmartin #include <signal.h>
399633fa7dSmartin #include <unistd.h>
409633fa7dSmartin
419633fa7dSmartin /*
429633fa7dSmartin * Helper to test the hardcoded assumptions from t_spawnattr.c
43b47c7056Sandvar * Exit with appropriate exit status and print diagnostics to
449633fa7dSmartin * stderr explaining what is wrong.
459633fa7dSmartin */
469633fa7dSmartin int
main(int argc,char ** argv)479633fa7dSmartin main(int argc, char **argv)
489633fa7dSmartin {
499633fa7dSmartin int parent_pipe, res = EXIT_SUCCESS;
509633fa7dSmartin sigset_t sig;
519633fa7dSmartin struct sigaction act;
529633fa7dSmartin ssize_t rd;
539633fa7dSmartin char tmp;
549633fa7dSmartin
559633fa7dSmartin sigemptyset(&sig);
569633fa7dSmartin if (sigprocmask(0, NULL, &sig) < 0) {
579633fa7dSmartin fprintf(stderr, "%s: sigprocmask error\n", getprogname());
589633fa7dSmartin res = EXIT_FAILURE;
599633fa7dSmartin }
609633fa7dSmartin if (!sigismember(&sig, SIGUSR1)) {
619633fa7dSmartin fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname());
629633fa7dSmartin res = EXIT_FAILURE;
639633fa7dSmartin }
649633fa7dSmartin if (sigaction(SIGUSR1, NULL, &act) < 0) {
659633fa7dSmartin fprintf(stderr, "%s: sigaction error\n", getprogname());
669633fa7dSmartin res = EXIT_FAILURE;
679633fa7dSmartin }
689633fa7dSmartin if (act.sa_sigaction != (void *)SIG_DFL) {
699633fa7dSmartin fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n",
709633fa7dSmartin getprogname());
719633fa7dSmartin res = EXIT_FAILURE;
729633fa7dSmartin }
739633fa7dSmartin
749633fa7dSmartin if (argc >= 2) {
759633fa7dSmartin parent_pipe = atoi(argv[1]);
769633fa7dSmartin if (parent_pipe > 2) {
779633fa7dSmartin printf("%s: waiting for command from parent on pipe "
789633fa7dSmartin "%d\n", getprogname(), parent_pipe);
799633fa7dSmartin rd = read(parent_pipe, &tmp, 1);
809633fa7dSmartin if (rd == 1) {
819633fa7dSmartin printf("%s: got command %c from parent\n",
829633fa7dSmartin getprogname(), tmp);
839633fa7dSmartin } else if (rd == -1) {
849633fa7dSmartin printf("%s: %d is no pipe, errno %d\n",
859633fa7dSmartin getprogname(), parent_pipe, errno);
869633fa7dSmartin res = EXIT_FAILURE;
879633fa7dSmartin }
889633fa7dSmartin }
899633fa7dSmartin }
909633fa7dSmartin
919633fa7dSmartin return res;
929633fa7dSmartin }
93