Jayanth Othayoth | 1756c06 | 2017-05-23 22:48:11 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <map> |
| 4 | #include <memory> |
| 5 | #include <systemd/sd-event.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dump |
| 11 | { |
| 12 | |
| 13 | /** @struct CustomFd |
| 14 | * |
| 15 | * RAII wrapper for file descriptor. |
| 16 | */ |
| 17 | struct CustomFd |
| 18 | { |
| 19 | private: |
| 20 | /** @brief File descriptor */ |
| 21 | int fd = -1; |
| 22 | |
| 23 | public: |
| 24 | CustomFd(const CustomFd&) = delete; |
| 25 | CustomFd& operator=(const CustomFd&) = delete; |
| 26 | CustomFd(CustomFd&&) = delete; |
| 27 | CustomFd& operator=(CustomFd&&) = delete; |
| 28 | |
| 29 | /** @brief Saves File descriptor and uses it to do file operation |
| 30 | * |
| 31 | * @param[in] fd - File descriptor |
| 32 | */ |
| 33 | CustomFd(int fd) : fd(fd) {} |
| 34 | |
| 35 | ~CustomFd(); |
| 36 | |
| 37 | int operator()() const |
| 38 | { |
| 39 | return fd; |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | namespace inotify |
| 44 | { |
| 45 | /** @class Watch |
| 46 | * |
| 47 | * @brief Adds inotify watch on core file directories. |
| 48 | * |
| 49 | * The inotify watch is hooked up with sd-event, so that on call back, |
| 50 | * appropriate actions are taken to collect the core files. |
| 51 | */ |
| 52 | class Watch |
| 53 | { |
| 54 | public: |
| 55 | /** @brief ctor - hook inotify watch with sd-event |
| 56 | * |
| 57 | * @param[in] loop - sd-event object |
| 58 | */ |
| 59 | Watch(sd_event* loop); |
| 60 | |
| 61 | Watch(const Watch&) = delete; |
| 62 | Watch& operator=(const Watch&) = delete; |
| 63 | Watch(Watch&&) = default; |
| 64 | Watch& operator=(Watch&&) = default; |
| 65 | |
| 66 | /* @brief dtor - remove inotify watch and close fd's */ |
| 67 | ~Watch(); |
| 68 | |
| 69 | private: |
| 70 | /** @brief sd-event callback |
| 71 | * |
| 72 | * @param[in] s - event source, floating (unused) in our case |
| 73 | * @param[in] fd - inotify fd |
| 74 | * @param[in] revents - events that matched for fd |
| 75 | * @param[in] userdata - pointer to Watch object |
| 76 | * @returns 0 on success, -1 on fail |
| 77 | */ |
| 78 | static int callback(sd_event_source* s, |
| 79 | int fd, |
| 80 | uint32_t revents, |
| 81 | void* userdata); |
| 82 | |
| 83 | /** initialize an inotify instance and returns file descriptor */ |
| 84 | int inotifyInit(); |
| 85 | |
| 86 | /** @brief core file directory watch descriptor */ |
| 87 | int wd = -1; |
| 88 | |
| 89 | /** @brief file descriptor manager */ |
| 90 | CustomFd fd; |
| 91 | }; |
| 92 | |
| 93 | } // namespace inotify |
| 94 | } // namespace dump |
| 95 | } // namespace phosphor |