summaryrefslogtreecommitdiffstats
path: root/watch.hpp
blob: 6a274aa92b66d237eda517e4f54832ac8c12e079 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once

#include "dns_updater.hpp"
#include "util.hpp"
#include "types.hpp"

#include <sys/inotify.h>
#include <systemd/sd-event.h>

#include <experimental/filesystem>
#include <functional>
#include <map>

namespace phosphor
{
namespace network
{
namespace inotify
{

namespace fs = std::experimental::filesystem;

// Auxiliary callback to be invoked on inotify events
using UserCallBack = std::function<void(const std::string&)>;

/** @class Watch
 *
 *  @brief Adds inotify watch on directory
 *
 *  @details Calls back user function on matching events
 */
class Watch
{
    public:
        Watch() = delete;
        Watch(const Watch&) = delete;
        Watch& operator=(const Watch&) = delete;
        Watch(Watch&&) = delete;
        Watch& operator=(Watch&&) = delete;

        /** @brief Hooks inotify watch with sd-event
         *
         *  @param[in] eventPtr - Reference to sd_event wrapped in unique_ptr
         *  @param[in] path     - File path to be watched
         *  @param[in] userFunc - User specific callback function on events
         *  @param[in] flags    - Flags to be supplied to inotify
         *  @param[in] mask     - Mask of events to be supplied to inotify
         *  @param[in] events   - Events to be watched
         */
        Watch(phosphor::network::EventPtr& eventPtr,
              const fs::path path,
              UserCallBack userFunc,
              int flags = IN_NONBLOCK,
              uint32_t mask = IN_CLOSE_WRITE,
              uint32_t events = EPOLLIN);

        /** @brief Remove inotify watch and close fd's */
        ~Watch()
        {
            if ((fd() >= 0) && (wd >= 0))
            {
                inotify_rm_watch(fd(), wd);
            }
        }

    private:
        /** @brief Callback invoked when inotify event fires
         *
         *  @details On a matching event, calls back into user supplied
         *           function if there is one registered
         *
         *  @param[in] eventSource - Event source
         *  @param[in] fd          - Inotify fd
         *  @param[in] retEvents   - Events that matched for fd
         *  @param[in] userData    - Pointer to Watch object
         *
         *  @returns 0 on success, -1 on fail
         */
        static int processEvents(sd_event_source* eventSource,
                                 int fd,
                                 uint32_t retEvents,
                                 void* userData);

        /** @brief Initializes an inotify instance
         *
         *  @return Descriptor on success, -1 on failure
         */
        int inotifyInit();

        /** @brief File path to be watched */
        const fs::path path;

        /** @brief User callback function */
        UserCallBack userFunc;

        /** @brief Inotify flags */
        int flags;

        /** @brief Mask of events */
        uint32_t mask;

        /** @brief Events to be watched */
        uint32_t events;

        /** @brief Watch descriptor */
        int wd = -1;

        /** @brief File descriptor manager */
        phosphor::Descriptor fd;
};

} // namespace inotify
} // namespace network
} // namespace phosphor
OpenPOWER on IntegriCloud