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 Microsystem.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Lin Ma <lin.ma@sun.com>
32 #include "fen-kernel.h"
35 #define FK_W if (fk_debug_enabled) g_warning
36 #ifdef GIO_COMPILATION
37 static gboolean fk_debug_enabled = FALSE;
39 static gboolean fk_debug_enabled = TRUE;
42 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
44 #define F_PORT(pp) (((_f *)(fo))->port->port)
45 #define F_NAME(pp) (((_f *)(fo))->fobj->fo_name)
46 #define FEN_ALL_EVENTS (FILE_MODIFIED | FILE_ATTRIB | FILE_NOFOLLOW)
47 #define FEN_IGNORE_EVENTS (FILE_ACCESS)
48 #define PROCESS_PORT_EVENTS_TIME 400 /* in milliseconds */
50 static GHashTable *_obj_fen_hash = NULL; /* <user_data, port> */
51 static ulong max_port_events = 512;
52 static GList *pn_vq; /* the queue of ports which don't have the max objs */
53 static GList *pn_fq; /* the queue of ports which have the max objs */
54 static GQueue *g_eventq = NULL;
55 static void (*add_event_cb) (gpointer, fnode_event_t*);
59 long ref; /* how many fds are associated to this port */
72 static gboolean port_fetch_event_cb (void *arg);
73 static pnode_t *pnode_new ();
74 static void pnode_delete (pnode_t *pn);
77 is_ported (gpointer f)
79 _f* fo = g_hash_table_lookup (_obj_fen_hash, f);
88 printevent (const char *pname, int event, const char *tag)
91 str = g_string_new ("");
93 g_string_printf (str, "[%s] [%-20s]", tag, pname);
94 if (event & FILE_ACCESS) {
95 str = g_string_append (str, " ACCESS");
97 if (event & FILE_MODIFIED) {
98 str = g_string_append (str, " MODIFIED");
100 if (event & FILE_ATTRIB) {
101 str = g_string_append (str, " ATTRIB");
103 if (event & FILE_DELETE) {
104 str = g_string_append (str, " DELETE");
106 if (event & FILE_RENAME_TO) {
107 str = g_string_append (str, " RENAME_TO");
109 if (event & FILE_RENAME_FROM) {
110 str = g_string_append (str, " RENAME_FROM");
112 if (event & UNMOUNTED) {
113 str = g_string_append (str, " UNMOUNTED");
115 if (event & MOUNTEDOVER) {
116 str = g_string_append (str, " MOUNTEDOVER");
119 FK_W ("%s\n", str->str);
120 g_string_free (str, TRUE);
124 port_add_kevent (int e, gpointer f)
126 fnode_event_t *ev, *tail;
128 gboolean has_twin = FALSE;
130 /* printevent (F_NAME(f), e, "org"); */
132 * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
133 * FILE_MODIFIED will trigger FILE_ATTRIB.
136 if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
140 if (e == FILE_RENAME_FROM) {
143 if (e == FILE_RENAME_TO) {
149 case FILE_RENAME_FROM:
158 g_assert_not_reached ();
162 tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
164 if (tail->user_data == f) {
166 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
167 /* skip the current */
169 } else if (e == FILE_MODIFIED && !has_twin
170 && tail->e == FILE_ATTRIB) {
171 tail->e = FILE_MODIFIED;
172 tail->has_twin = TRUE;
174 } else if (e == FILE_ATTRIB
175 && tail->e == FILE_MODIFIED && !tail->has_twin) {
176 tail->has_twin = TRUE;
182 if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
183 g_queue_push_tail (g_eventq, ev);
188 port_process_kevents ()
192 while ((ev = (fnode_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
193 FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e));
194 add_event_cb (ev->user_data, ev);
199 port_fetch_event_cb (void *arg)
201 pnode_t *pn = (pnode_t *)arg;
204 port_event_t pe[PE_ALLOC];
209 /* FK_W ("IN <======== %s\n", __func__); */
212 memset (&timeout, 0, sizeof (timespec_t));
215 if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
217 for (i = 0; i < nget; i++) {
218 fo = (_f*)pe[i].portev_user;
220 switch (pe[i].portev_source) {
221 case PORT_SOURCE_FILE:
222 /* If got FILE_EXCEPTION or add to port failed,
224 fo->is_active = FALSE;
226 printevent (F_NAME(fo), pe[i].portev_events, "RAW");
227 port_add_kevent (pe[i].portev_events, fo->user_data);
229 /* fnode is deleted */
232 if (pe[i].portev_events & FILE_EXCEPTION) {
233 g_hash_table_remove (_obj_fen_hash, fo->user_data);
235 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
236 pnode_delete (fo->port);
241 /* case PORT_SOURCE_TIMER: */
242 FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
246 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
249 } while (nget == PE_ALLOC);
251 /* Processing g_eventq */
252 port_process_kevents ();
255 pn->port_source_id = 0;
259 /* FK_W ("OUT ========> %s\n", __func__); */
264 * ref - 1 if remove a watching file succeeded.
267 pnode_delete (pnode_t *pn)
269 g_assert (pn->ref <= max_port_events);
271 if (pn->ref == max_port_events) {
272 FK_W ("PORT : move to visible queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
273 pn_fq = g_list_remove (pn_fq, pn);
274 pn_vq = g_list_prepend (pn_vq, pn);
276 if ((-- pn->ref) == 0) {
277 /* Should dispatch the source */
279 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
283 * malloc pnode_t and port_create, start thread at pnode_ref.
284 * if pnode_new succeeded, the pnode_t will never
285 * be freed. So pnode_t can be freed only in pnode_new.
286 * Note pnode_monitor_remove_all can also free pnode_t, but currently no one
295 pn = (pnode_t*)pn_vq->data;
296 g_assert (pn->ref < max_port_events);
298 pn = g_new0 (pnode_t, 1);
300 if ((pn->port = port_create ()) >= 0) {
301 g_assert (g_list_find (pn_vq, pn) == NULL);
302 pn_vq = g_list_prepend (pn_vq, pn);
304 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
311 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
313 if (pn->ref == max_port_events) {
314 FK_W ("PORT : move to full queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
315 pn_vq = g_list_remove (pn_vq, pn);
316 pn_fq = g_list_prepend (pn_fq, pn);
317 g_assert (g_list_find (pn_vq, pn) == NULL);
319 /* attach the source */
320 if (pn->port_source_id == 0) {
321 pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
324 g_assert (pn->port_source_id > 0);
335 * Unsafe, need lock fen_lock.
338 port_add_internal (file_obj_t* fobj, off_t* len,
339 gpointer f, gboolean need_stat)
345 g_assert (f && fobj);
346 FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
348 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
353 FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
354 g_hash_table_insert (_obj_fen_hash, f, fo);
361 if (fo->port == NULL) {
362 fo->port = pnode_new ();
366 if (FN_STAT (F_NAME(fo), &buf) != 0) {
367 FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
371 fo->fobj->fo_atime = buf.st_atim;
372 fo->fobj->fo_mtime = buf.st_mtim;
373 fo->fobj->fo_ctime = buf.st_ctim;
377 if (port_associate (F_PORT(fo),
382 fo->is_active = TRUE;
383 FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
386 FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
388 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
389 g_hash_table_remove (_obj_fen_hash, f);
390 pnode_delete (fo->port);
397 port_add (file_obj_t* fobj, off_t* len, gpointer f)
399 return port_add_internal (fobj, len, f, TRUE);
403 port_add_simple (file_obj_t* fobj, gpointer f)
405 return port_add_internal (fobj, NULL, f, FALSE);
412 * Unsafe, need lock fen_lock.
415 port_remove (gpointer f)
419 FK_W ("%s\n", __func__);
420 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
422 fo->user_data = NULL;
423 g_hash_table_remove (_obj_fen_hash, f);
425 if (port_dissociate (F_PORT(fo),
427 (uintptr_t)fo->fobj) == 0) {
429 * Note, we can run foode_delete if dissociating is failed,
430 * because there may be some pending events (mostly like
431 * FILE_DELETE) in the port_get. If we delete the foode
432 * the fnode may be deleted, then port_get will run on an invalid
435 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
436 pnode_delete (fo->port);
439 FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
445 _event_string (int event)
449 return "FILE_DELETE";
450 case FILE_RENAME_FROM:
451 return "FILE_RENAME_FROM";
453 return "FILE_MODIFIED";
455 return "FILE_RENAME_TO";
457 return "MOUNTEDOVER";
459 return "FILE_ATTRIB";
463 return "FILE_ACCESS";
465 return "EVENT_UNKNOWN";
470 * Get Solaris resouce values.
475 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
478 FK_W ("%s\n", __func__);
479 if ((rblk = malloc (rctlblk_size ())) == NULL) {
480 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
483 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
484 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
488 max_port_events = rctlblk_get_value(rblk);
489 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
492 if ((_obj_fen_hash = g_hash_table_new(g_direct_hash,
493 g_direct_equal)) == NULL) {
494 FK_W ("[kernel] fobj hash initializing faild\n");
497 if ((g_eventq = g_queue_new ()) == NULL) {
498 FK_W ("[kernel] FEN global event queue initializing faild\n");
500 if (user_add_event == NULL) {
503 add_event_cb = user_add_event;
508 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
512 if ((ev = g_new (fnode_event_t, 1)) != NULL) {
515 ev->user_data = user_data;
516 ev->has_twin = has_twin;
517 /* Default isn't a pending event. */
518 ev->is_pending = FALSE;
524 fnode_event_delete (fnode_event_t* ev)