Fixed bad and wrong code #3553

This commit is contained in:
Alexey Milovidov 2018-11-14 04:59:32 +03:00
parent 1aa89ed63a
commit 2daab83a23
2 changed files with 21 additions and 23 deletions

View File

@ -1,7 +1,7 @@
#include "hasLinuxCapability.h"
#if defined(__linux__)
#include "hasLinuxCapability.h"
#include <syscall.h>
#include <unistd.h>
#include <linux/capability.h>
@ -11,39 +11,34 @@
namespace DB
{
namespace ErrorCodes
{
extern const int NETLINK_ERROR;
}
namespace
static __user_cap_data_struct getCapabilities()
{
bool hasLinuxCapabilityImpl(decltype(CAP_NET_ADMIN) cap)
{
/// See man getcap.
__user_cap_header_struct request{};
request.version = _LINUX_CAPABILITY_VERSION_1; /// It's enough to check just single CAP_NET_ADMIN capability we are interested.
request.pid = getpid();
/// See man getcap.
__user_cap_header_struct request{};
request.version = _LINUX_CAPABILITY_VERSION_1; /// It's enough to check just single CAP_NET_ADMIN capability we are interested.
request.pid = getpid();
__user_cap_data_struct response{};
__user_cap_data_struct response{};
/// Avoid dependency on 'libcap'.
if (0 != syscall(SYS_capget, &request, &response))
throwFromErrno("Cannot do 'capget' syscall", ErrorCodes::NETLINK_ERROR);
/// Avoid dependency on 'libcap'.
if (0 != syscall(SYS_capget, &request, &response))
throwFromErrno("Cannot do 'capget' syscall", ErrorCodes::NETLINK_ERROR);
if (!((1 << cap) & response.effective))
return false;
return true;
}
return response;
}
bool hasLinuxCapability(decltype(CAP_NET_ADMIN) cap)
bool hasLinuxCapability(int cap)
{
static bool res = hasLinuxCapabilityImpl(cap);
return res;
static __user_cap_data_struct capabilities = getCapabilities();
return (1 << cap) & capabilities.effective;
}
}
#endif

View File

@ -4,7 +4,10 @@
namespace DB
{
bool hasLinuxCapability(decltype(CAP_NET_ADMIN) cap);
/// Check that the current process has Linux capability. Examples: CAP_IPC_LOCK, CAP_NET_ADMIN.
bool hasLinuxCapability(int cap);
}
#endif