1207536Smav/*- 2207536Smav * Copyright (c) 2011 Jilles Tjoelker 3207536Smav * All rights reserved. 4207536Smav * 5207536Smav * Redistribution and use in source and binary forms, with or without 6207536Smav * modification, are permitted provided that the following conditions 7207536Smav * are met: 8207536Smav * 1. Redistributions of source code must retain the above copyright 9207536Smav * notice, this list of conditions and the following disclaimer. 10207536Smav * 2. Redistributions in binary form must reproduce the above copyright 11207536Smav * notice, this list of conditions and the following disclaimer in the 12207536Smav * documentation and/or other materials provided with the distribution. 13207536Smav * 14207536Smav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15207536Smav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16207536Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17207536Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18207536Smav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19207536Smav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20207536Smav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21207536Smav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22207536Smav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23207536Smav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24207536Smav * SUCH DAMAGE. 25207536Smav */ 26207536Smav 27207536Smav/* 28207536Smav * Test program for posix_spawn() and posix_spawnp() as specified by 29207536Smav * IEEE Std. 1003.1-2008. 30207536Smav */ 31207536Smav 32207536Smav#include <sys/cdefs.h> 33207536Smav__FBSDID("$FreeBSD: releng/11.0/lib/libc/tests/gen/posix_spawn_test.c 290572 2015-11-09 06:24:11Z ngie $"); 34207536Smav 35207536Smav#include <sys/wait.h> 36207536Smav#include <errno.h> 37207536Smav#include <stdio.h> 38207536Smav#include <stdlib.h> 39207536Smav#include <string.h> 40207536Smav#include <spawn.h> 41207536Smav 42207536Smav#include <atf-c.h> 43207536Smav 44207536Smavchar *myenv[2] = { "answer=42", NULL }; 45207536Smav 46207536SmavATF_TC_WITHOUT_HEAD(posix_spawn_simple_test); 47207536SmavATF_TC_BODY(posix_spawn_simple_test, tc) 48207536Smav{ 49207536Smav char *myargs[4]; 50207536Smav int error, status; 51207536Smav pid_t pid, waitres; 52207536Smav 53207536Smav /* Make sure we have no child processes. */ 54207536Smav while (waitpid(-1, NULL, 0) != -1) 55207536Smav ; 56207536Smav ATF_REQUIRE_MSG(errno == ECHILD, "errno was not ECHILD: %d", errno); 57207536Smav 58207536Smav /* Simple test. */ 59207536Smav myargs[0] = "sh"; 60207536Smav myargs[1] = "-c"; 61207536Smav myargs[2] = "exit $answer"; 62207536Smav myargs[3] = NULL; 63207536Smav error = posix_spawnp(&pid, myargs[0], NULL, NULL, myargs, myenv); 64207536Smav ATF_REQUIRE(error == 0); 65207536Smav waitres = waitpid(pid, &status, 0); 66207536Smav ATF_REQUIRE(waitres == pid); 67207536Smav ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 42); 68207536Smav} 69207536Smav 70207536SmavATF_TC_WITHOUT_HEAD(posix_spawn_no_such_command_negative_test); 71207536SmavATF_TC_BODY(posix_spawn_no_such_command_negative_test, tc) 72207536Smav{ 73207536Smav char *myargs[4]; 74207536Smav int error, status; 75207536Smav pid_t pid, waitres; 76207536Smav 77207536Smav /* 78207536Smav * If the executable does not exist, the function shall either fail 79207536Smav * and not create a child process or succeed and create a child 80207536Smav * process that exits with status 127. 81207536Smav */ 82207536Smav myargs[0] = "/var/empty/nonexistent"; 83207536Smav myargs[1] = NULL; 84207536Smav error = posix_spawn(&pid, myargs[0], NULL, NULL, myargs, myenv); 85207536Smav if (error == 0) { 86207536Smav waitres = waitpid(pid, &status, 0); 87207536Smav ATF_REQUIRE(waitres == pid); 88207536Smav ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 127); 89207536Smav } else { 90207536Smav ATF_REQUIRE(error == ENOENT); 91207536Smav waitres = waitpid(-1, NULL, 0); 92207536Smav ATF_REQUIRE(waitres == -1 && errno == ECHILD); 93207536Smav } 94207536Smav} 95207536Smav 96207536SmavATF_TP_ADD_TCS(tp) 97207536Smav{ 98207536Smav 99207536Smav ATF_TP_ADD_TC(tp, posix_spawn_simple_test); 100207536Smav ATF_TP_ADD_TC(tp, posix_spawn_no_such_command_negative_test); 101207536Smav 102207536Smav return (atf_no_error()); 103207536Smav} 104207536Smav