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