xref: /netbsd-src/tests/lib/libc/gen/posix_spawn/h_spawnattr.c (revision c0746c1e9a05705192ac7c39a7590c9a43d72db6)
1*c0746c1eSchristos /* $NetBSD: h_spawnattr.c,v 1.3 2021/11/07 15:46:20 christos Exp $ */
20ce98f42Smartin 
30ce98f42Smartin /*-
40ce98f42Smartin  * Copyright (c) 2012 The NetBSD Foundation, Inc.
50ce98f42Smartin  * All rights reserved.
60ce98f42Smartin  *
70ce98f42Smartin  * This code is derived from software contributed to The NetBSD Foundation
80ce98f42Smartin  * by Charles Zhang <charles@NetBSD.org> and
90ce98f42Smartin  * Martin Husemann <martin@NetBSD.org>.
100ce98f42Smartin  *
110ce98f42Smartin  * Redistribution and use in source and binary forms, with or without
120ce98f42Smartin  * modification, are permitted provided that the following conditions
130ce98f42Smartin  * are met:
140ce98f42Smartin  * 1. Redistributions of source code must retain the above copyright
150ce98f42Smartin  *    notice, this list of conditions and the following disclaimer.
160ce98f42Smartin  * 2. Redistributions in binary form must reproduce the above copyright
170ce98f42Smartin  *    notice, this list of conditions and the following disclaimer in the
180ce98f42Smartin  *    documentation and/or other materials provided with the distribution.
190ce98f42Smartin  *
200ce98f42Smartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
210ce98f42Smartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
220ce98f42Smartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
230ce98f42Smartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
240ce98f42Smartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
250ce98f42Smartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
260ce98f42Smartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
270ce98f42Smartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
280ce98f42Smartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
290ce98f42Smartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
300ce98f42Smartin  * POSSIBILITY OF SUCH DAMAGE.
310ce98f42Smartin  */
32*c0746c1eSchristos #include <sys/cdefs.h>
33*c0746c1eSchristos __RCSID("$NetBSD: h_spawnattr.c,v 1.3 2021/11/07 15:46:20 christos Exp $");
340ce98f42Smartin 
350ce98f42Smartin #include <errno.h>
360ce98f42Smartin #include <stdio.h>
370ce98f42Smartin #include <stdlib.h>
380ce98f42Smartin #include <signal.h>
390ce98f42Smartin #include <unistd.h>
400ce98f42Smartin 
410ce98f42Smartin /*
420ce98f42Smartin  * Helper to test the hardcoded assumptions from t_spawnattr.c
43c69f42d3Sandvar  * Exit with appropriate exit status and print diagnostics to
440ce98f42Smartin  * stderr explaining what is wrong.
450ce98f42Smartin  */
460ce98f42Smartin int
main(int argc,char ** argv)470ce98f42Smartin main(int argc, char **argv)
480ce98f42Smartin {
490ce98f42Smartin 	int parent_pipe, res = EXIT_SUCCESS;
500ce98f42Smartin 	sigset_t sig;
510ce98f42Smartin 	struct sigaction act;
520ce98f42Smartin 	ssize_t rd;
530ce98f42Smartin 	char tmp;
540ce98f42Smartin 
550ce98f42Smartin 	sigemptyset(&sig);
560ce98f42Smartin 	if (sigprocmask(0, NULL, &sig) < 0) {
570ce98f42Smartin 		fprintf(stderr, "%s: sigprocmask error\n", getprogname());
580ce98f42Smartin 		res = EXIT_FAILURE;
590ce98f42Smartin 	}
600ce98f42Smartin 	if (!sigismember(&sig, SIGUSR1)) {
610ce98f42Smartin 		fprintf(stderr, "%s: SIGUSR not in procmask\n", getprogname());
620ce98f42Smartin 		res = EXIT_FAILURE;
630ce98f42Smartin 	}
640ce98f42Smartin 	if (sigaction(SIGUSR1, NULL, &act) < 0) {
650ce98f42Smartin 		fprintf(stderr, "%s: sigaction error\n", getprogname());
660ce98f42Smartin 		res = EXIT_FAILURE;
670ce98f42Smartin 	}
680ce98f42Smartin 	if (act.sa_sigaction != (void *)SIG_DFL) {
690ce98f42Smartin 		fprintf(stderr, "%s: SIGUSR1 action != SIG_DFL\n",
700ce98f42Smartin 		    getprogname());
710ce98f42Smartin 		res = EXIT_FAILURE;
720ce98f42Smartin 	}
730ce98f42Smartin 
740ce98f42Smartin 	if (argc >= 2) {
750ce98f42Smartin 		parent_pipe = atoi(argv[1]);
760ce98f42Smartin 		if (parent_pipe > 2) {
770ce98f42Smartin 			printf("%s: waiting for command from parent on pipe "
780ce98f42Smartin 			    "%d\n", getprogname(), parent_pipe);
790ce98f42Smartin 			rd = read(parent_pipe, &tmp, 1);
800ce98f42Smartin 			if (rd == 1) {
810ce98f42Smartin 				printf("%s: got command %c from parent\n",
820ce98f42Smartin 				    getprogname(), tmp);
830ce98f42Smartin 			} else if (rd == -1) {
840ce98f42Smartin 				printf("%s: %d is no pipe, errno %d\n",
850ce98f42Smartin 				    getprogname(), parent_pipe, errno);
860ce98f42Smartin 				res = EXIT_FAILURE;
870ce98f42Smartin 			}
880ce98f42Smartin 		}
890ce98f42Smartin 	}
900ce98f42Smartin 
910ce98f42Smartin 	return res;
920ce98f42Smartin }
93