* mount.c (mountee_control): New variable. (mountee_notify_port): Likewise. (start_mountee): Store the control port of the mountee in the global variable mountee_control. (mountee_server): New function. (_mountee_listen_thread_proc): Likewise. (setup_unionmount): Request to be notified when the mountee goes away. Detach a separate thread to wait for the notification. * mount.h (mountee_control): New variable. --- Hello,
On Sun, Aug 16, 2009 at 09:55:26PM +0200, olafbuddenha...@gmx.net wrote: > On Mon, Aug 03, 2009 at 11:37:50PM +0300, Sergiu Ivanov wrote: > > On Wed, Jul 29, 2009 at 10:47:53AM +0200, olafbuddenha...@gmx.net wrote: > > > On Fri, Jul 17, 2009 at 01:57:33PM +0300, Sergiu Ivanov wrote: > > > > > +static void > > > > +_mountee_listen_thread_proc (any_t * arg) > > > > +{ > > > > + while (1) > > > > + mach_msg_server (mountee_server, 0, mountee_listen_port); > > > > +} /* _mountee_listen_thread */ > > > > > > The comment here seems pointless, when the function started only a few > > > lines above... > > > > > > Also, it's wrong :-) > > > > I'm not sure I can correctly understand what you are talking about :-( > > I'm talkin about the "/* _mountee_listen_thread */" comment at the end > of the function. These comments might be useful for long functions, > where the beginning is not visible while looking at the end -- but for a > function that only has a few lines, it's pointless. It's just an > unnecessary maintenance burden -- it seems that you actually forgot to > update it at some point, as the comment doesn't match the function name. I see... I've removed the comment. I generally like to put such comments after the final closing brace of the function because it helps me greatly when I walking through the code, but it never really occurred to me that short functions don't need such decoration :-( Regards, scolobb --- mount.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- mount.h | 1 + 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/mount.c b/mount.c index 51140be..214edbf 100644 --- a/mount.c +++ b/mount.c @@ -24,6 +24,7 @@ #include <hurd/fsys.h> #include <fcntl.h> +#include <cthreads.h> #include "mount.h" #include "lib.h" @@ -34,6 +35,7 @@ char * mountee_argz; size_t mountee_argz_len; mach_port_t mountee_root; +mach_port_t mountee_control = MACH_PORT_NULL; int mountee_started = 0; @@ -41,6 +43,10 @@ int mountee_started = 0; operates (transparent/non-transparent). */ int transparent_mount = 1; +/* The port for receiving the notification about the shutdown of the + mountee. */ +mach_port_t mountee_notify_port; + /* Starts the mountee (given by `argz` and `argz_len`), attaches it to the node `np` and opens a port `port` to the mountee with `flags`. */ @@ -51,8 +57,6 @@ start_mountee (node_t * np, char * argz, size_t argz_len, int flags, error_t err; mach_port_t underlying_port; - mach_port_t mountee_control; - /* Identity information about the unionfs process (for fsys_getroot). */ uid_t * uids; @@ -133,6 +137,31 @@ start_mountee (node_t * np, char * argz, size_t argz_len, int flags, return err; } /* start_mountee */ +/* Listens to the MACH_NOTIFY_DEAD_NAME notification for the port on + the control port of the mountee. */ +error_t +mountee_server (mach_msg_header_t * inp, mach_msg_header_t * outp) +{ + if (inp->msgh_id == MACH_NOTIFY_DEAD_NAME) + { + /* Terminate operations conducted by unionfs and shut down. */ + netfs_shutdown (FSYS_GOAWAY_FORCE); + exit (0); + } + + return 1; +} /* mountee_server */ + +/* The main proc of the thread for listening for the + MACH_NOTIFY_DEAD_NAME notification on the control port of the + mountee. */ +static void +_mountee_listen_thread_proc (any_t * arg) +{ + while (1) + mach_msg_server (mountee_server, 0, mountee_notify_port); +} + /* Sets up a proxy node, sets the translator on it, and registers the filesystem published by the translator in the list of merged filesystems. */ @@ -158,6 +187,38 @@ setup_unionmount (void) mountee_started = 1; - return 0; + /* The previously registered send-once right (used to hold the value + returned from mach_port_request_notification) */ + mach_port_t prev; + + /* Setup the port for receiving notifications. */ + err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, + &mountee_notify_port); + if (err) + return err; + err = mach_port_insert_right (mach_task_self (), mountee_notify_port, + mountee_notify_port, MACH_MSG_TYPE_MAKE_SEND); + if (err) + { + mach_port_deallocate (mach_task_self (), mountee_notify_port); + return err; + } + + /* Request to be notified when the mountee goes away and + `mountee_control` becomes a dead name. */ + err = mach_port_request_notification (mach_task_self (), mountee_control, + MACH_NOTIFY_DEAD_NAME, 1, mountee_notify_port, + MACH_MSG_TYPE_MAKE_SEND_ONCE, &prev); + assert (prev == MACH_PORT_NULL); + if(err) + { + mach_port_deallocate (mach_task_self(), mountee_notify_port); + return err; + } + + /* Create a new thread for listening to the notification. */ + cthread_detach (cthread_fork ((cthread_fn_t) _mountee_listen_thread_proc, NULL)); + + return err; } /* setup_unionmount */ diff --git a/mount.h b/mount.h index d51fdeb..e82c45c 100644 --- a/mount.h +++ b/mount.h @@ -34,6 +34,7 @@ extern char * mountee_argz; extern size_t mountee_argz_len; extern mach_port_t mountee_root; +extern mach_port_t mountee_control; extern int mountee_started; -- 1.6.3.3