162306a36Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci============================ 462306a36Sopenharmony_ciLinux Directory Notification 562306a36Sopenharmony_ci============================ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci Stephen Rothwell <sfr@canb.auug.org.au> 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciThe intention of directory notification is to allow user applications 1062306a36Sopenharmony_cito be notified when a directory, or any of the files in it, are changed. 1162306a36Sopenharmony_ciThe basic mechanism involves the application registering for notification 1262306a36Sopenharmony_cion a directory using a fcntl(2) call and the notifications themselves 1362306a36Sopenharmony_cibeing delivered using signals. 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ciThe application decides which "events" it wants to be notified about. 1662306a36Sopenharmony_ciThe currently defined events are: 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci ========= ===================================================== 1962306a36Sopenharmony_ci DN_ACCESS A file in the directory was accessed (read) 2062306a36Sopenharmony_ci DN_MODIFY A file in the directory was modified (write,truncate) 2162306a36Sopenharmony_ci DN_CREATE A file was created in the directory 2262306a36Sopenharmony_ci DN_DELETE A file was unlinked from directory 2362306a36Sopenharmony_ci DN_RENAME A file in the directory was renamed 2462306a36Sopenharmony_ci DN_ATTRIB A file in the directory had its attributes 2562306a36Sopenharmony_ci changed (chmod,chown) 2662306a36Sopenharmony_ci ========= ===================================================== 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciUsually, the application must reregister after each notification, but 2962306a36Sopenharmony_ciif DN_MULTISHOT is or'ed with the event mask, then the registration will 3062306a36Sopenharmony_ciremain until explicitly removed (by registering for no events). 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ciBy default, SIGIO will be delivered to the process and no other useful 3362306a36Sopenharmony_ciinformation. However, if the F_SETSIG fcntl(2) call is used to let the 3462306a36Sopenharmony_cikernel know which signal to deliver, a siginfo structure will be passed to 3562306a36Sopenharmony_cithe signal handler and the si_fd member of that structure will contain the 3662306a36Sopenharmony_cifile descriptor associated with the directory in which the event occurred. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciPreferably the application will choose one of the real time signals 3962306a36Sopenharmony_ci(SIGRTMIN + <n>) so that the notifications may be queued. This is 4062306a36Sopenharmony_ciespecially important if DN_MULTISHOT is specified. Note that SIGRTMIN 4162306a36Sopenharmony_ciis often blocked, so it is better to use (at least) SIGRTMIN + 1. 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ciImplementation expectations (features and bugs :-)) 4462306a36Sopenharmony_ci--------------------------------------------------- 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ciThe notification should work for any local access to files even if the 4762306a36Sopenharmony_ciactual file system is on a remote server. This implies that remote 4862306a36Sopenharmony_ciaccess to files served by local user mode servers should be notified. 4962306a36Sopenharmony_ciAlso, remote accesses to files served by a local kernel NFS server should 5062306a36Sopenharmony_cibe notified. 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ciIn order to make the impact on the file system code as small as possible, 5362306a36Sopenharmony_cithe problem of hard links to files has been ignored. So if a file (x) 5462306a36Sopenharmony_ciexists in two directories (a and b) then a change to the file using the 5562306a36Sopenharmony_ciname "a/x" should be notified to a program expecting notifications on 5662306a36Sopenharmony_cidirectory "a", but will not be notified to one expecting notifications on 5762306a36Sopenharmony_cidirectory "b". 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ciAlso, files that are unlinked, will still cause notifications in the 6062306a36Sopenharmony_cilast directory that they were linked to. 6162306a36Sopenharmony_ci 6262306a36Sopenharmony_ciConfiguration 6362306a36Sopenharmony_ci------------- 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ciDnotify is controlled via the CONFIG_DNOTIFY configuration option. When 6662306a36Sopenharmony_cidisabled, fcntl(fd, F_NOTIFY, ...) will return -EINVAL. 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ciExample 6962306a36Sopenharmony_ci------- 7062306a36Sopenharmony_ciSee tools/testing/selftests/filesystems/dnotify_test.c for an example. 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ciNOTE 7362306a36Sopenharmony_ci---- 7462306a36Sopenharmony_ciBeginning with Linux 2.6.13, dnotify has been replaced by inotify. 7562306a36Sopenharmony_ciSee Documentation/filesystems/inotify.rst for more information on it. 76