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