2 * filewatcher.c: File System Watcher internal calls
5 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
14 #ifdef HAVE_SYS_TYPES_H
15 #include <sys/types.h>
17 #ifdef HAVE_SYS_EVENT_H
18 #include <sys/event.h>
20 #ifdef HAVE_SYS_TIME_H
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/exception.h>
26 #include <mono/metadata/filewatcher.h>
27 #include <mono/metadata/marshal.h>
28 #include <mono/utils/mono-dl.h>
29 #include <mono/utils/mono-io-portability.h>
34 * We use the managed watcher on windows, so the code inside this #if is never used
37 ves_icall_System_IO_FSW_SupportsFSW (void)
43 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
44 MonoString **filename,
53 static int (*FAMNextEvent) (gpointer, gpointer);
56 ves_icall_System_IO_FSW_SupportsFSW (void)
62 int lib_used = 4; /* gamin */
66 inotify_instance = ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ();
67 if (inotify_instance != -1) {
68 close (inotify_instance);
69 return 5; /* inotify */
72 fam_module = mono_dl_open ("libgamin-1.so", MONO_DL_LAZY, NULL);
73 if (fam_module == NULL) {
74 lib_used = 2; /* FAM */
75 fam_module = mono_dl_open ("libfam.so", MONO_DL_LAZY, NULL);
78 if (fam_module == NULL)
81 err = mono_dl_symbol (fam_module, "FAMNextEvent", (gpointer *) &FAMNextEvent);
83 if (FAMNextEvent == NULL)
90 /* Almost copied from fam.h. Weird, I know */
95 typedef struct FAMEvent {
99 gchar filename [PATH_MAX];
105 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
106 MonoString **filename,
112 if (FAMNextEvent (conn, &ev) == 1) {
113 *filename = mono_string_new (mono_domain_get (), ev.filename);
115 *reqnum = ev.fr.reqnum;
123 #ifndef HAVE_SYS_INOTIFY_H
124 int ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
129 int ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *directory, gint32 mask)
134 int ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
139 #include <sys/inotify.h>
143 ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
145 return inotify_init ();
149 ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *name, gint32 mask)
157 str = mono_string_to_utf8 (name);
158 path = mono_portability_find_file (str, TRUE);
162 retval = inotify_add_watch (fd, path, mask);
166 errno = ERROR_ACCESS_DENIED;
169 errno = ERROR_INVALID_HANDLE;
172 errno = ERROR_INVALID_ACCESS;
175 errno = ERROR_INVALID_DATA;
178 errno = ERROR_NOT_ENOUGH_MEMORY;
181 errno = ERROR_TOO_MANY_OPEN_FILES;
184 errno = ERROR_GEN_FAILURE;
187 mono_marshal_set_last_error ();
196 ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
198 return inotify_rm_watch (fd, watch_descriptor);
205 interrupt_kevent (gpointer data)
209 /* Interrupt the kevent () call by closing the fd */
211 /* Signal to managed code that the fd is closed */
216 * ves_icall_System_IO_KqueueMonitor_kevent_notimeout:
218 * Call kevent (), while handling runtime interruptions.
221 ves_icall_System_IO_KqueueMonitor_kevent_notimeout (int *kq_ptr, gpointer changelist, int nchanges, gpointer eventlist, int nevents)
224 gboolean interrupted;
226 mono_thread_info_install_interrupt (interrupt_kevent, kq_ptr, &interrupted);
233 MONO_PREPARE_BLOCKING;
234 res = kevent (*kq_ptr, changelist, nchanges, eventlist, nevents, NULL);
235 MONO_FINISH_BLOCKING;
237 mono_thread_info_uninstall_interrupt (&interrupted);
245 ves_icall_System_IO_KqueueMonitor_kevent_notimeout (int *kq_ptr, gpointer changelist, int nchanges, gpointer eventlist, int nevents)
247 g_assert_not_reached ();
251 #endif /* #if HAVE_KQUEUE */