[libc-commits] [libc] [libc] add sys/inotify (PR #230314)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Thu Oct 8 16:02:44 PDT 2026
https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/230314
The sys/inotify header has linux functions for watching a directory.
This PR adds those functions, and their relevant macros and type. Tests
are fairly simple since the functions are all syscall wrappers.
Assisted-by: Automated tooling, human reviewed
>From ca1bd7520b636d1d2c61c2f3412d2930110e87c4 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Thu, 8 Oct 2026 23:00:07 +0000
Subject: [PATCH] [libc] add sys/inotify
The sys/inotify header has linux functions for watching a directory.
This PR adds those functions, and their relevant macros and type. Tests
are fairly simple since the functions are all syscall wrappers.
Assisted-by: Automated tooling, human reviewed
---
libc/config/linux/aarch64/entrypoints.txt | 6 ++
libc/config/linux/aarch64/headers.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 6 ++
libc/config/linux/arm/headers.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 6 ++
libc/config/linux/riscv/headers.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 6 ++
libc/config/linux/x86_64/headers.txt | 1 +
libc/hdr/CMakeLists.txt | 8 ++
libc/hdr/sys_inotify_macros.h | 27 ++++++
libc/include/CMakeLists.txt | 11 +++
libc/include/llvm-libc-macros/CMakeLists.txt | 8 ++
.../llvm-libc-macros/linux/CMakeLists.txt | 6 ++
.../linux/sys-inotify-macros.h | 54 ++++++++++++
.../llvm-libc-macros/sys-inotify-macros.h | 21 +++++
libc/include/llvm-libc-types/CMakeLists.txt | 1 +
.../llvm-libc-types/struct_inotify_event.h | 27 ++++++
libc/include/sys/inotify.yaml | 82 +++++++++++++++++++
.../linux/syscall_wrappers/CMakeLists.txt | 49 +++++++++++
.../syscall_wrappers/inotify_add_watch.h | 35 ++++++++
.../linux/syscall_wrappers/inotify_init.h | 40 +++++++++
.../linux/syscall_wrappers/inotify_init1.h | 33 ++++++++
.../linux/syscall_wrappers/inotify_rm_watch.h | 33 ++++++++
libc/src/sys/CMakeLists.txt | 1 +
libc/src/sys/inotify/CMakeLists.txt | 31 +++++++
libc/src/sys/inotify/inotify_add_watch.h | 26 ++++++
libc/src/sys/inotify/inotify_init.h | 25 ++++++
libc/src/sys/inotify/inotify_init1.h | 25 ++++++
libc/src/sys/inotify/inotify_rm_watch.h | 25 ++++++
libc/src/sys/inotify/linux/CMakeLists.txt | 56 +++++++++++++
.../sys/inotify/linux/inotify_add_watch.cpp | 34 ++++++++
libc/src/sys/inotify/linux/inotify_init.cpp | 32 ++++++++
libc/src/sys/inotify/linux/inotify_init1.cpp | 32 ++++++++
.../sys/inotify/linux/inotify_rm_watch.cpp | 32 ++++++++
libc/test/src/sys/CMakeLists.txt | 1 +
libc/test/src/sys/inotify/CMakeLists.txt | 3 +
.../test/src/sys/inotify/linux/CMakeLists.txt | 73 +++++++++++++++++
.../inotify/linux/inotify_add_watch_test.cpp | 40 +++++++++
.../sys/inotify/linux/inotify_init1_test.cpp | 34 ++++++++
.../sys/inotify/linux/inotify_init_test.cpp | 50 +++++++++++
.../inotify/linux/inotify_rm_watch_test.cpp | 45 ++++++++++
41 files changed, 1028 insertions(+)
create mode 100644 libc/hdr/sys_inotify_macros.h
create mode 100644 libc/include/llvm-libc-macros/linux/sys-inotify-macros.h
create mode 100644 libc/include/llvm-libc-macros/sys-inotify-macros.h
create mode 100644 libc/include/llvm-libc-types/struct_inotify_event.h
create mode 100644 libc/include/sys/inotify.yaml
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_add_watch.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init1.h
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_rm_watch.h
create mode 100644 libc/src/sys/inotify/CMakeLists.txt
create mode 100644 libc/src/sys/inotify/inotify_add_watch.h
create mode 100644 libc/src/sys/inotify/inotify_init.h
create mode 100644 libc/src/sys/inotify/inotify_init1.h
create mode 100644 libc/src/sys/inotify/inotify_rm_watch.h
create mode 100644 libc/src/sys/inotify/linux/CMakeLists.txt
create mode 100644 libc/src/sys/inotify/linux/inotify_add_watch.cpp
create mode 100644 libc/src/sys/inotify/linux/inotify_init.cpp
create mode 100644 libc/src/sys/inotify/linux/inotify_init1.cpp
create mode 100644 libc/src/sys/inotify/linux/inotify_rm_watch.cpp
create mode 100644 libc/test/src/sys/inotify/CMakeLists.txt
create mode 100644 libc/test/src/sys/inotify/linux/CMakeLists.txt
create mode 100644 libc/test/src/sys/inotify/linux/inotify_add_watch_test.cpp
create mode 100644 libc/test/src/sys/inotify/linux/inotify_init1_test.cpp
create mode 100644 libc/test/src/sys/inotify/linux/inotify_init_test.cpp
create mode 100644 libc/test/src/sys/inotify/linux/inotify_rm_watch_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b8ea3258262c83..5bfad95231133e 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -427,6 +427,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.sysmacros.major
libc.src.sys.sysmacros.minor
+ # sys/inotify.h entrypoints
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.inotify.inotify_init1
+ libc.src.sys.inotify.inotify_rm_watch
+
# termios.h entrypoints
libc.src.termios.cfgetispeed
libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 4317f3e7bd18ea..5a2083c8f39d39 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -52,6 +52,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_epoll
libc.include.sys_fcntl
libc.include.sys_file
+ libc.include.sys_inotify
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_mount
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index f7714b2049a27f..ac66f1f5184842 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -213,6 +213,12 @@ set(TARGET_LIBC_ENTRYPOINTS
# stdio.h entrypoints
libc.src.stdio.tmpfile
+ # sys/inotify.h entrypoints
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.inotify.inotify_init1
+ libc.src.sys.inotify.inotify_rm_watch
+
# sys/ioctl.h entrypoints
libc.src.sys.ioctl.ioctl
diff --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt
index af0631843a157f..a46840383142ae 100644
--- a/libc/config/linux/arm/headers.txt
+++ b/libc/config/linux/arm/headers.txt
@@ -27,6 +27,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.unistd
libc.include.wchar
libc.include.wctype
+ libc.include.sys_inotify
libc.include.sys_param
libc.include.sys_personality
libc.include.sys_sysinfo
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index af7586e61e098f..e5c886d53779dc 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -447,6 +447,12 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/auxv.h entrypoints
libc.src.sys.auxv.getauxval
+ # sys/inotify.h entrypoints
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.inotify.inotify_init1
+ libc.src.sys.inotify.inotify_rm_watch
+
# termios.h entrypoints
libc.src.termios.cfgetispeed
libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/riscv/headers.txt b/libc/config/linux/riscv/headers.txt
index a0c7330ebe19f5..19d13a8574bc41 100644
--- a/libc/config/linux/riscv/headers.txt
+++ b/libc/config/linux/riscv/headers.txt
@@ -53,6 +53,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_epoll
libc.include.sys_fcntl
libc.include.sys_file
+ libc.include.sys_inotify
libc.include.sys_ioctl
libc.include.sys_mman
libc.include.sys_param
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b386e049255b56..aec72d78fb5354 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -467,6 +467,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.sys.sysmacros.major
libc.src.sys.sysmacros.minor
+ # sys/inotify.h entrypoints
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.inotify.inotify_init1
+ libc.src.sys.inotify.inotify_rm_watch
+
# termios.h entrypoints
libc.src.termios.cfgetispeed
libc.src.termios.cfgetospeed
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index ea327ad0386dae..426972553b93b2 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -53,6 +53,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.sys_epoll
libc.include.sys_fcntl
libc.include.sys_file
+ libc.include.sys_inotify
libc.include.sys_ioctl
libc.include.sys_ipc
libc.include.sys_mman
diff --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 63077607bb4fbc..cf8b1f36d349ec 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -205,6 +205,14 @@ add_proxy_header_library(
libc.include.llvm-libc-macros.sys_file_macros
)
+add_proxy_header_library(
+ sys_inotify_macros
+ HDRS
+ sys_inotify_macros.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-macros.sys_inotify_macros
+)
+
add_proxy_header_library(
sys_ioctl_macros
HDRS
diff --git a/libc/hdr/sys_inotify_macros.h b/libc/hdr/sys_inotify_macros.h
new file mode 100644
index 00000000000000..997a21bde9e89c
--- /dev/null
+++ b/libc/hdr/sys_inotify_macros.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy header for macro values defined in sys/inotify.h.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_SYS_INOTIFY_MACROS_H
+#define LLVM_LIBC_HDR_SYS_INOTIFY_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-inotify-macros.h"
+
+#else // Overlay mode
+
+#include <sys/inotify.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_INOTIFY_MACROS_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 2fe1f92afc1d19..7b8dee2b4aa77e 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -747,6 +747,17 @@ add_header_macro(
.llvm-libc-types.struct_winsize
)
+add_header_macro(
+ sys_inotify
+ ../libc/include/sys/inotify.yaml
+ sys/inotify.h
+ DEPENDS
+ .llvm_libc_common_h
+ .llvm-libc-macros.sys_inotify_macros
+ .llvm-libc-types.struct_inotify_event
+ .stdint
+)
+
add_header_macro(
sys_ipc
../libc/include/sys/ipc.yaml
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 309d1c78da42eb..dccbba272022f7 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -292,6 +292,14 @@ add_macro_header(
sys-ioctl-macros.h
)
+add_macro_header(
+ sys_inotify_macros
+ HDR
+ sys-inotify-macros.h
+ DEPENDS
+ .fcntl_macros
+)
+
add_macro_header(
sys_ipc_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/CMakeLists.txt b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
index a634b0a9eb9e98..2b3fa2c027fa85 100644
--- a/libc/include/llvm-libc-macros/linux/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/linux/CMakeLists.txt
@@ -46,6 +46,12 @@ add_header(
sys-ioctl-macros.h
)
+add_header(
+ sys_inotify_macros
+ HDR
+ sys-inotify-macros.h
+)
+
add_header(
sys_ipc_macros
HDR
diff --git a/libc/include/llvm-libc-macros/linux/sys-inotify-macros.h b/libc/include/llvm-libc-macros/linux/sys-inotify-macros.h
new file mode 100644
index 00000000000000..03679938d0d4df
--- /dev/null
+++ b/libc/include/llvm-libc-macros/linux/sys-inotify-macros.h
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros defined in sys/inotify.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_LINUX_SYS_INOTIFY_MACROS_H
+#define LLVM_LIBC_MACROS_LINUX_SYS_INOTIFY_MACROS_H
+
+#include "fcntl-macros.h"
+
+#define IN_CLOEXEC O_CLOEXEC
+#define IN_NONBLOCK O_NONBLOCK
+
+#define IN_ACCESS 0x00000001
+#define IN_MODIFY 0x00000002
+#define IN_ATTRIB 0x00000004
+#define IN_CLOSE_WRITE 0x00000008
+#define IN_CLOSE_NOWRITE 0x00000010
+#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
+#define IN_OPEN 0x00000020
+#define IN_MOVED_FROM 0x00000040
+#define IN_MOVED_TO 0x00000080
+#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO)
+#define IN_CREATE 0x00000100
+#define IN_DELETE 0x00000200
+#define IN_DELETE_SELF 0x00000400
+#define IN_MOVE_SELF 0x00000800
+
+#define IN_UNMOUNT 0x00002000
+#define IN_Q_OVERFLOW 0x00004000
+#define IN_IGNORED 0x00008000
+
+#define IN_ONLYDIR 0x01000000
+#define IN_DONT_FOLLOW 0x02000000
+#define IN_EXCL_UNLINK 0x04000000
+#define IN_MASK_CREATE 0x10000000
+#define IN_MASK_ADD 0x20000000
+#define IN_ISDIR 0x40000000
+#define IN_ONESHOT 0x80000000
+
+#define IN_ALL_EVENTS \
+ (IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | IN_CLOSE_NOWRITE | \
+ IN_OPEN | IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE | IN_DELETE | \
+ IN_DELETE_SELF | IN_MOVE_SELF)
+
+#endif // LLVM_LIBC_MACROS_LINUX_SYS_INOTIFY_MACROS_H
diff --git a/libc/include/llvm-libc-macros/sys-inotify-macros.h b/libc/include/llvm-libc-macros/sys-inotify-macros.h
new file mode 100644
index 00000000000000..b77e9842ad14f5
--- /dev/null
+++ b/libc/include/llvm-libc-macros/sys-inotify-macros.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Macros defined in sys/inotify.h header file.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_MACROS_SYS_INOTIFY_MACROS_H
+#define LLVM_LIBC_MACROS_SYS_INOTIFY_MACROS_H
+
+#ifdef __linux__
+#include "linux/sys-inotify-macros.h"
+#endif
+
+#endif // LLVM_LIBC_MACROS_SYS_INOTIFY_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index ea710ef48857cf..816996332f1b65 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -315,6 +315,7 @@ add_header(regoff_t HDR regoff_t.h)
add_header(regmatch_t HDR regmatch_t.h DEPENDS .regoff_t)
add_header(struct_epoll_event HDR struct_epoll_event.h)
add_header(struct_epoll_data HDR struct_epoll_data.h)
+add_header(struct_inotify_event HDR struct_inotify_event.h DEPENDS libc.include.llvm-libc-macros.stdint_macros)
add_header(
float128
HDR
diff --git a/libc/include/llvm-libc-types/struct_inotify_event.h b/libc/include/llvm-libc-types/struct_inotify_event.h
new file mode 100644
index 00000000000000..08c505b7954b3c
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_inotify_event.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Definition of struct inotify_event.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_INOTIFY_EVENT_H
+#define LLVM_LIBC_TYPES_STRUCT_INOTIFY_EVENT_H
+
+#include "../llvm-libc-macros/stdint-macros.h"
+
+struct inotify_event {
+ int wd;
+ uint32_t mask;
+ uint32_t cookie;
+ uint32_t len;
+ char name[];
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_INOTIFY_EVENT_H
diff --git a/libc/include/sys/inotify.yaml b/libc/include/sys/inotify.yaml
new file mode 100644
index 00000000000000..d102f19a5efb33
--- /dev/null
+++ b/libc/include/sys/inotify.yaml
@@ -0,0 +1,82 @@
+header: sys/inotify.h
+standards:
+ - linux
+macros:
+ - macro_name: IN_ACCESS
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MODIFY
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_ATTRIB
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_CLOSE_WRITE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_CLOSE_NOWRITE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_CLOSE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_OPEN
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MOVED_FROM
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MOVED_TO
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MOVE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_CREATE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_DELETE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_DELETE_SELF
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MOVE_SELF
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_UNMOUNT
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_Q_OVERFLOW
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_IGNORED
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_ONLYDIR
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_DONT_FOLLOW
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_EXCL_UNLINK
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MASK_CREATE
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_MASK_ADD
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_ISDIR
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_ONESHOT
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_ALL_EVENTS
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_CLOEXEC
+ macro_header: sys-inotify-macros.h
+ - macro_name: IN_NONBLOCK
+ macro_header: sys-inotify-macros.h
+types:
+ - type_name: struct_inotify_event
+enums: []
+objects: []
+functions:
+ - name: inotify_init
+ return_type: int
+ arguments:
+ - type: void
+ - name: inotify_init1
+ return_type: int
+ arguments:
+ - type: int
+ - name: inotify_add_watch
+ return_type: int
+ arguments:
+ - type: int
+ - type: const char *
+ - type: uint32_t
+ - name: inotify_rm_watch
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 1adad38b86e100..47ddb551680199 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1558,3 +1558,52 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ inotify_init
+ HDRS
+ inotify_init.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ inotify_init1
+ HDRS
+ inotify_init1.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ inotify_add_watch
+ HDRS
+ inotify_add_watch.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
+add_header_library(
+ inotify_rm_watch
+ HDRS
+ inotify_rm_watch.h
+ DEPENDS
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_add_watch.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_add_watch.h
new file mode 100644
index 00000000000000..dc0b65f1a17d1b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_add_watch.h
@@ -0,0 +1,35 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for inotify_add_watch.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_ADD_WATCH_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_ADD_WATCH_H
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> inotify_add_watch(int fd, const char *pathname,
+ uint32_t mask) {
+ return syscall_checked<int>(SYS_inotify_add_watch, fd, pathname, mask);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_ADD_WATCH_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init.h
new file mode 100644
index 00000000000000..ebb2c924862f65
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for inotify_init.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> inotify_init(void) {
+#ifdef SYS_inotify_init
+ return syscall_checked<int>(SYS_inotify_init);
+#elif defined(SYS_inotify_init1)
+ return syscall_checked<int>(SYS_inotify_init1, 0);
+#else
+#error \
+ "inotify_init and inotify_init1 are unavailable. Unable to build inotify_init."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init1.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init1.h
new file mode 100644
index 00000000000000..68a2dc5fb492d4
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_init1.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for inotify_init1.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT1_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT1_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> inotify_init1(int flags) {
+ return syscall_checked<int>(SYS_inotify_init1, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_INIT1_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_rm_watch.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_rm_watch.h
new file mode 100644
index 00000000000000..c10746cf7e8c8b
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/inotify_rm_watch.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for inotify_rm_watch.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_RM_WATCH_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_RM_WATCH_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> inotify_rm_watch(int fd, int wd) {
+ return syscall_checked<int>(SYS_inotify_rm_watch, fd, wd);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_INOTIFY_RM_WATCH_H
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index 3ac5b92a2a7c11..4603b59d13a9a0 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -1,6 +1,7 @@
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(file)
+add_subdirectory(inotify)
add_subdirectory(ipc)
add_subdirectory(mman)
add_subdirectory(personality)
diff --git a/libc/src/sys/inotify/CMakeLists.txt b/libc/src/sys/inotify/CMakeLists.txt
new file mode 100644
index 00000000000000..9e5eecef21ce2b
--- /dev/null
+++ b/libc/src/sys/inotify/CMakeLists.txt
@@ -0,0 +1,31 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+ inotify_init
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.inotify_init
+)
+
+add_entrypoint_object(
+ inotify_init1
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.inotify_init1
+)
+
+add_entrypoint_object(
+ inotify_add_watch
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.inotify_add_watch
+)
+
+add_entrypoint_object(
+ inotify_rm_watch
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.inotify_rm_watch
+)
diff --git a/libc/src/sys/inotify/inotify_add_watch.h b/libc/src/sys/inotify/inotify_add_watch.h
new file mode 100644
index 00000000000000..f5d9903ed80df5
--- /dev/null
+++ b/libc/src/sys/inotify/inotify_add_watch.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for inotify_add_watch function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_ADD_WATCH_H
+#define LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_ADD_WATCH_H
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int inotify_add_watch(int fd, const char *pathname, uint32_t mask);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_ADD_WATCH_H
diff --git a/libc/src/sys/inotify/inotify_init.h b/libc/src/sys/inotify/inotify_init.h
new file mode 100644
index 00000000000000..83280d49719f6e
--- /dev/null
+++ b/libc/src/sys/inotify/inotify_init.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for inotify_init function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT_H
+#define LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int inotify_init(void);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT_H
diff --git a/libc/src/sys/inotify/inotify_init1.h b/libc/src/sys/inotify/inotify_init1.h
new file mode 100644
index 00000000000000..2be32dea527523
--- /dev/null
+++ b/libc/src/sys/inotify/inotify_init1.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for inotify_init1 function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT1_H
+#define LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT1_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int inotify_init1(int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_INIT1_H
diff --git a/libc/src/sys/inotify/inotify_rm_watch.h b/libc/src/sys/inotify/inotify_rm_watch.h
new file mode 100644
index 00000000000000..2886acd970c506
--- /dev/null
+++ b/libc/src/sys/inotify/inotify_rm_watch.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for inotify_rm_watch function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_RM_WATCH_H
+#define LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_RM_WATCH_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int inotify_rm_watch(int fd, int wd);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_INOTIFY_INOTIFY_RM_WATCH_H
diff --git a/libc/src/sys/inotify/linux/CMakeLists.txt b/libc/src/sys/inotify/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..b0c977eeca96e0
--- /dev/null
+++ b/libc/src/sys/inotify/linux/CMakeLists.txt
@@ -0,0 +1,56 @@
+add_entrypoint_object(
+ inotify_init
+ SRCS
+ inotify_init.cpp
+ HDRS
+ ../inotify_init.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.inotify_init
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ inotify_init1
+ SRCS
+ inotify_init1.cpp
+ HDRS
+ ../inotify_init1.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.inotify_init1
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ inotify_add_watch
+ SRCS
+ inotify_add_watch.cpp
+ HDRS
+ ../inotify_add_watch.h
+ DEPENDS
+ libc.hdr.stdint_proxy
+ libc.src.__support.OSUtil.linux.syscall_wrappers.inotify_add_watch
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
+add_entrypoint_object(
+ inotify_rm_watch
+ SRCS
+ inotify_rm_watch.cpp
+ HDRS
+ ../inotify_rm_watch.h
+ DEPENDS
+ libc.src.__support.OSUtil.linux.syscall_wrappers.inotify_rm_watch
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
diff --git a/libc/src/sys/inotify/linux/inotify_add_watch.cpp b/libc/src/sys/inotify/linux/inotify_add_watch.cpp
new file mode 100644
index 00000000000000..51e6ad89a51a4e
--- /dev/null
+++ b/libc/src/sys/inotify/linux/inotify_add_watch.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of the inotify_add_watch function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/inotify/inotify_add_watch.h"
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/inotify_add_watch.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inotify_add_watch,
+ (int fd, const char *pathname, uint32_t mask)) {
+ ErrorOr<int> ret = linux_syscalls::inotify_add_watch(fd, pathname, mask);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/inotify/linux/inotify_init.cpp b/libc/src/sys/inotify/linux/inotify_init.cpp
new file mode 100644
index 00000000000000..b494e17509ff01
--- /dev/null
+++ b/libc/src/sys/inotify/linux/inotify_init.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of the inotify_init function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/inotify/inotify_init.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/inotify_init.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inotify_init, (void)) {
+ ErrorOr<int> ret = linux_syscalls::inotify_init();
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/inotify/linux/inotify_init1.cpp b/libc/src/sys/inotify/linux/inotify_init1.cpp
new file mode 100644
index 00000000000000..d529532bb77965
--- /dev/null
+++ b/libc/src/sys/inotify/linux/inotify_init1.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of the inotify_init1 function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/inotify/inotify_init1.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/inotify_init1.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inotify_init1, (int flags)) {
+ ErrorOr<int> ret = linux_syscalls::inotify_init1(flags);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/inotify/linux/inotify_rm_watch.cpp b/libc/src/sys/inotify/linux/inotify_rm_watch.cpp
new file mode 100644
index 00000000000000..f20e2652ceeb01
--- /dev/null
+++ b/libc/src/sys/inotify/linux/inotify_rm_watch.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of the inotify_rm_watch function.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/inotify/inotify_rm_watch.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/inotify_rm_watch.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, inotify_rm_watch, (int fd, int wd)) {
+ ErrorOr<int> ret = linux_syscalls::inotify_rm_watch(fd, wd);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 1e2905b07eb85c..b7a8a409832fac 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -14,6 +14,7 @@ add_subdirectory(prctl)
add_subdirectory(auxv)
add_subdirectory(epoll)
add_subdirectory(file)
+add_subdirectory(inotify)
add_subdirectory(ipc)
add_subdirectory(uio)
add_subdirectory(time)
diff --git a/libc/test/src/sys/inotify/CMakeLists.txt b/libc/test/src/sys/inotify/CMakeLists.txt
new file mode 100644
index 00000000000000..b4bbe81c92ff2e
--- /dev/null
+++ b/libc/test/src/sys/inotify/CMakeLists.txt
@@ -0,0 +1,3 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+ add_subdirectory(${LIBC_TARGET_OS})
+endif()
diff --git a/libc/test/src/sys/inotify/linux/CMakeLists.txt b/libc/test/src/sys/inotify/linux/CMakeLists.txt
new file mode 100644
index 00000000000000..2d21578c4e2400
--- /dev/null
+++ b/libc/test/src/sys/inotify/linux/CMakeLists.txt
@@ -0,0 +1,73 @@
+add_custom_target(libc_sys_inotify_unittests)
+
+add_libc_test(
+ inotify_init_test
+ SUITE
+ libc_sys_inotify_unittests
+ SRCS
+ inotify_init_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.sys_resource_macros
+ libc.hdr.types.struct_rlimit
+ libc.src.__support.CPP.scope
+ libc.src.errno.errno
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.resource.getrlimit
+ libc.src.sys.resource.setrlimit
+ libc.src.unistd.close
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+ inotify_init1_test
+ SUITE
+ libc_sys_inotify_unittests
+ SRCS
+ inotify_init1_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.sys_inotify_macros
+ libc.src.errno.errno
+ libc.src.sys.inotify.inotify_init1
+ libc.src.unistd.close
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+ inotify_add_watch_test
+ SUITE
+ libc_sys_inotify_unittests
+ SRCS
+ inotify_add_watch_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.sys_inotify_macros
+ libc.src.errno.errno
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.unistd.close
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+ inotify_rm_watch_test
+ SUITE
+ libc_sys_inotify_unittests
+ SRCS
+ inotify_rm_watch_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.sys_inotify_macros
+ libc.src.__support.CPP.scope
+ libc.src.errno.errno
+ libc.src.sys.inotify.inotify_add_watch
+ libc.src.sys.inotify.inotify_init
+ libc.src.sys.inotify.inotify_rm_watch
+ libc.src.unistd.close
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/inotify/linux/inotify_add_watch_test.cpp b/libc/test/src/sys/inotify/linux/inotify_add_watch_test.cpp
new file mode 100644
index 00000000000000..a4193367b6f5f6
--- /dev/null
+++ b/libc/test/src/sys/inotify/linux/inotify_add_watch_test.cpp
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for inotify_add_watch.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_inotify_macros.h"
+#include "src/sys/inotify/inotify_add_watch.h"
+#include "src/sys/inotify/inotify_init.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcInotifyAddWatchTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInotifyAddWatchTest, Basic) {
+ auto test_dir = libc_make_test_file_path(".");
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::inotify_init(),
+ returns(GT(0)).with_errno(EQ(0)));
+ EXPECT_THAT(LIBC_NAMESPACE::inotify_add_watch(fd, test_dir, IN_ALL_EVENTS),
+ returns(GT(0)).with_errno(EQ(0)));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST_F(LlvmLibcInotifyAddWatchTest, Fail) {
+ auto test_dir = libc_make_test_file_path(".");
+ ASSERT_THAT(LIBC_NAMESPACE::inotify_add_watch(-1, test_dir, IN_ALL_EVENTS),
+ Fails(EBADF));
+}
diff --git a/libc/test/src/sys/inotify/linux/inotify_init1_test.cpp b/libc/test/src/sys/inotify/linux/inotify_init1_test.cpp
new file mode 100644
index 00000000000000..38ec64c2a2c603
--- /dev/null
+++ b/libc/test/src/sys/inotify/linux/inotify_init1_test.cpp
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for inotify_init1.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_inotify_macros.h"
+#include "src/sys/inotify/inotify_init1.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcInotifyInit1Test = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInotifyInit1Test, Basic) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::inotify_init1(IN_CLOEXEC),
+ returns(GT(0)).with_errno(EQ(0)));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST_F(LlvmLibcInotifyInit1Test, Fail) {
+ ASSERT_THAT(LIBC_NAMESPACE::inotify_init1(-1), Fails(EINVAL));
+}
diff --git a/libc/test/src/sys/inotify/linux/inotify_init_test.cpp b/libc/test/src/sys/inotify/linux/inotify_init_test.cpp
new file mode 100644
index 00000000000000..de415fe10593d6
--- /dev/null
+++ b/libc/test/src/sys/inotify/linux/inotify_init_test.cpp
@@ -0,0 +1,50 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for inotify_init.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/struct_rlimit.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/inotify/inotify_init.h"
+#include "src/sys/resource/getrlimit.h"
+#include "src/sys/resource/setrlimit.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcInotifyInitTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInotifyInitTest, Basic) {
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::inotify_init(),
+ returns(GT(0)).with_errno(EQ(0)));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+}
+
+TEST_F(LlvmLibcInotifyInitTest, Fail) {
+ struct rlimit orig_limits;
+ ASSERT_THAT(LIBC_NAMESPACE::getrlimit(RLIMIT_NOFILE, &orig_limits),
+ Succeeds(0));
+ LIBC_NAMESPACE::cpp::scope_exit restore_limits([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::setrlimit(RLIMIT_NOFILE, &orig_limits),
+ Succeeds(0));
+ });
+
+ struct rlimit zero_limits{0, orig_limits.rlim_max};
+ ASSERT_THAT(LIBC_NAMESPACE::setrlimit(RLIMIT_NOFILE, &zero_limits),
+ Succeeds(0));
+
+ ASSERT_THAT(LIBC_NAMESPACE::inotify_init(), Fails(EMFILE));
+}
diff --git a/libc/test/src/sys/inotify/linux/inotify_rm_watch_test.cpp b/libc/test/src/sys/inotify/linux/inotify_rm_watch_test.cpp
new file mode 100644
index 00000000000000..bb2f7bb60a6e20
--- /dev/null
+++ b/libc/test/src/sys/inotify/linux/inotify_rm_watch_test.cpp
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for inotify_rm_watch.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_inotify_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/inotify/inotify_add_watch.h"
+#include "src/sys/inotify/inotify_init.h"
+#include "src/sys/inotify/inotify_rm_watch.h"
+#include "src/unistd/close.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcInotifyRmWatchTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcInotifyRmWatchTest, Basic) {
+ auto test_dir = libc_make_test_file_path(".");
+ int fd;
+ ASSERT_THAT(fd = LIBC_NAMESPACE::inotify_init(),
+ returns(GT(0)).with_errno(EQ(0)));
+ LIBC_NAMESPACE::cpp::scope_exit close_fd(
+ [&] { EXPECT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0)); });
+
+ int wd;
+ ASSERT_THAT(
+ wd = LIBC_NAMESPACE::inotify_add_watch(fd, test_dir, IN_ALL_EVENTS),
+ returns(GT(0)).with_errno(EQ(0)));
+ ASSERT_THAT(LIBC_NAMESPACE::inotify_rm_watch(fd, wd), Succeeds(0));
+}
+
+TEST_F(LlvmLibcInotifyRmWatchTest, Fail) {
+ ASSERT_THAT(LIBC_NAMESPACE::inotify_rm_watch(-1, 0), Fails(EBADF));
+}
More information about the libc-commits
mailing list