xref: /freebsd/lib/libproc/tests/target_prog.c (revision 1bdc41d25293032c4cba3940e17875d42e2cf797)
19351ac6dSMark Johnston /*-
29351ac6dSMark Johnston  * Copyright (c) 2014 Mark Johnston <markj@FreeBSD.org>
39351ac6dSMark Johnston  * All rights reserved.
49351ac6dSMark Johnston  *
59351ac6dSMark Johnston  * Redistribution and use in source and binary forms, with or without
69351ac6dSMark Johnston  * modification, are permitted provided that the following conditions
79351ac6dSMark Johnston  * are met:
89351ac6dSMark Johnston  * 1. Redistributions of source code must retain the above copyright
99351ac6dSMark Johnston  *    notice, this list of conditions and the following disclaimer.
109351ac6dSMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
119351ac6dSMark Johnston  *    notice, this list of conditions and the following disclaimer in the
129351ac6dSMark Johnston  *    documentation and/or other materials provided with the distribution.
139351ac6dSMark Johnston  *
149351ac6dSMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
159351ac6dSMark Johnston  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
169351ac6dSMark Johnston  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
179351ac6dSMark Johnston  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
189351ac6dSMark Johnston  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199351ac6dSMark Johnston  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
209351ac6dSMark Johnston  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
219351ac6dSMark Johnston  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
229351ac6dSMark Johnston  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
239351ac6dSMark Johnston  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
249351ac6dSMark Johnston  * SUCH DAMAGE.
259351ac6dSMark Johnston  */
269351ac6dSMark Johnston 
279351ac6dSMark Johnston #include <sys/cdefs.h>
289351ac6dSMark Johnston __FBSDID("$FreeBSD$");
299351ac6dSMark Johnston 
309351ac6dSMark Johnston #include <err.h>
319351ac6dSMark Johnston #include <signal.h>
329351ac6dSMark Johnston #include <stdlib.h>
339351ac6dSMark Johnston #include <string.h>
349351ac6dSMark Johnston #include <unistd.h>
359351ac6dSMark Johnston 
369351ac6dSMark Johnston static volatile sig_atomic_t saw;
379351ac6dSMark Johnston 
389351ac6dSMark Johnston static void
399351ac6dSMark Johnston usr1(int sig __unused)
409351ac6dSMark Johnston {
419351ac6dSMark Johnston 
429351ac6dSMark Johnston 	saw = 1;
439351ac6dSMark Johnston }
449351ac6dSMark Johnston 
45*1bdc41d2SMark Johnston void	foo(void);
46*1bdc41d2SMark Johnston void	qux(void);
47*1bdc41d2SMark Johnston 
48*1bdc41d2SMark Johnston void
49*1bdc41d2SMark Johnston foo(void)
50*1bdc41d2SMark Johnston {
51*1bdc41d2SMark Johnston }
52*1bdc41d2SMark Johnston __weak_reference(foo, _foo);
53*1bdc41d2SMark Johnston 
54*1bdc41d2SMark Johnston static void
55*1bdc41d2SMark Johnston bar(void)
56*1bdc41d2SMark Johnston {
57*1bdc41d2SMark Johnston }
58*1bdc41d2SMark Johnston __strong_reference(bar, baz);
59*1bdc41d2SMark Johnston 
60*1bdc41d2SMark Johnston void
61*1bdc41d2SMark Johnston qux(void)
62*1bdc41d2SMark Johnston {
63*1bdc41d2SMark Johnston }
64*1bdc41d2SMark Johnston __strong_reference(qux, $qux);
65*1bdc41d2SMark Johnston 
669351ac6dSMark Johnston int
679351ac6dSMark Johnston main(int argc, char **argv)
689351ac6dSMark Johnston {
699351ac6dSMark Johnston 
70*1bdc41d2SMark Johnston 	bar(); /* force the symbol to be emitted */
71*1bdc41d2SMark Johnston 
729351ac6dSMark Johnston 	if (argc == 1)
739351ac6dSMark Johnston 		return (EXIT_SUCCESS);
749351ac6dSMark Johnston 	if (argc == 2 && strcmp(argv[1], "-s") == 0) {
759351ac6dSMark Johnston 		if (signal(SIGUSR1, usr1) == SIG_ERR)
769351ac6dSMark Johnston 			err(1, "signal");
779351ac6dSMark Johnston 		if (kill(getpid(), SIGUSR1) != 0)
789351ac6dSMark Johnston 			err(1, "kill");
799351ac6dSMark Johnston 		return (saw == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
809351ac6dSMark Johnston 	}
819351ac6dSMark Johnston 	return (EXIT_FAILURE);
829351ac6dSMark Johnston }
83