aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/hasLinuxCapability.cpp
blob: 5d823b4ecaf7e4dd03ea7f676b859adb7c270121 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#if defined(OS_LINUX)

#include "hasLinuxCapability.h"

#include <syscall.h>
#include <unistd.h>
#include <linux/capability.h>
#include <Common/Exception.h>


namespace DB
{

namespace ErrorCodes
{
    extern const int NETLINK_ERROR;
}

static __user_cap_data_struct getCapabilities()
{
    /// 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{};

    /// Avoid dependency on 'libcap'.
    if (0 != syscall(SYS_capget, &request, &response))
        throwFromErrno("Cannot do 'capget' syscall", ErrorCodes::NETLINK_ERROR);

    return response;
}

bool hasLinuxCapability(int cap)
{
    static __user_cap_data_struct capabilities = getCapabilities();
    return (1 << cap) & capabilities.effective;
}

}

#endif