incron  0.5.10
usertable.h
Go to the documentation of this file.
00001 
00003 
00016 #ifndef _USERTABLE_H_
00017 #define _USERTABLE_H_
00018 
00019 #include <map>
00020 #include <deque>
00021 #include <sys/poll.h>
00022 
00023 #include "inotify-cxx.h"
00024 #include "incrontab.h"
00025 
00026 
00027 class UserTable;
00028 
00030 typedef std::map<std::string, UserTable*> SUT_MAP;
00031 
00033 typedef void (*proc_done_cb)(InotifyWatch*);
00034 
00036 typedef struct
00037 {
00038   proc_done_cb onDone;  
00039   InotifyWatch* pWatch; 
00040 } ProcData_t;
00041 
00043 typedef std::map<int, UserTable*> FDUT_MAP;
00044 
00046 typedef std::map<InotifyWatch*, IncronTabEntry*> IWCE_MAP;
00047 
00049 typedef std::map<pid_t, ProcData_t> PROC_MAP;
00050 
00052 
00055 class EventDispatcher
00056 {
00057 public:
00059 
00065   EventDispatcher(int iPipeFd, Inotify* pIn, InotifyWatch* pSys, InotifyWatch* pUser);
00066   
00068   ~EventDispatcher();
00069 
00071 
00074   bool ProcessEvents();
00075   
00077 
00080   void Register(UserTable* pTab);
00081   
00083 
00086   void Unregister(UserTable* pTab);
00087   
00089 
00092   inline size_t GetSize() const
00093   {
00094     return m_size;
00095   }
00096   
00098 
00101   inline struct pollfd* GetPollData()
00102   {
00103     return m_pPoll;
00104   }
00105   
00107   void Rebuild();
00108   
00110 
00113   inline void Clear()
00114   {
00115     m_maps.clear();
00116   }
00117   
00118 private:
00119   int m_iPipeFd;    
00120   int m_iMgmtFd;    
00121   Inotify* m_pIn;   
00122   InotifyWatch* m_pSys;   
00123   InotifyWatch* m_pUser;  
00124   FDUT_MAP m_maps;  
00125   size_t m_size;    
00126   struct pollfd* m_pPoll; 
00127   
00129   void ProcessMgmtEvents();
00130 };
00131 
00132 
00134 
00139 class UserTable
00140 {
00141 public:
00143 
00148   UserTable(EventDispatcher* pEd, const std::string& rUser, bool fSysTable);
00149   
00151   virtual ~UserTable();
00152   
00154 
00159   void Load();
00160   
00162 
00166   void Dispose();
00167   
00169 
00172   void OnEvent(InotifyEvent& rEvt);
00173   
00175 
00179   static void FinishDone();
00180   
00182 
00189   bool MayAccess(const std::string& rPath, bool fNoFollow) const;
00190   
00192 
00195   bool IsSystem() const;
00196   
00198 
00201   Inotify* GetInotify()
00202   {
00203     return &m_in;
00204   }
00205   
00207 
00217   inline static bool CheckUser(const char* user)
00218   {
00219     struct passwd* pw = getpwnam(user);
00220     if (pw == NULL)
00221       return false;
00222       
00223     return IncronTab::CheckUser(user);
00224   }
00225   
00227 
00230   void RunAsUser(char* const* argv) const;
00231   
00232 private:
00233   Inotify m_in;           
00234   EventDispatcher* m_pEd; 
00235   std::string m_user;     
00236   bool m_fSysTable;       
00237   IncronTab m_tab;        
00238   IWCE_MAP m_map;         
00239 
00240   static PROC_MAP s_procMap;  
00241   
00243 
00247   IncronTabEntry* FindEntry(InotifyWatch* pWatch);
00248   
00250 
00256   bool PrepareArgs(const std::string& rCmd, int& argc, char**& argv);
00257   
00259 
00263   void CleanupArgs(int argc, char** argv);
00264   
00265 };
00266 
00267 #endif //_USERTABLE_H_