Parent: [4e0d1e] (diff)

Child: [f43b0e] (diff)

Download this file

rclmonprc.cpp    283 lines (255 with data), 6.9 kB

  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
#include "autoconfig.h"
#ifdef RCL_MONITOR
#ifndef lint
static char rcsid[] = "@(#$Id: rclmonprc.cpp,v 1.4 2006-10-24 12:48:09 dockes Exp $ (C) 2006 J.F.Dockes";
#endif
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* Recoll real time monitor processing. This file has the code to retrieve
* event from the event queue and do the database-side processing, and the
* initialization function.
*/
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include "debuglog.h"
#include "rclmon.h"
#include "debuglog.h"
#include "indexer.h"
#include "pathut.h"
typedef map<string, RclMonEvent> queue_type;
/** Private part of RclEQ: things that we don't wish to exist in the interface
* include file.
*/
class RclEQData {
public:
queue_type m_queue;
RclConfig *m_config;
bool m_ok;
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
RclEQData()
: m_config(0), m_ok(false)
{
if (!pthread_mutex_init(&m_mutex, 0) && !pthread_cond_init(&m_cond, 0))
m_ok = true;
}
};
static RclMonEventQueue rclEQ;
RclMonEventQueue::RclMonEventQueue()
{
m_data = new RclEQData;
}
RclMonEventQueue::~RclMonEventQueue()
{
delete m_data;
}
bool RclMonEventQueue::empty()
{
return m_data == 0 ? true : m_data->m_queue.empty();
}
RclMonEvent RclMonEventQueue::pop()
{
RclMonEvent ev;
if (!empty()) {
ev = m_data->m_queue.begin()->second;
m_data->m_queue.erase(m_data->m_queue.begin());
}
return ev;
}
/** Wait until there is something to process on the queue.
* Must be called with the queue locked
*/
bool RclMonEventQueue::wait()
{
if (!empty())
return true;
if (pthread_cond_wait(&m_data->m_cond, &m_data->m_mutex)) {
LOGERR(("RclMonEventQueue::wait: pthread_cond_wait failed\n"));
return false;
}
return true;
}
bool RclMonEventQueue::lock()
{
if (pthread_mutex_lock(&m_data->m_mutex)) {
LOGERR(("RclMonEventQueue::lock: pthread_mutex_lock failed\n"));
return false;
}
return true;
}
bool RclMonEventQueue::unlock()
{
if (pthread_mutex_unlock(&m_data->m_mutex)) {
LOGERR(("RclMonEventQueue::lock: pthread_mutex_unlock failed\n"));
return false;
}
return true;
}
void RclMonEventQueue::setConfig(RclConfig *cnf)
{
m_data->m_config = cnf;
}
RclConfig *RclMonEventQueue::getConfig()
{
return m_data->m_config;
}
extern int stopindexing;
bool RclMonEventQueue::ok()
{
if (m_data == 0)
return false;
return !stopindexing && m_data->m_ok;
}
void RclMonEventQueue::setTerminate()
{
lock();
m_data->m_ok = false;
pthread_cond_broadcast(&m_data->m_cond);
unlock();
}
bool RclMonEventQueue::pushEvent(const RclMonEvent &ev)
{
LOGDEB2(("RclMonEventQueue::pushEvent for %s\n", ev.m_path.c_str()));
lock();
// It seems that a newer event is always correct to override any
// older. TBVerified ?
m_data->m_queue[ev.m_path] = ev;
pthread_cond_broadcast(&m_data->m_cond);
unlock();
return true;
}
static string pidfile;
/** There can only be one real time indexing process running */
static bool processlock(const string &confdir)
{
pidfile = path_cat(confdir, RCL_MONITOR_PIDFILENAME);
for (int i = 0; i < 2; i++) {
int fd = open(pidfile.c_str(), O_RDWR|O_CREAT|O_EXCL, 0644);
if (fd < 0) {
if (errno != EEXIST) {
LOGERR(("processlock: cant create %s, errno %d\n",
pidfile.c_str(), errno));
return false;
}
if ((fd = open(pidfile.c_str(), O_RDONLY)) < 0) {
LOGERR(("processlock: cant open existing %s, errno %d\n",
pidfile.c_str(), errno));
return false;
}
char buf[20];
memset(buf, 0, sizeof(buf));
if (read(fd, buf, 19) < 0) {
LOGERR(("processlock: cant read existing %s, errno %d\n",
pidfile.c_str(), errno));
close(fd);
return false;
}
close(fd);
int pid = atoi(buf);
if (pid <= 0 || (kill(pid, 0) < 0 && errno == ESRCH)) {
// File exists but no process
if (unlink(pidfile.c_str()) < 0) {
LOGERR(("processlock: cant unlink existing %s, errno %d\n",
pidfile.c_str(), errno));
return false;
}
// Let's retry
continue;
} else {
// Other process running
return false;
}
}
// File could be created, write my pid in there
char buf[20];
sprintf(buf, "%d\n", getpid());
if (write(fd, buf, strlen(buf)+1) != int(strlen(buf)+1)) {
LOGERR(("processlock: cant write to %s, errno %d\n",
pidfile.c_str(), errno));
close(fd);
return false;
}
close(fd);
// Ok
break;
}
return true;
}
static void processunlock()
{
unlink(pidfile.c_str());
}
pthread_t rcv_thrid;
bool startMonitor(RclConfig *conf, bool nofork)
{
if (!processlock(conf->getConfDir())) {
LOGERR(("startMonitor: lock error. Other process running ?\n"));
return false;
}
atexit(processunlock);
rclEQ.setConfig(conf);
if (pthread_create(&rcv_thrid, 0, &rclMonRcvRun, &rclEQ) != 0) {
LOGERR(("startMonitor: cant create event-receiving thread\n"));
return false;
}
if (!rclEQ.lock()) {
return false;
}
LOGDEB(("start_monitoring: entering main loop\n"));
while (rclEQ.wait()) {
LOGDEB2(("startMonitor: wait returned\n"));
if (!rclEQ.ok())
break;
list<string> modified;
list<string> deleted;
// Process event queue
while (!rclEQ.empty()) {
// Retrieve event
RclMonEvent ev = rclEQ.pop();
switch (ev.m_etyp) {
case RclMonEvent::RCLEVT_MODIFY:
LOGDEB(("Monitor: Modify/Check on %s\n", ev.m_path.c_str()));
modified.push_back(ev.m_path);
break;
case RclMonEvent::RCLEVT_DELETE:
LOGDEB(("Monitor: Delete on %s\n", ev.m_path.c_str()));
deleted.push_back(ev.m_path);
break;
case RclMonEvent::RCLEVT_RENAME:
LOGDEB(("Monitor: Rename on %s\n", ev.m_path.c_str()));
break;
default:
LOGDEB(("Monitor: got Other on %s\n", ev.m_path.c_str()));
}
}
// Unlock queue before processing lists
rclEQ.unlock();
// Process
if (!indexfiles(conf, modified))
break;
if (!purgefiles(conf, deleted))
break;
// Lock queue before waiting again
rclEQ.lock();
}
return true;
}
#endif // RCL_MONITOR