blob: 5452b206d27acc09593456cfb09ba3c9a2b074b3 [file] [log] [blame]
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05001#pragma once
2
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05003#include "dump_utils.hpp"
4
5#include <sys/inotify.h>
6#include <systemd/sd-event.h>
7
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05008#include <experimental/filesystem>
Brad Bishop78610792018-02-21 13:08:15 -05009#include <functional>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050010#include <map>
11
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050012namespace phosphor
13{
14namespace dump
15{
16namespace inotify
17{
18
19namespace fs = std::experimental::filesystem;
20
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050021// User specific call back function input map(path:event) type.
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050022using UserMap = std::map<fs::path, uint32_t>;
23
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050024// User specific callback function wrapper type.
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050025using UserType = std::function<void(const UserMap&)>;
26
27/** @class Watch
28 *
29 * @brief Adds inotify watch on directory.
30 *
31 * The inotify watch is hooked up with sd-event, so that on call back,
32 * appropriate actions are taken to collect files from the directory
33 * initialized by the object.
34 */
35class Watch
36{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050037 public:
38 /** @brief ctor - hook inotify watch with sd-event
39 *
40 * @param[in] eventObj - Event loop object
41 * @param[in] flags - inotify flags
42 * @param[in] mask - Mask of events
43 * @param[in] events - Events to be watched
44 * @param[in] path - File path to be watched
45 * @param[in] userFunc - User specific callback fnction wrapper.
46 *
47 */
48 Watch(const EventPtr& eventObj, int flags, uint32_t mask, uint32_t events,
49 const fs::path& path, UserType userFunc);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050050
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050051 Watch(const Watch&) = delete;
52 Watch& operator=(const Watch&) = delete;
53 Watch(Watch&&) = default;
54 Watch& operator=(Watch&&) = default;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050055
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050056 /* @brief dtor - remove inotify watch and close fd's */
57 ~Watch();
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050058
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050059 private:
60 /** @brief sd-event callback.
61 * @details Collects the files and event info and call the
62 * appropriate user function for further action.
63 *
64 * @param[in] s - event source, floating (unused) in our case
65 * @param[in] fd - inotify fd
66 * @param[in] revents - events that matched for fd
67 * @param[in] userdata - pointer to Watch object
68 *
69 * @returns 0 on success, -1 on fail
70 */
71 static int callback(sd_event_source* s, int fd, uint32_t revents,
72 void* userdata);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050073
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050074 /** initialize an inotify instance and returns file descriptor */
75 int inotifyInit();
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050076
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050077 /** @brief inotify flags */
78 int flags;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050079
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050080 /** @brief Mask of events */
81 uint32_t mask;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050082
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050083 /** @brief Events to be watched */
84 uint32_t events;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050085
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050086 /** @brief File path to be watched */
87 fs::path path;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050088
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050089 /** @brief dump file directory watch descriptor */
90 int wd = -1;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050091
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050092 /** @brief file descriptor manager */
93 CustomFd fd;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050094
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050095 /** @brief The user level callback function wrapper */
96 UserType userFunc;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050097};
98
99} // namespace inotify
100} // namespace dump
101} // namespace phosphor