inotify

Section: Environments, Tables, and Troff Macros (7)
Updated: 2026-02-14
Index JM Home Page roff page
 

名前

inotify - ファイルシステムイベントを監視する  

説明

inotify API はファイルシステムイベントを監視するための機構を提供する。 inotify は個々のファイルやディレクトリを監視するのに使える。 ディレクトリを監視する場合、inotify はディレクトリ自身と ディレクトリ内のファイルのイベントを返す。

この API では以下のシステムコールが使用される。

•
inotify_init(2) は inotify インスタンスを作成し、inotify インスタンスを参照する ファイルディスクリプターを返す。 より新しい inotify_init1(2) も inotify_init(2) と同様だが、 こちらにはいくつかの追加の機能を利用するための flags 引数がある。
•
inotify_add_watch(2) は inotify インスタンスに関連づけられた「監視対象 (watch) リスト」を操作する。 監視対象リストの各アイテム ("watch") は、 ファイルまたはディレクトリのパス名と、 そのパス名で参照されるファイルに対して カーネルが監視する複数のイベントの集合を指定する。 inotify_add_watch(2) は新しい監視アイテムの作成や既存の監視対象の変更ができる。 各監視対象は一意の「監視対象ディスクリプター」を持つ。 これは監視対象を作成したときに inotify_add_watch(2) から返される整数である。
•
監視しているファイルやディレクトリでイベントが起こると、 それらのイベントはアプリケーションから inotify ファイルディスクリプターから read(2) を使って構造化データとして読み出すことができる (下記参照)。
•
inotify_rm_watch(2) は inotify の監視対象リストからアイテムを削除する。
•
inotify インスタンスを指している 全てのファイルディスクリプターが (close(2) を使って) クローズされた場合、 その下層にあるオブジェクトとそのリソースは、 カーネルで再利用するために解放される。 関連が切られた監視対象は自動的に解放される。

注意深くプログラミングすることで、 アプリケーションは inotify を使ってファイルシステムオブジェクトの集合の状態を効率的に監視しキャッシュしておくことができる。 しかしながら、ロバストなアプリケーションでは、監視ロジックのバグや以下に説明があるような種類の競合条件によりファイルシステムの状態とキャッシュが一致しない状態になることがあるという事実も考慮に入れておくべきである。 おそらく何らかの一貫性のチェックを行い、不一致が検出された場合にはキャッシュを再構築するのが懸命だろう。  

inotify ファイルディスクリプターからのイベントの読み出し

どのようなイベントが起こっていたかを知るには、 アプリケーションで inotify ファイルディスクリプターを read(2) すればよい。 これまでに何もイベントが起こっていない場合、 停止 (blocking) モードのファイルディスクリプターであれば、 少なくとも 1 つのイベントが起こるまで read(2) は停止する (シグナルにより割り込まれなかった場合。 シグナルによる割り込みがあった場合、呼び出しはエラー EINTR で失敗する。 signal(7) 参照)。

read(2) が成功すると、以下の構造体を 1 つ以上含むバッファーが返される:





struct inotify_event {
    int      wd;       /* 監視対象ディスクリプター */
    uint32_t mask;     /* イベントを示すマスク */
    uint32_t cookie;   /* 関連するイベント群を関連づける
                          一意なクッキー (rename(2) 用) */
    uint32_t len;      /* name フィールドのサイズ */
    char     name[];   /* ヌルで終端された任意の名前 */
};

wd はイベント発生の監視対象を指定する。 これは、前もって行われた inotify_add_watch(2) 呼び出しで返された監視対象ディスクリプターのうちの 1 つである。

mask には発生したイベント (下記参照) を記述するためのビットが含まれる。

cookie は関連するイベントを関連づけるための一意な整数である。 現在のところ、この値は rename イベントに対してのみ使われており、 結果のペアである IN_MOVED_FROM と IN_MOVED_TO イベントを アプリケーションで関連づけることができる。 他のイベント種別の場合には、 cookie は 0 に設定する。

The name field is present only when an event is returned for a file inside a watched directory; it identifies the filename within the watched directory. This filename is null-terminated, and may include further null bytes ('\0') to align subsequent reads to a suitable address boundary.

The len field counts all of the bytes in name, including the null bytes; the size of each inotify_event structure is thus sizeof(struct inotify_event)+size.

The behavior when the buffer given to read(2) is too small to return information about the next event depends on the kernel version: before Linux 2.6.21, read(2) returns 0; since Linux 2.6.21, read(2) fails with the error EINVAL. Specifying a buffer of size

sizeof(struct inotify_event) + NAME_MAX + 1

を指定すれば、少なくとも 1 イベントで読み出しを行うには十分である。  

inotify イベント

inotify_add_watch(2) の mask 引数と、inotify ファイル構造体を read(2) したときに返される inotify_event 構造体の mask フィールドは、ともに inotify イベントを識別するための ビットマスクである。 以下のビットが inotify_add_watch(2) を呼ぶときの mask に指定可能であり、 read(2) で返される mask フィールドで返される:
IN_ACCESS (+)
(read(2), execve(2) などで) ファイルがアクセスされた。
IN_ATTRIB (*)
Metadata changed---for example, permissions (e.g., chmod(2)), timestamps (e.g., utimensat(2)), extended attributes (setxattr(2)), link count (since Linux 2.6.25; e.g., for the target of link(2) and for unlink(2)), and user/group ID (e.g., chown(2)).
IN_CLOSE_WRITE (+)
書き込みのためにオープンされたファイルがクローズされた。
IN_CLOSE_NOWRITE (*)
書き込み用としてはオープンされていないファイルやディレクトリがクローズされた。
IN_CREATE (+)
監視対象ディレクトリ内でファイルやディレクトリが作成された (open(2) O_CREAT, mkdir(2), link(2), symlink(2), UNIX ドメインソケットに対する bind(2) など)。
IN_DELETE (+)
監視対象ディレクトリ内でファイルやディレクトリが削除された。
IN_DELETE_SELF
監視対象のファイルやディレクトリ自身が削除あれた。 (このイベントはオブジェクトが別のファイルシステムに移動された場合にも発生する。 mv(1) は実際には別のファイルシステムにファイルをコピーした後、元のファイルシステムからそのファイルを削除するからである。) また、 結果的に監視ディスクリプターに対して IN_IGNORED イベントも生成される。
IN_MODIFY (+)
ファイルが変更された (write(2), truncate(2) など)。
IN_MOVE_SELF
監視対象のディレクトリまたはファイル自身が移動された。
IN_MOVED_FROM (+)
ファイル名の変更を行った際に変更前のファイル名が含まれるディレクトリに対して生成される。
IN_MOVED_TO (+)
ファイル名の変更を行った際に新しいファイル名が含まれるディレクトリに対して生成される。
IN_OPEN (*)
ファイルやディレクトリがオープンされた。

Inotify monitoring is inode-based: when monitoring a file (but not when monitoring the directory containing a file), an event can be generated for activity on any link to the file (in the same or a different directory).

ディレクトリを監視する場合:

•
上記でアスタリスク (*) が付いたイベントは、 ディレクトリ自身とディレクトリ内のオブジェクトのどちらに対しても発生する。
•
上記でプラス記号 (+) が付いたイベントは、 ディレクトリ内のオブジェクトに対してのみ発生する (ディレクトリ自身に対しては発生しない)。

Note: when monitoring a directory, events are not generated for the files inside the directory when the events are performed via a pathname (i.e., a link) that lies outside the monitored directory.

監視対象のディレクトリ内のオブジェクトに対してイベントが発生した場合、 inotify_event 構造体で返される name フィールドは、ディレクトリ内のファイル名を表す。

IN_ALL_EVENTS マクロは上記のイベント全てのマスクとして定義される。 このマクロは inotify_add_watch(2) を呼び出すときの mask 引数として使える。

以下の 2 つの便利なマクロが定義されている。

IN_MOVE
IN_MOVED_FROM | IN_MOVED_TO と等価。
IN_CLOSE
IN_CLOSE_WRITE | IN_CLOSE_NOWRITE と等価。

その他にも以下のビットを inotify_add_watch(2) を呼ぶときの mask に指定できる:

IN_DONT_FOLLOW (Linux 2.6.15 以降)
pathname がシンボリックリンクである場合に辿らない。 (Linux 2.6.15 以降)
IN_EXCL_UNLINK (Linux 2.6.36 以降)
デフォルトでは、あるディレクトリの子ファイルに関するイベントを監視 (watch) した際、ディレクトリからその子ファイルが削除 (unlink) された場合であってもその子ファイルに対してイベントが生成される。このことは、アプリケーションによってはあまり興味のないイベントが大量に発生することにつながる (例えば、/tmp を監視している場合、たくさんのアプリケーションが、すぐにその名前が削除される一時ファイルをそのディレクトリに作成する)。 IN_EXCL_UNLINK を指定するとこのデフォルトの動作を変更でき、監視対象のディレクトリから子ファイルが削除された後に子ファイルに関するイベントが生成されなくなる。
IN_MASK_ADD
If a watch instance already exists for the filesystem object corresponding to pathname, add (OR) the events in mask to the watch mask (instead of replacing the mask); the error EINVAL results if IN_MASK_CREATE is also specified.
IN_ONESHOT
pathname に対応するファイルシステムオブジェクトを 1 イベントについてだけ監視し、 イベントが発生したら監視対象リストから削除する。
IN_ONLYDIR (Linux 2.6.15 以降)
Watch pathname only if it is a directory; the error ENOTDIR results if pathname is not a directory. Using this flag provides an application with a race-free way of ensuring that the monitored object is a directory.
IN_MASK_CREATE (Linux 4.18 以降)
Watch pathname only if it does not already have a watch associated with it; the error EEXIST results if pathname is already being watched.
Using this flag provides an application with a way of ensuring that new watches do not modify existing ones. This is useful because multiple paths may refer to the same inode, and multiple calls to inotify_add_watch(2) without this flag may clobber existing watch masks.

以下のビットが read(2) で返される mask フィールドに設定される:

IN_IGNORED
監視対象が (inotify_rm_watch(2) により) 明示的に 削除された。もしくは (ファイルの削除、またはファイル システムのアンマウントにより) 自動的に削除された。「バグ」も参照のこと。
IN_ISDIR
このイベントの対象がディレクトリである。
IN_Q_OVERFLOW
イベントキューが溢れた (このイベントの場合、wd は -1 である)。
IN_UNMOUNT
監視対象オブジェクトを含むファイルシステムがアンマウントされた。さらに、この監視対象ディスクリプターに対して IN_IGNORED イベントが生成される。
 

例

アプリケーションがディレクトリ dir とファイル dir/myfile のすべてのイベントを監視しているとする。 以下に、これらの 2 つのオブジェクトに対して生成されるイベントの例を示す。
fd = open("dir/myfile", O_RDWR);
dir と dir/myfile の両方に対して IN_OPEN イベントが生成される。
read(fd, buf, count);
dir と dir/myfile の両方に対して IN_ACCESS イベントが生成される
write(fd, buf, count);
dir と dir/myfile の両方に対して IN_MODIFY イベントが生成される
fchmod(fd, mode);
dir と dir/myfile の両方に対して IN_ATTRIB イベントが生成される
close(fd);
dir と dir/myfile の両方に対して IN_CLOSE_WRITE イベントが生成される

アプリケーションがディレクトリ dir1 と dir2、およびファイル dir1/myfile を監視しているとする。 以下に生成されるイベントの例を示す。

link("dir1/myfile", "dir2/new");
myfile に対して IN_ATTRIB イベントが生成され、 dir2 に対して IN_CREATE イベントが生成される。
rename("dir1/myfile", "dir2/myfile");
dir1 に対してイベント IN_MOVED_FROM が、 dir2 に対してイベント IN_MOVED_TO が、 myfile に対してイベント IN_MOVE_SELF が生成される。この際 イベント IN_MOVED_FROM と IN_MOVED_TO は同じ cookie 値を持つ。

dir1/xx と dir2/yy は同じファイルを参照するリンクで (他のリンクはないものとする)、 アプリケーションは dir1, dir2, dir1/xx, dir2/yy を監視しているものとする。 以下に示す順序で下記の呼び出しを実行すると、以下のイベントが生成される。

unlink("dir2/yy");
xx に対して IN_ATTRIB イベントが生成され (リンク数が変化したため)、 dir2 に対して IN_DELETE イベントが生成される。
unlink("dir1/xx");
xx に対してイベント IN_ATTRIB, IN_DELETE_SELF, IN_IGNORED が生成され、 dir1 に対して IN_DELETE イベントが生成される。

アプリケーションがディレクトリ dir と (空の) ディレクトリ dir/subdir を監視しているものとする。 以下に生成されるイベントの例を示す。

mkdir("dir/new", mode);
dir に対して IN_CREATE | IN_ISDIR イベントが生成される。
rmdir("dir/subdir");
subdir に対してイベント IN_DELETE_SELF と IN_IGNORED が生成され、 dir に対して IN_DELETE | IN_ISDIR イベントが生成される。
 

/proc インターフェース

以下のインターフェースは、inotify で消費される カーネルメモリーの総量を制限するのに使用できる:
/proc/sys/fs/inotify/max_queued_events
このファイルの値は、アプリケーションが inotify_init(2) を呼び出すときに使用され、対応する inotify インスタンスについて キューに入れられるイベントの数の上限を設定する。 この制限を超えたイベントは破棄されるが、 IN_Q_OVERFLOW イベントが常に生成される。
/proc/sys/fs/inotify/max_user_instances
1 つの実ユーザー ID に対して生成できる inotify インスタンスの数の上限を指定する。
/proc/sys/fs/inotify/max_user_watches
作成可能な監視対象の数の実 UID 単位の上限を指定する。
 

標準規格

Linux.  

歴史

Inotify was merged into Linux 2.6.13. The required library interfaces were added in glibc 2.4. (IN_DONT_FOLLOW, IN_MASK_ADD, and IN_ONLYDIR were added in glibc 2.5.)  

注意

inotify ファイルディスクリプターは select(2), poll(2), epoll(7) を使って監視できる。 イベントがある場合、ファイルディスクリプターは読み込み可能と通知する。

Linux 2.6.25 以降では、シグナル駆動 (signal-driven) I/O の通知が inotify ファイルディスクリプターについて利用可能である。 fcntl(2) に書かれている (O_ASYNC フラグを設定するための) F_SETFL, F_SETOWN, F_SETSIG の議論を参照のこと。 シグナルハンドラーに渡される siginfo_t 構造体は、以下のフィールドが設定される (siginfo_t は sigaction(2) で説明されている)。 si_fd には inotify ファイルディスクリプター番号が、 si_signo にはシグナル番号が、 si_code には POLL_IN が、 si_band には POLLIN が設定される。

inotify ファイルディスクリプターに対して 連続して生成される出力 inotify イベントが同一の場合 (wd, mask, cookie, name が等しい場合)、 前のイベントがまだ読み込まれていなければ、 連続するイベントが 1 つのイベントにまとめられる (ただし「バグ」の節も参照のこと)。 これによりイベントキューに必要なカーネルメモリー量が減るが、 これはまたアプリケーションがファイルイベント数を信頼性を持って数えるのに inotify を使用できないということでもある。

inotify ファイルディスクリプターの読み込みで返されるイベントは、 順序付けられたキューになる。 従って、たとえば、あるディレクトリの名前を別の名前に変更した場合、 inotify ファイルディスクリプターについての正しい順番で イベントが生成されることが保証される。

The set of watch descriptors that is being monitored via an inotify file descriptor can be viewed via the entry for the inotify file descriptor in the process's /proc/pid/fdinfo directory. See proc(5) for further details. The FIONREAD ioctl(2) returns the number of bytes available to read from an inotify file descriptor.  

制限と警告

inotify API では、inotify イベントが発生するきっかけとなったユーザーやプロセスに関する情報は提供されない。とりわけ、inotify 経由でイベントを監視しているプロセスが、自分自身がきっかけとなったイベントと他のプロセスがきっかけとなったイベントを区別する簡単な手段はない。

inotify は、ファイルシステム API 経由でユーザー空間プログラムがきっかけとなったイベントだけを報告する。 結果として、 inotify はネットワークファイルシステムで発生したリモートのイベントを捉えることはできない (このようなイベントを捉えるにはアプリケーションはファイルシステムをポーリングする必要がある)。 さらに、 /proc, /sys, /dev/pts といったいくつかの疑似ファイルシステムは inotify で監視することができない。

inotify API は mmap(2), msync(2), munmap(2) により起こったファイルのアクセスと変更を報告しない。

inotify API では影響が受けるファイルをファイル名で特定する。 しかしながら、アプリケーションが inotify イベントを処理する時点では、 そのファイル名がすでに削除されたり変更されたりしている可能性がある。

inotify API では監視対象ディスクリプターを通してイベントが区別される。 (必要であれば) 監視対象ディスクリプターとパス名のマッピングをキャッシュしておくのはアプリケーションの役目である。 ディレクトリの名前変更の場合、キャッシュしている複数のパス名に影響がある点に注意すること。

inotify によるディレクトリの監視は再帰的に行われない: あるディレクトリ以下の サブディレクトリを監視する場合、 監視対象を追加で作成しなければならない。 大きなディレクトリツリーの場合には、この作業にかなり時間がかかることがある。

ディレクトリツリー全体を監視していて、 そのツリー内に新しいサブディレクトリが作成されるか、 既存のディレクトリが名前が変更されそのツリー内に移動した場合、 新しいサブディレクトリに対する watch を作成するまでに、 新しいファイル (やサブディレクトリ) がそのサブディレクトリ内にすでに作成されている場合がある点に注意すること。 したがって、watch を追加した直後にサブディレクトリの内容をスキャンしたいと思う場合もあるだろう (必要ならそのサブディレクトリ内のサブディレクトリに対する watch も再帰的に追加することもあるだろう)。

イベントキューはオーバーフローする場合があることに注意すること。 この場合、イベントは失なわれる。 ロバスト性が求められるアプリケーションでは、 イベントが失なわれる可能性も含めて適切に処理を行うべきである。 例えば、アプリケーション内のキャッシュの一部分または全てを再構築する必要があるかもしれない。 (単純だが、おそらくコストがかかる方法は、 inotify ファイルディスクリプターをクローズし、 キャッシュを空にし、 新しい inotify ファイルディスクリプターを作成し、 監視しているオブジェクトの監視対象ディスクリプターとキャッシュエントリーの再作成を行う方法である。)

If a filesystem is mounted on top of a monitored directory, no event is generated, and no events are generated for objects immediately under the new mount point. If the filesystem is subsequently unmounted, events will subsequently be generated for the directory and the objects it contains.  

rename() イベントの取り扱い

上述の通り、 rename(2) により生成される IN_MOVED_FROM と IN_MOVED_TO イベントの組は、共有される cookie 値によって対応を取ることができる。 しかし、対応を取る場合にはいくつか難しい点がある。

これらの 2 つのイベントは、 inotify ファイルディスクリプターから読み出しを行った場合に、通常はイベントストリーム内で連続している。 しかしながら、連続していることは保証されていない。 複数のプロセスが監視対象オブジェクトでイベントを発生させた場合、 (めったに起こらないことだが) イベント IN_MOVED_FROM と IN_MOVED_TO の間に任意の数の他のイベントがはさまる可能性がある。 さらに、対となるイベントがアトミックにキューに挿入されることも保証されていない。 IN_MOVED_FROM が現れたが IN_MOVED_TO は現れていないという短い期間がありえるということだ。

したがって、 rename(2) により生成された IN_MOVED_FROM と IN_MOVED_TO のイベントの組の対応を取るのは本質的に難しいことである (監視対象のディレクトリの外へオブジェクトの rename が行われた場合には IN_MOVED_TO イベントは存在しさえしないことを忘れてはならない)。 (イベントは常に連続しているとの仮定を置くといった) 発見的な方法を使うと、ほとんどの場合でイベントの組をうまく見つけることができるが、 いくつかの場合に見逃すことが避けられず、 アプリケーションが IN_MOVED_FROM と IN_MOVED_TO イベントが無関係だとみなしてしまう可能性がある。 結果的に、監視対象ディスクリプターが破棄され再作成された場合、これらの監視対象ディスクリプターは、処理待ちイベントの監視対象ディスクリプターと一貫性のないものになってしまう (inotify ファイルディスクリプターの再作成とキャッシュの再構成はこの状況に対処するのに有用な方法なのだが)。

Applications should also allow for the possibility that the IN_MOVED_FROM event was the last event that could fit in the buffer returned by the current call to read(2), and the accompanying IN_MOVED_TO event might be fetched only on the next read(2), which should be done with a (small) timeout to allow for the fact that insertion of the IN_MOVED_FROM+IN_MOVED_TO event pair is not atomic, and also the possibility that there may not be any IN_MOVED_TO event.  

バグ

Before Linux 3.19, fallocate(2) did not create any inotify events. Since Linux 3.19, calls to fallocate(2) generate IN_MODIFY events.

Before Linux 2.6.16, the IN_ONESHOT mask flag does not work.

元々は設計/実装時の意図通り、 イベントが一つ発生し watch が削除された際に IN_ONESHOT フラグでは IN_IGNORED イベントが発生しなかった。 しかし、 別の変更での意図していなかった影響により、 Linux 2.6.36 以降では、 この場合に IN_IGNORED イベントが生成される。

Before Linux 2.6.25, the kernel code that was intended to coalesce successive identical events (i.e., the two most recent events could potentially be coalesced if the older had not yet been read) instead checked if the most recent event could be coalesced with the oldest unread event.

When a watch descriptor is removed by calling inotify_rm_watch(2) (or because a watch file is deleted or the filesystem that contains it is unmounted), any pending unread events for that watch descriptor remain available to read. As watch descriptors are subsequently allocated with inotify_add_watch(2), the kernel cycles through the range of possible watch descriptors (1 to INT_MAX) incrementally. When allocating a free watch descriptor, no check is made to see whether that watch descriptor number has any pending unread events in the inotify queue. Thus, it can happen that a watch descriptor is reallocated even when pending unread events exist for a previous incarnation of that watch descriptor number, with the result that the application might then read those events and interpret them as belonging to the file associated with the newly recycled watch descriptor. In practice, the likelihood of hitting this bug may be extremely low, since it requires that an application cycle through INT_MAX watch descriptors, release a watch descriptor while leaving unread events for that watch descriptor in the queue, and then recycle that watch descriptor. For this reason, and because there have been no reports of the bug occurring in real-world applications, as of Linux 3.15, no kernel changes have yet been made to eliminate this possible bug.  

例

以下のプログラムは inotify API の使用例を示したものである。 コマンドライン引数で渡されたディレクトリに印を付け、 タイプが IN_OPEN, IN_CLOSE_NOWRITE IN_CLOSE_WRITE のイベントを待つ。

以下は、 ファイル /home/user/temp/foo を編集し、 ディレクトリ /tmp の一覧表示を行った場合の出力である。 対象のファイルとディレクトリがオープンされる前に、イベント IN_OPEN が発生している。 対象ファイルがクローズされた後にイベント IN_CLOSE_WRITE が発生している。 対象ディレクトリがクローズされた後にイベント IN_CLOSE_NOWRITE が発生している。 ユーザーが ENTER キーを押すると、プログラムの実行は終了する。  

出力例

$ ./a.out /tmp /home/user/temp;
Press enter key to terminate.
Listening for events.
IN_OPEN: /home/user/temp/foo [file]
IN_CLOSE_WRITE: /home/user/temp/foo [file]
IN_OPEN: /tmp/ [directory]
IN_CLOSE_NOWRITE: /tmp/ [directory]

Listening for events stopped.
 

プログラムソース

#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <string.h>

/* Read all available inotify events from the file descriptor 'fd'.
   wd is the table of watch descriptors for the directories in argv.
   argc is the size of wd and argv.
   argv is the list of watched directories.
   Entry 0 of wd and argv is unused.  */

static void
handle_events(int fd, int *wd, int argc, char* argv[])
{
    /* Some systems cannot read integer variables if they are not
       properly aligned.  On other systems, incorrect alignment may
       decrease performance.  Hence, the buffer used for reading from
       the inotify file descriptor should have the same alignment as
       struct inotify_event.  */

    char buf[4096]
        __attribute__ ((aligned(__alignof__(struct inotify_event))));
    const struct inotify_event *event;
    ssize_t size;

    /* Loop while events can be read from inotify file descriptor.  */

    for (;;) {

        /* Read some events.  */

        size = read(fd, buf, sizeof(buf));
        if (size == -1 && errno != EAGAIN) {
            perror("read");
            exit(EXIT_FAILURE);
        }

        /* If the nonblocking read() found no events to read, then
           it returns -1 with errno set to EAGAIN.  In that case,
           we exit the loop.  */

        if (size <= 0)
            break;

        /* Loop over all events in the buffer.  */

        for (char *ptr = buf; ptr < buf + size;
                ptr += sizeof(struct inotify_event) + event->len) {

            event = (const struct inotify_event *) ptr;

            /* Print event type.  */

            if (event->mask & IN_OPEN)
                printf("IN_OPEN: ");
            if (event->mask & IN_CLOSE_NOWRITE)
                printf("IN_CLOSE_NOWRITE: ");
            if (event->mask & IN_CLOSE_WRITE)
                printf("IN_CLOSE_WRITE: ");

            /* Print the name of the watched directory.  */

            for (size_t i = 1; i < argc; ++i) {
                if (wd[i] == event->wd) {
                    printf("%s/", argv[i]);
                    break;
                }
            }

            /* Print the name of the file.  */

            if (event->len)
                printf("%s", event->name);

            /* Print type of filesystem object.  */

            if (event->mask & IN_ISDIR)
                printf(" [directory]\n");
            else
                printf(" [file]\n");
        }
    }
}

int
main(int argc, char* argv[])
{
    char buf;
    int fd, i, poll_num;
    int *wd;
    nfds_t nfds;
    struct pollfd fds[2];

    if (argc < 2) {
        printf("Usage: %s PATH [PATH ...]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    printf("Press ENTER key to terminate.\n");

    /* Create the file descriptor for accessing the inotify API.  */

    fd = inotify_init1(IN_NONBLOCK);
    if (fd == -1) {
        perror("inotify_init1");
        exit(EXIT_FAILURE);
    }

    /* Allocate memory for watch descriptors.  */

    wd = calloc(argc, sizeof(int));
    if (wd == NULL) {
        perror("calloc");
        exit(EXIT_FAILURE);
    }

    /* Mark directories for events
       - file was opened
       - file was closed */

    for (i = 1; i < argc; i++) {
        wd[i] = inotify_add_watch(fd, argv[i],
                                  IN_OPEN | IN_CLOSE);
        if (wd[i] == -1) {
            fprintf(stderr, "Cannot watch '%s': %s\n",
                    argv[i], strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    /* Prepare for polling.  */

    nfds = 2;

    fds[0].fd = STDIN_FILENO;       /* Console input */
    fds[0].events = POLLIN;

    fds[1].fd = fd;                 /* Inotify input */
    fds[1].events = POLLIN;

    /* Wait for events and/or terminal input.  */

    printf("Listening for events.\n");
    for (;;) {
        poll_num = poll(fds, nfds, -1);
        if (poll_num == -1) {
            if (errno == EINTR)
                continue;
            perror("poll");
            exit(EXIT_FAILURE);
        }

        if (poll_num > 0) {

            if (fds[0].revents & POLLIN) {

                /* Console input is available.  Empty stdin and quit.  */

                while (read(STDIN_FILENO, &buf, 1) > 0 && buf != '\n')
                    continue;
                break;
            }

            if (fds[1].revents & POLLIN) {

                /* Inotify events are available.  */

                handle_events(fd, wd, argc, argv);
            }
        }
    }

    printf("Listening for events stopped.\n");

    /* Close inotify file descriptor.  */

    close(fd);

    free(wd);
    exit(EXIT_SUCCESS);
}
 

関連項目

inotifywait(1), inotifywatch(1), inotify_add_watch(2), inotify_init(2), inotify_init1(2), inotify_rm_watch(2), read(2), stat(2), fanotify(7)

Linux カーネルソース内の Documentation/filesystems/inotify.rxt


 

Index

名前
説明
inotify ファイルディスクリプターからのイベントの読み出し
inotify イベント
例
/proc インターフェース
標準規格
歴史
注意
制限と警告
rename() イベントの取り扱い
バグ
例
出力例
プログラムソース
関連項目

This document was created by man2html, using the manual pages.