Rev 47 | Rev 51 | 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 main file |
||
3 | /** |
||
4 | * \file icd-main.cpp |
||
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 | #include <map> |
||
17 | #include <signal.h> |
||
18 | #include <wait.h> |
||
19 | #include <pwd.h> |
||
20 | #include <dirent.h> |
||
21 | #include <syslog.h> |
||
22 | #include <errno.h> |
||
47 | luk | 23 | #include <sys/poll.h> |
45 | luk | 24 | |
25 | #include "inotify-cxx.h" |
||
26 | #include "incrontab.h" |
||
27 | |||
28 | #include "usertable.h" |
||
29 | |||
49 | luk | 30 | /// Daemon yes/no |
47 | luk | 31 | #define DAEMON true |
45 | luk | 32 | |
49 | luk | 33 | /// Application name |
45 | luk | 34 | #define INCRON_APP_NAME "incrond" |
49 | luk | 35 | |
36 | /// Logging options (console as fallback, log PID) |
||
45 | luk | 37 | #define INCRON_LOG_OPTS (LOG_CONS | LOG_PID) |
49 | luk | 38 | |
39 | /// Logging facility (use CRON) |
||
45 | luk | 40 | #define INCRON_LOG_FACIL LOG_CRON |
41 | |||
42 | |||
49 | luk | 43 | /// User name to user table mapping definition |
45 | luk | 44 | typedef std::map<std::string, UserTable*> SUT_MAP; |
45 | |||
49 | luk | 46 | /// User name to user table mapping table |
45 | luk | 47 | SUT_MAP g_ut; |
48 | |||
49 | luk | 49 | /// Finish program yes/no |
47 | luk | 50 | volatile bool g_fFinish = false; |
45 | luk | 51 | |
49 | luk | 52 | |
47 | luk | 53 | /// Handles a signal. |
54 | /** |
||
55 | * For SIGTERM and SIGINT it sets the program finish variable. |
||
56 | * |
||
57 | * \param[in] signo signal number |
||
58 | */ |
||
45 | luk | 59 | void on_signal(int signo) |
60 | { |
||
47 | luk | 61 | if (signo == SIGTERM || signo == SIGINT) |
62 | g_fFinish = true; |
||
45 | luk | 63 | } |
64 | |||
47 | luk | 65 | /// Checks whether an user exists and has permission to use incron. |
66 | /** |
||
67 | * It searches for the given user name in the user database. |
||
68 | * If it failes it returns 'false'. Otherwise it checks |
||
69 | * permission files for this user (see InCronTab::CheckUser()). |
||
70 | * |
||
71 | * \param[in] user user name |
||
72 | * \return true = user has permission to use incron, false = otherwise |
||
73 | * |
||
74 | * \sa InCronTab::CheckUser() |
||
75 | */ |
||
45 | luk | 76 | bool check_user(const char* user) |
77 | { |
||
78 | struct passwd* pw = getpwnam(user); |
||
79 | if (pw == NULL) |
||
80 | return false; |
||
81 | |||
82 | return InCronTab::CheckUser(user); |
||
83 | } |
||
84 | |||
47 | luk | 85 | /// Attempts to load all user incron tables. |
86 | /** |
||
87 | * Loaded tables are registered for processing events. |
||
88 | * |
||
89 | * \param[in] pIn inotify object |
||
90 | * \param[in] pEd inotify event dispatcher |
||
91 | * |
||
92 | * \throw InotifyException thrown if base table directory cannot be read |
||
93 | */ |
||
94 | void load_tables(Inotify* pIn, EventDispatcher* pEd) throw (InotifyException) |
||
45 | luk | 95 | { |
96 | DIR* d = opendir(INCRON_TABLE_BASE); |
||
47 | luk | 97 | if (d == NULL) |
98 | throw InotifyException("cannot open table directory", errno); |
||
45 | luk | 99 | |
100 | syslog(LOG_NOTICE, "loading user tables"); |
||
101 | |||
102 | struct dirent* pDe = NULL; |
||
103 | while ((pDe = readdir(d)) != NULL) { |
||
104 | std::string un(pDe->d_name); |
||
105 | if (pDe->d_type == DT_REG && un != "." && un != "..") { |
||
106 | if (check_user(pDe->d_name)) { |
||
107 | syslog(LOG_INFO, "loading table for user %s", pDe->d_name); |
||
108 | UserTable* pUt = new UserTable(pIn, pEd, un); |
||
109 | g_ut.insert(SUT_MAP::value_type(un, pUt)); |
||
110 | pUt->Load(); |
||
111 | } |
||
112 | else { |
||
113 | syslog(LOG_WARNING, "table for invalid user %s found (ignored)", pDe->d_name); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | closedir(d); |
||
119 | } |
||
120 | |||
47 | luk | 121 | /// Main application function. |
122 | /** |
||
123 | * \param[in] argc argument count |
||
124 | * \param[in] argv argument array |
||
125 | * \return 0 on success, 1 on error |
||
126 | * |
||
127 | * \attention In daemon mode, it finishes immediately. |
||
128 | */ |
||
45 | luk | 129 | int main(int argc, char** argv) |
130 | { |
||
131 | openlog(INCRON_APP_NAME, INCRON_LOG_OPTS, INCRON_LOG_FACIL); |
||
132 | |||
133 | syslog(LOG_NOTICE, "starting service"); |
||
134 | |||
47 | luk | 135 | try { |
136 | Inotify in; |
||
137 | in.SetNonBlock(true); |
||
138 | |||
139 | EventDispatcher ed(&in); |
||
140 | |||
141 | try { |
||
142 | load_tables(&in, &ed); |
||
143 | } catch (InotifyException e) { |
||
144 | int err = e.GetErrorNumber(); |
||
145 | syslog(LOG_CRIT, "%s: (%i) %s", e.GetMessage().c_str(), err, strerror(err)); |
||
146 | syslog(LOG_NOTICE, "stopping service"); |
||
147 | closelog(); |
||
148 | return 1; |
||
149 | } |
||
150 | |||
151 | signal(SIGTERM, on_signal); |
||
152 | signal(SIGINT, on_signal); |
||
153 | signal(SIGCHLD, on_signal); |
||
154 | |||
155 | if (DAEMON) |
||
156 | daemon(0, 0); |
||
157 | |||
158 | uint32_t wm = IN_CLOSE_WRITE | IN_DELETE | IN_MOVE | IN_DELETE_SELF | IN_UNMOUNT; |
||
159 | InotifyWatch watch(INCRON_TABLE_BASE, wm); |
||
160 | in.Add(watch); |
||
161 | |||
162 | syslog(LOG_NOTICE, "ready to process filesystem events"); |
||
163 | |||
164 | InotifyEvent e; |
||
165 | |||
166 | struct pollfd pfd; |
||
167 | pfd.fd = in.GetDescriptor(); |
||
49 | luk | 168 | pfd.events = (short) POLLIN; |
47 | luk | 169 | pfd.revents = (short) 0; |
170 | |||
171 | while (!g_fFinish) { |
||
172 | |||
173 | int res = poll(&pfd, 1, -1); |
||
174 | if (res > 0) { |
||
175 | in.WaitForEvents(true); |
||
176 | UserTable::FinishDone(); |
||
177 | } |
||
178 | else if (res < 0) { |
||
179 | if (errno != EINTR) |
||
180 | throw InotifyException("polling failed", errno, NULL); |
||
181 | } |
||
182 | |||
45 | luk | 183 | while (in.GetEvent(e)) { |
184 | |||
185 | if (e.GetWatch() == &watch) { |
||
186 | if (e.IsType(IN_DELETE_SELF) || e.IsType(IN_UNMOUNT)) { |
||
187 | syslog(LOG_CRIT, "base directory destroyed, exitting"); |
||
188 | g_fFinish = true; |
||
189 | } |
||
190 | else if (!e.GetName().empty()) { |
||
191 | SUT_MAP::iterator it = g_ut.find(e.GetName()); |
||
192 | if (it != g_ut.end()) { |
||
193 | UserTable* pUt = (*it).second; |
||
194 | if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
195 | syslog(LOG_INFO, "table for user %s changed, reloading", e.GetName().c_str()); |
||
196 | pUt->Dispose(); |
||
197 | pUt->Load(); |
||
198 | } |
||
199 | else if (e.IsType(IN_MOVED_FROM) || e.IsType(IN_DELETE)) { |
||
200 | syslog(LOG_INFO, "table for user %s destroyed, removing", e.GetName().c_str()); |
||
201 | delete pUt; |
||
202 | g_ut.erase(it); |
||
203 | } |
||
204 | } |
||
205 | else if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) { |
||
206 | if (check_user(e.GetName().c_str())) { |
||
207 | syslog(LOG_INFO, "table for user %s created, loading", e.GetName().c_str()); |
||
208 | UserTable* pUt = new UserTable(&in, &ed, e.GetName()); |
||
209 | g_ut.insert(SUT_MAP::value_type(e.GetName(), pUt)); |
||
210 | pUt->Load(); |
||
211 | } |
||
212 | } |
||
213 | } |
||
214 | } |
||
215 | else { |
||
216 | ed.DispatchEvent(e); |
||
217 | } |
||
218 | } |
||
219 | } |
||
47 | luk | 220 | } catch (InotifyException e) { |
221 | int err = e.GetErrorNumber(); |
||
222 | syslog(LOG_CRIT, "*** unhandled exception occurred ***"); |
||
223 | syslog(LOG_CRIT, " %s", e.GetMessage().c_str()); |
||
224 | syslog(LOG_CRIT, " error: (%i) %s", err, strerror(err)); |
||
45 | luk | 225 | } |
226 | |||
227 | syslog(LOG_NOTICE, "stopping service"); |
||
228 | |||
229 | closelog(); |
||
230 | |||
231 | return 0; |
||
232 | } |