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