//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
extern "C" {
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
}
#include <cassert>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "auto_array.hpp"
#include "env.hpp"
#include "exceptions.hpp"
#include "fs.hpp"
#include "process.hpp"
#include "text.hpp"
#include "user.hpp"
// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------
bool);
//!
//! An implementation of access(2) but using the effective user value
//! instead of the real one. Also avoids false positives for root when
//! asking for execute permissions, which appear in SunOS.
//!
static
void
{
"Cannot get information from file " +
/* Early return if we are only checking for existence and the file
* exists (stat call returned). */
return;
bool ok = false;
ok = true;
}
/* Allow root to execute the file if any of its execution bits
* are set. */
ok = true;
}
} else {
}
}
}
}
if (!ok)
EACCES);
}
//!
//! \brief A controlled version of access(2).
//!
//! This function reimplements the standard access(2) system call to
//! safely control its exit status and raise an exception in case of
//! failure.
//!
static
bool
{
try {
return true;
} catch (const tools::system_error& e) {
return false;
else
throw e;
}
}
// The cleanup routines below are tricky: they are executed immediately after
// a test case's death, and after we have forcibly killed any stale processes.
// However, even if the processes are dead, this does not mean that the file
// system we are scanning is stable. In particular, if the test case has
// does not mean that the file system is truly unmounted.
//
// The code below attempts to cope with this by catching errors and either
// before giving up.
// The erase parameter in this routine is to control nested mount points.
// We want to descend into a mount point to unmount anything that is
// mounted under it, but we do not want to delete any files while doing
// this traversal. In other words, we erase files until we cross the
// first mount point, and after that point we only scan and unmount.
static
void
{
try {
do_unmount(p);
if (erase) {
else
}
} catch (const tools::system_error& e) {
throw e;
}
}
static
void
bool erase)
{
if (retries > 0) {
retries--;
goto retry_chmod;
} else {
errno);
}
}
}
{
bool ok = false;
while (!ok) {
try {
ok = true;
} catch (const tools::system_error& e) {
retries--;
if (retries == 0)
throw e;
}
}
}
}
}
static
void
{
// At least, FreeBSD's unmount(2) requires the path to be absolute.
// Let's make it absolute in all cases just to be safe that this does
// not affect other systems.
#if defined(HAVE_UNMOUNT)
retries--;
goto retry_unmount;
} else {
}
}
#else
// We could use umount(2) instead if it was available... but
// trying to do so under, e.g. Linux, is a nightmare because we
// stools::fser to just leave the system-specific umount(8) tool deal
// with it, at least for now.
#endif
}
static
{
do {
if (pos == 0)
else if (component != ".")
}
else
}
// ------------------------------------------------------------------------
// The "path" class.
// ------------------------------------------------------------------------
{
}
{
}
const char*
const
{
}
const
{
return m_data;
}
bool
const
{
}
bool
const
{
return m_data == "/";
}
const
{
return path(".");
else if (endpos == 0)
return path("/");
else
}
const
{
begpos = 0;
else
begpos++;
}
const
{
assert(!is_absolute());
return get_current_dir() / m_data;
}
bool
const
{
}
bool
const
{
}
const
{
}
const
{
}
bool
const
{
}
// ------------------------------------------------------------------------
// The "file_info" class.
// ------------------------------------------------------------------------
{
"lstat(2) failed", errno);
switch (type) {
#if defined(S_IFWHT)
#endif
default:
"error", EINVAL);
}
}
{
}
const
{
}
const
{
}
const
{
}
const
{
}
int
const
{
return m_type;
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
bool
const
{
}
// ------------------------------------------------------------------------
// The "directory" class.
// ------------------------------------------------------------------------
{
}
}
const
{
return ns;
}
// ------------------------------------------------------------------------
// The "temp_dir" class.
// ------------------------------------------------------------------------
{
errno);
}
{
}
const
{
return *m_path;
}
// ------------------------------------------------------------------------
// Free functions.
// ------------------------------------------------------------------------
bool
{
try {
return true;
} catch (const system_error& e) {
return false;
else
throw;
}
}
bool
{
// Do not bother to provide a default value for PATH. If it is not
// there something is broken in the user's environment.
bool found = false;
found = true;
}
return found;
}
bool
{
if (!exists(p))
return false;
}
void
{
"Is a directory",
EPERM);
errno);
}
void
{
/* Some operating systems (e.g. OpenSolaris 200906) return
* EEXIST instead of ENOTEMPTY for non-empty directories.
* Homogenize the return value so that callers don't need
* to bother about differences in operating systems. */
}
errno);
}
}
{
"chdir(2) failed", errno);
}
return olddir;
}
void
{
}
{
#if defined(HAVE_GETCWD_DYN)
#else
#endif
"getcwd() failed", errno);
}