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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
/// inotify C++ interface implementation
/**
* \file inotify-cxx.cpp
*
* inotify C++ interface
*
* Copyright (C) 2006, 2007 Lukas Jelinek <lukas@aiken.cz>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of one of the following licenses:
*
* \li 1. X11-style license (see LICENSE-X11)
* \li 2. GNU Lesser General Public License, version 2.1 (see LICENSE-LGPL)
* \li 3. GNU General Public License, version 2 (see LICENSE-GPL)
*
* If you want to help with choosing the best license for you,
* please visit http://www.gnu.org/licenses/license-list.html.
*
*/
//added by ZenJu
#include <cstdio>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "inotify-cxx.h"
/// procfs inotify base path
#define PROCFS_INOTIFY_BASE "/proc/sys/fs/inotify/"
/// dump separator (between particular entries)
#define DUMP_SEP \
({ \
if (!rStr.empty()) { \
rStr.append(","); \
} \
})
int32_t InotifyEvent::GetDescriptor() const
{
return m_pWatch != NULL // if watch exists
? m_pWatch->GetDescriptor() // return its descriptor
: -1; // else return -1
}
uint32_t InotifyEvent::GetMaskByName(const std::string& rName)
{
if (rName == "IN_ACCESS")
return IN_ACCESS;
else if (rName == "IN_MODIFY")
return IN_MODIFY;
else if (rName == "IN_ATTRIB")
return IN_ATTRIB;
else if (rName == "IN_CLOSE_WRITE")
return IN_CLOSE_WRITE;
else if (rName == "IN_CLOSE_NOWRITE")
return IN_CLOSE_NOWRITE;
else if (rName == "IN_OPEN")
return IN_OPEN;
else if (rName == "IN_MOVED_FROM")
return IN_MOVED_FROM;
else if (rName == "IN_MOVED_TO")
return IN_MOVED_TO;
else if (rName == "IN_CREATE")
return IN_CREATE;
else if (rName == "IN_DELETE")
return IN_DELETE;
else if (rName == "IN_DELETE_SELF")
return IN_DELETE_SELF;
else if (rName == "IN_UNMOUNT")
return IN_UNMOUNT;
else if (rName == "IN_Q_OVERFLOW")
return IN_Q_OVERFLOW;
else if (rName == "IN_IGNORED")
return IN_IGNORED;
else if (rName == "IN_CLOSE")
return IN_CLOSE;
else if (rName == "IN_MOVE")
return IN_MOVE;
else if (rName == "IN_ISDIR")
return IN_ISDIR;
else if (rName == "IN_ONESHOT")
return IN_ONESHOT;
else if (rName == "IN_ALL_EVENTS")
return IN_ALL_EVENTS;
#ifdef IN_DONT_FOLLOW
else if (rName == "IN_DONT_FOLLOW")
return IN_DONT_FOLLOW;
#endif // IN_DONT_FOLLOW
#ifdef IN_ONLYDIR
else if (rName == "IN_ONLYDIR")
return IN_ONLYDIR;
#endif // IN_ONLYDIR
#ifdef IN_MOVE_SELF
else if (rName == "IN_MOVE_SELF")
return IN_MOVE_SELF;
#endif // IN_MOVE_SELF
return (uint32_t) 0;
}
void InotifyEvent::DumpTypes(uint32_t uValue, std::string& rStr)
{
rStr = "";
if (IsType(uValue, IN_ALL_EVENTS)) {
rStr.append("IN_ALL_EVENTS");
}
else {
if (IsType(uValue, IN_ACCESS)) {
DUMP_SEP;
rStr.append("IN_ACCESS");
}
if (IsType(uValue, IN_MODIFY)) {
DUMP_SEP;
rStr.append("IN_MODIFY");
}
if (IsType(uValue, IN_ATTRIB)) {
DUMP_SEP;
rStr.append("IN_ATTRIB");
}
if (IsType(uValue, IN_CREATE)) {
DUMP_SEP;
rStr.append("IN_CREATE");
}
if (IsType(uValue, IN_DELETE)) {
DUMP_SEP;
rStr.append("IN_DELETE");
}
if (IsType(uValue, IN_DELETE_SELF)) {
DUMP_SEP;
rStr.append("IN_DELETE_SELF");
}
if (IsType(uValue, IN_OPEN)) {
DUMP_SEP;
rStr.append("IN_OPEN");
}
if (IsType(uValue, IN_CLOSE)) {
DUMP_SEP;
rStr.append("IN_CLOSE");
}
#ifdef IN_MOVE_SELF
if (IsType(uValue, IN_MOVE_SELF)) {
DUMP_SEP;
rStr.append("IN_MOVE_SELF");
}
#endif // IN_MOVE_SELF
else {
if (IsType(uValue, IN_CLOSE_WRITE)) {
DUMP_SEP;
rStr.append("IN_CLOSE_WRITE");
}
if (IsType(uValue, IN_CLOSE_NOWRITE)) {
DUMP_SEP;
rStr.append("IN_CLOSE_NOWRITE");
}
}
if (IsType(uValue, IN_MOVE)) {
DUMP_SEP;
rStr.append("IN_MOVE");
}
else {
if (IsType(uValue, IN_MOVED_FROM)) {
DUMP_SEP;
rStr.append("IN_MOVED_FROM");
}
if (IsType(uValue, IN_MOVED_TO)) {
DUMP_SEP;
rStr.append("IN_MOVED_TO");
}
}
}
if (IsType(uValue, IN_UNMOUNT)) {
DUMP_SEP;
rStr.append("IN_UNMOUNT");
}
if (IsType(uValue, IN_Q_OVERFLOW)) {
DUMP_SEP;
rStr.append("IN_Q_OVERFLOW");
}
if (IsType(uValue, IN_IGNORED)) {
DUMP_SEP;
rStr.append("IN_IGNORED");
}
if (IsType(uValue, IN_ISDIR)) {
DUMP_SEP;
rStr.append("IN_ISDIR");
}
if (IsType(uValue, IN_ONESHOT)) {
DUMP_SEP;
rStr.append("IN_ONESHOT");
}
#ifdef IN_DONT_FOLLOW
if (IsType(uValue, IN_DONT_FOLLOW)) {
DUMP_SEP;
rStr.append("IN_DONT_FOLLOW");
}
#endif // IN_DONT_FOLLOW
#ifdef IN_ONLYDIR
if (IsType(uValue, IN_ONLYDIR)) {
DUMP_SEP;
rStr.append("IN_ONLYDIR");
}
#endif // IN_ONLYDIR
}
void InotifyEvent::DumpTypes(std::string& rStr) const
{
DumpTypes(m_uMask, rStr);
}
void InotifyWatch::SetMask(uint32_t uMask) throw (InotifyException)
{
IN_WRITE_BEGIN
if (m_wd != -1) {
int wd = inotify_add_watch(m_pInotify->GetDescriptor(), m_path.c_str(), uMask);
if (wd != m_wd) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("changing mask failed"), wd == -1 ? errno : EINVAL, this);
}
}
m_uMask = uMask;
IN_WRITE_END
}
void InotifyWatch::SetEnabled(bool fEnabled) throw (InotifyException)
{
IN_WRITE_BEGIN
if (fEnabled == m_fEnabled) {
IN_WRITE_END_NOTHROW
return;
}
if (m_pInotify != NULL) {
if (fEnabled) {
m_wd = inotify_add_watch(m_pInotify->GetDescriptor(), m_path.c_str(), m_uMask);
if (m_wd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("enabling watch failed"), errno, this);
}
m_pInotify->m_watches.insert(IN_WATCH_MAP::value_type(m_wd, this));
}
else {
if (inotify_rm_watch(m_pInotify->GetDescriptor(), m_wd) != 0) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("disabling watch failed"), errno, this);
}
m_pInotify->m_watches.erase(m_wd);
m_wd = -1;
}
}
m_fEnabled = fEnabled;
IN_WRITE_END
}
void InotifyWatch::__Disable()
{
IN_WRITE_BEGIN
if (!m_fEnabled) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("event cannot occur on disabled watch"), EINVAL, this);
}
if (m_pInotify != NULL) {
m_pInotify->m_watches.erase(m_wd);
m_wd = -1;
}
m_fEnabled = false;
IN_WRITE_END
}
Inotify::Inotify() throw (InotifyException)
{
IN_LOCK_INIT
m_fd = inotify_init();
if (m_fd == -1) {
IN_LOCK_DONE
throw InotifyException(IN_EXC_MSG("inotify init failed"), errno, NULL);
}
}
Inotify::~Inotify()
{
Close();
IN_LOCK_DONE
}
void Inotify::Close()
{
IN_WRITE_BEGIN
if (m_fd != -1) {
RemoveAll();
close(m_fd);
m_fd = -1;
}
IN_WRITE_END
}
void Inotify::Add(InotifyWatch* pWatch) throw (InotifyException)
{
IN_WRITE_BEGIN
// invalid descriptor - this case shouldn't occur - go away
if (m_fd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
}
// this path already watched - go away
if (FindWatch(pWatch->GetPath()) != NULL) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("path already watched"), EBUSY, this);
}
// for enabled watch
if (pWatch->IsEnabled()) {
// try to add watch to kernel
int wd = inotify_add_watch(m_fd, pWatch->GetPath().c_str(), pWatch->GetMask());
// adding failed - go away
if (wd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("adding watch failed"), errno, this);
}
// this path already watched (but defined another way)
InotifyWatch* pW = FindWatch(wd);
if (pW != NULL) {
// try to recover old watch because it may be modified - then go away
if (inotify_add_watch(m_fd, pW->GetPath().c_str(), pW->GetMask()) < 0) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("watch collision detected and recovery failed"), errno, this);
}
else {
// recovery failed - go away
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("path already watched (but defined another way)"), EBUSY, this);
}
}
pWatch->m_wd = wd;
m_watches.insert(IN_WATCH_MAP::value_type(pWatch->m_wd, pWatch));
}
m_paths.insert(IN_WP_MAP::value_type(pWatch->m_path, pWatch));
pWatch->m_pInotify = this;
IN_WRITE_END
}
void Inotify::Remove(InotifyWatch* pWatch) throw (InotifyException)
{
IN_WRITE_BEGIN
// invalid descriptor - this case shouldn't occur - go away
if (m_fd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
}
// for enabled watch
if (pWatch->m_wd != -1) {
// removing watch failed - go away
if (inotify_rm_watch(m_fd, pWatch->m_wd) == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("removing watch failed"), errno, this);
}
m_watches.erase(pWatch->m_wd);
pWatch->m_wd = -1;
}
m_paths.erase(pWatch->m_path);
pWatch->m_pInotify = NULL;
IN_WRITE_END
}
void Inotify::RemoveAll()
{
IN_WRITE_BEGIN
IN_WP_MAP::iterator it = m_paths.begin();
while (it != m_paths.end()) {
InotifyWatch* pW = (*it).second;
if (pW->m_wd != -1) {
inotify_rm_watch(m_fd, pW->m_wd);
pW->m_wd = -1;
}
pW->m_pInotify = NULL;
it++;
}
m_watches.clear();
m_paths.clear();
IN_WRITE_END
}
void Inotify::WaitForEvents(bool fNoIntr) throw (InotifyException)
{
ssize_t len = 0;
do {
len = read(m_fd, m_buf, INOTIFY_BUFLEN);
} while (fNoIntr && len == -1 && errno == EINTR);
if (len == -1 && !(errno == EWOULDBLOCK || errno == EINTR))
throw InotifyException(IN_EXC_MSG("reading events failed"), errno, this);
if (len == -1)
return;
IN_WRITE_BEGIN
ssize_t i = 0;
while (i < len) {
struct inotify_event* pEvt = (struct inotify_event*) &m_buf[i];
InotifyWatch* pW = FindWatch(pEvt->wd);
if (pW != NULL) {
InotifyEvent evt(pEvt, pW);
if ( InotifyEvent::IsType(pW->GetMask(), IN_ONESHOT)
|| InotifyEvent::IsType(evt.GetMask(), IN_IGNORED))
pW->__Disable();
m_events.push_back(evt);
}
i += INOTIFY_EVENT_SIZE + (ssize_t) pEvt->len;
}
IN_WRITE_END
}
bool Inotify::GetEvent(InotifyEvent* pEvt) throw (InotifyException)
{
if (pEvt == NULL)
throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this);
IN_WRITE_BEGIN
bool b = !m_events.empty();
if (b) {
*pEvt = m_events.front();
m_events.pop_front();
}
IN_WRITE_END
return b;
}
bool Inotify::PeekEvent(InotifyEvent* pEvt) throw (InotifyException)
{
if (pEvt == NULL)
throw InotifyException(IN_EXC_MSG("null pointer to event"), EINVAL, this);
IN_READ_BEGIN
bool b = !m_events.empty();
if (b) {
*pEvt = m_events.front();
}
IN_READ_END
return b;
}
InotifyWatch* Inotify::FindWatch(int iDescriptor)
{
IN_READ_BEGIN
IN_WATCH_MAP::iterator it = m_watches.find(iDescriptor);
InotifyWatch* pW = it == m_watches.end() ? NULL : (*it).second;
IN_READ_END
return pW;
}
InotifyWatch* Inotify::FindWatch(const std::string& rPath)
{
IN_READ_BEGIN
IN_WP_MAP::iterator it = m_paths.find(rPath);
InotifyWatch* pW = it == m_paths.end() ? NULL : (*it).second;
IN_READ_END
return pW;
}
void Inotify::SetNonBlock(bool fNonBlock) throw (InotifyException)
{
IN_WRITE_BEGIN
if (m_fd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
}
int res = fcntl(m_fd, F_GETFL);
if (res == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("cannot get inotify flags"), errno, this);
}
if (fNonBlock) {
res |= O_NONBLOCK;
}
else {
res &= ~O_NONBLOCK;
}
if (fcntl(m_fd, F_SETFL, res) == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("cannot set inotify flags"), errno, this);
}
IN_WRITE_END
}
void Inotify::SetCloseOnExec(bool fClOnEx) throw (InotifyException)
{
IN_WRITE_BEGIN
if (m_fd == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("invalid file descriptor"), EBUSY, this);
}
int res = fcntl(m_fd, F_GETFD);
if (res == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("cannot get inotify flags"), errno, this);
}
if (fClOnEx) {
res |= FD_CLOEXEC;
}
else {
res &= ~FD_CLOEXEC;
}
if (fcntl(m_fd, F_SETFD, res) == -1) {
IN_WRITE_END_NOTHROW
throw InotifyException(IN_EXC_MSG("cannot set inotify flags"), errno, this);
}
IN_WRITE_END
}
uint32_t Inotify::GetCapability(InotifyCapability_t cap) throw (InotifyException)
{
FILE* f = fopen(GetCapabilityPath(cap).c_str(), "r");
if (f == NULL)
throw InotifyException(IN_EXC_MSG("cannot get capability"), errno, NULL);
unsigned int val = 0;
if (fscanf(f, "%u", &val) != 1) {
fclose(f);
throw InotifyException(IN_EXC_MSG("cannot get capability"), EIO, NULL);
}
fclose(f);
return (uint32_t) val;
}
void Inotify::SetCapability(InotifyCapability_t cap, uint32_t val) throw (InotifyException)
{
FILE* f = fopen(GetCapabilityPath(cap).c_str(), "w");
if (f == NULL)
throw InotifyException(IN_EXC_MSG("cannot set capability"), errno, NULL);
if (fprintf(f, "%u", (unsigned int) val) <= 0) {
fclose(f);
throw InotifyException(IN_EXC_MSG("cannot set capability"), EIO, NULL);
}
fclose(f);
}
std::string Inotify::GetCapabilityPath(InotifyCapability_t cap) throw (InotifyException)
{
std::string path(PROCFS_INOTIFY_BASE);
switch (cap) {
case IN_MAX_EVENTS:
path.append("max_queued_events");
break;
case IN_MAX_INSTANCES:
path.append("max_user_instances");
break;
case IN_MAX_WATCHES:
path.append("max_user_watches");
break;
default:
throw InotifyException(IN_EXC_MSG("unknown capability type"), EINVAL, NULL);
}
return path;
}
|