Path: blob/main/contrib/atf/atf-c/detail/process_helpers.c
39536 views
/* Copyright (c) 2008 The NetBSD Foundation, Inc.1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13* CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */2425#include <sys/types.h>2627#include <assert.h> /* NO_CHECK_STYLE */28#include <signal.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <unistd.h>3334static35int36h_echo(const char *msg)37{38printf("%s\n", msg);39return EXIT_SUCCESS;40}4142static43int44h_exit_failure(void)45{46return EXIT_FAILURE;47}4849static50int51h_exit_signal(void)52{53kill(getpid(), SIGKILL);54assert(0); /* NO_CHECK_STYLE */55return EXIT_FAILURE;56}5758static59int60h_exit_success(void)61{62return EXIT_SUCCESS;63}6465static66int67h_stdout_stderr(const char *id)68{69fprintf(stdout, "Line 1 to stdout for %s\n", id);70fprintf(stdout, "Line 2 to stdout for %s\n", id);71fprintf(stderr, "Line 1 to stderr for %s\n", id);72fprintf(stderr, "Line 2 to stderr for %s\n", id);7374return EXIT_SUCCESS;75}7677static78void79check_args(const int argc, const char *const argv[], const int required)80{81if (argc < required) {82fprintf(stderr, "Usage: %s helper-name [args]\n", argv[0]);83exit(EXIT_FAILURE);84}85}8687int88main(int argc, const char *const argv[])89{90int exitcode;9192check_args(argc, argv, 2);9394if (strcmp(argv[1], "echo") == 0) {95check_args(argc, argv, 3);96exitcode = h_echo(argv[2]);97} else if (strcmp(argv[1], "exit-failure") == 0)98exitcode = h_exit_failure();99else if (strcmp(argv[1], "exit-signal") == 0)100exitcode = h_exit_signal();101else if (strcmp(argv[1], "exit-success") == 0)102exitcode = h_exit_success();103else if (strcmp(argv[1], "stdout-stderr") == 0) {104check_args(argc, argv, 3);105exitcode = h_stdout_stderr(argv[2]);106} else {107fprintf(stderr, "%s: Unknown helper %s\n", argv[0], argv[1]);108exitcode = EXIT_FAILURE;109}110111return exitcode;112}113114115