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)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
18 #ifdef HAVE_SYS_EVENT_H
19 #include <sys/event.h>
21 #ifdef HAVE_SYS_TIME_H
25 #include <mono/metadata/appdomain.h>
26 #include <mono/metadata/exception.h>
27 #include <mono/metadata/filewatcher.h>
28 #include <mono/metadata/marshal.h>
29 #include <mono/utils/mono-dl.h>
30 #include <mono/utils/mono-io-portability.h>
31 #include <mono/metadata/w32error.h>
37 * We use the managed watcher on windows, so the code inside this #if is never used
40 ves_icall_System_IO_FSW_SupportsFSW (void)
46 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
47 MonoString **filename,
56 static int (*FAMNextEvent) (gpointer, gpointer);
59 ves_icall_System_IO_FSW_SupportsFSW (void)
65 int lib_used = 4; /* gamin */
69 inotify_instance = ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ();
70 if (inotify_instance != -1) {
71 close (inotify_instance);
72 return 5; /* inotify */
75 fam_module = mono_dl_open ("libgamin-1.so", MONO_DL_LAZY, NULL);
76 if (fam_module == NULL) {
77 lib_used = 2; /* FAM */
78 fam_module = mono_dl_open ("libfam.so", MONO_DL_LAZY, NULL);
81 if (fam_module == NULL)
84 err = mono_dl_symbol (fam_module, "FAMNextEvent", (gpointer *) &FAMNextEvent);
86 if (FAMNextEvent == NULL)
93 /* Almost copied from fam.h. Weird, I know */
98 typedef struct FAMEvent {
102 gchar filename [PATH_MAX];
108 ves_icall_System_IO_FAMW_InternalFAMNextEvent (gpointer conn,
109 MonoString **filename,
115 if (FAMNextEvent (conn, &ev) == 1) {
116 *filename = mono_string_new (mono_domain_get (), ev.filename);
118 *reqnum = ev.fr.reqnum;
126 #ifndef HAVE_SYS_INOTIFY_H
127 int ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
132 int ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *directory, gint32 mask)
137 int ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
142 #include <sys/inotify.h>
146 ves_icall_System_IO_InotifyWatcher_GetInotifyInstance ()
148 return inotify_init ();
152 ves_icall_System_IO_InotifyWatcher_AddWatch (int fd, MonoString *name, gint32 mask)
161 str = mono_string_to_utf8_checked (name, &error);
162 if (mono_error_set_pending_exception (&error))
164 path = mono_portability_find_file (str, TRUE);
168 retval = inotify_add_watch (fd, path, mask);
172 errno = ERROR_ACCESS_DENIED;
175 errno = ERROR_INVALID_HANDLE;
178 errno = ERROR_INVALID_ACCESS;
181 errno = ERROR_INVALID_DATA;
184 errno = ERROR_NOT_ENOUGH_MEMORY;
187 errno = ERROR_TOO_MANY_OPEN_FILES;
190 errno = ERROR_GEN_FAILURE;
193 mono_marshal_set_last_error ();
202 ves_icall_System_IO_InotifyWatcher_RemoveWatch (int fd, gint32 watch_descriptor)
204 return inotify_rm_watch (fd, watch_descriptor);
211 interrupt_kevent (gpointer data)
215 /* Interrupt the kevent () call by closing the fd */
217 /* Signal to managed code that the fd is closed */
222 * ves_icall_System_IO_KqueueMonitor_kevent_notimeout:
224 * Call kevent (), while handling runtime interruptions.
227 ves_icall_System_IO_KqueueMonitor_kevent_notimeout (int *kq_ptr, gpointer changelist, int nchanges, gpointer eventlist, int nevents)
230 gboolean interrupted;
232 mono_thread_info_install_interrupt (interrupt_kevent, kq_ptr, &interrupted);
240 res = kevent (*kq_ptr, changelist, nchanges, eventlist, nevents, NULL);
243 mono_thread_info_uninstall_interrupt (&interrupted);
251 ves_icall_System_IO_KqueueMonitor_kevent_notimeout (int *kq_ptr, gpointer changelist, int nchanges, gpointer eventlist, int nevents)
253 g_assert_not_reached ();
257 #endif /* #if HAVE_KQUEUE */