99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync/* $Id$ */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync/** @file
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * IPRT Testcase - fork() issues.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync/*
c7814cf6e1240a519cbec0441e033d0e2470ed00vboxsync * Copyright (C) 2009-2010 Oracle Corporation
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync *
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * available from http://www.virtualbox.org. This file is free software;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * you can redistribute it and/or modify it under the terms of the GNU
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * General Public License (GPL) as published by the Free Software
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync *
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * The contents of this file may alternatively be used under the terms
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * of the Common Development and Distribution License Version 1.0
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * VirtualBox OSE distribution, in which case the provisions of the
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * CDDL are applicable instead of those of the GPL.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync *
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * You may elect to license modified versions of this file under the
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * terms and conditions of either the GPL or the CDDL or both.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync/*******************************************************************************
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync* Header Files *
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync*******************************************************************************/
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#include <iprt/test.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#include <iprt/process.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#include <iprt/stream.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#include <iprt/string.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#include <iprt/initterm.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#ifndef RT_OS_WINDOWS
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync# include <unistd.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync# include <sys/wait.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync# include <errno.h>
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#endif
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsyncint main()
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync{
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Init the runtime and stuff.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTEST hTest;
176991ff77f632fbe0e92e3aa17cf249367a309bvboxsync int rc = RTTestInitAndCreate("tstFork", &hTest);
176991ff77f632fbe0e92e3aa17cf249367a309bvboxsync if (rc)
176991ff77f632fbe0e92e3aa17cf249367a309bvboxsync return rc;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTestBanner(hTest);
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#ifdef RT_OS_WINDOWS
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Skipped\n");
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#else
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync * Get values that are supposed to or change across the fork.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTPROCESS const ProcBefore = RTProcSelf();
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Fork.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync pid_t pid = fork();
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync if (pid == 0)
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync {
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Check that the values has changed.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync rc = 0;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync if (ProcBefore == RTProcSelf())
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync {
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTestFailed(hTest, "%RTproc == %RTproc [child]", ProcBefore, RTProcSelf());
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync rc = 1;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync }
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync return rc;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync }
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync if (pid != -1)
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync {
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Check that the values didn't change.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTEST_CHECK(hTest, ProcBefore == RTProcSelf());
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Wait for the child.
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync rc = 1;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync while ( waitpid(pid, &rc, 0)
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync && errno == EINTR)
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync rc = 1;
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync if (!WIFEXITED(rc) || WEXITSTATUS(rc) != 0)
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync RTTestFailed(hTest, "rc=%#x", rc);
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync }
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync else
8f2f51347c7b5fe7c697debe7c2e46b46dd16489vboxsync RTTestFailed(hTest, "fork() failed: %d - %s", errno, strerror(errno));
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync#endif
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync /*
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync * Summary
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync */
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync return RTTestSummaryAndDestroy(hTest);
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync}
99add19170a36a25b07ccd30aa5fec1eb0f2a201vboxsync