xref: /freebsd/lib/libc/gen/inotify.c (revision 245ff4c4b2c2728674121a5d9736a5e079bd00b2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2025 Klara, Inc.
5  */
6 
7 #include "namespace.h"
8 #include <sys/fcntl.h>
9 #include <sys/inotify.h>
10 #include <sys/specialfd.h>
11 #include "un-namespace.h"
12 #include "libc_private.h"
13 
14 /*
15  * Provide compatibility with libinotify, which uses different values for these
16  * flags.
17  */
18 #define	IN_NONBLOCK_OLD	0x80000
19 #define	IN_CLOEXEC_OLD	0x00800
20 
21 int
inotify_add_watch(int fd,const char * pathname,uint32_t mask)22 inotify_add_watch(int fd, const char *pathname, uint32_t mask)
23 {
24 	return (inotify_add_watch_at(fd, AT_FDCWD, pathname, mask));
25 }
26 
27 int
inotify_init1(int flags)28 inotify_init1(int flags)
29 {
30 	struct specialfd_inotify args;
31 
32 	if ((flags & IN_NONBLOCK_OLD) != 0) {
33 		flags &= ~IN_NONBLOCK_OLD;
34 		flags |= IN_NONBLOCK;
35 	}
36 	if ((flags & IN_CLOEXEC_OLD) != 0) {
37 		flags &= ~IN_CLOEXEC_OLD;
38 		flags |= IN_CLOEXEC;
39 	}
40 	args.flags = flags;
41 	return (__sys___specialfd(SPECIALFD_INOTIFY, &args, sizeof(args)));
42 }
43 
44 int
inotify_init(void)45 inotify_init(void)
46 {
47 	return (inotify_init1(0));
48 }
49