| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "nlprocexecsocket.h" |
| 5 | |
| 6 | #include "processevent.h" |
| 7 | |
| 8 | extern "C" { |
| 9 | #include "processeventconnector.h" |
| 10 | } |
| 11 | |
| 12 | NLProcExecSocket::FDHandle::~FDHandle() |
| 13 | { |
| 14 | process_event_connector_close(fd); |
| 15 | } |
| 16 | |
| 17 | NLProcExecSocket::NLProcExecSocket() |
| 18 | { |
| 19 | sockFd_.fd = process_event_connector_new(); |
| 20 | if (sockFd_.fd < 0) |
| 21 | throw std::runtime_error("Cannot create netlink socket"); |
| 22 | |
| 23 | if (process_event_connector_set_timeout(sockFd_.fd, 5) < 0) |
| 24 | throw std::runtime_error("Cannot set socket timeout"); |
| 25 | |
| 26 | if (process_event_connector_install_filter(sockFd_.fd) < 0) |
| 27 | throw std::runtime_error("Cannot install socket filters"); |
| 28 | |
| 29 | if (process_event_connector_bind(sockFd_.fd) < 0) |
| 30 | throw BindError("Cannot bind to socket"); |
| 31 | |
| 32 | if (process_event_connector_subscribe(sockFd_.fd, true) < 0) |
| 33 | throw std::runtime_error("Cannot subscribe to proc events"); |
| 34 | } |
| 35 | |
| 36 | NLProcExecSocket::~NLProcExecSocket() |
| 37 | { |
| 38 | process_event_connector_subscribe(sockFd_.fd, false); |
| 39 | } |
| 40 | |
| 41 | ProcessEvent NLProcExecSocket::waitForEvent() const |
| 42 | { |
| 43 | auto event = process_event_connector_read_event(sockFd_.fd); |
| 44 | switch (event.type) { |
| 45 | case process_event_type::PROCESS_EVENT_EXEC: |
| 46 | return ProcessEvent{ProcessEvent::Type::EXEC, event.pid}; |
| 47 | break; |
| 48 | case process_event_type::PROCESS_EVENT_EXIT: |
| 49 | return ProcessEvent{ProcessEvent::Type::EXIT, event.pid}; |
| 50 | break; |
| 51 | default: |
| 52 | return ProcessEvent{ProcessEvent::Type::IGNORE, -1}; |
| 53 | } |
| 54 | } |