aboutsummaryrefslogtreecommitdiff
path: root/doc/direvent.texi
blob: 47db4106e3c989841ffeed2fb48de54624399795 (plain)
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
\input texinfo @c -*-texinfo-*-
@smallbook
@c %**start of header
@setfilename direvent.info
@settitle GNU Direvent
@c %**end of header
@c @setchapternewpage odd

@defcodeindex pr
@defcodeindex op
@defcodeindex kw
@defcodeindex fl

@syncodeindex fn cp
@syncodeindex vr cp
@syncodeindex ky cp
@syncodeindex pg cp
@syncodeindex tp cp
@syncodeindex op cp
@syncodeindex pr cp
@syncodeindex kw cp
@syncodeindex fl cp

@include version.texi

@macro GNUDIREVENT
@acronym{GNU} @command{direvent}
@end macro

@ifinfo
@dircategory Individual utilities
@direntry
* direvent: (direvent).                Monitor changes in directories.
@end direntry
@end ifinfo

@copying
Copyright @copyright{} 2013-2014 Sergey Poznyakoff

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
A copy of the license is included in the section entitled ``GNU Free
Documentation License''.
@end copying

@titlepage
@title GNU Direvent
@subtitle version @value{VERSION}, @value{UPDATED}
@author Sergey Poznyakoff.
@page
@vskip 0pt plus 1filll
@insertcopying
@end titlepage

@ifnothtml
@page
@summarycontents
@page
@end ifnothtml
@contents

@ifnottex
@node Top
@top Direvent

This edition of the @cite{GNU Direvent} manual, last updated @value{UPDATED},
documents GNU Direvent Version @value{VERSION}.
@end ifnottex

@menu

* Intro::                   Introduction.
* Overview::
* Quick start::
* Invocation::
* Configuration::
* System dependencies::
* Reporting Bugs::          How to Report a Bug.

Appendices

* Copying This Manual::  The GNU Free Documentation License.
* Concept Index::        Index of Concepts.

@detailmenu
@end detailmenu
@end menu

@node Intro
@chapter Introduction
@GNUDIREVENT{} monitors events in file system directories.  For each
event that occurs in a set of pre-configured directories, the program
calls an external program associated with it, supplying it the
information about the event and the location within the file system
where it took place.

@GNUDIREVENT{} provides an easy way to configure
your system to react immediately if certain files undergo changes.
This may be helpful, for example, to track changes in important
configuration files.

Interfaces for tracking changes to file systems
are highly system-specific.  @GNUDIREVENT{} aims to provide a uniform
and system-independent command-level interface.  As of version
@value{VERSION} @command{direvent} works with modern Linux kernels
(since v. 2.6.13) and BSD systems (FreeBSD, NetBSD, OpenBSD, Darwin).

@node Overview
@chapter Overview
@GNUDIREVENT{} monitors a set of directories on the file system and
reacts when a file system event occurs in any of them.  Directories
and events to monitor are specified in the configuration file.  When
an event occurs, the program reacts by invoking an external command
configured for that event.			      

@cindex events
@cindex file system events
File system events can be divided into two major groups.  The
@dfn{system-dependent events} are specific for each particular kernel
interface.  In the contrast, @dfn{generic events} don't depend on the
underlying system.  They provide a higher level of abstraction and
make it possible to port @GNUDIREVENT{} configurations between various
systems and architectures.

@cindex generic events
@cindex events, generic
The generic events are:

@table @asis
@cindex create, generic event
@item create
A file was created.
@cindex delete, generic event
@item delete
A file was deleted;
@cindex write, generic event
@item write
A file was written to;
@cindex attrib, generic event
@item attrib
File attributes have changed.  This includes changes in the file
ownership, mode, link count, etc.
@end table

@anchor{handler}
@cindex watcher, introduced
@cindex handler, introduced
A @dfn{watcher} is a configuration entity that associates a set of
directories with a set of events and instructs @command{direvent} to
run a specified external command when any of these events occur in any
of these directories.  This external command (called a @dfn{handler})
can obtain information about the event that triggered it
from the environment variables, or from its command line.

Watchers are defined in the configuration file, which
@command{direvent} reads at startup.  This file has a simple and easy
to use syntax.

Three types of comments are allowed: inline comments, that begin with
a @samp{#} or @samp{//} and extend to the end of line, and multi-line
comments, which comprise everything enclosed between @samp{/*} and
@samp{*/}.  Comments and empty lines are ignored.  Whitespace
characters are ignored as well, except as they serve to separate
tokens.

A token is a string of consecutive characters from the following
classes: alphanumeric characters, underscores, dots, asteriscs,
slashes, semicolons, commercial at's, and dashes.

Any other sequence of characters must be enclosed in double quotation
marks in order to represent a single token.

Adjacent quoted strings are concatenated.

A configuration statement consists of a keyword and value separated by
any amount of whitespace and is terminated with a semicolon.  A block
statement is a collection of statements enclosed in curly braces.

@cindex watcher declaration, summary
The following block statement declares a watcher:

@example
@group
watcher @{
    path @var{pathname} [recursive [@var{level}]];
    file @var{pattern-list};
    event  @var{event-list};
    command @var{command-line};
    user @var{name};
    timeout @var{number};
    environ @var{env-spec};
    option @var{string-list};
@}
@end group
@end example

Each @code{watcher} statement instructs @command{direvent} to monitor
events listed in @var{event-list} occurring in the directories
specified by @var{pathname}s in @code{path} statements (any number of
@code{path} statements can be given).  When any such event is detected,
the supplied @var{command-line} will be executed.

Each directory defined with the @code{recursive} keyword will be
watched recursively.  This means that for each subdirectory created in
it, @command{direvent} will install a watcher similar to that of its parent
directory.  Optional @var{level} statement can be used to set up a cut-off
nesting level, beyond which the recursive operation is disabled.

The rest of statements are optional.  The @code{file} statement
instructs @GNUDIREVENT{} to react only if the event concerned the
file whose name matches one of the patterns given in its argument.
The @code{user} statement can be used to execute the
@var{command-line} as the user @var{name} (provided, of course, that
@command{direvent} is started with root privileges).  The
@code{timeout} specifies the maximum amount of time (in seconds) the
command is allowed to run.  It defaults to 5.  The @code{environ}
statement modifies the command environment.  Finally, the
@code{option} statement supplies additional options.  It can be used,
for example, to divert the command's output to syslog.

@node Quick start
@chapter Quick Start
Let's suppose you have a directory where users can upload their files
and you want these files to be processed right after upload, in real
time.  Let this directory be @file{/home/ftp/incoming} and the program
to process the upload be @file{/usr/bin/upload}.  Let's also suppose
that this program expects name of the uploaded file as its argument.

To make @command{direvent} handle this task, you would need to create a
watcher for the upload directory which would handle the @samp{create} event:

@example
watcher @{
    path /home/ftp/incoming;
    event create;
    # @r{more statements follow...}
@end example

On this event, the watcher is to invoke @file{/usr/bin/upload}
with the name of the created file as an argument.  To make it
possible, the @command{direvent} configuration file provides @dfn{macro
variables}, which can be used in the @code{command} argument at
configuration time and which are expanded to the actual values before
the command is executed.  Macro variables are referred to using the
same syntax as shell variables: a dollar sign followed by the variable
name, optionally enclosed in curly braces.  The @samp{file} variable
is expanded to the name of the file for which the event is reported.
This name is relative to the current working directory which, by the
time the handler is executed, is set to the directory where the event
occurred.  Thus, the handler can be configured as:

@example
    command "/usr/bin/upload $file";
@end example

To summarize, the watcher declaration is:

@example
watcher @{
    path /home/ftp/incoming;
    event create;
    command "/usr/bin/upload $file";
@}    
@end example

@cindex handler execution environment
@anchor{handler environment}
Before invoking the handler, the following operations are performed:

@enumerate 1
@item
The current working directory is set to the directory where the event
occurred.
@item
If the @code{environ} statement is present in the watcher, the
environment is modified according to its rules.  (@pxref{environ,
environment modification})
@item
The standard input is closed.
@item
If the @samp{stdout} option is supplied,
the standard output is captured and redirected to the syslog.
Otherwise it is closed.
@item
If the @samp{stderr} option is supplied,
the standard error is captured and redirected to the syslog.
Otherwise it is closed.
@item
File descriptors above 2 are closed.
@item
Macro variables are expanded.  @xref{macro expansion}.
@item
If the @samp{shell} option is set, the handler is invoked via the
shell, as @code{/bin/sh -c "@var{command}"}.

Otherwise, word splitting is performed on the resulting command line.
The first word is treated as the pathname of the program, which is
then invoked via the @code{execve} system call.
@end enumerate

@node Invocation
@chapter Invocation
The invocation syntax is:

@example
direvent  [@var{options}] [@var{config}]
@end example

@noindent
where @var{options} are command line options discussed below and
optional @var{config} supplies the configuration file to use instead
of the default @file{/etc/direvent.conf}.

The options are:

@table @option
@opindex -d
@opindex --debug
@item -d
@itemx --debug
Increase debug level.
@opindex -F
@opindex --facility
@item -F @var{name}
@itemx --facility=@var{name}
Set syslog facility.
@opindex -f
@opindex --foreground
@item -f
@itemx --foreground
Remain in foreground.
@opindex -I
@opindex --include
@anchor{include option}
@item -I @var{dir}
@itemx --include=@var{dir}
Add @var{dir} to the beginning of the include search path
(@pxref{include search path}).
@opindex -l
@item -l @var{prio}
While connected to a terminal, @command{direvent} outputs its diagnostics
messages to stderr and, if configured, to syslog.  This option
limits the amount of information output to the standard error.
The @var{prio} argument is one of the following priorities (in order
of increasing severity): @samp{debug}, @samp{info}, @samp{notice},
@samp{warning}, @samp{err}, @samp{crit}, @samp{alert}, @samp{emerg}.
When this option is given, only messages with the priority level equal
to or greater than @var{prio} will be duplicated on the standard error.
@opindex -P
@opindex --pidfile
@item -P @var{file}
@itemx --pidfile=@var{file}
Upon successful startup store the PID of the daemon process in
@var{file}.
@opindex -T
@opindex --self-test
@cindex self-test mode
@anchor{self-test mode}
@item -T @var{command}
@itemx --self-test=@var{command}
Run in @dfn{self-test mode}.  In this mode, @command{direvent} starts
external command supplied as the argument to this option and continues
running until the command exits.  If @var{command} terminates normally,
@command{direvent} exits with the code returned by it.  If
@var{command} terminates on signal, @command{direvent} exits with 
code @samp{0} if this signal was @code{SIGHUP}, and with code @samp{2}
otherwise.

The @var{command} can include any command line options or arguments,
provided that it is properly quoted.  It is invoked as 
@command{/bin/sh -c @var{command}} in the environment of
the parent @command{direvent} process.

This mode is used in @command{direvent} test suite.  The idea is
to configure the handler (@pxref{handler}) so that it sends
@code{SIGHUP} to @var{command} before exiting.  To this effect,
the special macro variable @code{$self_test_pid} is
defined (@pxref{macro expansion}) to the PID of the running
@var{command} process.  For example, consider configuration file
@file{test.conf}, which contains the following:

@example
watcher @{
    path /tmp;
    command "/bin/kill -HUP $self_test_pid";
@}    
@end example

Then, the following command can be used to check whether
@command{direvent} correctly reacts on file creation in
the watched directory:

@example
$ direvent --foreground \
    --self-test 'touch /tmp/file && /usr/bin/sleep 20 && exit 1' \
    test.conf
@end example

The command will return @samp{0} if the handler was invoked, and @samp{1}
if it was not.
@opindex -t
@opindex --lint
@item -t
@itemx --lint
Check configuration file for errors and exit.
@opindex -u
@opindex --user
@item -u @var{name}
@itemx --user=@var{name}
Run as this user.  This option overrides the @code{user} configuration
statement (@pxref{general settings, user}).
@end table

The following options are @dfn{informative}.  They cause the program
to display the requested piece of information and terminate:

@table @option
@opindex -H
@opindex --config-help
@item -H
@itemx --config-help
Show configuration file summary.
@opindex -h
@opindex --help
@item -h
@itemx --help
Give a short usage summary.
@opindex --usage
@item --usage
Display available command line options.
@opindex -V
@opindex --version
@item -V
@itemx --version
Print program version.
@end table
      
@node Configuration
@chapter Configuration

@menu
* syntax::
* macro expansion::
* general settings::
* syslog::
* watcher::
@end menu

@node syntax
@section Configuration Syntax
  The configuration file consists of statements and comments.

  There are three classes of lexical tokens: keywords, values, and
separators.  Blanks, tabs, newlines and comments, collectively called
@dfn{white space} are ignored except as they serve to separate
tokens.  Some white space is required to separate otherwise adjacent 
keywords and values.

@menu
* Comments::
* Pragmatic Comments::
* Statements::
@end menu

@node Comments
@subsection Comments
@cindex Comments in a configuration file
@cindex single-line comments
  @dfn{Comments} may appear anywhere where white space may appear in the
configuration file.  There are two kinds of comments:
single-line and multi-line comments.  @dfn{Single-line} comments start
with @samp{#} or @samp{//} and continue to the end of the line:

@smallexample
# This is a comment
// This too is a comment
@end smallexample

@cindex multi-line comments
  @dfn{Multi-line} or @dfn{C-style} comments start with the two
characters @samp{/*} (slash, star) and continue until the first
occurrence of @samp{*/} (star, slash).

  Multi-line comments cannot be nested.  However, single-line comments
may well appear within multi-line ones.

@node Pragmatic Comments
@subsection Pragmatic Comments
@cindex comments, pragmatic
@cindex pragmatic comments
  Pragmatic comments are similar to usual single-line comments,
except that they cause some changes in the way the configuration is
parsed.  Pragmatic comments begin with a @samp{#} sign and end with the
next physical newline character.

@table @code
@kwindex #include
@item #include <@var{file}>
@itemx #include "@var{file}"
Include the contents of the file @var{file}.  If @var{file} is an
absolute file name, the named file is included.  An error message will
be issued if it does not exist.

If @var{file} contains wildcard characters (@samp{*}, @samp{[},
@samp{]} or @samp{?}), it is interpreted as a shell globbing pattern and
all files matching that pattern are included, in lexicographical
order.  If no matching files are found, the directive is replaced with
an empty line.

Otherwise, the form with angle brackets searches for file in the
@dfn{include search path}, while the second one looks for it in the
current working directory first, and, if not found there, in the
include search path.  If the file is not found, an error message will
be issued.

@cindex include search path, preprocessor
@cindex include directories, preprocessor
@cindex preprocessor include search path
@anchor{include search path}
@dfn{Include search path} is formed by two directory sets: the
user-defined search path, as defined by eventual @option{-I}
(@pxref{include option}) command line options, and the standard include
search path, defined at compile time.  The latter can be inspected
using the @option{--help} option.

The order of directories is as follows.  First, @command{direvent} scans
any directories given with @option{-I} options, in the same order as
given on the command line.  If @var{file} is not found in any of them,
the standard include search path is scanned.  It is defined at the
compile time and by default consists of two directories:

@itemize @bullet
@item @file{@var{prefix}/share/direvent/@value{VERSION}/include}
@item @file{@var{prefix}/share/direvent/include}
@end itemize

@noindent
where @var{prefix} is the installation prefix.  The default can be
changed when configuring the package.  To inspect the actual standard
include search path at the runtime, run @code{direvent --help}, and
look for the string @samp{Include search path:} in its output.

@kwindex #include_once
@item #include_once <@var{file}>
@itemx #include_once @var{file}
  Same as @code{#incl