xref: /minix3/external/bsd/atf/dist/atf-c/detail/process_helpers.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*
2*11be35a1SLionel Sambuc  * Automated Testing Framework (atf)
3*11be35a1SLionel Sambuc  *
4*11be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc  */
29*11be35a1SLionel Sambuc 
30*11be35a1SLionel Sambuc #include <sys/types.h>
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <assert.h> /* NO_CHECK_STYLE */
33*11be35a1SLionel Sambuc #include <signal.h>
34*11be35a1SLionel Sambuc #include <stdio.h>
35*11be35a1SLionel Sambuc #include <stdlib.h>
36*11be35a1SLionel Sambuc #include <string.h>
37*11be35a1SLionel Sambuc #include <unistd.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc static
40*11be35a1SLionel Sambuc int
h_echo(const char * msg)41*11be35a1SLionel Sambuc h_echo(const char *msg)
42*11be35a1SLionel Sambuc {
43*11be35a1SLionel Sambuc     printf("%s\n", msg);
44*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
45*11be35a1SLionel Sambuc }
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc static
48*11be35a1SLionel Sambuc int
h_exit_failure(void)49*11be35a1SLionel Sambuc h_exit_failure(void)
50*11be35a1SLionel Sambuc {
51*11be35a1SLionel Sambuc     return EXIT_FAILURE;
52*11be35a1SLionel Sambuc }
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc static
55*11be35a1SLionel Sambuc int
h_exit_signal(void)56*11be35a1SLionel Sambuc h_exit_signal(void)
57*11be35a1SLionel Sambuc {
58*11be35a1SLionel Sambuc     kill(getpid(), SIGKILL);
59*11be35a1SLionel Sambuc     assert(0); /* NO_CHECK_STYLE */
60*11be35a1SLionel Sambuc     return EXIT_FAILURE;
61*11be35a1SLionel Sambuc }
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc static
64*11be35a1SLionel Sambuc int
h_exit_success(void)65*11be35a1SLionel Sambuc h_exit_success(void)
66*11be35a1SLionel Sambuc {
67*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
68*11be35a1SLionel Sambuc }
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc static
71*11be35a1SLionel Sambuc int
h_stdout_stderr(const char * id)72*11be35a1SLionel Sambuc h_stdout_stderr(const char *id)
73*11be35a1SLionel Sambuc {
74*11be35a1SLionel Sambuc     fprintf(stdout, "Line 1 to stdout for %s\n", id);
75*11be35a1SLionel Sambuc     fprintf(stdout, "Line 2 to stdout for %s\n", id);
76*11be35a1SLionel Sambuc     fprintf(stderr, "Line 1 to stderr for %s\n", id);
77*11be35a1SLionel Sambuc     fprintf(stderr, "Line 2 to stderr for %s\n", id);
78*11be35a1SLionel Sambuc 
79*11be35a1SLionel Sambuc     return EXIT_SUCCESS;
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc static
83*11be35a1SLionel Sambuc void
check_args(const int argc,const char * const argv[],const int required)84*11be35a1SLionel Sambuc check_args(const int argc, const char *const argv[], const int required)
85*11be35a1SLionel Sambuc {
86*11be35a1SLionel Sambuc     if (argc < required) {
87*11be35a1SLionel Sambuc         fprintf(stderr, "Usage: %s helper-name [args]\n", argv[0]);
88*11be35a1SLionel Sambuc         exit(EXIT_FAILURE);
89*11be35a1SLionel Sambuc     }
90*11be35a1SLionel Sambuc }
91*11be35a1SLionel Sambuc 
92*11be35a1SLionel Sambuc int
main(int argc,const char * const argv[])93*11be35a1SLionel Sambuc main(int argc, const char *const argv[])
94*11be35a1SLionel Sambuc {
95*11be35a1SLionel Sambuc     int exitcode;
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc     check_args(argc, argv, 2);
98*11be35a1SLionel Sambuc 
99*11be35a1SLionel Sambuc     if (strcmp(argv[1], "echo") == 0) {
100*11be35a1SLionel Sambuc         check_args(argc, argv, 3);
101*11be35a1SLionel Sambuc         exitcode = h_echo(argv[2]);
102*11be35a1SLionel Sambuc     } else if (strcmp(argv[1], "exit-failure") == 0)
103*11be35a1SLionel Sambuc         exitcode = h_exit_failure();
104*11be35a1SLionel Sambuc     else if (strcmp(argv[1], "exit-signal") == 0)
105*11be35a1SLionel Sambuc         exitcode = h_exit_signal();
106*11be35a1SLionel Sambuc     else if (strcmp(argv[1], "exit-success") == 0)
107*11be35a1SLionel Sambuc         exitcode = h_exit_success();
108*11be35a1SLionel Sambuc     else if (strcmp(argv[1], "stdout-stderr") == 0) {
109*11be35a1SLionel Sambuc         check_args(argc, argv, 3);
110*11be35a1SLionel Sambuc         exitcode = h_stdout_stderr(argv[2]);
111*11be35a1SLionel Sambuc     } else {
112*11be35a1SLionel Sambuc         fprintf(stderr, "%s: Unknown helper %s\n", argv[0], argv[1]);
113*11be35a1SLionel Sambuc         exitcode = EXIT_FAILURE;
114*11be35a1SLionel Sambuc     }
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc     return exitcode;
117*11be35a1SLionel Sambuc }
118