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;
138 * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
139 * FILE_MODIFIED will trigger FILE_ATTRIB.
142 if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
146 if (e == FILE_RENAME_FROM) {
149 if (e == FILE_RENAME_TO) {
155 case FILE_RENAME_FROM:
164 g_assert_not_reached ();
168 tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
170 if (tail->user_data == f) {
172 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
173 /* skip the current */
175 } else if (e == FILE_MODIFIED && !has_twin
176 && tail->e == FILE_ATTRIB) {
177 tail->e = FILE_MODIFIED;
178 tail->has_twin = TRUE;
180 } else if (e == FILE_ATTRIB
181 && tail->e == FILE_MODIFIED && !tail->has_twin) {
182 tail->has_twin = TRUE;
188 if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
189 g_queue_push_tail (g_eventq, ev);
194 port_process_kevents ()
198 while ((ev = (fnode_event_t*)g_queue_pop_head (g_eventq)) != NULL) {
199 FK_W ("[%s] 0x%p %s\n", __func__, ev, _event_string (ev->e));
200 add_event_cb (ev->user_data, ev);
205 port_fetch_event_cb (void *arg)
207 pnode_t *pn = (pnode_t *)arg;
210 port_event_t pe[PE_ALLOC];
215 /* FK_W ("IN <======== %s\n", __func__); */
218 memset (&timeout, 0, sizeof (timespec_t));
221 if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
223 for (i = 0; i < nget; i++) {
224 fo = (_f*)pe[i].portev_user;
226 switch (pe[i].portev_source) {
227 case PORT_SOURCE_FILE:
228 /* If got FILE_EXCEPTION or add to port failed,
230 fo->is_active = FALSE;
233 printevent(F_NAME(fo), pe[i].portev_events, "RAW"));
234 port_add_kevent (pe[i].portev_events, fo->user_data);
236 /* fnode is deleted */
239 if (pe[i].portev_events & FILE_EXCEPTION) {
240 g_hash_table_remove (_obj_fen_hash, fo->user_data);
242 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
243 pnode_delete (fo->port);
248 /* case PORT_SOURCE_TIMER: */
249 FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
253 FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
256 } while (nget == PE_ALLOC);
258 /* Processing g_eventq */
259 port_process_kevents ();
262 pn->port_source_id = 0;
266 /* FK_W ("OUT ========> %s\n", __func__); */
271 * ref - 1 if remove a watching file succeeded.
274 pnode_delete (pnode_t *pn)
276 g_assert (pn->ref <= max_port_events);
278 if (pn->ref == max_port_events) {
279 FK_W ("PORT : move to visible queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
280 pn_fq = g_list_remove (pn_fq, pn);
281 pn_vq = g_list_prepend (pn_vq, pn);
283 if ((-- pn->ref) == 0) {
284 /* Should dispatch the source */
286 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
290 * malloc pnode_t and port_create, start thread at pnode_ref.
291 * if pnode_new succeeded, the pnode_t will never
292 * be freed. So pnode_t can be freed only in pnode_new.
293 * Note pnode_monitor_remove_all can also free pnode_t, but currently no one
302 pn = (pnode_t*)pn_vq->data;
303 g_assert (pn->ref < max_port_events);
305 pn = g_new0 (pnode_t, 1);
307 if ((pn->port = port_create ()) >= 0) {
308 g_assert (g_list_find (pn_vq, pn) == NULL);
309 pn_vq = g_list_prepend (pn_vq, pn);
311 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
318 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
320 if (pn->ref == max_port_events) {
321 FK_W ("PORT : move to full queue - [pn] 0x%p [ref] %d\n", pn, pn->ref);
322 pn_vq = g_list_remove (pn_vq, pn);
323 pn_fq = g_list_prepend (pn_fq, pn);
324 g_assert (g_list_find (pn_vq, pn) == NULL);
326 /* attach the source */
327 if (pn->port_source_id == 0) {
328 pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
331 g_assert (pn->port_source_id > 0);
342 * Unsafe, need lock fen_lock.
345 port_add_internal (file_obj_t* fobj, off_t* len,
346 gpointer f, gboolean need_stat)
352 g_assert (f && fobj);
353 FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
355 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
360 FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
361 g_hash_table_insert (_obj_fen_hash, f, fo);
368 if (fo->port == NULL) {
369 fo->port = pnode_new ();
373 if (FN_STAT (F_NAME(fo), &buf) != 0) {
374 FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
378 fo->fobj->fo_atime = buf.st_atim;
379 fo->fobj->fo_mtime = buf.st_mtim;
380 fo->fobj->fo_ctime = buf.st_ctim;
384 if (port_associate (F_PORT(fo),
389 fo->is_active = TRUE;
390 FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
393 FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
395 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
396 g_hash_table_remove (_obj_fen_hash, f);
397 pnode_delete (fo->port);
404 port_add (file_obj_t* fobj, off_t* len, gpointer f)
406 return port_add_internal (fobj, len, f, TRUE);
410 port_add_simple (file_obj_t* fobj, gpointer f)
412 return port_add_internal (fobj, NULL, f, FALSE);
419 * Unsafe, need lock fen_lock.
422 port_remove (gpointer f)
426 FK_W ("%s\n", __func__);
427 if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
429 fo->user_data = NULL;
430 g_hash_table_remove (_obj_fen_hash, f);
432 if (port_dissociate (F_PORT(fo),
434 (uintptr_t)fo->fobj) == 0) {
436 * Note, we can run foode_delete if dissociating is failed,
437 * because there may be some pending events (mostly like
438 * FILE_DELETE) in the port_get. If we delete the foode
439 * the fnode may be deleted, then port_get will run on an invalid
442 FK_W ("[ FREE_FO ] [0x%p]\n", fo);
443 pnode_delete (fo->port);
446 FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
452 _event_string (int event)
456 return "FILE_DELETE";
457 case FILE_RENAME_FROM:
458 return "FILE_RENAME_FROM";
460 return "FILE_MODIFIED";
462 return "FILE_RENAME_TO";
464 return "MOUNTEDOVER";
466 return "FILE_ATTRIB";
470 return "FILE_ACCESS";
472 return "EVENT_UNKNOWN";
477 * Get Solaris resouce values.
482 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
485 FK_W ("%s\n", __func__);
486 if ((rblk = malloc (rctlblk_size ())) == NULL) {
487 FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
490 if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
491 FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
495 max_port_events = rctlblk_get_value(rblk);
496 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
499 if ((_obj_fen_hash = g_hash_table_new(g_direct_hash,
500 g_direct_equal)) == NULL) {
501 FK_W ("[kernel] fobj hash initializing faild\n");
504 if ((g_eventq = g_queue_new ()) == NULL) {
505 FK_W ("[kernel] FEN global event queue initializing faild\n");
507 if (user_add_event == NULL) {
510 add_event_cb = user_add_event;
515 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
519 if ((ev = g_new (fnode_event_t, 1)) != NULL) {
522 ev->user_data = user_data;
523 ev->has_twin = has_twin;
524 /* Default isn't a pending event. */
525 ev->is_pending = FALSE;
531 fnode_event_delete (fnode_event_t* ev)