-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathusertable.h
334 lines (282 loc) · 8.22 KB
/
usertable.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/// inotify cron daemon user tables header
/**
* \file usertable.h
*
* inotify cron system
*
* Copyright (C) 2006, 2007, 2008 Lukas Jelinek, <lukas@aiken.cz>
*
* This program is free software; you can use it, redistribute
* it and/or modify it under the terms of the GNU General Public
* License, version 2 (see LICENSE-GPL).
*
*/
#ifndef _USERTABLE_H_
#define _USERTABLE_H_
#include <vector>
#include <map>
#include <deque>
#include <string>
#include <sys/poll.h>
#include "inotify-cxx.h"
#include "incrontab.h"
class UserTable;
/// User name to user table mapping definition
typedef std::map<std::string, UserTable*> SUT_MAP;
/// Callback for calling after a process finishes.
typedef void (*proc_done_cb)(InotifyWatch*);
/// A list of strings, to hold filesystem paths
typedef std::vector<std::string> TPathList;
/// Child process data
typedef struct
{
proc_done_cb onDone; ///< function called after process finishes
InotifyWatch* pWatch; ///< related watch
} ProcData_t;
/// fd-to-usertable mapping
typedef std::map<int, UserTable*> FDUT_MAP;
/// Watch-to-tableentry mapping
typedef std::map<InotifyWatch*, IncronTabEntry*> IWCE_MAP;
/// Child process list
typedef std::map<pid_t, ProcData_t> PROC_MAP;
/// Event dispatcher class.
/**
* This class processes events and distributes them as needed.
*/
class EventDispatcher
{
public:
/// Constructor.
/**
* \param[in] iPipeFd pipe descriptor
* \param[in] pIn inotify object for table management
* \param[in] pSys watch for system tables
* \param[in] pUser watch for user tables
*/
EventDispatcher(int iPipeFd, Inotify* pIn, InotifyWatch* pSys, InotifyWatch* pUser);
/// Destructor.
~EventDispatcher();
/// Processes events.
/**
* \return pipe event occurred yes/no
*/
bool ProcessEvents();
/// Registers an user table.
/**
* \param[in] pTab user table
*/
void Register(UserTable* pTab);
/// Unregisters an user table.
/**
* \param[in] pTab user table
*/
void Unregister(UserTable* pTab);
/// Returns the poll data size.
/**
* \return poll data size
*/
inline size_t GetSize() const
{
return m_size;
}
/// Returns the poll data.
/**
* \return poll data
*/
inline struct pollfd* GetPollData()
{
return m_pPoll;
}
/// Rebuilds the poll array data.
void Rebuild();
/// Removes all registered user tables.
/**
* It doesn't cause poll data rebuilding.
*/
inline void Clear()
{
m_maps.clear();
}
private:
int m_iPipeFd; ///< pipe file descriptor
int m_iMgmtFd; ///< table management file descriptor
Inotify* m_pIn; ///< table management inotify object
InotifyWatch* m_pSys; ///< watch for system tables
InotifyWatch* m_pUser; ///< watch for user tables
FDUT_MAP m_maps; ///< watch-to-usertable mapping
size_t m_size; ///< poll data size
struct pollfd* m_pPoll; ///< poll data array
/// Processes events on the table management inotify object.
void ProcessMgmtEvents();
};
/// User table class.
/**
* This class processes inotify events for an user. It creates
* child processes which do appropriate actions as defined
* in the user table file.
*/
class UserTable
{
public:
/// Constructor.
/**
* \param[in] pEd event dispatcher
* \param[in] rUser user name
* \param[in] fSysTable system table yes/no
*/
UserTable(EventDispatcher* pEd, const std::string& rUser, bool fSysTable);
/// Destructor.
virtual ~UserTable();
/// Loads the table.
/**
* All loaded entries have their inotify watches and are
* registered for event dispatching.
* If loading fails the table remains empty.
*/
void Load();
/// Removes all entries from the table.
/**
* All entries are unregistered from the event dispatcher and
* their watches are destroyed.
*/
void Dispose();
/// Processes an inotify event.
/**
* \param[in] rEvt inotify event
*/
void OnEvent(InotifyEvent& rEvt);
/// Cleans-up all zombie child processes and enables disabled watches.
/**
* \attention This method must be called AFTER processing all events
* which has been caused by the processes.
*/
static void FinishDone();
/// Checks all processes, see which have finished
/**
*/
static void ClearProcesses();
/// Removes an entry from s_procMap
/**
* \param[in] pid pid of process to clear
*/
static void ClearProcess(pid_t pid);
/// Checks whether the user may access a file.
/**
* Any access right (RWX) is sufficient.
*
* \param[in] rPath absolute file path
* \param[in] fNoFollow don't follow a symbolic link
* \return true = access granted, false = otherwise
*/
bool MayAccess(const std::string& rPath, bool fNoFollow) const;
/// Checks whether it is a system table.
/**
* \return true = system table, false = user table
*/
bool IsSystem() const;
/// Returns the related inotify object.
/**
* \return related inotify object
*/
Inotify* GetInotify()
{
return &m_in;
}
/// Checks whether an user exists and has permission to use incron.
/**
* It searches for the given user name in the user database.
* If it failes it returns 'false'. Otherwise it checks
* permission files for this user (see InCronTab::CheckUser()).
*
* \param[in] user user name
* \return true = user has permission to use incron, false = otherwise
*
* \sa InCronTab::CheckUser()
*/
inline static bool CheckUser(const char* user)
{
struct passwd* pw = getpwnam(user);
if (pw == NULL)
return false;
return IncronTab::CheckUser(user);
}
/// Runs a program as the table's user.
/**
* \attention Don't call from the main process (before forking)!
*/
void RunAsUser(char* const* argv) const;
/// Sets max fork()s allowed
/**
* \param[in] maxForks Max forks
*/
static void SetMaxForks(size_t maxForks) {
m_maxForks = maxForks;
}
/// Enable recursive mode (scans directories recursively on startup/reload)
/**
* \param[in] maxForks Max forks
*/
static void EnableRecursive() {
m_recursive = true;
}
private:
Inotify m_in; ///< inotify object
EventDispatcher* m_pEd; ///< event dispatcher
std::string m_user; ///< user name
bool m_fSysTable; ///< system table yes/no
IncronTab m_tab; ///< incron table
IWCE_MAP m_map; ///< watch-to-entry mapping
static size_t m_maxForks; ///< max forks we can spawn
static bool m_recursive; ///< max forks we can spawn
static PROC_MAP s_procMap; ///< child process mapping
/// Finds an entry for a watch.
/**
* \param[in] pWatch inotify watch
* \return pointer to the appropriate entry; NULL if no such entry exists
*/
IncronTabEntry* FindEntry(InotifyWatch* pWatch);
/// Prepares arguments for creating a child process.
/**
* \param[in] rCmd command string
* \param[out] argc argument count
* \param[out] argv argument array
* \return true = success, false = failure
*/
bool PrepareArgs(const std::string& rCmd, int& argc, char**& argv);
/// Frees memory allocated for arguments.
/**
* \param[in] argc argument count
* \param[in] argv argument array
*/
void CleanupArgs(int argc, char** argv);
/// Add a single watch on a directory
/**
* \param[in] sPath path to watch
* \param[in] pEntry incrontab entry
*/
void AddWatch(const std::string& sPath, IncronTabEntry* pEntry);
/// Removes a single watch on a directory
/**
* \param[in] sPath path to stop watching
*/
void RemoveWatch(const std::string& sPath);
// Recursively search for all directories under sPath
/**
* \param[in] sPath path to start searching from
* \param[out] rResults a vector with all directories
*/
static void GetDirectoryTree(const std::string& sPath, TPathList& rResults);
/// Returns true if given sPath is a directory, false otherwise
/**
* \param[in] sPath path to inspect
*/
static bool IsDirectory(const std::string& sPath);
/// Handle directory events, to maintain watches on all directories
/**
* \param[in] rEvt inotify event
* \param[in] pWatch corresponding watch
* \param[in] pEntry corresponding incrontab entry
*/
void OnEventDirectory(const InotifyEvent& rEvt, InotifyWatch* pWatch, IncronTabEntry* pIncronTabEntry);
};
#endif //_USERTABLE_H_