143 lines
3.6 KiB
C++
143 lines
3.6 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Config/Platforms/Posix.h
|
|
*
|
|
* @brief Posix configuration file.
|
|
*/
|
|
|
|
#if (!defined ACTIV_CONFIG_PLATFORMS_POSIX_H)
|
|
#define ACTIV_CONFIG_PLATFORMS_POSIX_H
|
|
|
|
#define ACTIV_PLATFORM_POSIX
|
|
|
|
#include <unistd.h>
|
|
#include <cerrno>
|
|
#include <netinet/in.h>
|
|
|
|
#if (defined ACTIV_PLATFORM_LINUX)
|
|
#include <sys/prctl.h>
|
|
#endif
|
|
|
|
#define ACTIV_WAIT_INFINITE 0xffffffff
|
|
|
|
#if (defined _POSIX_THREAD_PROCESS_SHARED) && (_POSIX_THREAD_PROCESS_SHARED > 0)
|
|
// FreeBSD defines _POSIX_THREAD_PROCESS_SHARED as -1 (as of version 10).
|
|
#define ACTIV_POSIX_HAS_PSHARED
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
typedef int SOCKET;
|
|
const SOCKET INVALID_SOCKET = -1;
|
|
const int SOCKET_ERROR = -1;
|
|
|
|
typedef int HANDLE;
|
|
const HANDLE INVALID_HANDLE_VALUE = -1;
|
|
|
|
typedef size_t SocketBufferLength;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Close a socket handle.
|
|
*
|
|
* @param socket the socket to close.
|
|
*
|
|
* @return 0 on success or SOCKET_ERROR on error.
|
|
*/
|
|
inline int closesocket(const SOCKET socket)
|
|
{
|
|
return ::close(socket);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Class to scope setting of a new effective user id.
|
|
*/
|
|
class ScopedEffectiveUserId
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param uid the user id to set as the effective user id.
|
|
*/
|
|
ScopedEffectiveUserId(const uid_t uid);
|
|
|
|
/**
|
|
* @brief Destructor.
|
|
*
|
|
* If the constructor set a new effective user id, the original effective user id is restored.
|
|
*/
|
|
~ScopedEffectiveUserId();
|
|
|
|
/**
|
|
* @brief Get the original effective user id.
|
|
*
|
|
* @return User id.
|
|
*/
|
|
uid_t GetOriginalEffectiveUid() const;
|
|
|
|
/**
|
|
* @brief Did the effective user get set?
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
operator bool() const;
|
|
|
|
private:
|
|
#if (defined ACTIV_PLATFORM_LINUX)
|
|
const bool m_isDumpable;
|
|
#endif
|
|
const uid_t m_originalEffectiveUid;
|
|
const bool m_hasSetEffectiveUid;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedEffectiveUserId::ScopedEffectiveUserId(const uid_t uid) :
|
|
#if (defined ACTIV_PLATFORM_LINUX)
|
|
m_isDumpable(::prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)),
|
|
#endif
|
|
m_originalEffectiveUid(::geteuid()),
|
|
m_hasSetEffectiveUid((0 == ::getuid()) && (uid != m_originalEffectiveUid) && (0 == ::seteuid(uid)))
|
|
{
|
|
// Assume only root can change user id. Not necessary to check capabilities for our needs. This is to avoid
|
|
// seteuid() generating signal 33 on Linux even if it doesn't succeed.
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedEffectiveUserId::~ScopedEffectiveUserId()
|
|
{
|
|
if (m_hasSetEffectiveUid)
|
|
{
|
|
(void) ::seteuid(m_originalEffectiveUid);
|
|
|
|
#if (defined ACTIV_PLATFORM_LINUX)
|
|
(void) ::prctl(PR_SET_DUMPABLE, m_isDumpable, 0, 0, 0);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline uid_t ScopedEffectiveUserId::GetOriginalEffectiveUid() const
|
|
{
|
|
return m_originalEffectiveUid;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedEffectiveUserId::operator bool() const
|
|
{
|
|
return m_hasSetEffectiveUid;
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_CONFIG_PLATFORMS_POSIX_H)
|