Default disable all loggings. Fixed two macro nits.
[platform/upstream/glib.git] / gio / fen / fen-kernel.c
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set expandtab ts=4 shiftwidth=4: */
3 /* 
4  * Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved.
5  * Use is subject to license terms.
6  *
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.
11  *
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.
16  *
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.
21  *
22  * Authors: Lin Ma <lin.ma@sun.com>
23  */
24
25 #include "config.h"
26 #include <rctl.h>
27 #include <strings.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <glib.h>
33 #include "fen-kernel.h"
34 #include "fen-dump.h"
35
36 #ifdef GIO_COMPILATION
37 #define FK_W if (fk_debug_enabled) g_warning
38 static gboolean fk_debug_enabled = FALSE;
39 #else
40 #include "gam_error.h"
41 #define FK_W(...) GAM_DEBUG(DEBUG_INFO, __VA_ARGS__)
42 #endif
43
44 G_GNUC_INTERNAL G_LOCK_DEFINE (fen_lock);
45 #define PE_ALLOC        64
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 */
51
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*);
58
59 typedef struct pnode
60 {
61         long ref;       /* how many fds are associated to this port */
62         int port;
63     guint port_source_id;
64 } pnode_t;
65
66 typedef struct {
67     pnode_t*    port;
68     file_obj_t* fobj;
69
70     gboolean    is_active;
71     gpointer    user_data;
72 } _f;
73
74 static gboolean port_fetch_event_cb (void *arg);
75 static pnode_t *pnode_new ();
76 static void pnode_delete (pnode_t *pn);
77
78 gboolean
79 is_ported (gpointer f)
80 {
81     _f* fo = g_hash_table_lookup (_obj_fen_hash, f);
82     
83     if (fo) {
84         return fo->is_active;
85     }
86     return FALSE;
87 }
88
89 static gchar*
90 printevent (const char *pname, int event, const char *tag)
91 {
92     static gchar        *event_string = NULL;
93     GString                     *str;
94
95     if (event_string) {
96         g_free(event_string);
97     }
98
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");
103     }
104     if (event & FILE_MODIFIED) {
105         str = g_string_append (str, " MODIFIED");
106     }
107     if (event & FILE_ATTRIB) {
108         str = g_string_append (str, " ATTRIB");
109     }
110     if (event & FILE_DELETE) {
111         str = g_string_append (str, " DELETE");
112     }
113     if (event & FILE_RENAME_TO) {
114         str = g_string_append (str, " RENAME_TO");
115     }
116     if (event & FILE_RENAME_FROM) {
117         str = g_string_append (str, " RENAME_FROM");
118     }
119     if (event & UNMOUNTED) {
120         str = g_string_append (str, " UNMOUNTED");
121     }
122     if (event & MOUNTEDOVER) {
123         str = g_string_append (str, " MOUNTEDOVER");
124     }
125     event_string = str->str;
126     g_string_free (str, FALSE);
127     return event_string;
128 }
129
130 static void
131 port_add_kevent (int e, gpointer f)
132 {
133     fnode_event_t *ev, *tail;
134     GTimeVal t;
135     gboolean has_twin = FALSE;
136     
137     FK_W("%s\n", printevent(F_NAME(f), e, "org"));
138     /*
139      * Child FILE_DELETE | FILE_RENAME_FROM will trigger parent FILE_MODIFIED.
140      * FILE_MODIFIED will trigger FILE_ATTRIB.
141      */
142
143     if ((e & FILE_ATTRIB) && e != FILE_ATTRIB) {
144         e ^= FILE_ATTRIB;
145         has_twin = TRUE;
146     }
147     if (e == FILE_RENAME_FROM) {
148         e = FILE_DELETE;
149     }
150     if (e == FILE_RENAME_TO) {
151         e = FILE_MODIFIED;
152     }
153     
154     switch (e) {
155     case FILE_DELETE:
156     case FILE_RENAME_FROM:
157     case FILE_MODIFIED:
158     case FILE_ATTRIB:
159     case UNMOUNTED:
160     case MOUNTEDOVER:
161         break;
162     case FILE_RENAME_TO:
163     case FILE_ACCESS:
164     default:
165         g_assert_not_reached ();
166         return;
167     }
168
169     tail = (fnode_event_t*) g_queue_peek_tail (g_eventq);
170     if (tail) {
171         if (tail->user_data == f) {
172             if (tail->e == e) {
173                 tail->has_twin = (has_twin | (tail->has_twin ^ has_twin));
174                 /* skip the current */
175                 return;
176             } else if (e == FILE_MODIFIED && !has_twin
177               && tail->e == FILE_ATTRIB) {
178                 tail->e = FILE_MODIFIED;
179                 tail->has_twin = TRUE;
180                 return;
181             } else if (e == FILE_ATTRIB
182               && tail->e == FILE_MODIFIED && !tail->has_twin) {
183                 tail->has_twin = TRUE;
184                 return;
185             }
186         }
187     }
188     
189     if ((ev = fnode_event_new (e, has_twin, f)) != NULL) {
190         g_queue_push_tail (g_eventq, ev);
191     }
192 }
193
194 static void
195 port_process_kevents ()
196 {
197     fnode_event_t *ev;
198     
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);
202     }
203 }
204
205 static gboolean
206 port_fetch_event_cb (void *arg)
207 {
208         pnode_t *pn = (pnode_t *)arg;
209     _f* fo;
210         uint_t nget = 0;
211         port_event_t pe[PE_ALLOC];
212     timespec_t timeout;
213     gpointer f;
214     gboolean ret = TRUE;
215     
216     /* FK_W ("IN <======== %s\n", __func__); */
217     G_LOCK (fen_lock);
218     
219     memset (&timeout, 0, sizeof (timespec_t));
220     do {
221         nget = 1;
222         if (port_getn (pn->port, pe, PE_ALLOC, &nget, &timeout) == 0) {
223             int i;
224             for (i = 0; i < nget; i++) {
225                 fo = (_f*)pe[i].portev_user;
226                 /* handle event */
227                 switch (pe[i].portev_source) {
228                 case PORT_SOURCE_FILE:
229                     /* If got FILE_EXCEPTION or add to port failed,
230                        delete the pnode */
231                     fo->is_active = FALSE;
232                     if (fo->user_data) {
233                         FK_W("%s\n",
234                           printevent(F_NAME(fo), pe[i].portev_events, "RAW"));
235                         port_add_kevent (pe[i].portev_events, fo->user_data);
236                     } else {
237                         /* fnode is deleted */
238                         goto L_delete;
239                     }
240                     if (pe[i].portev_events & FILE_EXCEPTION) {
241                         g_hash_table_remove (_obj_fen_hash, fo->user_data);
242                     L_delete:
243                         FK_W ("[ FREE_FO ] [0x%p]\n", fo);
244                         pnode_delete (fo->port);
245                         g_free (fo);
246                     }
247                     break;
248                 default:
249                     /* case PORT_SOURCE_TIMER: */
250                     FK_W ("[kernel] unknown portev_source %d\n", pe[i].portev_source);
251                 }
252             }
253         } else {
254             FK_W ("[kernel] port_getn %s\n", g_strerror (errno));
255             nget = 0;
256         }
257     } while (nget == PE_ALLOC);
258
259         /* Processing g_eventq */
260     port_process_kevents ();
261     
262     if (pn->ref == 0) {
263         pn->port_source_id = 0;
264         ret = FALSE;
265     }
266     G_UNLOCK (fen_lock);
267     /* FK_W ("OUT ========> %s\n", __func__); */
268         return ret;
269 }
270
271 /*
272  * ref - 1 if remove a watching file succeeded.
273  */
274 static void
275 pnode_delete (pnode_t *pn)
276 {
277     g_assert (pn->ref <= max_port_events);
278     
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);
283         }
284         if ((-- pn->ref) == 0) {
285         /* Should dispatch the source */
286         }
287         FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
288 }
289
290 /*
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
295  * invork it.
296  */
297 static pnode_t *
298 pnode_new ()
299 {
300         pnode_t *pn = NULL;
301
302         if (pn_vq) {
303                 pn = (pnode_t*)pn_vq->data;
304         g_assert (pn->ref < max_port_events);
305         } else {
306                 pn = g_new0 (pnode_t, 1);
307                 if (pn != NULL) {
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);
311             } else {
312                 FK_W ("PORT_CREATE %s\n", g_strerror (errno));
313                 g_free (pn);
314                 pn = NULL;
315                         }
316                 }
317         }
318         if (pn) {
319                 FK_W ("%s [pn] 0x%p [ref] %d\n", __func__, pn, pn->ref);
320         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);
326         }
327         /* attach the source */
328         if (pn->port_source_id == 0) {
329             pn->port_source_id = g_timeout_add (PROCESS_PORT_EVENTS_TIME,
330               port_fetch_event_cb,
331               (void *)pn);
332             g_assert (pn->port_source_id > 0);
333         }
334         }
335
336         return pn;
337 }
338
339 /**
340  * port_add_internal
341  *
342  * < private >
343  * Unsafe, need lock fen_lock.
344  */
345 static gboolean
346 port_add_internal (file_obj_t* fobj, off_t* len,
347   gpointer f, gboolean need_stat)
348 {
349     int ret;
350     struct stat buf;
351     _f* fo = NULL;
352
353     g_assert (f && fobj);
354     FK_W ("%s [0x%p] %s\n", __func__, f, fobj->fo_name);
355
356     if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) == NULL) {
357         fo = g_new0 (_f, 1);
358         fo->fobj = fobj;
359         fo->user_data = f;
360         g_assert (fo);
361         FK_W ("[ NEW_FO ] [0x%p] %s\n", fo, F_NAME(fo));
362         g_hash_table_insert (_obj_fen_hash, f, fo);
363     }
364
365     if (fo->is_active) {
366         return TRUE;
367     }
368
369     if (fo->port == NULL) {
370         fo->port = pnode_new ();
371     }
372     
373     if (need_stat) {
374         if (FN_STAT (F_NAME(fo), &buf) != 0) {
375             FK_W ("LSTAT [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
376             goto L_exit;
377         }
378         g_assert (len);
379         fo->fobj->fo_atime = buf.st_atim;
380         fo->fobj->fo_mtime = buf.st_mtim;
381         fo->fobj->fo_ctime = buf.st_ctim;
382         *len = buf.st_size;
383     }
384     
385     if (port_associate (F_PORT(fo),
386           PORT_SOURCE_FILE,
387           (uintptr_t)fo->fobj,
388           FEN_ALL_EVENTS,
389           (void *)fo) == 0) {
390         fo->is_active = TRUE;
391         FK_W ("%s %s\n", "PORT_ASSOCIATE", F_NAME(fo));
392         return TRUE;
393     } else {
394         FK_W ("PORT_ASSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
395     L_exit:
396         FK_W ("[ FREE_FO ] [0x%p]\n", fo);
397         g_hash_table_remove (_obj_fen_hash, f);
398         pnode_delete (fo->port);
399         g_free (fo);
400     }
401     return FALSE;
402 }
403
404 gboolean
405 port_add (file_obj_t* fobj, off_t* len, gpointer f)
406 {
407     return port_add_internal (fobj, len, f, TRUE);
408 }
409
410 gboolean
411 port_add_simple (file_obj_t* fobj, gpointer f)
412 {
413     return port_add_internal (fobj, NULL, f, FALSE);
414 }
415
416 /**
417  * port_remove
418  *
419  * < private >
420  * Unsafe, need lock fen_lock.
421  */
422 void
423 port_remove (gpointer f)
424 {
425     _f* fo = NULL;
426
427     FK_W ("%s\n", __func__);
428     if ((fo = g_hash_table_lookup (_obj_fen_hash, f)) != NULL) {
429         /* Marked */
430         fo->user_data = NULL;
431         g_hash_table_remove (_obj_fen_hash, f);
432         
433         if (port_dissociate (F_PORT(fo),
434               PORT_SOURCE_FILE,
435               (uintptr_t)fo->fobj) == 0) {
436             /*
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
441              * address.
442              */
443             FK_W ("[ FREE_FO ] [0x%p]\n", fo);
444             pnode_delete (fo->port);
445             g_free (fo);
446         } else {
447             FK_W ("PORT_DISSOCIATE [%-20s] %s\n", F_NAME(fo), g_strerror (errno));
448         }
449     }
450 }
451
452 const gchar *
453 _event_string (int event)
454 {
455     switch (event) {
456     case FILE_DELETE:
457         return "FILE_DELETE";
458     case FILE_RENAME_FROM:
459         return "FILE_RENAME_FROM";
460     case FILE_MODIFIED:
461         return "FILE_MODIFIED";
462     case FILE_RENAME_TO:
463         return "FILE_RENAME_TO";
464     case MOUNTEDOVER:
465         return "MOUNTEDOVER";
466     case FILE_ATTRIB:
467         return "FILE_ATTRIB";
468     case UNMOUNTED:
469         return "UNMOUNTED";
470     case FILE_ACCESS:
471         return "FILE_ACCESS";
472     default:
473         return "EVENT_UNKNOWN";
474     }
475 }
476
477 /**
478  * Get Solaris resouce values.
479  *
480  */
481
482 extern gboolean
483 port_class_init (void (*user_add_event) (gpointer, fnode_event_t*))
484 {
485         rctlblk_t *rblk;
486     FK_W ("%s\n", __func__);
487         if ((rblk = malloc (rctlblk_size ())) == NULL) {
488         FK_W ("[kernel] rblk malloc %s\n", g_strerror (errno));
489                 return FALSE;
490         }
491         if (getrctl ("process.max-port-events", NULL, rblk, RCTL_FIRST) == -1) {
492         FK_W ("[kernel] getrctl %s\n", g_strerror (errno));
493         free (rblk);
494         return FALSE;
495         } else {
496         max_port_events = rctlblk_get_value(rblk);
497                 FK_W ("[kernel] max_port_events = %u\n", max_port_events);
498         free (rblk);
499         }
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");
503         return FALSE;
504     }
505     if ((g_eventq = g_queue_new ()) == NULL) {
506                 FK_W ("[kernel] FEN global event queue initializing faild\n");
507     }
508     if (user_add_event == NULL) {
509         return FALSE;
510     }
511     add_event_cb = user_add_event;
512         return TRUE;
513 }
514
515 fnode_event_t*
516 fnode_event_new (int event, gboolean has_twin, gpointer user_data)
517 {
518     fnode_event_t *ev;
519     
520     if ((ev = g_new (fnode_event_t, 1)) != NULL) {
521         g_assert (ev);
522         ev->e = event;
523         ev->user_data = user_data;
524         ev->has_twin = has_twin;
525         /* Default isn't a pending event. */
526         ev->is_pending = FALSE;
527     }
528     return ev;
529 }
530
531 void
532 fnode_event_delete (fnode_event_t* ev)
533 {
534     g_free (ev);
535 }