157718be8SEnji Cooper /* $NetBSD: t_fork.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $ */ 257718be8SEnji Cooper 357718be8SEnji Cooper /* 457718be8SEnji Cooper * Copyright (c) 2008 The NetBSD Foundation, Inc. 557718be8SEnji Cooper * All rights reserved. 657718be8SEnji Cooper * 757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without 857718be8SEnji Cooper * modification, are permitted provided that the following conditions 957718be8SEnji Cooper * are met: 1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright 1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer. 1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright 1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the 1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution. 1557718be8SEnji Cooper * 1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE. 2757718be8SEnji Cooper */ 2857718be8SEnji Cooper 2957718be8SEnji Cooper #include <sys/cdefs.h> 3057718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\ 3157718be8SEnji Cooper The NetBSD Foundation, inc. All rights reserved."); 3257718be8SEnji Cooper __RCSID("$NetBSD: t_fork.c,v 1.1 2010/07/16 15:42:53 jmmv Exp $"); 3357718be8SEnji Cooper 3457718be8SEnji Cooper /* 3557718be8SEnji Cooper * Written by Love H�rnquist �strand <lha@NetBSD.org>, March 2003. 3657718be8SEnji Cooper * Public domain. 3757718be8SEnji Cooper */ 3857718be8SEnji Cooper 3957718be8SEnji Cooper #include <sys/types.h> 4057718be8SEnji Cooper #include <sys/wait.h> 4157718be8SEnji Cooper 4257718be8SEnji Cooper #include <errno.h> 4357718be8SEnji Cooper #include <pthread.h> 4457718be8SEnji Cooper #include <signal.h> 4557718be8SEnji Cooper #include <stdio.h> 4657718be8SEnji Cooper #include <stdlib.h> 4757718be8SEnji Cooper #include <string.h> 4857718be8SEnji Cooper #include <unistd.h> 4957718be8SEnji Cooper 5057718be8SEnji Cooper #include <atf-c.h> 5157718be8SEnji Cooper 5257718be8SEnji Cooper #include "h_common.h" 5357718be8SEnji Cooper 5457718be8SEnji Cooper static pid_t parent; 5557718be8SEnji Cooper static int thread_survived = 0; 5657718be8SEnji Cooper 5757718be8SEnji Cooper static void * 5857718be8SEnji Cooper print_pid(void *arg) 5957718be8SEnji Cooper { 6057718be8SEnji Cooper sleep(3); 6157718be8SEnji Cooper 6257718be8SEnji Cooper thread_survived = 1; 6357718be8SEnji Cooper if (parent != getpid()) { 64*a81c27fdSEnji Cooper #ifdef __FreeBSD__ 65*a81c27fdSEnji Cooper _exit(1); 66*a81c27fdSEnji Cooper #else 6757718be8SEnji Cooper exit(1); 68*a81c27fdSEnji Cooper #endif 6957718be8SEnji Cooper } 7057718be8SEnji Cooper return NULL; 7157718be8SEnji Cooper } 7257718be8SEnji Cooper 7357718be8SEnji Cooper ATF_TC(fork); 7457718be8SEnji Cooper ATF_TC_HEAD(fork, tc) 7557718be8SEnji Cooper { 7657718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", 7757718be8SEnji Cooper "Checks that child process doesn't get threads"); 7857718be8SEnji Cooper } 7957718be8SEnji Cooper ATF_TC_BODY(fork, tc) 8057718be8SEnji Cooper { 8157718be8SEnji Cooper pthread_t p; 8257718be8SEnji Cooper pid_t fork_pid; 8357718be8SEnji Cooper 8457718be8SEnji Cooper parent = getpid(); 8557718be8SEnji Cooper 8657718be8SEnji Cooper PTHREAD_REQUIRE(pthread_create(&p, NULL, print_pid, NULL)); 8757718be8SEnji Cooper 8857718be8SEnji Cooper fork_pid = fork(); 8957718be8SEnji Cooper ATF_REQUIRE(fork_pid != -1); 9057718be8SEnji Cooper 9157718be8SEnji Cooper if (fork_pid) { 9257718be8SEnji Cooper int status; 9357718be8SEnji Cooper 9457718be8SEnji Cooper PTHREAD_REQUIRE(pthread_join(p, NULL)); 9557718be8SEnji Cooper ATF_REQUIRE_MSG(thread_survived, "thread did not survive in parent"); 9657718be8SEnji Cooper 9757718be8SEnji Cooper waitpid(fork_pid, &status, 0); 9857718be8SEnji Cooper ATF_REQUIRE_MSG(WIFEXITED(status), "child died wrongly"); 9957718be8SEnji Cooper ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), 0, "thread survived in child"); 10057718be8SEnji Cooper } else { 10157718be8SEnji Cooper sleep(5); 102*a81c27fdSEnji Cooper #ifdef __FreeBSD__ 103*a81c27fdSEnji Cooper _exit(thread_survived ? 1 : 0); 104*a81c27fdSEnji Cooper #else 10557718be8SEnji Cooper exit(thread_survived ? 1 : 0); 106*a81c27fdSEnji Cooper #endif 10757718be8SEnji Cooper } 10857718be8SEnji Cooper } 10957718be8SEnji Cooper 11057718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 11157718be8SEnji Cooper { 11257718be8SEnji Cooper ATF_TP_ADD_TC(tp, fork); 11357718be8SEnji Cooper 11457718be8SEnji Cooper return atf_no_error(); 11557718be8SEnji Cooper } 116