1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00

fix(net): Always close socket handle

Refs #283
This commit is contained in:
Michael Carlberg 2017-01-01 15:10:32 +01:00
parent ccddf052ec
commit 9c191cb025
2 changed files with 16 additions and 24 deletions

View file

@ -19,6 +19,8 @@ POLYBAR_NS
namespace chrono = std::chrono; namespace chrono = std::chrono;
class file_descriptor;
namespace net { namespace net {
DEFINE_ERROR(network_error); DEFINE_ERROR(network_error);
@ -58,7 +60,7 @@ namespace net {
class network { class network {
public: public:
explicit network(string interface); explicit network(string interface);
virtual ~network(); virtual ~network() {}
virtual bool query(bool accumulate = false); virtual bool query(bool accumulate = false);
virtual bool connected() const = 0; virtual bool connected() const = 0;
@ -73,7 +75,7 @@ namespace net {
bool test_interface() const; bool test_interface() const;
string format_speedrate(float bytes_diff, int minwidth) const; string format_speedrate(float bytes_diff, int minwidth) const;
int m_socketfd{0}; unique_ptr<file_descriptor> m_socketfd;
link_status m_status{}; link_status m_status{};
string m_interface; string m_interface;
bool m_tuntap{false}; bool m_tuntap{false};

View file

@ -46,19 +46,13 @@ namespace net {
if (if_nametoindex(m_interface.c_str()) == 0) { if (if_nametoindex(m_interface.c_str()) == 0) {
throw network_error("Invalid network interface \"" + m_interface + "\""); 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"); throw network_error("Failed to open socket");
} }
check_tuntap();
}
/** check_tuntap();
* Destruct network interface
*/
network::~network() {
if (m_socketfd != -1) {
close(m_socketfd);
}
} }
/** /**
@ -66,7 +60,6 @@ namespace net {
*/ */
bool network::query(bool accumulate) { bool network::query(bool accumulate) {
struct ifaddrs* ifaddr; struct ifaddrs* ifaddr;
if (getifaddrs(&ifaddr) == -1 || ifaddr == nullptr) { if (getifaddrs(&ifaddr) == -1 || ifaddr == nullptr) {
return false; return false;
} }
@ -164,7 +157,7 @@ namespace net {
request.ifr_data = reinterpret_cast<caddr_t>(&driver); request.ifr_data = reinterpret_cast<caddr_t>(&driver);
if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) {
return; return;
} }
@ -226,7 +219,7 @@ namespace net {
data.cmd = ETHTOOL_GSET; data.cmd = ETHTOOL_GSET;
request.ifr_data = reinterpret_cast<caddr_t>(&data); request.ifr_data = reinterpret_cast<caddr_t>(&data);
if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) {
return false; return false;
} }
@ -251,7 +244,7 @@ namespace net {
data.cmd = ETHTOOL_GLINK; data.cmd = ETHTOOL_GLINK;
request.ifr_data = reinterpret_cast<caddr_t>(&data); request.ifr_data = reinterpret_cast<caddr_t>(&data);
if (ioctl(m_socketfd, SIOCETHTOOL, &request) == -1) { if (ioctl(*m_socketfd, SIOCETHTOOL, &request) == -1) {
return false; return false;
} }
@ -278,15 +271,14 @@ namespace net {
return false; return false;
} }
auto socket_fd = iw_sockets_open(); auto socket_fd = file_util::make_file_descriptor(iw_sockets_open());
if (!*socket_fd) {
if (socket_fd == -1) {
return false; return false;
} }
struct iwreq req {}; 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; return false;
} }
@ -295,10 +287,8 @@ namespace net {
return false; return false;
} }
query_essid(socket_fd); query_essid(*socket_fd);
query_quality(socket_fd); query_quality(*socket_fd);
iw_sockets_close(socket_fd);
return true; return true;
} }