Subversion Repositories public

Rev

Rev 65 | Rev 69 | Go to most recent revision | 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
 *
63 luk 8
 * Copyright (C) 2006, 2007 Lukas Jelinek, <lukas@aiken.cz>
45 luk 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>
67 luk 21
#include <sys/poll.h>
45 luk 22
 
23
#include "inotify-cxx.h"
24
#include "incrontab.h"
25
 
26
 
27
class UserTable;
28
 
67 luk 29
/// User name to user table mapping definition
30
typedef std::map<std::string, UserTable*> SUT_MAP;
31
 
47 luk 32
/// Callback for calling after a process finishes.
33
typedef void (*proc_done_cb)(InotifyWatch*);
45 luk 34
 
47 luk 35
/// Child process data
36
typedef struct
37
{
38
  pid_t pid;            ///< PID
39
  proc_done_cb onDone;  ///< function called after process finishes
40
  InotifyWatch* pWatch; ///< related watch
41
} ProcData_t;
42
 
67 luk 43
/// fd-to-usertable mapping
44
typedef std::map<int, UserTable*> FDUT_MAP;
47 luk 45
 
46
/// Watch-to-tableentry mapping
45 luk 47
typedef std::map<InotifyWatch*, InCronTabEntry*> IWCE_MAP;
48
 
47 luk 49
/// Child process list
50
typedef std::deque<ProcData_t> PROC_LIST;
51
 
52
/// Event dispatcher class.
53
/**
67 luk 54
 * This class processes events and distributes them as needed.
47 luk 55
 */
45 luk 56
class EventDispatcher
57
{
58
public:
47 luk 59
  /// Constructor.
60
  /**
67 luk 61
   * \param[in] iPipeFd pipe descriptor
62
   * \param[in] pIn inotify object for table management
63
   * \param[in] pSys watch for system tables
64
   * \param[in] pUser watch for user tables
47 luk 65
   */
67 luk 66
  EventDispatcher(int iPipeFd, Inotify* pIn, InotifyWatch* pSys, InotifyWatch* pUser);
45 luk 67
 
47 luk 68
  /// Destructor.
67 luk 69
  ~EventDispatcher();
45 luk 70
 
67 luk 71
  /// Processes events.
47 luk 72
  /**
67 luk 73
   * \return pipe event occurred yes/no
47 luk 74
   */
67 luk 75
  bool ProcessEvents();
45 luk 76
 
67 luk 77
  /// Registers an user table.
47 luk 78
  /**
79
   * \param[in] pTab user table
80
   */
67 luk 81
  void Register(UserTable* pTab);
45 luk 82
 
67 luk 83
  /// Unregisters an user table.
47 luk 84
  /**
67 luk 85
   * \param[in] pTab user table
47 luk 86
   */
67 luk 87
  void Unregister(UserTable* pTab);
45 luk 88
 
67 luk 89
  /// Returns the poll data size.
47 luk 90
  /**
67 luk 91
   * \return poll data size
47 luk 92
   */
67 luk 93
  inline size_t GetSize() const
94
  {
95
    return m_size;
96
  }
45 luk 97
 
67 luk 98
  /// Returns the poll data.
99
  /**
100
   * \return poll data
101
   */
102
  inline struct pollfd* GetPollData()
103
  {
104
    return m_pPoll;
105
  }
106
 
107
 
45 luk 108
private:
67 luk 109
  int m_iPipeFd;    ///< pipe file descriptor
110
  int m_iMgmtFd;    ///< table management file descriptor
111
  Inotify* m_pIn;   ///< table management inotify object 
112
  InotifyWatch* m_pSys;   ///< watch for system tables
113
  InotifyWatch* m_pUser;  ///< watch for user tables 
114
  FDUT_MAP m_maps;  ///< watch-to-usertable mapping
115
  size_t m_size;    ///< poll data size
116
  struct pollfd* m_pPoll; ///< poll data array
45 luk 117
 
67 luk 118
  /// Rebuilds the poll array data.
119
  void Rebuild();
120
 
121
  /// Processes events on the table management inotify object. 
122
  void ProcessMgmtEvents();
45 luk 123
};
124
 
125
 
47 luk 126
/// User table class.
127
/**
128
 * This class processes inotify events for an user. It creates
129
 * child processes which do appropriate actions as defined
130
 * in the user table file.
131
 */
45 luk 132
class UserTable
133
{
134
public:
47 luk 135
  /// Constructor.
136
  /**
137
   * \param[in] pEd event dispatcher
138
   * \param[in] rUser user name
67 luk 139
   * \param[in] fSysTable system table yes/no
47 luk 140
   */
67 luk 141
        UserTable(EventDispatcher* pEd, const std::string& rUser, bool fSysTable);
47 luk 142
 
143
  /// Destructor.
45 luk 144
        virtual ~UserTable();
145
 
47 luk 146
  /// Loads the table.
147
  /**
148
   * All loaded entries have their inotify watches and are
149
   * registered for event dispatching.
150
   * If loading fails the table remains empty.
151
   */
45 luk 152
  void Load();
153
 
47 luk 154
  /// Removes all entries from the table.
155
  /**
156
   * All entries are unregistered from the event dispatcher and
157
   * their watches are destroyed.
158
   */
45 luk 159
  void Dispose();
160
 
47 luk 161
  /// Processes an inotify event.
162
  /**
163
   * \param[in] rEvt inotify event
164
   */
45 luk 165
  void OnEvent(InotifyEvent& rEvt);
47 luk 166
 
167
  /// Cleans-up all zombie child processes and enables disabled watches.
168
  /**
169
   * \attention This method must be called AFTER processing all events
170
   *            which has been caused by the processes.
171
   */
172
  static void FinishDone();
65 luk 173
 
174
  /// Checks whether the user may access a file.
175
  /**
176
   * Any access right (RWX) is sufficient.
177
   *
178
   * \param[in] rPath absolute file path
179
   * \param[in] fNoFollow don't follow a symbolic link
180
   * \return true = access granted, false = otherwise
181
   */
182
  bool MayAccess(const std::string& rPath, bool fNoFollow) const;
183
 
67 luk 184
  /// Checks whether it is a system table.
185
  /**
186
   * \return true = system table, false = user table
187
   */
188
  bool IsSystem() const;
189
 
190
  /// Returns the related inotify object.
191
  /**
192
   * \return related inotify object
193
   */
194
  Inotify* GetInotify()
195
  {
196
    return &m_in;
197
  }
198
 
199
  /// Checks whether an user exists and has permission to use incron.
200
  /**
201
   * It searches for the given user name in the user database.
202
   * If it failes it returns 'false'. Otherwise it checks
203
   * permission files for this user (see InCronTab::CheckUser()).
204
   *
205
   * \param[in] user user name
206
   * \return true = user has permission to use incron, false = otherwise
207
   *
208
   * \sa InCronTab::CheckUser()
209
   */
210
  inline static bool CheckUser(const char* user)
211
  {
212
    struct passwd* pw = getpwnam(user);
213
    if (pw == NULL)
214
      return false;
215
 
216
    return InCronTab::CheckUser(user);
217
  }
218
 
45 luk 219
private:
67 luk 220
  Inotify m_in;           ///< inotify object
47 luk 221
  EventDispatcher* m_pEd; ///< event dispatcher
222
  std::string m_user;     ///< user name
67 luk 223
  bool m_fSysTable;       ///< system table yes/no
47 luk 224
  InCronTab m_tab;        ///< incron table
225
  IWCE_MAP m_map;         ///< watch-to-entry mapping
226
 
227
  static PROC_LIST s_procList;  ///< child process list
45 luk 228
 
47 luk 229
  /// Finds an entry for a watch.
230
  /**
231
   * \param[in] pWatch inotify watch
232
   * \return pointer to the appropriate entry; NULL if no such entry exists
233
   */
45 luk 234
  InCronTabEntry* FindEntry(InotifyWatch* pWatch);
235
 
47 luk 236
  /// Prepares arguments for creating a child process.
237
  /**
238
   * \param[in] rCmd command string
239
   * \param[out] argc argument count
240
   * \param[out] argv argument array
241
   * \return true = success, false = failure
242
   */
45 luk 243
  bool PrepareArgs(const std::string& rCmd, int& argc, char**& argv);
47 luk 244
 
51 luk 245
  /// Frees memory allocated for arguments.
246
  /**
247
   * \param[in] argc argument count
248
   * \param[in] argv argument array
249
   */
250
  void CleanupArgs(int argc, char** argv);
251
 
45 luk 252
};
253
 
254
#endif //_USERTABLE_H_