2 * filewatcher.c: File System Watcher internal calls
5 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 * (C) 2004,2005,2006 Novell, Inc. (http://www.novell.com)
14 #include <mono/metadata/appdomain.h>
15 #include <mono/metadata/exception.h>
16 #include <mono/metadata/filewatcher.h>
17 #include <mono/metadata/marshal.h>
18 #include <mono/utils/mono-dl.h>
19 #if (defined (PLATFORM_WIN32) && WINVER >= 0x0400)
23 * We use the managed watcher on windows, so the code inside this #if is never used
26 ves_icall_System_IO_FSW_SupportsFSW (void)
32 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
33 MonoString **filename,
42 static int (*FAMNextEvent) (gpointer, gpointer);
45 ves_icall_System_IO_FSW_SupportsFSW (void)
52 int lib_used = 4; /* gamin */
59 inotify_instance = ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ();
60 if (inotify_instance != -1) {
61 close (inotify_instance);
62 return 5; /* inotify */
66 fam_module = mono_dl_open ("libgamin-1.so", MONO_DL_LAZY, NULL);
68 if (fam_module == NULL) {
69 lib_used = 2; /* FAM */
71 fam_module = mono_dl_open ("libfam.so", MONO_DL_LAZY, NULL);
75 if (fam_module == NULL)
78 err = mono_dl_symbol (fam_module, "FAMNextEvent", (gpointer *) &FAMNextEvent);
80 if (FAMNextEvent == NULL)
87 /* Almost copied from fam.h. Weird, I know */
92 typedef struct FAMEvent {
96 gchar filename [PATH_MAX];
102 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
103 MonoString **filename,
111 if (FAMNextEvent (conn, &ev) == 1) {
112 *filename = mono_string_new (mono_domain_get (), ev.filename);
114 *reqnum = ev.fr.reqnum;
122 #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) && !defined(__NR_inotify_init)
123 # if defined(__i386__)
124 # define __NR_inotify_init 291
125 # elif defined(__x86_64__)
126 # define __NR_inotify_init 253
127 # elif defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
128 # define __NR_inotify_init 275
129 # elif defined (__s390__) || defined (__s390x__)
130 # define __NR_inotify_init 284
131 # elif defined(__sparc__) || defined (__sparc64__)
132 # define __NR_inotify_init 151
133 # elif defined (__ia64__)
134 # define __NR_inotify_init 1277
135 # elif defined (__arm__)
136 # define __NR_inotify_init 316
137 # elif defined(__alpha__)
138 # define __NR_inotify_init 444
140 #ifdef __NR_inotify_init
141 # ifndef __NR_inotify_add_watch
142 # define __NR_inotify_add_watch (__NR_inotify_init + 1)
144 # ifndef __NR_inotify_rm_watch
145 # define __NR_inotify_rm_watch (__NR_inotify_init + 2)
150 #if !defined(__linux__) || !defined(__NR_inotify_init)
151 int ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
156 int ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *directory, gint32 mask)
161 int ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, int watch_descriptor)
169 ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
171 return syscall (__NR_inotify_init);
175 ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *name, gint32 mask)
185 str = mono_string_to_utf8 (name);
186 retval = syscall (__NR_inotify_add_watch, fd, str, mask);
190 errno = ERROR_ACCESS_DENIED;
193 errno = ERROR_INVALID_HANDLE;
196 errno = ERROR_INVALID_ACCESS;
199 errno = ERROR_INVALID_DATA;
202 errno = ERROR_NOT_ENOUGH_MEMORY;
205 errno = ERROR_TOO_MANY_OPEN_FILES;
208 errno = ERROR_GEN_FAILURE;
211 mono_marshal_set_last_error ();
218 ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
220 return syscall (__NR_inotify_rm_watch, fd, watch_descriptor);