1 #pragma once 2 3 #include <sdbusplus/bus.hpp> 4 #include <unistd.h> 5 #include <fcntl.h> 6 7 8 namespace phosphor 9 { 10 namespace fan 11 { 12 namespace util 13 { 14 15 class FileDescriptor 16 { 17 public: 18 FileDescriptor() = delete; 19 FileDescriptor(const FileDescriptor&) = delete; 20 FileDescriptor(FileDescriptor&&) = default; 21 FileDescriptor& operator=(const FileDescriptor&) = delete; 22 FileDescriptor& operator=(FileDescriptor&&) = default; 23 24 FileDescriptor(int fd) : fd(fd) 25 { 26 } 27 28 ~FileDescriptor() 29 { 30 if (fd != -1) 31 { 32 close(fd); 33 } 34 } 35 36 int operator()() 37 { 38 return fd; 39 } 40 41 void open(const std::string& pathname, int flags) 42 { 43 fd = ::open(pathname.c_str(), flags); 44 if (-1 == fd) 45 { 46 throw std::runtime_error( 47 "Failed to open file device: " + pathname); 48 } 49 } 50 51 bool is_open() 52 { 53 return fd != -1; 54 } 55 56 private: 57 int fd = -1; 58 59 }; 60 61 /** 62 * @brief Get the inventory service name from the mapper object 63 * 64 * @return The inventory manager service name 65 */ 66 std::string getInvService(sdbusplus::bus::bus& bus); 67 68 69 /** 70 * @brief Get the service name from the mapper for the 71 * interface and path passed in. 72 * 73 * @param[in] path - the dbus path name 74 * @param[in] interface - the dbus interface name 75 * @param[in] bus - the dbus object 76 * 77 * @return The service name 78 */ 79 std::string getService(const std::string& path, 80 const std::string& interface, 81 sdbusplus::bus::bus& bus); 82 83 } 84 } 85 } 86