summaryrefslogtreecommitdiffstats
path: root/watch.hpp
blob: 5452b206d27acc09593456cfb09ba3c9a2b074b3 (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
#pragma once

#include "dump_utils.hpp"

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

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

namespace phosphor
{
namespace dump
{
namespace inotify
{

namespace fs = std::experimental::filesystem;

// User specific call back function input map(path:event) type.
using UserMap = std::map<fs::path, uint32_t>;

// User specific callback function wrapper type.
using UserType = std::function<void(const UserMap&)>;

/** @class Watch
 *
 *  @brief Adds inotify watch on directory.
 *
 *  The inotify watch is hooked up with sd-event, so that on call back,
 *  appropriate actions are taken to collect files from the directory
 *  initialized by the object.
 */
class Watch
{
  public:
    /** @brief ctor - hook inotify watch with sd-event
     *
     *  @param[in] eventObj - Event loop object
     *  @param[in] flags - inotify flags
     *  @param[in] mask  - Mask of events
     *  @param[in] events - Events to be watched
     *  @param[in] path - File path to be watched
     *  @param[in] userFunc - User specific callback fnction wrapper.
     *
     */
    Watch(const EventPtr& eventObj, int flags, uint32_t mask, uint32_t events,
          const fs::path& path, UserType userFunc);

    Watch(const Watch&) = delete;
    Watch& operator=(const Watch&) = delete;
    Watch(Watch&&) = default;
    Watch& operator=(Watch&&) = default;

    /* @brief dtor - remove inotify watch and close fd's */
    ~Watch();

  private:
    /** @brief sd-event callback.
     *  @details Collects the files and event info and call the
     *           appropriate user function for further action.
     *
     *  @param[in] s - event source, floating (unused) in our case
     *  @param[in] fd - inotify fd
     *  @param[in] revents - events that matched for fd
     *  @param[in] userdata - pointer to Watch object
     *
     *  @returns 0 on success, -1 on fail
     */
    static int callback(sd_event_source* s, int fd, uint32_t revents,
                        void* userdata);

    /**  initialize an inotify instance and returns file descriptor */
    int inotifyInit();

    /** @brief inotify flags */
    int flags;

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

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

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

    /** @brief dump file directory watch descriptor */
    int wd = -1;

    /** @brief file descriptor manager */
    CustomFd fd;

    /** @brief The user level callback function wrapper */
    UserType userFunc;
};

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