1*3b0a9131SEnji Cooper /* $NetBSD: t_fork.c,v 1.2 2017/01/16 16:28:27 christos 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."); 32*3b0a9131SEnji Cooper __RCSID("$NetBSD: t_fork.c,v 1.2 2017/01/16 16:28:27 christos 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()) { 64a81c27fdSEnji Cooper _exit(1); 6557718be8SEnji Cooper } 6657718be8SEnji Cooper return NULL; 6757718be8SEnji Cooper } 6857718be8SEnji Cooper 6957718be8SEnji Cooper ATF_TC(fork); 7057718be8SEnji Cooper ATF_TC_HEAD(fork, tc) 7157718be8SEnji Cooper { 7257718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", 7357718be8SEnji Cooper "Checks that child process doesn't get threads"); 7457718be8SEnji Cooper } 7557718be8SEnji Cooper ATF_TC_BODY(fork, tc) 7657718be8SEnji Cooper { 7757718be8SEnji Cooper pthread_t p; 7857718be8SEnji Cooper pid_t fork_pid; 7957718be8SEnji Cooper 8057718be8SEnji Cooper parent = getpid(); 8157718be8SEnji Cooper 8257718be8SEnji Cooper PTHREAD_REQUIRE(pthread_create(&p, NULL, print_pid, NULL)); 8357718be8SEnji Cooper 8457718be8SEnji Cooper fork_pid = fork(); 8557718be8SEnji Cooper ATF_REQUIRE(fork_pid != -1); 8657718be8SEnji Cooper 8757718be8SEnji Cooper if (fork_pid) { 8857718be8SEnji Cooper int status; 8957718be8SEnji Cooper 9057718be8SEnji Cooper PTHREAD_REQUIRE(pthread_join(p, NULL)); 9157718be8SEnji Cooper ATF_REQUIRE_MSG(thread_survived, "thread did not survive in parent"); 9257718be8SEnji Cooper 9357718be8SEnji Cooper waitpid(fork_pid, &status, 0); 9457718be8SEnji Cooper ATF_REQUIRE_MSG(WIFEXITED(status), "child died wrongly"); 9557718be8SEnji Cooper ATF_REQUIRE_EQ_MSG(WEXITSTATUS(status), 0, "thread survived in child"); 9657718be8SEnji Cooper } else { 9757718be8SEnji Cooper sleep(5); 98a81c27fdSEnji Cooper _exit(thread_survived ? 1 : 0); 9957718be8SEnji Cooper } 10057718be8SEnji Cooper } 10157718be8SEnji Cooper 10257718be8SEnji Cooper ATF_TP_ADD_TCS(tp) 10357718be8SEnji Cooper { 10457718be8SEnji Cooper ATF_TP_ADD_TC(tp, fork); 10557718be8SEnji Cooper 10657718be8SEnji Cooper return atf_no_error(); 10757718be8SEnji Cooper } 108