inotify/dnotify.c

This is inotify/dnotify.c, an example to accompany the book, The Linux Programming Interface.

This file is not printed in the book; it is a supplementary file for Chapter 19.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

 

Download inotify/dnotify.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* dnotify.c

   Demonstrate the use of the (obsolete) dnotify feature to obtain directory
   change notifications. (Modern programs should use inotify instead of
   dnotify. The inotify API is available in Linux 2.6.13 and later.)

   Usage is as shown in usageError() below. An example is the following:

        dnotify dir1:a xyz/dir2:acdM

   See also demo_inotify.c.

   This program is Linux-specific.
*/
#define _GNU_SOURCE             /* To get DN_* constants from <fcntl.h> */
#include <fcntl.h>
#include <signal.h>
#include "tlpi_hdr.h"
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[])
{
    if (argc < 2 || strcmp(argv[1], "--help") == 0)
        usageError(argv[0], NULL);

    /* Establish handler for notification signal */

    const int NOTIFY_SIG = SIGRTMIN;

    struct sigaction sa;
    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 (int fnum = 1; fnum < argc; fnum++) {
        int events;
        char *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 */

        int 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 */
}

 

Download inotify/dnotify.c

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1