blob: a9ec18e017ac7eb9b4c40466b1e995b15d7af654 [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 Othayoth3fc6df42021-04-08 03:45:24 -05008#include <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
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050019// User specific call back function input map(path:event) type.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050020using UserMap = std::map<std::filesystem::path, uint32_t>;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050021
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050022// User specific callback function wrapper type.
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050023using UserType = std::function<void(const UserMap&)>;
24
25/** @class Watch
26 *
27 * @brief Adds inotify watch on directory.
28 *
29 * The inotify watch is hooked up with sd-event, so that on call back,
30 * appropriate actions are taken to collect files from the directory
31 * initialized by the object.
32 */
33class Watch
34{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050035 public:
36 /** @brief ctor - hook inotify watch with sd-event
37 *
38 * @param[in] eventObj - Event loop object
39 * @param[in] flags - inotify flags
40 * @param[in] mask - Mask of events
41 * @param[in] events - Events to be watched
42 * @param[in] path - File path to be watched
43 * @param[in] userFunc - User specific callback fnction wrapper.
44 *
45 */
46 Watch(const EventPtr& eventObj, int flags, uint32_t mask, uint32_t events,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050047 const std::filesystem::path& path, UserType userFunc);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050048
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050049 Watch(const Watch&) = delete;
50 Watch& operator=(const Watch&) = delete;
51 Watch(Watch&&) = default;
52 Watch& operator=(Watch&&) = default;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050053
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050054 /* @brief dtor - remove inotify watch and close fd's */
55 ~Watch();
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050056
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050057 private:
58 /** @brief sd-event callback.
59 * @details Collects the files and event info and call the
60 * appropriate user function for further action.
61 *
62 * @param[in] s - event source, floating (unused) in our case
63 * @param[in] fd - inotify fd
64 * @param[in] revents - events that matched for fd
65 * @param[in] userdata - pointer to Watch object
66 *
67 * @returns 0 on success, -1 on fail
68 */
69 static int callback(sd_event_source* s, int fd, uint32_t revents,
70 void* userdata);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050071
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050072 /** initialize an inotify instance and returns file descriptor */
73 int inotifyInit();
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050074
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050075 /** @brief inotify flags */
76 int flags;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050077
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050078 /** @brief Mask of events */
79 uint32_t mask;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050080
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050081 /** @brief Events to be watched */
82 uint32_t events;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050083
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050084 /** @brief File path to be watched */
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050085 std::filesystem::path path;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050086
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050087 /** @brief dump file directory watch descriptor */
88 int wd = -1;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050089
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050090 /** @brief file descriptor manager */
91 CustomFd fd;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050092
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050093 /** @brief The user level callback function wrapper */
94 UserType userFunc;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050095};
96
97} // namespace inotify
98} // namespace dump
99} // namespace phosphor