Commit c9139368 authored by Victor Kotlyar's avatar Victor Kotlyar
Browse files

Moved castor::server::ProcessCap to

cta::server::ProcessCap
parent 8a160874
......@@ -96,6 +96,7 @@ set (COMMON_LIB_SRC_FILES
priorities/MountCriteria.cpp
priorities/UserGroup.cpp
processCap/ProcessCap.cpp
processCap/ProcessCapDummy.cpp
processCap/SmartCap.cpp
remoteFS/RemoteFileStatus.cpp
remoteFS/RemotePath.cpp
......@@ -132,6 +133,7 @@ set (COMMON_UNIT_TESTS_LIB_SRC_FILES
checksum/ChecksumTest.cpp
dataStructures/ArchiveFileTest.cpp
dataStructures/StorageClassTest.cpp
processCap/SmartCapTest.cpp
log/LogContextTest.cpp
log/ParamTest.cpp
log/SyslogLoggerTest.cpp
......@@ -148,3 +150,4 @@ add_library (ctacommonunittests SHARED
${COMMON_UNIT_TESTS_LIB_SRC_FILES})
install(TARGETS ctacommonunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Exception.hpp"
#include "common/processCap/ProcessCap.hpp"
......
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
......@@ -95,4 +90,4 @@ private:
}; // class ProcessCap
} // namespace server
} // namespace castor
} // namespace cta
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "castor/server/ProcessCapDummy.hpp"
#include "common/processCap/ProcessCapDummy.hpp"
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::server::ProcessCapDummy::~ProcessCapDummy()
cta::server::ProcessCapDummy::~ProcessCapDummy()
throw() {
}
//------------------------------------------------------------------------------
// getProcText
//------------------------------------------------------------------------------
std::string castor::server::ProcessCapDummy::getProcText() {
std::string cta::server::ProcessCapDummy::getProcText() {
return m_text;
}
//------------------------------------------------------------------------------
// setProcText
//------------------------------------------------------------------------------
void castor::server::ProcessCapDummy::setProcText(const std::string &text) {
void cta::server::ProcessCapDummy::setProcText(const std::string &text) {
m_text = text;
}
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "castor/server/ProcessCap.hpp"
#include "common/processCap/ProcessCap.hpp"
#include <string>
#include <sys/capability.h>
namespace castor {
namespace cta {
namespace server {
/**
......@@ -70,4 +65,4 @@ private:
}; // class ProcessCapDummy
} // namespace server
} // namespace castor
} // namespace cta
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common/exception/Exception.hpp"
#include "common/processCap/SmartCap.hpp"
......
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
......
/******************************************************************************
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "castor/exception/Exception.hpp"
#include "castor/server/SmartCap.hpp"
#include "common/exception/Exception.hpp"
#include "common/processCap/SmartCap.hpp"
#include <gtest/gtest.h>
namespace unitTests {
class castor_server_SmartCapTest : public ::testing::Test {
class cta_server_SmartCapTest : public ::testing::Test {
protected:
virtual void SetUp() {
......@@ -38,21 +33,21 @@ protected:
}
};
TEST_F(castor_server_SmartCapTest, default_constructor) {
castor::server::SmartCap smartPtr;
TEST_F(cta_server_SmartCapTest, default_constructor) {
cta::server::SmartCap smartPtr;
ASSERT_EQ((cap_t)NULL, smartPtr.get());
}
TEST_F(castor_server_SmartCapTest, constructor) {
TEST_F(cta_server_SmartCapTest, constructor) {
cap_t cap = cap_get_proc();
ASSERT_NE((cap_t)NULL, cap);
castor::server::SmartCap smartPtr(cap);
cta::server::SmartCap smartPtr(cap);
ASSERT_EQ(cap, smartPtr.get());
}
TEST_F(castor_server_SmartCapTest, reset) {
castor::server::SmartCap smartPtr;
TEST_F(cta_server_SmartCapTest, reset) {
cta::server::SmartCap smartPtr;
ASSERT_EQ((cap_t)NULL, smartPtr.get());
cap_t cap = cap_get_proc();
......@@ -62,12 +57,12 @@ TEST_F(castor_server_SmartCapTest, reset) {
ASSERT_EQ(cap, smartPtr.get());
}
TEST_F(castor_server_SmartCapTest, assignment) {
TEST_F(cta_server_SmartCapTest, assignment) {
cap_t cap = cap_get_proc();
ASSERT_NE((cap_t)NULL, cap);
castor::server::SmartCap smartPtr1;
castor::server::SmartCap smartPtr2;
cta::server::SmartCap smartPtr1;
cta::server::SmartCap smartPtr2;
ASSERT_EQ((cap_t)NULL, smartPtr1.get());
ASSERT_EQ((cap_t)NULL, smartPtr2.get());
......@@ -80,9 +75,9 @@ TEST_F(castor_server_SmartCapTest, assignment) {
ASSERT_EQ(cap, smartPtr2.get());
}
TEST_F(castor_server_SmartCapTest, releaseNull) {
castor::server::SmartCap smartPtr;
ASSERT_THROW(smartPtr.release(), castor::exception::Exception);
TEST_F(cta_server_SmartCapTest, releaseNull) {
cta::server::SmartCap smartPtr;
ASSERT_THROW(smartPtr.release(), cta::exception::Exception);
}
} // namespace unitTests
......@@ -17,7 +17,6 @@ include_directories(${PROJECT_SOURCE_DIR}/tapeserver/h)
add_library (ctaserverunittests SHARED
AtomicCounterTest.cpp
DaemonTest.cpp
SmartCapTest.cpp
ThreadingBlockingQTests.cpp
# ThreadingMPTests.cpp is commented out because of errors caused by libust
ThreadingMTTests.cpp
......
/******************************************************************************
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include "castor/server/ProcessCap.hpp"
#include "castor/server/SmartCap.hpp"
#include "castor/utils/utils.hpp"
#include "common/exception/Exception.hpp"
#include <errno.h>
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
castor::server::ProcessCap::~ProcessCap()
throw() {
}
//------------------------------------------------------------------------------
// getProcText
//------------------------------------------------------------------------------
std::string castor::server::ProcessCap::getProcText() {
try {
SmartCap cap(getProc());
return toText((cap_t)cap.get());
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to get text representation of the capabilities of the process: "
<< ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// getProc
//------------------------------------------------------------------------------
cap_t castor::server::ProcessCap::getProc() {
cap_t cap = cap_get_proc();
if(NULL == cap) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to get the capabilities of the process: "
<< castor::utils::errnoToString(errno);
throw ex;
}
return cap;
}
//------------------------------------------------------------------------------
// toText
//------------------------------------------------------------------------------
std::string castor::server::ProcessCap::toText(
const cap_t cap) {
// Create a C++ string with the result of calling cap_to_text()
char *const text = cap_to_text(cap, NULL);
if(NULL == text) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to create string representation of capability state: "
<< castor::utils::errnoToString(errno);
throw ex;
}
std::string result(text);
// Free the memory allocated by cap_to_text()
if(cap_free(text)) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to free string representation of capability state: "
<< castor::utils::errnoToString(errno);
throw ex;
}
// Return the C++ string
return result;
}
//------------------------------------------------------------------------------
// setProcText
//------------------------------------------------------------------------------
void castor::server::ProcessCap::setProcText(const std::string &text) {
try {
SmartCap cap(fromText(text));
setProc(cap.get());
} catch(cta::exception::Exception &ne) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to set capabilities of process: " << ne.getMessage().str();
throw ex;
}
}
//------------------------------------------------------------------------------
// fromText
//------------------------------------------------------------------------------
cap_t castor::server::ProcessCap::fromText(const std::string &text) {
const cap_t cap = cap_from_text(text.c_str());
if(NULL == cap) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to create capability state from string representation"
": text='" << text << "': " << castor::utils::errnoToString(errno);
throw ex;
}
return cap;
}
//------------------------------------------------------------------------------
// setProc
//------------------------------------------------------------------------------
void castor::server::ProcessCap::setProc(const cap_t cap) {
if(cap_set_proc(cap)) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to set the capabilities of the process: "
<< castor::utils::errnoToString(errno);
throw ex;
}
}
/*
* The CERN Tape Archive (CTA) project
* Copyright (C) 2015 CERN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <sys/capability.h>
namespace castor {
namespace server {
/**
* Class providing support for UNIX capabilities.
*
* This class is used to provide support for UNIX capbilities, so that
* subclasses can be created that override its virtual member functions.
* Unit testing is the primary use-case where you may want a dummy capabilities
* object that does nothing.
*
* Please note that process capabilities are not supported on Mac OS X.
*/
class ProcessCap {
public:
/**
* Destructor.
*/
virtual ~ProcessCap() throw();
/**
* C++ wrapper around the C functions cap_get_proc() and cap_to_text().
*
* @return The string representation the capabilities of the current
* process.
*/
virtual std::string getProcText();
/**
* C++ wrapper around the C functions cap_from_text() and cap_set_proc().
*
* @text The string representation the capabilities that the current
* process should have.
*/
virtual void setProcText(const std::string &text);
private:
/**
* C++ wrapper around the C function cap_get_proc().
*
* @return The capability state.
*/
cap_t getProc();
/**
* C++ wrapper around the C function cap_to_text().
*
* @param cap The capability state.
*/
std::string toText(const cap_t cap);
/**
* C++ wrapper around the C function cap_from_text().
*
* @return The capability state.
*/
cap_t fromText(const std::string &text);
/**