+#else
+ return(FALSE);
+#endif
+}
+
+gboolean
+_wapi_handle_process_kill (pid_t process, guint32 signo, gint *errnum)
+{
+#if 0
+ WapiHandleRequest killproc = {0};
+ WapiHandleResponse killprocresp = {0};
+ gint result;
+
+ if (shared != TRUE) {
+ if (errnum) *errnum = EINVAL;
+ return FALSE;
+ }
+
+ killproc.type = WapiHandleRequestType_ProcessKill;
+ killproc.u.process_kill.pid = process;
+ killproc.u.process_kill.signo = signo;
+
+ _wapi_daemon_request_response (daemon_sock, &killproc, &killprocresp);
+
+ if (killprocresp.type != WapiHandleResponseType_ProcessKill) {
+ g_warning ("%s: bogus daemon response, type %d", __func__,
+ killprocresp.type);
+ g_assert_not_reached ();
+ }
+
+ result = killprocresp.u.process_kill.err;
+ if (result != 0 && errnum != NULL)
+ *errnum = (result == FALSE) ? result : 0;
+
+ return (result == 0);
+#else
+ return(FALSE);
+#endif
+}
+
+gboolean _wapi_handle_get_or_set_share (dev_t device, ino_t inode,
+ guint32 new_sharemode,
+ guint32 new_access,
+ guint32 *old_sharemode,
+ guint32 *old_access,
+ struct _WapiFileShare **share_info)
+{
+ struct _WapiFileShare *file_share;
+ guint32 now = (guint32)(time(NULL) & 0xFFFFFFFF);
+ int thr_ret, i, first_unused = -1;
+ gboolean exists = FALSE;
+
+ /* Marking this as COLLECTION_UNSAFE prevents entries from
+ * expiring under us as we search
+ */
+ _WAPI_HANDLE_COLLECTION_UNSAFE;
+
+ /* Prevent new entries racing with us */
+ do {
+ now = (guint32)(time(NULL) & 0xFFFFFFFF);
+
+ thr_ret = _wapi_timestamp_exclusion (&_wapi_fileshare_layout->share_check, now);
+ if (thr_ret == EBUSY) {
+ _wapi_handle_spin (100);
+ }
+ } while (thr_ret == EBUSY);
+ g_assert (thr_ret == 0);
+
+ /* If a linear scan gets too slow we'll have to fit a hash
+ * table onto the shared mem backing store
+ */
+ *share_info = NULL;
+ for (i = 0; i <= _wapi_fileshare_layout->hwm; i++) {
+ file_share = &_wapi_fileshare_layout->share_info[i];
+
+ /* Make a note of an unused slot, in case we need to
+ * store share info
+ */
+ if (first_unused == -1 && file_share->handle_refs == 0) {
+ first_unused = i;
+ continue;
+ }
+
+ if (file_share->handle_refs == 0) {
+ continue;
+ }
+
+ if (file_share->device == device &&
+ file_share->inode == inode) {
+ *old_sharemode = file_share->sharemode;
+ *old_access = file_share->access;
+ *share_info = file_share;
+
+ /* Increment the reference count while we
+ * still have sole access to the shared area.
+ * This makes the increment atomic wrt
+ * collections
+ */
+ InterlockedIncrement (&file_share->handle_refs);
+
+ exists = TRUE;
+ break;
+ }
+ }
+
+ if (!exists) {
+ if (i == _WAPI_FILESHARE_SIZE && first_unused == -1) {
+ /* No more space */
+ } else {
+ if (first_unused == -1) {
+ file_share = &_wapi_fileshare_layout->share_info[++i];
+ _wapi_fileshare_layout->hwm = i;
+ } else {
+ file_share = &_wapi_fileshare_layout->share_info[first_unused];
+ }
+
+ file_share->device = device;
+ file_share->inode = inode;
+ file_share->sharemode = new_sharemode;
+ file_share->access = new_access;
+ file_share->handle_refs = 1;
+ *share_info = file_share;
+ }
+ }
+
+ if (*share_info != NULL) {
+ InterlockedExchange (&(*share_info)->timestamp, now);
+ }
+
+ thr_ret = _wapi_timestamp_release (&_wapi_fileshare_layout->share_check, now);
+
+ _WAPI_HANDLE_COLLECTION_SAFE;
+
+ return(exists);
+}
+
+/* Scan /proc/<pids>/fd/ for open file descriptors to the file in
+ * question. If there are none, reset the share info.
+ *
+ * This implementation is Linux-specific; legacy systems will have to
+ * implement their own ways of finding out if a particular file is
+ * open by a process.
+ */
+void _wapi_handle_check_share (struct _WapiFileShare *share_info)
+{
+ DIR *proc_dir;
+ struct dirent *proc_entry;
+ gboolean found = FALSE;
+ pid_t self = getpid();
+ int pid;
+ guint32 now = (guint32)(time(NULL) & 0xFFFFFFFF);
+ int thr_ret;
+
+ proc_dir = opendir ("/proc");
+ if (proc_dir == NULL) {
+ return;
+ }
+
+ /* Marking this as COLLECTION_UNSAFE prevents entries from
+ * expiring under us if we remove this one
+ */
+ _WAPI_HANDLE_COLLECTION_UNSAFE;
+
+ /* Prevent new entries racing with us */
+ do {
+ now = (guint32)(time(NULL) & 0xFFFFFFFF);
+
+ thr_ret = _wapi_timestamp_exclusion (&_wapi_fileshare_layout->share_check, now);
+ if (thr_ret == EBUSY) {
+ _wapi_handle_spin (100);
+ }
+ } while (thr_ret == EBUSY);
+ g_assert (thr_ret == 0);
+
+ while ((proc_entry = readdir (proc_dir)) != NULL) {
+ /* We only care about numerically-named directories */
+ pid = atoi (proc_entry->d_name);
+ if (pid != 0 && pid != self) {
+ /* Look in /proc/<pid>/fd/ but ignore
+ * ourselves, as we have the file open too
+ */
+ DIR *fd_dir;
+ struct dirent *fd_entry;
+ char subdir[_POSIX_PATH_MAX];
+
+ g_snprintf (subdir, _POSIX_PATH_MAX, "/proc/%d/fd",
+ pid);
+
+ fd_dir = opendir (subdir);
+ if (fd_dir == NULL) {
+ continue;
+ }
+
+ while ((fd_entry = readdir (fd_dir)) != NULL) {
+ char path[_POSIX_PATH_MAX];
+ struct stat link_stat;
+
+ if (!strcmp (fd_entry->d_name, ".") ||
+ !strcmp (fd_entry->d_name, "..")) {
+ continue;
+ }
+
+ g_snprintf (path, _POSIX_PATH_MAX,
+ "/proc/%d/fd/%s", pid,
+ fd_entry->d_name);
+
+ stat (path, &link_stat);
+ if (link_stat.st_dev == share_info->device &&
+ link_stat.st_ino == share_info->inode) {
+#ifdef DEBUG
+ g_message ("%s: Found it at %s",
+ __func__, path);
+#endif
+
+ found = TRUE;
+ }
+ }
+
+ closedir (fd_dir);
+ }
+ }
+
+ closedir (proc_dir);
+
+ if (found == FALSE) {
+ /* Blank out this entry, as it is stale */
+#ifdef DEBUG
+ g_message ("%s: Didn't find it, destroying entry", __func__);
+#endif
+
+ memset (share_info, '\0', sizeof(struct _WapiFileShare));
+ }
+
+ thr_ret = _wapi_timestamp_release (&_wapi_fileshare_layout->share_check, now);
+
+ _WAPI_HANDLE_COLLECTION_SAFE;
+}
+
+void _wapi_handle_dump (void)
+{
+ struct _WapiHandleUnshared *handle_data;
+ guint32 i;
+
+ for (i = 0; i < _wapi_private_handle_count; i++) {
+ handle_data = &_wapi_private_handles[i];
+
+ if (handle_data->type == WAPI_HANDLE_UNUSED) {
+ continue;
+ }
+
+ g_print ("%3x [%7s] %s %d ", i,
+ _wapi_handle_typename[handle_data->type],
+ handle_data->signalled?"Sg":"Un", handle_data->ref);
+ handle_details[handle_data->type](&handle_data->u);
+ g_print ("\n");
+ }
+}
+
+static void _wapi_shared_details (gpointer handle_info)
+{
+ struct _WapiHandle_shared_ref *shared = (struct _WapiHandle_shared_ref *)handle_info;
+
+ g_print ("offset: 0x%x", shared->offset);
+}
+
+void _wapi_handle_update_refs (void)
+{
+ guint32 i, lock_now;
+ int thr_ret;
+
+ _WAPI_HANDLE_COLLECTION_UNSAFE;
+
+ /* Prevent file share entries racing with us */
+ do {
+ lock_now = (guint32)(time(NULL) & 0xFFFFFFFF);
+
+ thr_ret = _wapi_timestamp_exclusion (&_wapi_fileshare_layout->share_check, lock_now);
+
+ if (thr_ret == EBUSY) {
+ _wapi_handle_spin (100);
+ }
+ } while (thr_ret == EBUSY);
+ g_assert(thr_ret == 0);
+
+ for (i = 0; i < _wapi_private_handle_count; i++) {
+ struct _WapiHandleUnshared *handle = &_wapi_private_handles[i];
+ guint32 now = (guint32)(time (NULL) & 0xFFFFFFFF);
+
+ if (_WAPI_SHARED_HANDLE(handle->type)) {
+ struct _WapiHandleSharedMetadata *shared_meta;
+
+#ifdef DEBUG
+ g_message ("%s: (%d) handle 0x%x is SHARED", __func__,
+ getpid (), i);
+#endif
+
+ shared_meta = &_wapi_shared_layout->metadata[handle->u.shared.offset];
+
+#ifdef DEBUG
+ g_message ("%s: (%d) Updating timstamp of handle 0x%x",
+ __func__, getpid(),
+ handle->u.shared.offset);
+#endif
+
+ InterlockedExchange (&shared_meta->timestamp, now);
+ } else if (handle->type == WAPI_HANDLE_FILE) {
+ struct _WapiHandle_file *file_handle = &handle->u.file;
+
+#ifdef DEBUG
+ g_message ("%s: (%d) handle 0x%x is FILE", __func__,
+ getpid (), i);
+#endif
+
+ g_assert (file_handle->share_info != NULL);
+
+#ifdef DEBUG
+ g_message ("%s: (%d) Inc refs on fileshare 0x%x",
+ __func__, getpid(),
+ (file_handle->share_info - &_wapi_fileshare_layout->share_info[0]) / sizeof(struct _WapiFileShare));
+#endif
+
+ InterlockedExchange (&file_handle->share_info->timestamp, now);
+ }
+ }
+
+ thr_ret = _wapi_timestamp_release (&_wapi_fileshare_layout->share_check, lock_now);
+ _WAPI_HANDLE_COLLECTION_SAFE;