1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set expandtab ts=4 shiftwidth=4: */
4 * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved.
5 * Use is subject to license terms.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
22 * Authors: Lin Ma <lin.ma@sun.com>
33 #include "fen-kernel.h"
36 #ifdef GIO_COMPILATION
37 #define FK_W if (fk_debug_enabled) g_warning
38 static gboolean fk_debug_enabled = FALSE;
40 #include "gam_error.h"
41 #define FK_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
44 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
46 #define F_PORT(pfo) (((_f *)(pfo))->port->port)
47 #define F_NAME(pfo) (((_f *)(pfo))->fobj->fo_name)
48 #define FEN_ALL_EVENTS (FILE_MODIFIED | FILE_ATTRIB | FILE_NOFOLLOW)
49 #define FEN_IGNORE_EVENTS (FILE_ACCESS)
50 #define PROCESS_PORT_EVENTS_TIME 400 /* in milliseconds */
52 static GHashTable *_obj_fen_hash = NULL; /* <user_data, port> */
53 static ulong max_port_events = 512;
54 static GList *pn_vq; /* the queue of ports which don't have the max objs */
55 static GList *pn_fq; /* the queue of ports which have the max objs */
56 static GQueue *g_eventq = NULL;
57 static void (*add_event_cb) (gpointer, fnode_event_t*);
61 long ref; /* how many fds are associated to this port */
74 static gboolean port_fetch_event_cb (void *arg);
75 static pnode_t *pnode_new ();
76 static void pnode_delete (pnode_t *pn);
79 is_ported (gpointer f)
81 _f* fo = g_hash_table_lookup (_obj_fen_hash, f);
90 printevent (const char *pname, int event, const char *tag)
92 static gchar *event_string = NULL;
99 str = g_string_new ("");
100 g_string_printf (str, "[%s] [%-20s]", tag, pname);
101 if (event & FILE_ACCESS) {
102 str = g_string_append (str, " ACCESS");
104 if (event & FILE_MODIFIED) {
105 str = g_string_append (str, " MODIFIED");
107 if (event & FILE_ATTRIB) {
108 str = g_string_append (str, " ATTRIB");
110 if (event & FILE_DELETE) {
111 str = g_string_append (str, " DELETE");
113 if (event & FILE_RENAME_TO) {
114 str = g_string_append (str, " RENAME_TO");
116 if (event & FILE_RENAME_FROM) {
117 str = g_string_append (str, " RENAME_FROM");
119 if (event & UNMOUNTED) {
120 str = g_string_append (str, " UNMOUNTED");
122 if (event & MOUNTEDOVER) {
123 str = g_string_append (str, " MOUNTEDOVER");
125 event_string = str->str;
126 g_string_free (str, FALSE);
131 port_add_kevent (int e, gpointer f)
133 fnode_event_t *ev, *tail;
135 gboolean has_twin = FALSE;
137 FK_W("%s\n", printevent(F_NAME(f), e, "org"));
139 * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
140 * FILE_MODIFIED will trigger FILE_ATTRIB.
143 if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
147 if (e == FILE_RENAME_FROM) {
150 if (e == FILE_RENAME_TO) {
156 case FILE_RENAME_FROM:
165 g_assert_not_reached ();
169 tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
171 if (tail->user_data == f) {
173 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
174 /* skip the current */
176 } else if (e == FILE_MODIFIED && !has_twin
177 && tail->e == FILE_ATTRIB) {
178 tail->e = FILE_MODIFIED;
179 tail->has_twin = TRUE;
181 } else if (e == FILE_ATTRIB
182 && tail->e == FILE_MODIFIED && !tail->has_twin) {
183 tail->has_twin = TRUE;
189 if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
190 g_queue_push_tail (g_eventq, ev);
195 port_process_kevents ()
199 while ((ev = (fnode_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
200 FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e));
201 add_event_cb (ev->user_data, ev);
206 port_fetch_event_cb (void *arg)
208 pnode_t *pn = (pnode_t *)arg;
211 port_event_t pe[PE_ALLOC];
216 /* FK_W ("IN <======== %s\n", __func__); */
219 memset (&timeout, 0, sizeof (timespec_t));
222 if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
224 for (i = 0; i < nget; i++) {
225 fo = (_f*)pe[i].portev_user;
227 switch (pe[i].portev_source) {
228 case PORT_SOURCE_FILE:
229 /* If got FILE_EXCEPTION or add to port failed,
231 fo->is_active = FALSE;
234 printevent(F_NAME(fo), pe[i].portev_events, "RAW"));
235 port_add_kevent (pe[i].portev_events, fo->user_data);
237 /* fnode is deleted */
240 if (pe[i].portev_events & FILE_EXCEPTION) {
241 g_hash_table_remove (_obj_fen_hash, fo->user_data);
243 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
244 pnode_delete (fo->port);
249 /* case PORT_SOURCE_TIMER: */
250 FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
254 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
257 } while (nget == PE_ALLOC);
259 /* Processing g_eventq */
260 port_process_kevents ();
263 pn->port_source_id = 0;
267 /* FK_W ("OUT ========> %s\n", __func__); */
272 * ref - 1 if remove a watching file succeeded.
275 pnode_delete (pnode_t *pn)
277 g_assert (pn->ref <= max_port_events);
279 if (pn->ref == max_port_events) {
280 FK_W ("PORT : move to visible queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
281 pn_fq = g_list_remove (pn_fq, pn);
282 pn_vq = g_list_prepend (pn_vq, pn);
284 if ((-- pn->ref) == 0) {
285 /* Should dispatch the source */
287 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
291 * malloc pnode_t and port_create, start thread at pnode_ref.
292 * if pnode_new succeeded, the pnode_t will never
293 * be freed. So pnode_t can be freed only in pnode_new.
294 * Note pnode_monitor_remove_all can also free pnode_t, but currently no one
303 pn = (pnode_t*)pn_vq->data;
304 g_assert (pn->ref < max_port_events);
306 pn = g_new0 (pnode_t, 1);
308 if ((pn->port = port_create ()) >= 0) {
309 g_assert (g_list_find (pn_vq, pn) == NULL);
310 pn_vq = g_list_prepend (pn_vq, pn);
312 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
319 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
321 if (pn->ref == max_port_events) {
322 FK_W ("PORT : move to full queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
323 pn_vq = g_list_remove (pn_vq, pn);
324 pn_fq = g_list_prepend (pn_fq, pn);
325 g_assert (g_list_find (pn_vq, pn) == NULL);
327 /* attach the source */
328 if (pn->port_source_id == 0) {
329 pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
332 g_assert (pn->port_source_id > 0);
343 * Unsafe, need lock fen_lock.
346 port_add_internal (file_obj_t* fobj, off_t* len,
347 gpointer f, gboolean need_stat)
353 g_assert (f && fobj);
354 FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
356 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
361 FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
362 g_hash_table_insert (_obj_fen_hash, f, fo);
369 if (fo->port == NULL) {
370 fo->port = pnode_new ();
374 if (FN_STAT (F_NAME(fo), &buf) != 0) {
375 FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
379 fo->fobj->fo_atime = buf.st_atim;
380 fo->fobj->fo_mtime = buf.st_mtim;
381 fo->fobj->fo_ctime = buf.st_ctim;
385 if (port_associate (F_PORT(fo),
390 fo->is_active = TRUE;
391 FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
394 FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
396 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
397 g_hash_table_remove (_obj_fen_hash, f);
398 pnode_delete (fo->port);
405 port_add (file_obj_t* fobj, off_t* len, gpointer f)
407 return port_add_internal (fobj, len, f, TRUE);
411 port_add_simple (file_obj_t* fobj, gpointer f)
413 return port_add_internal (fobj, NULL, f, FALSE);
420 * Unsafe, need lock fen_lock.
423 port_remove (gpointer f)
427 FK_W ("%s\n", __func__);
428 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
430 fo->user_data = NULL;
431 g_hash_table_remove (_obj_fen_hash, f);
433 if (port_dissociate (F_PORT(fo),
435 (uintptr_t)fo->fobj) == 0) {
437 * Note, we can run foode_delete if dissociating is failed,
438 * because there may be some pending events (mostly like
439 * FILE_DELETE) in the port_get. If we delete the foode
440 * the fnode may be deleted, then port_get will run on an invalid
443 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
444 pnode_delete (fo->port);
447 FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
453 _event_string (int event)
457 return "FILE_DELETE";
458 case FILE_RENAME_FROM:
459 return "FILE_RENAME_FROM";
461 return "FILE_MODIFIED";
463 return "FILE_RENAME_TO";
465 return "MOUNTEDOVER";
467 return "FILE_ATTRIB";
471 return "FILE_ACCESS";
473 return "EVENT_UNKNOWN";
478 * Get Solaris resouce values.
483 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
486 FK_W ("%s\n", __func__);
487 if ((rblk = malloc (rctlblk_size ())) == NULL) {
488 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
491 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
492 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
496 max_port_events = rctlblk_get_value(rblk);
497 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
500 if ((_obj_fen_hash = g_hash_table_new(g_direct_hash,
501 g_direct_equal)) == NULL) {
502 FK_W ("[kernel] fobj hash initializing faild\n");
505 if ((g_eventq = g_queue_new ()) == NULL) {
506 FK_W ("[kernel] FEN global event queue initializing faild\n");
508 if (user_add_event == NULL) {
511 add_event_cb = user_add_event;
516 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
520 if ((ev = g_new (fnode_event_t, 1)) != NULL) {
523 ev->user_data = user_data;
524 ev->has_twin = has_twin;
525 /* Default isn't a pending event. */
526 ev->is_pending = FALSE;
532 fnode_event_delete (fnode_event_t* ev)