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 #if (defined (PLATFORM_WIN32) && WINVER >= 0x0400)
22 * We use the managed watcher on windows, so the code inside this #if is never used
25 ves_icall_System_IO_FSW_SupportsFSW (void)
31 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
32 MonoString **filename,
41 static int (*FAMNextEvent) (gpointer, gpointer);
44 ves_icall_System_IO_FSW_SupportsFSW (void)
51 int lib_used = 4; /* gamin */
56 inotify_instance = ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ();
57 if (inotify_instance != -1) {
58 close (inotify_instance);
59 return 5; /* inotify */
62 filename = g_module_build_path (NULL, "libgamin-1.so.0");
63 fam_module = g_module_open (filename, G_MODULE_BIND_LAZY);
65 if (fam_module == NULL) {
66 lib_used = 2; /* FAM */
67 filename = g_module_build_path (NULL, "libfam.so.0");
68 fam_module = g_module_open (filename, G_MODULE_BIND_LAZY);
72 if (fam_module == NULL)
75 g_module_symbol (fam_module, "FAMNextEvent", (gpointer *) &FAMNextEvent);
76 if (FAMNextEvent == NULL)
83 /* Almost copied from fam.h. Weird, I know */
88 typedef struct FAMEvent {
92 gchar filename [PATH_MAX];
98 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
99 MonoString **filename,
107 if (FAMNextEvent (conn, &ev) == 1) {
108 *filename = mono_string_new (mono_domain_get (), ev.filename);
110 *reqnum = ev.fr.reqnum;
118 #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) && !defined(__NR_inotify_init)
119 # if defined(__i386__)
120 # define __NR_inotify_init 291
121 # elif defined(__x86_64__)
122 # define __NR_inotify_init 253
123 # elif defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
124 # define __NR_inotify_init 275
125 # elif defined (__s390__) || defined (__s390x__)
126 # define __NR_inotify_init 284
127 # elif defined(__sparc__) || defined (__sparc64__)
128 # define __NR_inotify_init 151
129 # elif defined (__ia64__)
130 # define __NR_inotify_init 1277
131 # elif defined (__arm__)
132 # define __NR_inotify_init 316
133 # elif defined(__alpha__)
134 # define __NR_inotify_init 444
136 #ifdef __NR_inotify_init
137 # ifndef __NR_inotify_add_watch
138 # define __NR_inotify_add_watch (__NR_inotify_init + 1)
140 # ifndef __NR_inotify_rm_watch
141 # define __NR_inotify_rm_watch (__NR_inotify_init + 2)
146 #if !defined(__linux__) || !defined(__NR_inotify_init)
147 int ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
152 int ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *directory, gint32 mask)
157 int ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, int watch_descriptor)
165 ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
167 return syscall (__NR_inotify_init);
171 ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *name, gint32 mask)
181 str = mono_string_to_utf8 (name);
182 retval = syscall (__NR_inotify_add_watch, fd, str, mask);
186 errno = ERROR_ACCESS_DENIED;
189 errno = ERROR_INVALID_HANDLE;
192 errno = ERROR_INVALID_ACCESS;
195 errno = ERROR_INVALID_DATA;
198 errno = ERROR_NOT_ENOUGH_MEMORY;
201 errno = ERROR_TOO_MANY_OPEN_FILES;
204 errno = ERROR_GEN_FAILURE;
207 mono_marshal_set_last_error ();
214 ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
216 return syscall (__NR_inotify_rm_watch, fd, watch_descriptor);