From 9c191cb025d17cb6eb6af6c32f4527d5189bcdfc Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Sun, 1 Jan 2017 15:10:32 +0100 Subject: [PATCH] fix(net): Always close socket handle Refs #283 --- include/adapters/net.hpp | 6 ++++-- src/adapters/net.cpp | 34 ++++++++++++---------------------- 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/include/adapters/net.hpp b/include/adapters/net.hpp index 4529ef3b..3dc9a823 100644 --- a/include/adapters/net.hpp +++ b/include/adapters/net.hpp @@ -19,6 +19,8 @@ POLYBAR_NS namespace chrono = std::chrono; +class file_descriptor; + namespace net { DEFINE_ERROR(network_error); @@ -58,7 +60,7 @@ namespace net { class network { public: explicit network(string interface); - virtual ~network(); + virtual ~network() {} virtual bool query(bool accumulate = false); virtual bool connected() const = 0; @@ -73,7 +75,7 @@ namespace net { bool test_interface() const; string format_speedrate(float bytes_diff, int minwidth) const; - int m_socketfd{0}; + unique_ptr m_socketfd; link_status m_status{}; string m_interface; bool m_tuntap{false}; diff --git a/src/adapters/net.cpp b/src/adapters/net.cpp index 73af1cfa..75f6c5fa 100644 --- a/src/adapters/net.cpp +++ b/src/adapters/net.cpp @@ -46,19 +46,13 @@ namespace net { if (if_nametoindex(m_interface.c_str()) == 0) { throw network_error("Invalid network interface \"" + m_interface + "\""); } - if ((m_socketfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { + + m_socketfd = file_util::make_file_descriptor(socket(AF_INET, SOCK_DGRAM, 0)); + if (!*m_socketfd) { throw network_error("Failed to open socket"); } - check_tuntap(); - } - /** - * Destruct network interface - */ - network::~network() { - if (m_socketfd != -1) { - close(m_socketfd); - } + check_tuntap(); } /** @@ -66,7 +60,6 @@ namespace net { */ bool network::query(bool accumulate) { struct ifaddrs* ifaddr; - if (getifaddrs(&ifaddr) == -1 || ifaddr == nullptr) { return false; } @@ -164,7 +157,7 @@ namespace net { request.ifr_data = reinterpret_cast(&driver); - if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { + if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) { return; } @@ -226,7 +219,7 @@ namespace net { data.cmd = ETHTOOL_GSET; request.ifr_data = reinterpret_cast(&data); - if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { + if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) { return false; } @@ -251,7 +244,7 @@ namespace net { data.cmd = ETHTOOL_GLINK; request.ifr_data = reinterpret_cast(&data); - if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { + if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) { return false; } @@ -278,15 +271,14 @@ namespace net { return false; } - auto socket_fd = iw_sockets_open(); - - if (socket_fd == -1) { + auto socket_fd = file_util::make_file_descriptor(iw_sockets_open()); + if (!*socket_fd) { return false; } struct iwreq req {}; - if (iw_get_ext(socket_fd, m_interface.c_str(), SIOCGIWMODE, &req) == -1) { + if (iw_get_ext(*socket_fd, m_interface.c_str(), SIOCGIWMODE, &req) == -1) { return false; } @@ -295,10 +287,8 @@ namespace net { return false; } - query_essid(socket_fd); - query_quality(socket_fd); - - iw_sockets_close(socket_fd); + query_essid(*socket_fd); + query_quality(*socket_fd); return true; }