Rev 51 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
45 | luk | 1 | |
2 | /// inotify cron daemon user tables header |
||
3 | /** |
||
4 | * \file usertable.h |
||
5 | * |
||
6 | * inotify cron system |
||
7 | * |
||
8 | * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz> |
||
9 | * |
||
10 | * This program is free software; you can use it, redistribute |
||
11 | * it and/or modify it under the terms of the GNU General Public |
||
12 | * License, version 2 (see LICENSE-GPL). |
||
13 | * |
||
14 | */ |
||
15 | |||
16 | #ifndef _USERTABLE_H_ |
||
17 | #define _USERTABLE_H_ |
||
18 | |||
19 | #include <map> |
||
47 | luk | 20 | #include <deque> |
45 | luk | 21 | |
22 | #include "inotify-cxx.h" |
||
23 | #include "incrontab.h" |
||
24 | |||
25 | |||
26 | class UserTable; |
||
27 | |||
47 | luk | 28 | /// Callback for calling after a process finishes. |
29 | typedef void (*proc_done_cb)(InotifyWatch*); |
||
45 | luk | 30 | |
47 | luk | 31 | /// Child process data |
32 | typedef struct |
||
33 | { |
||
34 | pid_t pid; ///< PID |
||
35 | proc_done_cb onDone; ///< function called after process finishes |
||
36 | InotifyWatch* pWatch; ///< related watch |
||
37 | } ProcData_t; |
||
38 | |||
39 | /// Watch-to-usertable mapping |
||
45 | luk | 40 | typedef std::map<InotifyWatch*, UserTable*> IWUT_MAP; |
47 | luk | 41 | |
42 | /// Watch-to-tableentry mapping |
||
45 | luk | 43 | typedef std::map<InotifyWatch*, InCronTabEntry*> IWCE_MAP; |
44 | |||
47 | luk | 45 | /// Child process list |
46 | typedef std::deque<ProcData_t> PROC_LIST; |
||
47 | |||
48 | /// Event dispatcher class. |
||
49 | /** |
||
50 | * This class distributes inotify events to appropriate user tables. |
||
51 | */ |
||
45 | luk | 52 | class EventDispatcher |
53 | { |
||
54 | public: |
||
47 | luk | 55 | /// Constructor. |
56 | /** |
||
57 | * \param[in] pIn inotify object |
||
58 | */ |
||
45 | luk | 59 | EventDispatcher(Inotify* pIn); |
60 | |||
47 | luk | 61 | /// Destructor. |
45 | luk | 62 | ~EventDispatcher() {} |
63 | |||
47 | luk | 64 | /// Dispatches an event. |
65 | /** |
||
66 | * \param[in] rEvt inotify event |
||
67 | */ |
||
45 | luk | 68 | void DispatchEvent(InotifyEvent& rEvt); |
69 | |||
47 | luk | 70 | /// Registers a watch for an user table. |
71 | /** |
||
72 | * \param[in] pWatch inotify watch |
||
73 | * \param[in] pTab user table |
||
74 | */ |
||
45 | luk | 75 | void Register(InotifyWatch* pWatch, UserTable* pTab); |
76 | |||
47 | luk | 77 | /// Unregisters a watch. |
78 | /** |
||
79 | * \param[in] pWatch inotify watch |
||
80 | */ |
||
45 | luk | 81 | void Unregister(InotifyWatch* pWatch); |
82 | |||
47 | luk | 83 | /// Unregisters all watches for an user table. |
84 | /** |
||
85 | * \param[in] pTab user table |
||
86 | */ |
||
45 | luk | 87 | void UnregisterAll(UserTable* pTab); |
88 | |||
89 | private: |
||
47 | luk | 90 | Inotify* m_pIn; ///< inotify object |
91 | IWUT_MAP m_maps; ///< watch-to-usertable mapping |
||
45 | luk | 92 | |
47 | luk | 93 | /// Finds an user table for a watch. |
94 | /** |
||
95 | * \param[in] pW inotify watch |
||
96 | * \return pointer to the appropriate watch; NULL if no such watch exists |
||
97 | */ |
||
45 | luk | 98 | UserTable* FindTable(InotifyWatch* pW); |
99 | }; |
||
100 | |||
101 | |||
47 | luk | 102 | /// User table class. |
103 | /** |
||
104 | * This class processes inotify events for an user. It creates |
||
105 | * child processes which do appropriate actions as defined |
||
106 | * in the user table file. |
||
107 | */ |
||
45 | luk | 108 | class UserTable |
109 | { |
||
110 | public: |
||
47 | luk | 111 | /// Constructor. |
112 | /** |
||
113 | * \param[in] pIn inotify object |
||
114 | * \param[in] pEd event dispatcher |
||
115 | * \param[in] rUser user name |
||
116 | */ |
||
45 | luk | 117 | UserTable(Inotify* pIn, EventDispatcher* pEd, const std::string& rUser); |
47 | luk | 118 | |
119 | /// Destructor. |
||
45 | luk | 120 | virtual ~UserTable(); |
121 | |||
47 | luk | 122 | /// Loads the table. |
123 | /** |
||
124 | * All loaded entries have their inotify watches and are |
||
125 | * registered for event dispatching. |
||
126 | * If loading fails the table remains empty. |
||
127 | */ |
||
45 | luk | 128 | void Load(); |
129 | |||
47 | luk | 130 | /// Removes all entries from the table. |
131 | /** |
||
132 | * All entries are unregistered from the event dispatcher and |
||
133 | * their watches are destroyed. |
||
134 | */ |
||
45 | luk | 135 | void Dispose(); |
136 | |||
47 | luk | 137 | /// Processes an inotify event. |
138 | /** |
||
139 | * \param[in] rEvt inotify event |
||
140 | */ |
||
45 | luk | 141 | void OnEvent(InotifyEvent& rEvt); |
47 | luk | 142 | |
143 | /// Cleans-up all zombie child processes and enables disabled watches. |
||
144 | /** |
||
145 | * \attention This method must be called AFTER processing all events |
||
146 | * which has been caused by the processes. |
||
147 | */ |
||
148 | static void FinishDone(); |
||
45 | luk | 149 | private: |
47 | luk | 150 | Inotify* m_pIn; ///< inotify object |
151 | EventDispatcher* m_pEd; ///< event dispatcher |
||
152 | std::string m_user; ///< user name |
||
153 | InCronTab m_tab; ///< incron table |
||
154 | IWCE_MAP m_map; ///< watch-to-entry mapping |
||
155 | |||
156 | static PROC_LIST s_procList; ///< child process list |
||
45 | luk | 157 | |
47 | luk | 158 | /// Finds an entry for a watch. |
159 | /** |
||
160 | * \param[in] pWatch inotify watch |
||
161 | * \return pointer to the appropriate entry; NULL if no such entry exists |
||
162 | */ |
||
45 | luk | 163 | InCronTabEntry* FindEntry(InotifyWatch* pWatch); |
164 | |||
47 | luk | 165 | /// Prepares arguments for creating a child process. |
166 | /** |
||
167 | * \param[in] rCmd command string |
||
168 | * \param[out] argc argument count |
||
169 | * \param[out] argv argument array |
||
170 | * \return true = success, false = failure |
||
171 | */ |
||
45 | luk | 172 | bool PrepareArgs(const std::string& rCmd, int& argc, char**& argv); |
47 | luk | 173 | |
51 | luk | 174 | /// Frees memory allocated for arguments. |
175 | /** |
||
176 | * \param[in] argc argument count |
||
177 | * \param[in] argv argument array |
||
178 | */ |
||
179 | void CleanupArgs(int argc, char** argv); |
||
180 | |||
45 | luk | 181 | }; |
182 | |||
183 | #endif //_USERTABLE_H_ |