Return from async method has to also skip following code block
[mono.git] / mono / io-layer / daemon.c
index 17b0ede6cb4a992a162a992f6cc1a9c1dd78fe92..b5ff32afc11bb45751949388226d6298ad6478f8 100644 (file)
 #include <mono/io-layer/daemon-messages.h>
 #include <mono/io-layer/timefuncs-private.h>
 #include <mono/io-layer/daemon-private.h>
+#include <mono/io-layer/socket-wrappers.h>
 
+#define LOGDEBUG(...)
 #undef DEBUG
+// #define LOGDEBUG(...) g_message(__VA_ARGS__)
 
 /* The shared thread codepath doesn't seem to work yet... */
 #undef _POSIX_THREAD_PROCESS_SHARED
@@ -61,9 +64,24 @@ static int main_sock;
 /* Set to TRUE by the SIGCHLD signal handler */
 static volatile gboolean check_processes=FALSE;
 
+/* The file_share_hash is used to emulate the windows file sharing mode */
+typedef struct _share_key
+{
+       dev_t device;
+       ino_t inode;
+} ShareKey;
+
+typedef struct _share_data
+{
+       guint32 sharemode;
+       guint32 access;
+} ShareData;
+
+static GHashTable *file_share_hash = NULL;
+
 static gboolean fd_activity (GIOChannel *channel, GIOCondition condition,
                             gpointer data);
-
+static void check_sharing (dev_t device, ino_t inode);
 
 /* Deletes the shared memory segment.  If we're exiting on error,
  * clients will get EPIPEs.
@@ -92,14 +110,10 @@ static void maybe_exit (void)
 {
        guint32 i;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Seeing if we should exit");
-#endif
+       LOGDEBUG ("%s: Seeing if we should exit", __func__);
 
        if(nfds>1) {
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Still got clients");
-#endif
+               LOGDEBUG ("%s: Still got clients", __func__);
                return;
        }
 
@@ -110,11 +124,7 @@ static void maybe_exit (void)
            i<_wapi_shared_data[0]->num_segments * _WAPI_HANDLES_PER_SEGMENT;
            i++) {
                if(daemon_channel_data->open_handles[i]>0) {
-#ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Still got handle references");
-#endif
-
+                       LOGDEBUG ("%s: Still got handle references", __func__);
                        _wapi_shared_data[0]->daemon_running=DAEMON_RUNNING;
                        return;
                }
@@ -140,16 +150,11 @@ static void maybe_exit (void)
                fds[0].events=POLLIN;
                fds[0].revents=0;
                
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Last connect check");
-#endif
+               LOGDEBUG ("%s: Last connect check", __func__);
 
                if(poll (fds, 1, 0)>0) {
                        /* Someone did connect, so carry on running */
-#ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION
-                                  ": Someone connected");
-#endif
+                       LOGDEBUG ("%s: Someone connected", __func__);
 
                        _wapi_shared_data[0]->daemon_running=DAEMON_RUNNING;
                        return;
@@ -157,9 +162,7 @@ static void maybe_exit (void)
        }
 #endif
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Byebye");
-#endif
+       LOGDEBUG ("%s: Byebye", __func__);
        
        cleanup ();
        exit (0);
@@ -173,9 +176,8 @@ static void maybe_exit (void)
  */
 static void signal_handler (int signo)
 {
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": daemon received signal %d", signo);
-#endif
+       LOGDEBUG ("%s: daemon received signal %d", __func__, signo);
+
        cleanup ();
        exit (-1);
 }
@@ -193,10 +195,32 @@ static void sigchld_handler (int unused)
        check_processes=TRUE;
 }
 
+static guint sharedata_hash (gconstpointer key)
+{
+       ShareKey *sharekey = (ShareKey *)key;
+       
+       return(g_int_hash (&(sharekey->inode)));
+}
+
+static gboolean sharedata_equal (gconstpointer a, gconstpointer b)
+{
+       ShareKey *share_a = (ShareKey *)a;
+       ShareKey *share_b = (ShareKey *)b;
+       
+       return(share_a->device == share_b->device &&
+              share_a->inode == share_b->inode);
+}
+
+/* Catch this here rather than corrupt the shared data at runtime */
+#if MONO_SIZEOF_SUNPATH==0
+#error configure failed to discover size of unix socket path
+#endif
+
 /*
  * startup:
  *
- * Bind signals and attach to shared memory
+ * Bind signals, attach to shared memory and set up any internal data
+ * structures needed.
  */
 static void startup (void)
 {
@@ -234,6 +258,10 @@ static void startup (void)
                  "mono-handle-daemon-%d-%d-%ld", getuid (), getpid (),
                  time (NULL));
 #endif
+
+       file_share_hash = g_hash_table_new_full (sharedata_hash,
+                                                sharedata_equal, g_free,
+                                                g_free);
 }
 
 
@@ -258,12 +286,9 @@ static void ref_handle (ChannelData *channel_data, guint32 handle)
        _wapi_shared_data[segment]->handles[idx].ref++;
        channel_data->open_handles[handle]++;
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": handle 0x%x ref now %d (%d this process)", handle,
-                  _wapi_shared_data[segment]->handles[idx].ref,
-                  channel_data->open_handles[handle]);
-#endif
+       LOGDEBUG ("%s: handle 0x%x ref now %d (%d this process)", __func__, handle,
+                 _wapi_shared_data[segment]->handles[idx].ref,
+                 channel_data->open_handles[handle]);
 }
 
 /*
@@ -285,9 +310,7 @@ static gboolean unref_handle (ChannelData *channel_data, guint32 handle)
        }
        
        if (channel_data->open_handles[handle] == 0) {
-                g_warning(G_GNUC_PRETTY_FUNCTION
-                          ": unref on %d called when ref was already 0", 
-                          handle);
+                g_warning("%s: unref on %d called when ref was already 0", __func__, handle);
                 return TRUE;
         }
 
@@ -296,28 +319,45 @@ static gboolean unref_handle (ChannelData *channel_data, guint32 handle)
        _wapi_shared_data[segment]->handles[idx].ref--;
        channel_data->open_handles[handle]--;
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION
-                  ": handle 0x%x ref now %d (%d this process)", handle,
+       LOGDEBUG ("%s: handle 0x%x ref now %d (%d this process)", __func__, handle,
                   _wapi_shared_data[segment]->handles[idx].ref,
                   channel_data->open_handles[handle]);
-#endif
 
-       if(channel_data->open_handles[handle]==0) {
+       if (_wapi_shared_data[segment]->handles[idx].ref == 0) {
+               gboolean was_file;
+               dev_t device = 0;
+               ino_t inode = 0;
+               
                /* This client has released the handle */
                destroy=TRUE;
-       }
        
-       if(_wapi_shared_data[segment]->handles[idx].ref==0) {
                if (channel_data->open_handles[handle]!=0) {
-                       g_warning (G_GNUC_PRETTY_FUNCTION ": per-process open_handles mismatch, set to %d, should be 0", 
-                                       channel_data->open_handles[handle]);
+                       g_warning ("%s: per-process open_handles mismatch, set to %d, should be 0",__func__, channel_data->open_handles[handle]);
                }
                
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Destroying handle 0x%x",
-                          handle);
-#endif
+               LOGDEBUG ("%s: Destroying handle 0x%x", __func__, handle);
+
+               /* if this was a file handle, save the device and
+                * inode numbers so we can scan the share info data
+                * later to see if the last handle to a file has been
+                * closed, and delete the data if so.
+                */
+               was_file = (_wapi_shared_data[segment]->handles[idx].type == WAPI_HANDLE_FILE);
+               if (was_file) {
+                       struct _WapiHandle_file *file_handle;
+                       gboolean ok;
+                       
+                       ok = _wapi_lookup_handle (GUINT_TO_POINTER (handle),
+                                                 WAPI_HANDLE_FILE,
+                                                 (gpointer *)&file_handle,
+                                                 NULL);
+                       if (ok == FALSE) {
+                               g_warning ("%s: error looking up file handle %x", __func__, handle);
+                       } else {
+                               device = file_handle->device;
+                               inode = file_handle->inode;
+                       }
+               }
                
                _wapi_handle_ops_close_shared (GUINT_TO_POINTER (handle));
                
@@ -328,6 +368,10 @@ static gboolean unref_handle (ChannelData *channel_data, guint32 handle)
 
                memset (&_wapi_shared_data[segment]->handles[idx].u, '\0', sizeof(_wapi_shared_data[segment]->handles[idx].u));
                _wapi_shared_data[segment]->handles[idx].type=WAPI_HANDLE_UNUSED;
+
+               if (was_file) {
+                       check_sharing (device, inode);
+               }
        }
 
        if(channel_data == daemon_channel_data) {
@@ -415,14 +459,18 @@ static void rem_fd(GIOChannel *channel, ChannelData *channel_data)
        
        if(fd == main_sock) {
                /* We shouldn't be deleting the daemon's fd */
-               g_warning (G_GNUC_PRETTY_FUNCTION ": Deleting daemon fd!");
+               g_warning ("%s: Deleting daemon fd!", __func__);
                cleanup ();
                exit (-1);
        }
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Removing client fd %d", fd);
-#endif
+       LOGDEBUG ("%s: Removing client fd %d", __func__, fd);
+
+       if (channel_data->io_source == 0) {
+               LOGDEBUG ("%s: channel already closed for fd %d", __func__, fd);
+               return;
+       }
+
 
        g_io_channel_shutdown (channel, TRUE, NULL);
        g_source_remove (channel_data->io_source);
@@ -434,9 +482,7 @@ static void rem_fd(GIOChannel *channel, ChannelData *channel_data)
                handle_count=channel_data->open_handles[i];
                
                for(j=0; j<handle_count; j++) {
-#ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": closing handle 0x%x for client at index %d", i, g_io_channel_unix_get_fd (channel));
-#endif
+                       LOGDEBUG ("%s: closing handle 0x%x for client at index %d", __func__, i, g_io_channel_unix_get_fd (channel));
                        /* Ignore the hint to the client to destroy
                         * the handle private data
                         */
@@ -457,6 +503,96 @@ static void rem_fd(GIOChannel *channel, ChannelData *channel_data)
        }
 }
 
+static void sharemode_set (dev_t device, ino_t inode, guint32 sharemode,
+                          guint32 access)
+{
+       ShareKey *sharekey;
+       ShareData *sharedata;
+       
+       sharekey = g_new (ShareKey, 1);
+       sharekey->device = device;
+       sharekey->inode = inode;
+
+       sharedata = g_new (ShareData, 1);
+       sharedata->sharemode = sharemode;
+       sharedata->access = access;
+       
+       /* Setting share mode to include all access bits is really
+        * removing the share info
+        */
+       if (sharemode == (FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE)) {
+               g_hash_table_remove (file_share_hash, sharekey);
+       } else {
+               g_hash_table_insert (file_share_hash, sharekey, sharedata);
+       }
+}
+
+static gboolean sharemode_get (dev_t device, ino_t inode, guint32 *sharemode,
+                              guint32 *access)
+{
+       ShareKey sharekey;
+       ShareData *sharedata;
+       
+       sharekey.device = device;
+       sharekey.inode = inode;
+       
+       sharedata = (ShareData *)g_hash_table_lookup (file_share_hash,
+                                                      &sharekey);
+       if (sharedata == NULL) {
+               return(FALSE);
+       }
+       
+       *sharemode = sharedata->sharemode;
+       *access = sharedata->access;
+       
+       return(TRUE);
+}
+
+static gboolean share_compare (gpointer handle, gpointer user_data)
+{
+       struct _WapiHandle_file *file_handle;
+       gboolean ok;
+       ShareKey *sharekey = (ShareKey *)user_data;
+       
+       ok = _wapi_lookup_handle (handle, WAPI_HANDLE_FILE,
+                                 (gpointer *)&file_handle, NULL);
+       if (ok == FALSE) {
+               g_warning ("%s: error looking up file handle %p", __func__, handle);
+               return(FALSE);
+       }
+       
+       if (file_handle->device == sharekey->device &&
+           file_handle->inode == sharekey->inode) {
+               LOGDEBUG ("%s: found one, handle %p", __func__, handle);
+               return(TRUE);
+       } else {
+               return(FALSE);
+       }
+}
+
+static void check_sharing (dev_t device, ino_t inode)
+{
+       ShareKey sharekey;
+       gpointer file_handle;
+       
+       LOGDEBUG ("%s: Checking if anything has (dev 0x%llx, inode %lld) still open", __func__, device, inode);
+
+       sharekey.device = device;
+       sharekey.inode = inode;
+       
+       file_handle = _wapi_search_handle (WAPI_HANDLE_FILE, share_compare,
+                                          &sharekey, NULL, NULL);
+
+       if (file_handle == NULL) {
+               /* Delete this share info, as the last handle to it
+                * has been closed
+                */
+               LOGDEBUG ("%s: Deleting share data for (dev 0x%llx inode %lld)", __func__, device, inode);
+               
+               g_hash_table_remove (file_share_hash, &sharekey);
+       }
+}
+
 static gboolean process_compare (gpointer handle, gpointer user_data)
 {
        struct _WapiHandle_process *process_handle;
@@ -467,8 +603,7 @@ static gboolean process_compare (gpointer handle, gpointer user_data)
        ok=_wapi_lookup_handle (handle, WAPI_HANDLE_PROCESS,
                                (gpointer *)&process_handle, NULL);
        if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up process handle %p", handle);
+               g_warning ("%s: error looking up process handle %p", __func__, handle);
                return(FALSE);
        }
 
@@ -494,8 +629,7 @@ static gboolean process_thread_compare (gpointer handle, gpointer user_data)
        ok=_wapi_lookup_handle (handle, WAPI_HANDLE_THREAD,
                                (gpointer *)&thread_handle, NULL);
        if(ok==FALSE) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": error looking up thread handle %p", handle);
+               g_warning ("%s: error looking up thread handle %p", __func__, handle);
                return(FALSE);
        }
 
@@ -504,9 +638,7 @@ static gboolean process_thread_compare (gpointer handle, gpointer user_data)
                 * _wapi_handle_set_signal_state() unless we have
                 * process-shared pthread support.
                 */
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": Set thread handle %p signalled, because its process died", handle);
-#endif
+               LOGDEBUG ("%s: Set thread handle %p signalled, because its process died", __func__, handle);
 
                thread_handle->exitstatus=0;
 
@@ -549,8 +681,11 @@ static void process_post_mortem (pid_t pid, int status)
                                            (gpointer *)&process_handle_data,
                                            NULL);
        if(process_handle==0) {
-               g_warning (G_GNUC_PRETTY_FUNCTION
-                          ": Couldn't find handle for process %d!", pid);
+               /*
+                * This may happen if we use Process.EnableRaisingEvents +
+                * process.Exited event and the parent has finished.
+                */
+               LOGDEBUG ("%s: Couldn't find handle for process %d!", __func__, pid);
        } else {
                /* Signal the handle.  Don't use
                 * _wapi_handle_set_signal_state() unless we have
@@ -558,17 +693,17 @@ static void process_post_mortem (pid_t pid, int status)
                 */
                struct timeval tv;
                
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": Set process %d exitstatus to %d", pid,
+               LOGDEBUG ("%s: Set process %d exitstatus to %d", __func__, pid,
                           WEXITSTATUS (status));
-#endif
                
-               /* Technically WEXITSTATUS is only valid if the
-                * process exited normally, but I don't care if the
-                * process caught a signal or not.
+               /* If the child terminated due to the receipt of a signal,
+                * the exit status must be based on WTERMSIG, since WEXITSTATUS
+                * returns 0 in this case.
                 */
-               process_handle_data->exitstatus=WEXITSTATUS (status);
+               if (WIFSIGNALED(status))
+                       process_handle_data->exitstatus=128 + WTERMSIG (status);
+               else
+                       process_handle_data->exitstatus=WEXITSTATUS (status);
 
                /* Ignore errors */
                gettimeofday (&tv, NULL);
@@ -611,9 +746,7 @@ static void process_died (void)
        
        check_processes=FALSE;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Reaping processes");
-#endif
+       LOGDEBUG ("%s: Reaping processes", __func__);
 
        while(TRUE) {
                pid=waitpid (-1, &status, WNOHANG);
@@ -626,9 +759,7 @@ static void process_died (void)
                        return;
                } else {
                        /* pid contains the ID of a dead process */
-#ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": process %d reaped", pid);
-#endif
+                       LOGDEBUG ( "%s: process %d reaped", __func__, pid);
                        process_post_mortem (pid, status);
                }
        }
@@ -648,23 +779,11 @@ static void send_reply (GIOChannel *channel, WapiHandleResponse *resp)
        _wapi_daemon_response (g_io_channel_unix_get_fd (channel), resp);
 }
 
-/*
- * process_new:
- * @channel: The client making the request
- * @channel_data: Our data for this channel
- * @type: type to init handle to
- *
- * Find a free handle and initialize it to 'type', increase refcnt and
- * send back a reply to the client.
- */
-static void process_new (GIOChannel *channel, ChannelData *channel_data,
-                        WapiHandleType type)
+static guint32 new_handle_with_shared_check (WapiHandleType type)
 {
-       guint32 handle;
-       WapiHandleResponse resp={0};
-       
-       handle=_wapi_handle_new_internal (type);
-       if(handle==0) {
+       guint32 handle = 0;
+
+       while ((handle = _wapi_handle_new_internal (type)) == 0) {
                /* Try and allocate a new shared segment, and have
                 * another go
                 */
@@ -689,25 +808,41 @@ static void process_new (GIOChannel *channel, ChannelData *channel_data,
                                        channels[i].open_handles=_wapi_g_renew0 (channels[i].open_handles, old_len, new_len);
                                }
                        }
-
-                       handle=_wapi_handle_new_internal (type);
                } else {
                        /* Map failed.  Just return 0 meaning "out of
                         * handles"
                         */
+                       break;
                }
        }
        
+       return(handle);
+}
+
+/*
+ * process_new:
+ * @channel: The client making the request
+ * @channel_data: Our data for this channel
+ * @type: type to init handle to
+ *
+ * Find a free handle and initialize it to 'type', increase refcnt and
+ * send back a reply to the client.
+ */
+static void process_new (GIOChannel *channel, ChannelData *channel_data,
+                        WapiHandleType type)
+{
+       guint32 handle;
+       WapiHandleResponse resp={0};
+       
+       handle = new_handle_with_shared_check (type);
+       
        /* handle might still be set to 0.  This is handled at the
         * client end
         */
 
        ref_handle (channel_data, handle);
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": returning new handle 0x%x",
-                  handle);
-#endif
+       LOGDEBUG ("%s: returning new handle 0x%x", __func__, handle);
 
        resp.type=WapiHandleResponseType_New;
        resp.u.new.type=type;
@@ -738,10 +873,7 @@ static void process_open (GIOChannel *channel, ChannelData *channel_data,
        if(shared->type!=WAPI_HANDLE_UNUSED && handle!=0) {
                ref_handle (channel_data, handle);
 
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION
-                          ": returning new handle 0x%x", handle);
-#endif
+               LOGDEBUG ("%s: returning new handle 0x%x", __func__, handle);
 
                resp.type=WapiHandleResponseType_Open;
                resp.u.new.type=shared->type;
@@ -775,10 +907,8 @@ static void process_close (GIOChannel *channel, ChannelData *channel_data,
        resp.type=WapiHandleResponseType_Close;
        resp.u.close.destroy=unref_handle (channel_data, handle);
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": unreffing handle 0x%x", handle);
-#endif
-                       
+       LOGDEBUG ("%s: unreffing handle 0x%x", __func__, handle);
+
        send_reply (channel, &resp);
 }
 
@@ -795,10 +925,8 @@ static void process_scratch (GIOChannel *channel, guint32 length)
        
        resp.type=WapiHandleResponseType_Scratch;
        resp.u.scratch.idx=_wapi_handle_scratch_store_internal (length, &resp.u.scratch.remap);
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": allocating scratch index 0x%x",
-                  resp.u.scratch.idx);
-#endif
+
+       LOGDEBUG ("%s: allocating scratch index 0x%x", __func__, resp.u.scratch.idx);
                        
        send_reply (channel, &resp);
 }
@@ -817,18 +945,39 @@ static void process_scratch_free (GIOChannel *channel, guint32 scratch_idx)
        resp.type=WapiHandleResponseType_ScratchFree;
        _wapi_handle_scratch_delete_internal (scratch_idx);
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": deleting scratch index 0x%x",
-                  scratch_idx);
-#endif
+       LOGDEBUG ("%s: deleting scratch index 0x%x", __func__, scratch_idx);
                        
        send_reply (channel, &resp);
 }
 
+/*
+ * process_process_kill:
+ * @channel: The client making the request
+ * @process_kill: pid and signal to send to the pid.
+ *
+ * Sends the specified signal to the process.
+ */
+static void
+process_process_kill (GIOChannel *channel,
+                     WapiHandleRequest_ProcessKill process_kill)
+{
+       WapiHandleResponse resp = {0};
+
+       resp.type = WapiHandleResponseType_ProcessKill;
+
+       LOGDEBUG ("%s: kill (%d, %d)", __func__, process_kill.pid, process_kill.signo);
+
+       if (kill (process_kill.pid, process_kill.signo) == -1) {
+               resp.u.process_kill.err = errno;
+               LOGDEBUG ("%s: kill (%d, %d) failed: %d", __func__, process_kill.pid, process_kill.signo, resp.u.process_kill.err);
+       }
+
+       send_reply (channel, &resp);
+}
+
 /*
  * process_process_fork:
  * @channel: The client making the request
- * @open_handles: An array of handles referenced by this client
  * @process_fork: Describes the process to fork
  * @fds: stdin, stdout, and stderr for the new process
  *
@@ -843,7 +992,7 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
        guint32 process_handle, thread_handle;
        struct _WapiHandle_process *process_handle_data;
        struct _WapiHandle_thread *thread_handle_data;
-       pid_t pid;
+       pid_t pid = 0;
        
        resp.type=WapiHandleResponseType_ProcessFork;
        
@@ -853,11 +1002,11 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
         * client must check if either handle is 0 and take
         * appropriate error handling action.
         */
-       process_handle=_wapi_handle_new_internal (WAPI_HANDLE_PROCESS);
+       process_handle = new_handle_with_shared_check (WAPI_HANDLE_PROCESS);
        ref_handle (daemon_channel_data, process_handle);
        ref_handle (channel_data, process_handle);
        
-       thread_handle=_wapi_handle_new_internal (WAPI_HANDLE_THREAD);
+       thread_handle = new_handle_with_shared_check (WAPI_HANDLE_THREAD);
        ref_handle (daemon_channel_data, thread_handle);
        ref_handle (channel_data, thread_handle);
        
@@ -886,6 +1035,16 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
                cmd=_wapi_handle_scratch_lookup (process_fork.cmd);
                dir=_wapi_handle_scratch_lookup (process_fork.dir);
                env=_wapi_handle_scratch_lookup_string_array (process_fork.env);
+
+               _wapi_lookup_handle (GUINT_TO_POINTER (process_handle),
+                                    WAPI_HANDLE_PROCESS,
+                                    (gpointer *)&process_handle_data,
+                                    NULL);
+
+               _wapi_lookup_handle (GUINT_TO_POINTER (thread_handle),
+                                    WAPI_HANDLE_THREAD,
+                                    (gpointer *)&thread_handle_data,
+                                    NULL);
                
                ret=g_shell_parse_argv (cmd, NULL, &argv, &gerr);
                if(ret==FALSE) {
@@ -894,19 +1053,7 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
                         */
                        process_handle_data->exec_errno=gerr->code;
                } else {
-#ifdef DEBUG
-                       g_message (G_GNUC_PRETTY_FUNCTION ": forking");
-#endif
-
-                       _wapi_lookup_handle (GUINT_TO_POINTER (process_handle),
-                                            WAPI_HANDLE_PROCESS,
-                                            (gpointer *)&process_handle_data,
-                                            NULL);
-
-                       _wapi_lookup_handle (GUINT_TO_POINTER (thread_handle),
-                                            WAPI_HANDLE_THREAD,
-                                            (gpointer *)&thread_handle_data,
-                                            NULL);
+                       LOGDEBUG ("%s: forking", __func__);
 
                        /* Fork, exec cmd with args and optional env,
                         * and return the handles with pid and blank
@@ -934,7 +1081,7 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
                                }
                                
                                /* Close all file descriptors */
-                               for(i=3; i<getdtablesize (); i++) {
+                               for (i = getdtablesize () - 1; i > 2; i--) {
                                        close (i);
                                }
                        
@@ -958,21 +1105,17 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
                                }
 
 #ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION
-                                          ": exec()ing [%s] in dir [%s]",
-                                          cmd, dir);
+                               LOGDEBUG ("%s: exec()ing [%s] in dir [%s]", __func__, cmd, dir);
                                {
                                        i=0;
                                        while(argv[i]!=NULL) {
-                                               g_message ("arg %d: [%s]",
-                                                          i, argv[i]);
+                                               LOGDEBUG ("arg %d: [%s]", i, argv[i]);
                                                i++;
                                        }
 
                                        i=0;
                                        while(env[i]!=NULL) {
-                                               g_message ("env %d: [%s]",
-                                                          i, env[i]);
+                                               LOGDEBUG ("env %d: [%s]", i, env[i]);
                                                i++;
                                        }
                                }
@@ -996,12 +1139,20 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
 
                /* store process name, based on the last section of the cmd */
                {
-                       char *slash=strrchr (argv[0], '/');
+                       char *slash;
                        
-                       if(slash!=NULL) {
-                               process_handle_data->proc_name=_wapi_handle_scratch_store (slash+1, strlen (slash+1));
+                       /* This should never fail, but it seems it can...
+                        */
+                       if (argv[0] != NULL) {
+                               slash=strrchr (argv[0], '/');
+                       
+                               if(slash!=NULL) {
+                                       process_handle_data->proc_name=_wapi_handle_scratch_store (slash+1, strlen (slash+1));
+                               } else {
+                                       process_handle_data->proc_name=_wapi_handle_scratch_store (argv[0], strlen (argv[0]));
+                               }
                        } else {
-                               process_handle_data->proc_name=_wapi_handle_scratch_store (argv[0], strlen (argv[0]));
+                               process_handle_data->proc_name = _wapi_handle_scratch_store (cmd, strlen(cmd));
                        }
                }
                
@@ -1035,22 +1186,78 @@ static void process_process_fork (GIOChannel *channel, ChannelData *channel_data
 
                resp.u.process_fork.pid=pid;
        }
-                       
+
        resp.u.process_fork.process_handle=process_handle;
        resp.u.process_fork.thread_handle=thread_handle;
 
        send_reply (channel, &resp);
 }
 
+/*
+ * process_set_share:
+ * @channel: The client making the request
+ * @channel_data: The channel data
+ * @set_share: Set share data passed from the client
+ *
+ * Sets file share info
+ */
+static void process_set_share (GIOChannel *channel, ChannelData *channel_data,
+                              WapiHandleRequest_SetShare set_share)
+{
+       WapiHandleResponse resp = {0};
+
+       resp.type = WapiHandleResponseType_SetShare;
+       
+       LOGDEBUG ("%s: Setting share for file (dev:0x%llx, ino:%lld) mode 0x%x access 0x%x", __func__, set_share.device, set_share.inode, set_share.sharemode, set_share.access);
+       
+       sharemode_set (set_share.device, set_share.inode, set_share.sharemode,
+                      set_share.access);
+       
+       send_reply (channel, &resp);
+}
+
+/*
+ * process_get_or_set_share:
+ * @channel: The client making the request
+ * @channel_data: The channel data
+ * @get_share: GetOrSetShare data passed from the client
+ *
+ * Gets a file share status, and sets the status if it doesn't already
+ * exist
+ */
+static void process_get_or_set_share (GIOChannel *channel,
+                                     ChannelData *channel_data,
+                                     WapiHandleRequest_GetOrSetShare get_share)
+{
+       WapiHandleResponse resp = {0};
+       
+       resp.type = WapiHandleResponseType_GetOrSetShare;
+       
+       LOGDEBUG ("%s: Getting share status for file (dev:0x%llx, ino:%lld)", __func__, get_share.device, get_share.inode);
+
+       resp.u.get_or_set_share.exists = sharemode_get (get_share.device, get_share.inode, &resp.u.get_or_set_share.sharemode, &resp.u.get_or_set_share.access);
+       
+       if (resp.u.get_or_set_share.exists) {
+               LOGDEBUG ("%s: Share mode: 0x%x", __func__, resp.u.get_or_set_share.sharemode);
+       } else {
+               LOGDEBUG ("%s: file share info not already known, setting", __func__);
+               sharemode_set (get_share.device, get_share.inode,
+                              get_share.new_sharemode, get_share.new_access);
+       }
+       
+       send_reply (channel, &resp);
+}
+
 /*
  * read_message:
  * @channel: The client to read the request from
  * @open_handles: An array of handles referenced by this client
  *
  * Read a message (A WapiHandleRequest) from a client and dispatch
- * whatever it wants to the process_* calls.
+ * whatever it wants to the process_* calls.  Return TRUE if the message
+ * was read successfully, FALSE otherwise.
  */
-static void read_message (GIOChannel *channel, ChannelData *channel_data)
+static gboolean read_message (GIOChannel *channel, ChannelData *channel_data)
 {
        WapiHandleRequest req;
        int fds[3]={0, 1, 2};
@@ -1062,17 +1269,13 @@ static void read_message (GIOChannel *channel, ChannelData *channel_data)
                                  fds, &has_fds);
        if(ret==0) {
                /* Other end went away */
-#ifdef DEBUG
-               g_message ("Read 0 bytes on fd %d, closing it",
+               LOGDEBUG ("Read 0 bytes on fd %d, closing it",
                           g_io_channel_unix_get_fd (channel));
-#endif
                rem_fd (channel, channel_data);
-               return;
+               return(FALSE);
        }
        
-#ifdef DEBUG
-       g_message ("Process request %d", req.type);
-#endif
+       LOGDEBUG ("Process request %d", req.type);
        switch(req.type) {
        case WapiHandleRequestType_New:
                process_new (channel, channel_data, req.u.new.type);
@@ -1099,6 +1302,16 @@ static void read_message (GIOChannel *channel, ChannelData *channel_data)
                process_process_fork (channel, channel_data,
                                      req.u.process_fork, fds);
                break;
+       case WapiHandleRequestType_ProcessKill:
+               process_process_kill (channel, req.u.process_kill);
+               break;
+       case WapiHandleRequestType_SetShare:
+               process_set_share (channel, channel_data, req.u.set_share);
+               break;
+       case WapiHandleRequestType_GetOrSetShare:
+               process_get_or_set_share (channel, channel_data,
+                                         req.u.get_or_set_share);
+               break;
        case WapiHandleRequestType_Error:
                /* fall through */
        default:
@@ -1108,16 +1321,16 @@ static void read_message (GIOChannel *channel, ChannelData *channel_data)
        }
 
        if(has_fds==TRUE) {
-#ifdef DEBUG
-               g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[0]);
-               g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[1]);
-               g_message (G_GNUC_PRETTY_FUNCTION ": closing %d", fds[2]);
-#endif
+               LOGDEBUG ("%s: closing %d", __func__, fds[0]);
+               LOGDEBUG ("%s: closing %d", __func__, fds[1]);
+               LOGDEBUG ("%s: closing %d", __func__, fds[2]);
                
                close (fds[0]);
                close (fds[1]);
                close (fds[2]);
        }
+
+       return(TRUE);
 }
 
 /*
@@ -1137,15 +1350,12 @@ static gboolean fd_activity (GIOChannel *channel, GIOCondition condition,
        GMainContext *context=data;
        
        if(condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
-#ifdef DEBUG
-               g_message ("fd %d error", fd);
-#endif
-
+               LOGDEBUG ("fd %d error", fd);
                rem_fd (channel, channel_data);
                return(FALSE);
        }
 
-       if(condition & (G_IO_IN | G_IO_PRI)) {
+       if(condition & (_IO_PRI)) {
                if(fd==main_sock) {
                        int newsock;
                        struct sockaddr addr;
@@ -1153,23 +1363,17 @@ static gboolean fd_activity (GIOChannel *channel, GIOCondition condition,
                        
                        newsock=accept (main_sock, &addr, &addrlen);
                        if(newsock==-1) {
-                               g_critical ("accept error: %s",
-                                           g_strerror (errno));
+                               g_critical ("%s accept error: %s", __func__, g_strerror (errno));
                                cleanup ();
                                exit (-1);
                        }
 
-#ifdef DEBUG
-                       g_message ("accept returning %d", newsock);
-#endif
-
+                       LOGDEBUG ("accept returning %d", newsock);
                        add_fd (newsock, context);
                } else {
-#ifdef DEBUG
-                       g_message ("reading data on fd %d", fd);
-#endif
+                       LOGDEBUG ("reading data on fd %d", fd);
 
-                       read_message (channel, channel_data);
+                       return(read_message (channel, channel_data));
                }
                return(TRUE);
        }
@@ -1189,9 +1393,7 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
        int ret;
        GMainContext *context;
 
-#ifdef DEBUG
-       g_message ("Starting up...");
-#endif
+       LOGDEBUG ("Starting up...");
 
        _wapi_shared_data[0]=data;
        _wapi_shared_scratch=scratch;
@@ -1200,6 +1402,9 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
        /* Note that we've got the starting segment already */
        _wapi_shared_data[0]->num_segments=1;
        _wapi_shm_mapped_segments=1;
+
+       _wapi_fd_offset_table_size=getdtablesize ();
+       _wapi_shared_data[0]->fd_offset_table_size = _wapi_fd_offset_table_size;
        
        startup ();
        
@@ -1217,9 +1422,7 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
                exit(-1);
        }
 
-#ifdef DEBUG
-       g_message("bound");
-#endif
+       LOGDEBUG("bound");
 
        ret=listen(main_sock, 5);
        if(ret==-1) {
@@ -1227,10 +1430,7 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
                _wapi_shared_data[0]->daemon_running=DAEMON_DIED_AT_STARTUP;
                exit(-1);
        }
-
-#ifdef DEBUG
-       g_message("listening");
-#endif
+       LOGDEBUG("listening");
 
        context = g_main_context_new ();
 
@@ -1247,9 +1447,7 @@ void _wapi_daemon_main(gpointer data, gpointer scratch)
                        process_died ();
                }
                
-#ifdef DEBUG
-               g_message ("polling");
-#endif
+               LOGDEBUG ("polling");
 
                /* Block until something happens. We don't use
                 * g_main_loop_run() because we rely on the SIGCHLD