xref: /netbsd-src/external/bsd/libproc/dist/tests/target_prog.c (revision 52ebe1c2c4d4a306e5aa34057375d60716bcd6e9)
1fbcd1dd1Schristos /*-
2fbcd1dd1Schristos  * Copyright (c) 2014 Mark Johnston <markj@FreeBSD.org>
3fbcd1dd1Schristos  * All rights reserved.
4fbcd1dd1Schristos  *
5fbcd1dd1Schristos  * Redistribution and use in source and binary forms, with or without
6fbcd1dd1Schristos  * modification, are permitted provided that the following conditions
7fbcd1dd1Schristos  * are met:
8fbcd1dd1Schristos  * 1. Redistributions of source code must retain the above copyright
9fbcd1dd1Schristos  *    notice, this list of conditions and the following disclaimer.
10fbcd1dd1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
11fbcd1dd1Schristos  *    notice, this list of conditions and the following disclaimer in the
12fbcd1dd1Schristos  *    documentation and/or other materials provided with the distribution.
13fbcd1dd1Schristos  *
14fbcd1dd1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15fbcd1dd1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16fbcd1dd1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17fbcd1dd1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18fbcd1dd1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19fbcd1dd1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20fbcd1dd1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21fbcd1dd1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22fbcd1dd1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23fbcd1dd1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24fbcd1dd1Schristos  * SUCH DAMAGE.
25fbcd1dd1Schristos  */
26fbcd1dd1Schristos 
27fbcd1dd1Schristos #include <sys/cdefs.h>
28*52ebe1c2Schristos #ifdef __FBSDID
29fbcd1dd1Schristos __FBSDID("$FreeBSD: head/lib/libproc/tests/target_prog.c 271937 2014-09-21 21:25:41Z markj $");
30*52ebe1c2Schristos #endif
31*52ebe1c2Schristos __RCSID("$NetBSD: target_prog.c,v 1.2 2015/09/24 14:12:48 christos Exp $");
32fbcd1dd1Schristos 
33fbcd1dd1Schristos #include <err.h>
34fbcd1dd1Schristos #include <signal.h>
35fbcd1dd1Schristos #include <stdlib.h>
36fbcd1dd1Schristos #include <string.h>
37fbcd1dd1Schristos #include <unistd.h>
38fbcd1dd1Schristos 
39fbcd1dd1Schristos static volatile sig_atomic_t saw;
40fbcd1dd1Schristos 
41fbcd1dd1Schristos static void
usr1(int sig __unused)42fbcd1dd1Schristos usr1(int sig __unused)
43fbcd1dd1Schristos {
44fbcd1dd1Schristos 
45fbcd1dd1Schristos 	saw = 1;
46fbcd1dd1Schristos }
47fbcd1dd1Schristos 
48fbcd1dd1Schristos int
main(int argc,char ** argv)49fbcd1dd1Schristos main(int argc, char **argv)
50fbcd1dd1Schristos {
51fbcd1dd1Schristos 
52fbcd1dd1Schristos 	if (argc == 1)
53fbcd1dd1Schristos 		return (EXIT_SUCCESS);
54fbcd1dd1Schristos 	if (argc == 2 && strcmp(argv[1], "-s") == 0) {
55fbcd1dd1Schristos 		if (signal(SIGUSR1, usr1) == SIG_ERR)
56fbcd1dd1Schristos 			err(1, "signal");
57fbcd1dd1Schristos 		if (kill(getpid(), SIGUSR1) != 0)
58fbcd1dd1Schristos 			err(1, "kill");
59fbcd1dd1Schristos 		return (saw == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
60fbcd1dd1Schristos 	}
61fbcd1dd1Schristos 	return (EXIT_FAILURE);
62fbcd1dd1Schristos }
63