irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

irods_socket_information.cpp
Go to the documentation of this file.
1 #include <algorithm>
2 #include <arpa/inet.h>
3 #include <boost/filesystem.hpp>
4 #include <boost/format.hpp>
5 #include <iostream>
6 #include <netinet/in.h>
7 #include <sys/socket.h>
8 #include <vector>
9 
10 #include "rodsErrorTable.h"
11 #include "irods_exception.hpp"
12 
13 
14 std::string socket_fd_to_remote_address(const int fd) {
15  struct sockaddr_storage addr;
16  socklen_t addr_size = sizeof(addr);
17  const int getpeername_result = getpeername(fd, (struct sockaddr *)&addr, &addr_size); // 0 or -1 and errno
18  if (getpeername_result != 0) {
19  if (errno == ENOTCONN) {
20  return "";
21  }
22  THROW(SYS_INTERNAL_ERR, boost::format("getpeername of fd [%d] failed with errno [%d]") % fd % errno);
23  }
24 
25  char ipstr[INET6_ADDRSTRLEN] = {};
26  if (addr.ss_family == AF_INET) {
27  struct sockaddr_in *s = (struct sockaddr_in *)&addr;
28  inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr));
29  } else if (addr.ss_family == AF_INET6) {
30  struct sockaddr_in6 *s = (struct sockaddr_in6 *)&addr;
31  inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr));
32  }
33 
34  return ipstr;
35 }
36 
37 std::vector<int> get_open_socket_file_descriptors(void) {
38  std::vector<int> ret;
39  try {
40  const boost::filesystem::directory_iterator end;
41  boost::filesystem::directory_iterator it("/proc/self/fd");
42  for ( ; it != end; ++it) {
43  if (boost::filesystem::is_symlink(it->path())) {
44  boost::filesystem::path symlink_contents = boost::filesystem::read_symlink(it->path());
45  if (!symlink_contents.string().compare(0, 8, "socket:[")) {
46  ret.push_back(atoi(it->path().filename().string().c_str()));
47  }
48  }
49  }
50  } catch (const boost::filesystem::filesystem_error& e) {
51  THROW(SYS_INTERNAL_ERR, boost::format("boost::filesystem error [%s]") % e.what());
52  }
53 
54  return ret;
55 }
THROW
#define THROW(_code, _msg)
Definition: irods_exception.hpp:68
get_open_socket_file_descriptors
std::vector< int > get_open_socket_file_descriptors(void)
Definition: irods_socket_information.cpp:37
irods_exception.hpp
SYS_INTERNAL_ERR
@ SYS_INTERNAL_ERR
Definition: rodsErrorTable.h:211
irods::experimental::filesystem::client::end
auto end(const collection_iterator &) noexcept -> const collection_iterator
Definition: collection_iterator.hpp:88
terminate_irods_processes.e
e
Definition: terminate_irods_processes.py:19
socket_fd_to_remote_address
std::string socket_fd_to_remote_address(const int fd)
Definition: irods_socket_information.cpp:14
rodsErrorTable.h