dnotify_demo.c
[Posted July 8, 2014 by jake]
/* dnotify_demo.c
Demonstrate the use of the (obsolete) dnotify feature to obtain
directory change notifications.
Usage is as shown in usageError() below. An example is the following:
dnotify dir1:a xyz/dir2:acdM
This program is Linux-specific.
Copyright (C) 2014, Michael Kerrisk
Licensed under the GNU General Public License, version 2 or later.
*/
#define _GNU_SOURCE /* To get DN_* constants from <fcntl.h> */
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void /* Print (optional) message and usage info, then exit */
usageError(const char *progName, const char *msg)
{
if (msg != NULL)
fprintf(stderr, "%s", msg);
fprintf(stderr, "Usage: %s directory:[events]...\n", progName);
fprintf(stderr, " Events are:\n"
" a - access; A - attrib; c - create; d - delete\n"
" m - modify; r - rename; M - multishot\n"
" (default is all events)\n");
exit(EXIT_FAILURE);
}
static void
handler(int sig, siginfo_t *si, void *ucontext)
{
printf("Got event on descriptor %d\n", si->si_fd);
/* UNSAFE (see Section 21.1.2) */
}
int
main(int argc, char *argv[])
{
struct sigaction sa;
int fd, events, fnum;
const int NOTIFY_SIG = SIGRTMIN;
char *p;
if (argc < 2)
usageError(argv[0], NULL);
/* Establish handler for notification signal */
sa.sa_sigaction = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO; /* So handler gets siginfo_t arg. */
if (sigaction(NOTIFY_SIG, &sa, NULL) == -1)
errExit("sigaction");
for (fnum = 1; fnum < argc; fnum++) {
p = strchr(argv[fnum], ':'); /* Look for optional ':' */
if (p == NULL) { /* Default is all events + multishot */
events = DN_ACCESS | DN_ATTRIB | DN_CREATE | DN_DELETE |
DN_MODIFY | DN_RENAME | DN_MULTISHOT;
} else { /* ':' present, parse event chars */
*p = '\0'; /* Terminates directory component */
events = 0;
for (p++; *p != '\0'; p++) {
switch (*p) {
case 'a': events |= DN_ACCESS; break;
case 'A': events |= DN_ATTRIB; break;
case 'c': events |= DN_CREATE; break;
case 'd': events |= DN_DELETE; break;
case 'm': events |= DN_MODIFY; break;
case 'r': events |= DN_RENAME; break;
case 'M': events |= DN_MULTISHOT; break;
default: usageError(argv[0], "Bad event character\n");
}
}
}
/* Obtain a file descriptor for the directory to be monitored */
fd = open(argv[fnum], O_RDONLY);
if (fd == -1)
errExit("open");
printf("opened '%s' as file descriptor %d\n", argv[fnum], fd);
/* Use alternate signal instead of SIGIO for dnotify events */
if (fcntl(fd, F_SETSIG, NOTIFY_SIG) == -1)
errExit("fcntl - F_SETSIG");
/* Enable directory change notifications */
if (fcntl(fd, F_NOTIFY, events) == -1)
errExit("fcntl-F_NOTIFY");
printf("events: %o\n", (unsigned int) events);
}
for (;;)
pause(); /* Wait for events */
}