1998-12-31 08:43:48 -07:00
|
|
|
/*
|
|
|
|
* Server-side change notification management
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Alexandre Julliard
|
2006-01-27 04:13:56 -07:00
|
|
|
* Copyright (C) 2006 Mike McCormack
|
2002-03-09 16:29:33 -07:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 05:49:52 -07:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1998-12-31 08:43:48 -07:00
|
|
|
*/
|
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
1998-12-31 08:43:48 -07:00
|
|
|
#include <assert.h>
|
2003-03-26 16:41:43 -07:00
|
|
|
#include <fcntl.h>
|
1998-12-31 08:43:48 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2003-03-26 16:41:43 -07:00
|
|
|
#include <signal.h>
|
2004-02-04 18:45:58 -07:00
|
|
|
#include <sys/stat.h>
|
2006-02-17 03:40:38 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <dirent.h>
|
2006-01-30 10:15:31 -07:00
|
|
|
#include <errno.h>
|
2021-10-07 05:38:40 -07:00
|
|
|
#include <unistd.h>
|
2021-10-04 07:40:14 -07:00
|
|
|
#include <poll.h>
|
2019-04-20 06:14:53 -07:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#endif
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2005-11-28 09:32:54 -07:00
|
|
|
#include "ntstatus.h"
|
|
|
|
#define WIN32_NO_STATUS
|
2002-12-10 15:56:43 -07:00
|
|
|
#include "windef.h"
|
1999-05-15 03:48:19 -07:00
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
#include "file.h"
|
1999-05-15 03:48:19 -07:00
|
|
|
#include "handle.h"
|
|
|
|
#include "thread.h"
|
1999-06-22 10:26:53 -07:00
|
|
|
#include "request.h"
|
2008-11-03 15:37:17 -07:00
|
|
|
#include "process.h"
|
|
|
|
#include "security.h"
|
2005-11-28 09:32:54 -07:00
|
|
|
#include "winternl.h"
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
/* dnotify support */
|
|
|
|
|
2004-02-10 13:08:24 -07:00
|
|
|
#ifdef linux
|
|
|
|
#ifndef F_NOTIFY
|
|
|
|
#define F_NOTIFY 1026
|
|
|
|
#define DN_ACCESS 0x00000001 /* File accessed */
|
|
|
|
#define DN_MODIFY 0x00000002 /* File modified */
|
|
|
|
#define DN_CREATE 0x00000004 /* File created */
|
|
|
|
#define DN_DELETE 0x00000008 /* File removed */
|
|
|
|
#define DN_RENAME 0x00000010 /* File renamed */
|
2008-04-07 04:01:02 -07:00
|
|
|
#define DN_ATTRIB 0x00000020 /* File changed attributes */
|
2004-02-10 13:08:24 -07:00
|
|
|
#define DN_MULTISHOT 0x80000000 /* Don't remove notifier */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
/* inotify support */
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
struct inode;
|
|
|
|
|
|
|
|
static void free_inode( struct inode *inode );
|
|
|
|
|
|
|
|
static struct fd *inotify_fd;
|
|
|
|
|
2006-02-06 03:58:55 -07:00
|
|
|
struct change_record {
|
|
|
|
struct list entry;
|
2011-01-18 03:24:49 -07:00
|
|
|
unsigned int cookie;
|
2011-01-18 03:24:33 -07:00
|
|
|
struct filesystem_event event;
|
2006-02-06 03:58:55 -07:00
|
|
|
};
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
|
|
|
struct object obj; /* object header */
|
2003-03-26 16:41:43 -07:00
|
|
|
struct fd *fd; /* file descriptor to the directory */
|
2008-11-03 15:37:17 -07:00
|
|
|
mode_t mode; /* file stat.st_mode */
|
|
|
|
uid_t uid; /* file stat.st_uid */
|
2003-03-26 16:41:43 -07:00
|
|
|
struct list entry; /* entry in global change notifications list */
|
|
|
|
unsigned int filter; /* notification filter */
|
2020-05-02 06:10:14 -07:00
|
|
|
volatile int notified; /* SIGIO counter */
|
2006-02-07 08:50:36 -07:00
|
|
|
int want_data; /* return change data */
|
2006-02-21 00:58:19 -07:00
|
|
|
int subtree; /* do we want to watch subdirectories? */
|
2006-02-06 03:58:55 -07:00
|
|
|
struct list change_records; /* data for the change */
|
2006-02-17 03:40:38 -07:00
|
|
|
struct list in_entry; /* entry in the inode dirs list */
|
|
|
|
struct inode *inode; /* inode of the associated directory */
|
2016-05-01 22:39:16 -07:00
|
|
|
struct process *client_process; /* client process that has a cache for this directory */
|
|
|
|
int client_entry; /* entry in client process cache */
|
1998-12-31 08:43:48 -07:00
|
|
|
};
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static struct fd *dir_get_fd( struct object *obj );
|
2008-11-03 15:37:17 -07:00
|
|
|
static struct security_descriptor *dir_get_sd( struct object *obj );
|
|
|
|
static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
|
|
|
|
unsigned int set_info );
|
2006-01-27 04:13:56 -07:00
|
|
|
static void dir_dump( struct object *obj, int verbose );
|
2016-05-01 22:39:16 -07:00
|
|
|
static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
|
2006-01-27 04:13:56 -07:00
|
|
|
static void dir_destroy( struct object *obj );
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static const struct object_ops dir_ops =
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
sizeof(struct dir), /* size */
|
2021-02-04 05:44:18 -07:00
|
|
|
&file_type, /* type */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_dump, /* dump */
|
1999-12-31 17:56:27 -07:00
|
|
|
add_queue, /* add_queue */
|
|
|
|
remove_queue, /* remove_queue */
|
2007-04-04 10:54:33 -07:00
|
|
|
default_fd_signaled, /* signaled */
|
1999-12-31 17:56:27 -07:00
|
|
|
no_satisfied, /* satisfied */
|
2005-04-24 10:35:52 -07:00
|
|
|
no_signal, /* signal */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_get_fd, /* get_fd */
|
2021-02-05 04:10:44 -07:00
|
|
|
default_map_access, /* map_access */
|
2008-11-03 15:37:17 -07:00
|
|
|
dir_get_sd, /* get_sd */
|
|
|
|
dir_set_sd, /* set_sd */
|
2020-09-22 07:52:31 -07:00
|
|
|
no_get_full_name, /* get_full_name */
|
2005-11-22 07:55:42 -07:00
|
|
|
no_lookup_name, /* lookup_name */
|
2016-02-04 05:07:19 -07:00
|
|
|
no_link_name, /* link_name */
|
|
|
|
NULL, /* unlink_name */
|
2007-03-22 03:44:29 -07:00
|
|
|
no_open_file, /* open_file */
|
2019-03-25 06:37:09 -07:00
|
|
|
no_kernel_obj_list, /* get_kernel_obj_list */
|
2016-05-01 22:39:16 -07:00
|
|
|
dir_close_handle, /* close_handle */
|
2006-01-27 04:13:56 -07:00
|
|
|
dir_destroy /* destroy */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dir_get_poll_events( struct fd *fd );
|
2007-04-10 13:26:23 -07:00
|
|
|
static enum server_fd_type dir_get_fd_type( struct fd *fd );
|
2006-01-27 04:13:56 -07:00
|
|
|
|
|
|
|
static const struct fd_ops dir_fd_ops =
|
|
|
|
{
|
2007-04-10 08:07:27 -07:00
|
|
|
dir_get_poll_events, /* get_poll_events */
|
|
|
|
default_poll_event, /* poll_event */
|
2007-04-10 13:26:23 -07:00
|
|
|
dir_get_fd_type, /* get_fd_type */
|
2015-05-04 20:17:45 -07:00
|
|
|
no_fd_read, /* read */
|
|
|
|
no_fd_write, /* write */
|
|
|
|
no_fd_flush, /* flush */
|
2018-10-23 08:45:06 -07:00
|
|
|
default_fd_get_file_info, /* get_file_info */
|
2017-10-02 07:42:16 -07:00
|
|
|
no_fd_get_volume_info, /* get_volume_info */
|
2007-04-16 05:45:03 -07:00
|
|
|
default_fd_ioctl, /* ioctl */
|
2021-09-03 21:11:38 -07:00
|
|
|
default_fd_cancel_async, /* cancel_async */
|
2007-04-10 08:07:27 -07:00
|
|
|
default_fd_queue_async, /* queue_async */
|
2016-12-01 04:10:34 -07:00
|
|
|
default_fd_reselect_async /* reselect_async */
|
1998-12-31 08:43:48 -07:00
|
|
|
};
|
|
|
|
|
2003-03-26 16:41:43 -07:00
|
|
|
static struct list change_list = LIST_INIT(change_list);
|
|
|
|
|
2016-05-01 22:39:16 -07:00
|
|
|
/* per-process structure to keep track of cache entries on the client size */
|
|
|
|
struct dir_cache
|
|
|
|
{
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int count;
|
|
|
|
unsigned char state[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
enum dir_cache_state
|
|
|
|
{
|
|
|
|
DIR_CACHE_STATE_FREE,
|
|
|
|
DIR_CACHE_STATE_INUSE,
|
|
|
|
DIR_CACHE_STATE_RELEASED
|
|
|
|
};
|
|
|
|
|
|
|
|
/* return an array of cache entries that can be freed on the client side */
|
|
|
|
static int *get_free_dir_cache_entries( struct process *process, data_size_t *size )
|
|
|
|
{
|
|
|
|
int *ret;
|
|
|
|
struct dir_cache *cache = process->dir_cache;
|
|
|
|
unsigned int i, j, count;
|
|
|
|
|
|
|
|
if (!cache) return NULL;
|
|
|
|
for (i = count = 0; i < cache->count && count < *size / sizeof(*ret); i++)
|
|
|
|
if (cache->state[i] == DIR_CACHE_STATE_RELEASED) count++;
|
|
|
|
if (!count) return NULL;
|
|
|
|
|
|
|
|
if ((ret = malloc( count * sizeof(*ret) )))
|
|
|
|
{
|
|
|
|
for (i = j = 0; j < count; i++)
|
|
|
|
{
|
|
|
|
if (cache->state[i] != DIR_CACHE_STATE_RELEASED) continue;
|
|
|
|
cache->state[i] = DIR_CACHE_STATE_FREE;
|
|
|
|
ret[j++] = i;
|
|
|
|
}
|
|
|
|
*size = count * sizeof(*ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate a new client-side directory cache entry */
|
|
|
|
static int alloc_dir_cache_entry( struct dir *dir, struct process *process )
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
struct dir_cache *cache = process->dir_cache;
|
|
|
|
|
|
|
|
if (cache)
|
|
|
|
for (i = 0; i < cache->count; i++)
|
|
|
|
if (cache->state[i] == DIR_CACHE_STATE_FREE) goto found;
|
|
|
|
|
|
|
|
if (!cache || cache->count == cache->size)
|
|
|
|
{
|
|
|
|
unsigned int size = cache ? cache->size * 2 : 256;
|
|
|
|
if (!(cache = realloc( cache, offsetof( struct dir_cache, state[size] ))))
|
|
|
|
{
|
|
|
|
set_error( STATUS_NO_MEMORY );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
process->dir_cache = cache;
|
|
|
|
cache->size = size;
|
|
|
|
}
|
|
|
|
cache->count = i + 1;
|
|
|
|
|
|
|
|
found:
|
|
|
|
cache->state[i] = DIR_CACHE_STATE_INUSE;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release a directory cache entry; it will be freed on the client side on the next cache request */
|
|
|
|
static void release_dir_cache_entry( struct dir *dir )
|
|
|
|
{
|
|
|
|
struct dir_cache *cache;
|
|
|
|
|
|
|
|
if (!dir->client_process) return;
|
|
|
|
cache = dir->client_process->dir_cache;
|
|
|
|
cache->state[dir->client_entry] = DIR_CACHE_STATE_RELEASED;
|
|
|
|
release_object( dir->client_process );
|
|
|
|
dir->client_process = NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
static void dnotify_adjust_changes( struct dir *dir )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2004-03-01 14:32:02 -07:00
|
|
|
#if defined(F_SETSIG) && defined(F_NOTIFY)
|
2006-01-30 10:15:31 -07:00
|
|
|
int fd = get_unix_fd( dir->fd );
|
|
|
|
unsigned int filter = dir->filter;
|
2003-03-26 16:41:43 -07:00
|
|
|
unsigned int val;
|
|
|
|
if ( 0 > fcntl( fd, F_SETSIG, SIGIO) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
val = DN_MULTISHOT;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_FILE_NAME)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_DIR_NAME)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_RENAME | DN_DELETE | DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_ATTRIB;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SIZE)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_MODIFY;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
|
2004-02-04 18:45:58 -07:00
|
|
|
val |= DN_ACCESS;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_CREATION)
|
2004-02-04 18:45:58 -07:00
|
|
|
val |= DN_CREATE;
|
2005-06-10 12:54:46 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
2003-03-26 16:41:43 -07:00
|
|
|
val |= DN_ATTRIB;
|
|
|
|
fcntl( fd, F_NOTIFY, val );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* insert change in the global list */
|
2006-01-27 04:13:56 -07:00
|
|
|
static inline void insert_change( struct dir *dir )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 04:13:56 -07:00
|
|
|
list_add_head( &change_list, &dir->entry );
|
2003-03-26 16:41:43 -07:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove change from the global list */
|
2006-01-27 04:13:56 -07:00
|
|
|
static inline void remove_change( struct dir *dir )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
|
|
|
sigset_t sigset;
|
|
|
|
|
|
|
|
sigemptyset( &sigset );
|
|
|
|
sigaddset( &sigset, SIGIO );
|
|
|
|
sigprocmask( SIG_BLOCK, &sigset, NULL );
|
2006-01-27 04:13:56 -07:00
|
|
|
list_remove( &dir->entry );
|
2003-03-26 16:41:43 -07:00
|
|
|
sigprocmask( SIG_UNBLOCK, &sigset, NULL );
|
|
|
|
}
|
1998-12-31 08:43:48 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static void dir_dump( struct object *obj, int verbose )
|
1998-12-31 08:43:48 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert( obj->ops == &dir_ops );
|
2007-04-04 10:54:33 -07:00
|
|
|
fprintf( stderr, "Dirfile fd=%p filter=%08x\n", dir->fd, dir->filter );
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* enter here directly from SIGIO signal handler */
|
|
|
|
void do_change_notify( int unix_fd )
|
|
|
|
{
|
2006-01-30 10:15:31 -07:00
|
|
|
struct dir *dir;
|
2003-03-26 16:41:43 -07:00
|
|
|
|
|
|
|
/* FIXME: this is O(n) ... probably can be improved */
|
2006-01-30 10:15:31 -07:00
|
|
|
LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
if (get_unix_fd( dir->fd ) != unix_fd) continue;
|
2020-05-02 06:10:14 -07:00
|
|
|
dir->notified = 1;
|
2003-03-26 16:41:43 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGIO callback, called synchronously with the poll loop */
|
|
|
|
void sigio_callback(void)
|
|
|
|
{
|
2006-01-30 10:15:31 -07:00
|
|
|
struct dir *dir;
|
2003-03-26 16:41:43 -07:00
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
LIST_FOR_EACH_ENTRY( dir, &change_list, struct dir, entry )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2020-05-02 06:10:14 -07:00
|
|
|
if (!dir->notified) continue;
|
|
|
|
dir->notified = 0;
|
|
|
|
fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
1998-12-31 08:43:48 -07:00
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static struct fd *dir_get_fd( struct object *obj )
|
1999-05-15 03:48:19 -07:00
|
|
|
{
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
return (struct fd *)grab_object( dir->fd );
|
|
|
|
}
|
2003-03-26 16:41:43 -07:00
|
|
|
|
2008-11-03 15:37:17 -07:00
|
|
|
static int get_dir_unix_fd( struct dir *dir )
|
|
|
|
{
|
|
|
|
return get_unix_fd( dir->fd );
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct security_descriptor *dir_get_sd( struct object *obj )
|
|
|
|
{
|
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
int unix_fd;
|
|
|
|
struct stat st;
|
|
|
|
struct security_descriptor *sd;
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
|
|
|
|
unix_fd = get_dir_unix_fd( dir );
|
|
|
|
|
|
|
|
if (unix_fd == -1 || fstat( unix_fd, &st ) == -1)
|
|
|
|
return obj->sd;
|
|
|
|
|
|
|
|
/* mode and uid the same? if so, no need to re-generate security descriptor */
|
|
|
|
if (obj->sd &&
|
|
|
|
(st.st_mode & (S_IRWXU|S_IRWXO)) == (dir->mode & (S_IRWXU|S_IRWXO)) &&
|
|
|
|
(st.st_uid == dir->uid))
|
|
|
|
return obj->sd;
|
|
|
|
|
|
|
|
sd = mode_to_sd( st.st_mode,
|
|
|
|
security_unix_uid_to_sid( st.st_uid ),
|
|
|
|
token_get_primary_group( current->process->token ));
|
|
|
|
if (!sd) return obj->sd;
|
|
|
|
|
|
|
|
dir->mode = st.st_mode;
|
|
|
|
dir->uid = st.st_uid;
|
|
|
|
free( obj->sd );
|
|
|
|
obj->sd = sd;
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dir_set_sd( struct object *obj, const struct security_descriptor *sd,
|
|
|
|
unsigned int set_info )
|
|
|
|
{
|
|
|
|
struct dir *dir = (struct dir *)obj;
|
2022-01-25 04:27:28 -07:00
|
|
|
const struct sid *owner;
|
2009-08-17 08:26:51 -07:00
|
|
|
struct stat st;
|
2008-11-03 15:37:17 -07:00
|
|
|
mode_t mode;
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
assert( obj->ops == &dir_ops );
|
|
|
|
|
|
|
|
unix_fd = get_dir_unix_fd( dir );
|
|
|
|
|
2009-08-17 08:26:51 -07:00
|
|
|
if (unix_fd == -1 || fstat( unix_fd, &st ) == -1) return 1;
|
2008-11-03 15:37:17 -07:00
|
|
|
|
|
|
|
if (set_info & OWNER_SECURITY_INFORMATION)
|
|
|
|
{
|
|
|
|
owner = sd_get_owner( sd );
|
|
|
|
if (!owner)
|
|
|
|
{
|
|
|
|
set_error( STATUS_INVALID_SECURITY_DESCR );
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-25 04:27:28 -07:00
|
|
|
if (!obj->sd || !equal_sid( owner, sd_get_owner( obj->sd ) ))
|
2008-11-03 15:37:17 -07:00
|
|
|
{
|
|
|
|
/* FIXME: get Unix uid and call fchown */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (obj->sd)
|
|
|
|
owner = sd_get_owner( obj->sd );
|
|
|
|
else
|
2022-07-19 07:58:21 -07:00
|
|
|
owner = token_get_owner( current->process->token );
|
2008-11-03 15:37:17 -07:00
|
|
|
|
|
|
|
if (set_info & DACL_SECURITY_INFORMATION)
|
|
|
|
{
|
|
|
|
/* keep the bits that we don't map to access rights in the ACL */
|
2009-12-09 08:31:53 -07:00
|
|
|
mode = st.st_mode & (S_ISUID|S_ISGID|S_ISVTX);
|
2008-11-03 15:37:17 -07:00
|
|
|
mode |= sd_to_mode( sd, owner );
|
|
|
|
|
2009-12-08 05:10:01 -07:00
|
|
|
if (((st.st_mode ^ mode) & (S_IRWXU|S_IRWXG|S_IRWXO)) && fchmod( unix_fd, mode ) == -1)
|
2008-11-03 15:37:17 -07:00
|
|
|
{
|
2009-08-17 08:26:51 -07:00
|
|
|
file_set_error();
|
|
|
|
return 0;
|
2008-11-03 15:37:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-02-06 03:58:55 -07:00
|
|
|
static struct change_record *get_first_change_record( struct dir *dir )
|
|
|
|
{
|
|
|
|
struct list *ptr = list_head( &dir->change_records );
|
|
|
|
if (!ptr) return NULL;
|
|
|
|
list_remove( ptr );
|
|
|
|
return LIST_ENTRY( ptr, struct change_record, entry );
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:39:16 -07:00
|
|
|
static int dir_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
|
|
|
|
{
|
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
|
|
|
|
if (obj->handle_count == 1) release_dir_cache_entry( dir ); /* closing last handle, release cache */
|
|
|
|
return 1; /* ok to close */
|
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
static void dir_destroy( struct object *obj )
|
|
|
|
{
|
2006-02-06 03:58:55 -07:00
|
|
|
struct change_record *record;
|
2006-01-27 04:13:56 -07:00
|
|
|
struct dir *dir = (struct dir *)obj;
|
|
|
|
assert (obj->ops == &dir_ops);
|
1999-05-15 03:48:19 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
if (dir->filter)
|
|
|
|
remove_change( dir );
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
if (dir->inode)
|
|
|
|
{
|
|
|
|
list_remove( &dir->in_entry );
|
|
|
|
free_inode( dir->inode );
|
|
|
|
}
|
2006-01-30 10:15:31 -07:00
|
|
|
|
2006-02-06 03:58:55 -07:00
|
|
|
while ((record = get_first_change_record( dir ))) free( record );
|
|
|
|
|
2016-05-01 22:39:16 -07:00
|
|
|
release_dir_cache_entry( dir );
|
2006-01-27 04:13:56 -07:00
|
|
|
release_object( dir->fd );
|
2006-02-17 03:40:38 -07:00
|
|
|
|
|
|
|
if (inotify_fd && list_empty( &change_list ))
|
|
|
|
{
|
|
|
|
release_object( inotify_fd );
|
|
|
|
inotify_fd = NULL;
|
|
|
|
}
|
2006-01-27 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
2009-12-01 09:38:24 -07:00
|
|
|
struct dir *get_dir_obj( struct process *process, obj_handle_t handle, unsigned int access )
|
2006-01-27 04:13:56 -07:00
|
|
|
{
|
|
|
|
return (struct dir *)get_handle_obj( process, handle, access, &dir_ops );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dir_get_poll_events( struct fd *fd )
|
|
|
|
{
|
|
|
|
return 0;
|
2003-03-26 16:41:43 -07:00
|
|
|
}
|
|
|
|
|
2007-04-10 13:26:23 -07:00
|
|
|
static enum server_fd_type dir_get_fd_type( struct fd *fd )
|
2003-03-26 16:41:43 -07:00
|
|
|
{
|
2006-11-20 06:14:04 -07:00
|
|
|
return FD_TYPE_DIR;
|
2006-01-27 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-20 06:14:53 -07:00
|
|
|
#ifdef HAVE_SYS_INOTIFY_H
|
2006-02-05 04:19:56 -07:00
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
#define HASH_SIZE 31
|
|
|
|
|
|
|
|
struct inode {
|
2006-02-21 00:58:19 -07:00
|
|
|
struct list ch_entry; /* entry in the children list */
|
|
|
|
struct list children; /* children of this inode */
|
|
|
|
struct inode *parent; /* parent of this inode */
|
2006-02-17 03:40:38 -07:00
|
|
|
struct list dirs; /* directory handles watching this inode */
|
|
|
|
struct list ino_entry; /* entry in the inode hash */
|
|
|
|
struct list wd_entry; /* entry in the watch descriptor hash */
|
|
|
|
dev_t dev; /* device number */
|
|
|
|
ino_t ino; /* device's inode number */
|
|
|
|
int wd; /* inotify's watch descriptor */
|
2006-02-21 00:58:19 -07:00
|
|
|
char *name; /* basename name of the inode */
|
2006-02-17 03:40:38 -07:00
|
|
|
};
|
|
|
|
|
2010-01-21 04:57:45 -07:00
|
|
|
static struct list inode_hash[ HASH_SIZE ];
|
|
|
|
static struct list wd_hash[ HASH_SIZE ];
|
2006-02-17 03:40:38 -07:00
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static int inotify_add_dir( char *path, unsigned int filter );
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static struct inode *inode_from_wd( int wd )
|
|
|
|
{
|
|
|
|
struct list *bucket = &wd_hash[ wd % HASH_SIZE ];
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, wd_entry )
|
|
|
|
if (inode->wd == wd)
|
|
|
|
return inode;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct list *get_hash_list( dev_t dev, ino_t ino )
|
|
|
|
{
|
|
|
|
return &inode_hash[ (ino ^ dev) % HASH_SIZE ];
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static struct inode *find_inode( dev_t dev, ino_t ino )
|
2006-02-17 03:40:38 -07:00
|
|
|
{
|
|
|
|
struct list *bucket = get_hash_list( dev, ino );
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( inode, bucket, struct inode, ino_entry )
|
|
|
|
if (inode->ino == ino && inode->dev == dev)
|
|
|
|
return inode;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode *create_inode( dev_t dev, ino_t ino )
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = malloc( sizeof *inode );
|
|
|
|
if (inode)
|
|
|
|
{
|
2006-02-21 00:58:19 -07:00
|
|
|
list_init( &inode->children );
|
2006-02-17 03:40:38 -07:00
|
|
|
list_init( &inode->dirs );
|
|
|
|
inode->ino = ino;
|
|
|
|
inode->dev = dev;
|
|
|
|
inode->wd = -1;
|
2006-02-21 00:58:19 -07:00
|
|
|
inode->parent = NULL;
|
|
|
|
inode->name = NULL;
|
2006-02-17 03:40:38 -07:00
|
|
|
list_add_tail( get_hash_list( dev, ino ), &inode->ino_entry );
|
|
|
|
}
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static struct inode *get_inode( dev_t dev, ino_t ino )
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = find_inode( dev, ino );
|
|
|
|
if (inode)
|
|
|
|
return inode;
|
|
|
|
return create_inode( dev, ino );
|
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static void inode_set_wd( struct inode *inode, int wd )
|
|
|
|
{
|
|
|
|
if (inode->wd != -1)
|
|
|
|
list_remove( &inode->wd_entry );
|
|
|
|
inode->wd = wd;
|
|
|
|
list_add_tail( &wd_hash[ wd % HASH_SIZE ], &inode->wd_entry );
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static void inode_set_name( struct inode *inode, const char *name )
|
|
|
|
{
|
2006-10-09 14:34:36 -07:00
|
|
|
free (inode->name);
|
2006-02-21 00:58:19 -07:00
|
|
|
inode->name = name ? strdup( name ) : NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static void free_inode( struct inode *inode )
|
|
|
|
{
|
2006-02-21 00:58:19 -07:00
|
|
|
int subtree = 0, watches = 0;
|
2008-02-28 02:47:14 -07:00
|
|
|
struct inode *tmp, *next;
|
2006-02-21 00:58:19 -07:00
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
|
|
|
|
{
|
|
|
|
subtree |= dir->subtree;
|
|
|
|
watches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!subtree && !inode->parent)
|
|
|
|
{
|
2006-02-22 07:11:17 -07:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children,
|
|
|
|
struct inode, ch_entry )
|
2006-02-21 00:58:19 -07:00
|
|
|
{
|
|
|
|
assert( tmp != inode );
|
|
|
|
assert( tmp->parent == inode );
|
|
|
|
free_inode( tmp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (watches)
|
2006-02-17 03:40:38 -07:00
|
|
|
return;
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
if (inode->parent)
|
|
|
|
list_remove( &inode->ch_entry );
|
|
|
|
|
2008-02-28 02:47:14 -07:00
|
|
|
/* disconnect remaining children from the parent */
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( tmp, next, &inode->children, struct inode, ch_entry )
|
|
|
|
{
|
|
|
|
list_remove( &tmp->ch_entry );
|
|
|
|
tmp->parent = NULL;
|
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
if (inode->wd != -1)
|
|
|
|
{
|
2009-06-26 06:45:03 -07:00
|
|
|
inotify_rm_watch( get_unix_fd( inotify_fd ), inode->wd );
|
2006-02-17 03:40:38 -07:00
|
|
|
list_remove( &inode->wd_entry );
|
|
|
|
}
|
|
|
|
list_remove( &inode->ino_entry );
|
2006-02-21 00:58:19 -07:00
|
|
|
|
|
|
|
free( inode->name );
|
2006-02-17 03:40:38 -07:00
|
|
|
free( inode );
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static struct inode *inode_add( struct inode *parent,
|
|
|
|
dev_t dev, ino_t ino, const char *name )
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = get_inode( dev, ino );
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!inode->parent)
|
|
|
|
{
|
|
|
|
list_add_tail( &parent->children, &inode->ch_entry );
|
|
|
|
inode->parent = parent;
|
|
|
|
assert( inode != parent );
|
|
|
|
}
|
|
|
|
inode_set_name( inode, name );
|
|
|
|
|
|
|
|
return inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct inode *inode_from_name( struct inode *inode, const char *name )
|
|
|
|
{
|
|
|
|
struct inode *i;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( i, &inode->children, struct inode, ch_entry )
|
|
|
|
if (i->name && !strcmp( i->name, name ))
|
|
|
|
return i;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
static int inotify_get_poll_events( struct fd *fd );
|
|
|
|
static void inotify_poll_event( struct fd *fd, int event );
|
|
|
|
|
|
|
|
static const struct fd_ops inotify_fd_ops =
|
|
|
|
{
|
2007-04-10 08:07:27 -07:00
|
|
|
inotify_get_poll_events, /* get_poll_events */
|
|
|
|
inotify_poll_event, /* poll_event */
|
2007-04-12 11:21:53 -07:00
|
|
|
NULL, /* flush */
|
|
|
|
NULL, /* get_fd_type */
|
2007-04-16 05:45:03 -07:00
|
|
|
NULL, /* ioctl */
|
2007-04-12 11:21:53 -07:00
|
|
|
NULL, /* queue_async */
|
2016-12-01 04:10:34 -07:00
|
|
|
NULL /* reselect_async */
|
2006-01-30 10:15:31 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static int inotify_get_poll_events( struct fd *fd )
|
|
|
|
{
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2006-02-20 04:28:46 -07:00
|
|
|
static void inotify_do_change_notify( struct dir *dir, unsigned int action,
|
2011-01-18 03:24:49 -07:00
|
|
|
unsigned int cookie, const char *relpath )
|
2006-01-30 10:15:31 -07:00
|
|
|
{
|
2006-02-06 03:58:55 -07:00
|
|
|
struct change_record *record;
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
assert( dir->obj.ops == &dir_ops );
|
|
|
|
|
2006-02-07 08:50:36 -07:00
|
|
|
if (dir->want_data)
|
|
|
|
{
|
2006-02-20 04:28:46 -07:00
|
|
|
size_t len = strlen(relpath);
|
2011-01-18 03:24:33 -07:00
|
|
|
record = malloc( offsetof(struct change_record, event.name[len]) );
|
2006-02-07 08:50:36 -07:00
|
|
|
if (!record)
|
|
|
|
return;
|
|
|
|
|
2011-01-18 03:24:49 -07:00
|
|
|
record->cookie = cookie;
|
2011-01-18 03:24:33 -07:00
|
|
|
record->event.action = action;
|
|
|
|
memcpy( record->event.name, relpath, len );
|
|
|
|
record->event.len = len;
|
2006-02-07 08:50:36 -07:00
|
|
|
|
|
|
|
list_add_tail( &dir->change_records, &record->entry );
|
|
|
|
}
|
2006-02-06 03:58:55 -07:00
|
|
|
|
2007-04-02 11:09:29 -07:00
|
|
|
fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
|
2006-01-30 10:15:31 -07:00
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static unsigned int filter_from_event( struct inotify_event *ie )
|
|
|
|
{
|
|
|
|
unsigned int filter = 0;
|
|
|
|
|
|
|
|
if (ie->mask & (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE))
|
|
|
|
filter |= FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME;
|
|
|
|
if (ie->mask & IN_MODIFY)
|
2018-11-06 07:05:56 -07:00
|
|
|
filter |= FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS;
|
2006-02-17 03:40:38 -07:00
|
|
|
if (ie->mask & IN_ATTRIB)
|
|
|
|
filter |= FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY;
|
|
|
|
if (ie->mask & IN_CREATE)
|
|
|
|
filter |= FILE_NOTIFY_CHANGE_CREATION;
|
|
|
|
|
2010-12-16 04:23:04 -07:00
|
|
|
if (ie->mask & IN_ISDIR)
|
|
|
|
filter &= ~FILE_NOTIFY_CHANGE_FILE_NAME;
|
|
|
|
else
|
|
|
|
filter &= ~FILE_NOTIFY_CHANGE_DIR_NAME;
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
/* scan up the parent directories for watches */
|
|
|
|
static unsigned int filter_from_inode( struct inode *inode, int is_parent )
|
|
|
|
{
|
|
|
|
unsigned int filter = 0;
|
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
/* combine filters from parents watching subtrees */
|
|
|
|
while (inode)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( dir, &inode->dirs, struct dir, in_entry )
|
|
|
|
if (dir->subtree || !is_parent)
|
|
|
|
filter |= dir->filter;
|
|
|
|
is_parent = 1;
|
|
|
|
inode = inode->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter;
|
|
|
|
}
|
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
static char *get_path_from_fd( int fd, int sz )
|
|
|
|
{
|
2023-12-26 15:37:16 -07:00
|
|
|
#ifdef linux
|
|
|
|
char *ret = malloc( 32 + sz );
|
|
|
|
|
2024-03-19 15:15:58 -07:00
|
|
|
if (ret) snprintf( ret, 32 + sz, "/proc/self/fd/%u", fd );
|
2023-12-26 15:37:16 -07:00
|
|
|
return ret;
|
|
|
|
#elif defined(F_GETPATH)
|
2023-10-05 05:03:54 -07:00
|
|
|
char *ret = malloc( PATH_MAX + sz );
|
|
|
|
|
|
|
|
if (!ret) return NULL;
|
|
|
|
if (!fcntl( fd, F_GETPATH, ret )) return ret;
|
|
|
|
free( ret );
|
|
|
|
return NULL;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static char *inode_get_path( struct inode *inode, int sz )
|
2006-02-20 04:28:46 -07:00
|
|
|
{
|
|
|
|
struct list *head;
|
2006-02-21 00:58:19 -07:00
|
|
|
char *path;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
2006-02-20 04:28:46 -07:00
|
|
|
|
|
|
|
head = list_head( &inode->dirs );
|
2006-02-21 00:58:19 -07:00
|
|
|
if (head)
|
|
|
|
{
|
|
|
|
int unix_fd = get_unix_fd( LIST_ENTRY( head, struct dir, in_entry )->fd );
|
2023-10-05 05:03:54 -07:00
|
|
|
if (!(path = get_path_from_fd( unix_fd, sz + 1 ))) return NULL;
|
|
|
|
strcat( path, "/" );
|
2006-02-21 00:58:19 -07:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inode->name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
len = strlen( inode->name );
|
|
|
|
path = inode_get_path( inode->parent, sz + len + 1 );
|
|
|
|
if (!path)
|
|
|
|
return NULL;
|
2023-10-05 05:03:54 -07:00
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
strcat( path, inode->name );
|
|
|
|
strcat( path, "/" );
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2010-12-16 04:23:04 -07:00
|
|
|
static void inode_check_dir( struct inode *parent, const char *name )
|
2006-02-21 00:58:19 -07:00
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
unsigned int filter;
|
|
|
|
struct inode *inode;
|
|
|
|
struct stat st;
|
2010-12-16 04:23:04 -07:00
|
|
|
int wd = -1;
|
2006-02-20 04:28:46 -07:00
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
path = inode_get_path( parent, strlen(name) );
|
2006-02-20 04:28:46 -07:00
|
|
|
if (!path)
|
2010-12-16 04:23:04 -07:00
|
|
|
return;
|
2006-02-21 00:58:19 -07:00
|
|
|
|
|
|
|
strcat( path, name );
|
2006-02-20 04:28:46 -07:00
|
|
|
|
2010-12-16 04:23:04 -07:00
|
|
|
if (stat( path, &st ) < 0)
|
2006-02-21 00:58:19 -07:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
filter = filter_from_inode( parent, 1 );
|
|
|
|
if (!filter)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
inode = inode_add( parent, st.st_dev, st.st_ino, name );
|
|
|
|
if (!inode || inode->wd != -1)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
wd = inotify_add_dir( path, filter );
|
|
|
|
if (wd != -1)
|
|
|
|
inode_set_wd( inode, wd );
|
|
|
|
else
|
|
|
|
free_inode( inode );
|
|
|
|
|
|
|
|
end:
|
2006-02-20 04:28:46 -07:00
|
|
|
free( path );
|
2006-02-21 00:58:19 -07:00
|
|
|
}
|
2006-02-20 04:28:46 -07:00
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
static int prepend( char **path, const char *segment )
|
|
|
|
{
|
|
|
|
int extra;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
extra = strlen( segment ) + 1;
|
|
|
|
if (*path)
|
|
|
|
{
|
|
|
|
int len = strlen( *path ) + 1;
|
|
|
|
p = realloc( *path, len + extra );
|
|
|
|
if (!p) return 0;
|
|
|
|
memmove( &p[ extra ], p, len );
|
|
|
|
p[ extra - 1 ] = '/';
|
|
|
|
memcpy( p, segment, extra - 1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = malloc( extra );
|
|
|
|
if (!p) return 0;
|
|
|
|
memcpy( p, segment, extra );
|
|
|
|
}
|
|
|
|
|
|
|
|
*path = p;
|
|
|
|
|
|
|
|
return 1;
|
2006-02-20 04:28:46 -07:00
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static void inotify_notify_all( struct inotify_event *ie )
|
|
|
|
{
|
2006-02-20 04:28:46 -07:00
|
|
|
unsigned int filter, action;
|
2006-02-21 00:58:19 -07:00
|
|
|
struct inode *inode, *i;
|
|
|
|
char *path = NULL;
|
2006-02-17 03:40:38 -07:00
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
inode = inode_from_wd( ie->wd );
|
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "no inode matches %d\n", ie->wd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter = filter_from_event( ie );
|
2006-02-20 04:28:46 -07:00
|
|
|
|
|
|
|
if (ie->mask & IN_CREATE)
|
|
|
|
{
|
2010-12-16 04:23:04 -07:00
|
|
|
if (ie->mask & IN_ISDIR)
|
|
|
|
inode_check_dir( inode, ie->name );
|
|
|
|
|
2006-02-20 04:28:46 -07:00
|
|
|
action = FILE_ACTION_ADDED;
|
|
|
|
}
|
|
|
|
else if (ie->mask & IN_DELETE)
|
|
|
|
action = FILE_ACTION_REMOVED;
|
2010-12-16 04:23:04 -07:00
|
|
|
else if (ie->mask & IN_MOVED_FROM)
|
|
|
|
action = FILE_ACTION_RENAMED_OLD_NAME;
|
|
|
|
else if (ie->mask & IN_MOVED_TO)
|
|
|
|
action = FILE_ACTION_RENAMED_NEW_NAME;
|
2006-02-20 04:28:46 -07:00
|
|
|
else
|
|
|
|
action = FILE_ACTION_MODIFIED;
|
2006-02-17 03:40:38 -07:00
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
/*
|
2006-02-24 08:11:36 -07:00
|
|
|
* Work our way up the inode hierarchy
|
2006-02-21 00:58:19 -07:00
|
|
|
* extending the relative path as we go
|
2006-02-24 08:11:36 -07:00
|
|
|
* and notifying all recursive watches.
|
2006-02-21 00:58:19 -07:00
|
|
|
*/
|
|
|
|
if (!prepend( &path, ie->name ))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = inode; i; i = i->parent)
|
|
|
|
{
|
|
|
|
LIST_FOR_EACH_ENTRY( dir, &i->dirs, struct dir, in_entry )
|
|
|
|
if ((filter & dir->filter) && (i==inode || dir->subtree))
|
2011-01-18 03:24:49 -07:00
|
|
|
inotify_do_change_notify( dir, action, ie->cookie, path );
|
2006-02-21 00:58:19 -07:00
|
|
|
|
|
|
|
if (!i->name || !prepend( &path, i->name ))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free( path );
|
|
|
|
|
|
|
|
if (ie->mask & IN_DELETE)
|
|
|
|
{
|
|
|
|
i = inode_from_name( inode, ie->name );
|
|
|
|
if (i)
|
|
|
|
free_inode( i );
|
|
|
|
}
|
2006-02-17 03:40:38 -07:00
|
|
|
}
|
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
static void inotify_poll_event( struct fd *fd, int event )
|
|
|
|
{
|
|
|
|
int r, ofs, unix_fd;
|
|
|
|
char buffer[0x1000];
|
|
|
|
struct inotify_event *ie;
|
|
|
|
|
|
|
|
unix_fd = get_unix_fd( fd );
|
|
|
|
r = read( unix_fd, buffer, sizeof buffer );
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr,"inotify_poll_event(): inotify read failed!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-02-08 07:06:42 -07:00
|
|
|
for( ofs = 0; ofs < r - offsetof(struct inotify_event, name); )
|
2006-01-30 10:15:31 -07:00
|
|
|
{
|
|
|
|
ie = (struct inotify_event*) &buffer[ofs];
|
2006-02-08 07:06:42 -07:00
|
|
|
ofs += offsetof( struct inotify_event, name[ie->len] );
|
|
|
|
if (ofs > r) break;
|
2018-11-06 07:05:43 -07:00
|
|
|
if (ie->len) inotify_notify_all( ie );
|
2006-01-30 10:15:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static inline struct fd *create_inotify_fd( void )
|
2006-02-05 04:19:56 -07:00
|
|
|
{
|
|
|
|
int unix_fd;
|
|
|
|
|
|
|
|
unix_fd = inotify_init();
|
|
|
|
if (unix_fd<0)
|
|
|
|
return NULL;
|
2007-04-10 13:25:07 -07:00
|
|
|
return create_anonymous_fd( &inotify_fd_ops, unix_fd, NULL, 0 );
|
2006-02-05 04:19:56 -07:00
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
static int map_flags( unsigned int filter )
|
2006-01-30 10:15:31 -07:00
|
|
|
{
|
2006-02-21 00:58:19 -07:00
|
|
|
unsigned int mask;
|
|
|
|
|
|
|
|
/* always watch these so we can track subdirectories in recursive watches */
|
|
|
|
mask = (IN_MOVED_FROM | IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF);
|
2006-02-05 04:19:56 -07:00
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_ATTRIBUTES)
|
|
|
|
mask |= IN_ATTRIB;
|
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SIZE)
|
|
|
|
mask |= IN_MODIFY;
|
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_WRITE)
|
|
|
|
mask |= IN_MODIFY;
|
|
|
|
if (filter & FILE_NOTIFY_CHANGE_LAST_ACCESS)
|
2018-11-06 07:05:56 -07:00
|
|
|
mask |= IN_MODIFY;
|
2006-01-30 10:15:31 -07:00
|
|
|
if (filter & FILE_NOTIFY_CHANGE_SECURITY)
|
|
|
|
mask |= IN_ATTRIB;
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inotify_add_dir( char *path, unsigned int filter )
|
|
|
|
{
|
|
|
|
int wd = inotify_add_watch( get_unix_fd( inotify_fd ),
|
|
|
|
path, map_flags( filter ) );
|
|
|
|
if (wd != -1)
|
|
|
|
set_fd_events( inotify_fd, POLLIN );
|
|
|
|
return wd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_inotify( void )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (inotify_fd)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
inotify_fd = create_inotify_fd();
|
|
|
|
if (!inotify_fd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i=0; i<HASH_SIZE; i++)
|
|
|
|
{
|
|
|
|
list_init( &inode_hash[i] );
|
|
|
|
list_init( &wd_hash[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inotify_adjust_changes( struct dir *dir )
|
|
|
|
{
|
|
|
|
unsigned int filter;
|
|
|
|
struct inode *inode;
|
|
|
|
struct stat st;
|
2023-10-05 05:03:54 -07:00
|
|
|
char *path;
|
2006-02-17 03:40:38 -07:00
|
|
|
int wd, unix_fd;
|
|
|
|
|
|
|
|
if (!inotify_fd)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
unix_fd = get_unix_fd( dir->fd );
|
|
|
|
|
|
|
|
inode = dir->inode;
|
|
|
|
if (!inode)
|
|
|
|
{
|
|
|
|
/* check if this fd is already being watched */
|
|
|
|
if (-1 == fstat( unix_fd, &st ))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
inode = get_inode( st.st_dev, st.st_ino );
|
|
|
|
if (!inode)
|
|
|
|
inode = create_inode( st.st_dev, st.st_ino );
|
|
|
|
if (!inode)
|
|
|
|
return 0;
|
|
|
|
list_add_tail( &inode->dirs, &dir->in_entry );
|
|
|
|
dir->inode = inode;
|
|
|
|
}
|
|
|
|
|
2006-02-21 00:58:19 -07:00
|
|
|
filter = filter_from_inode( inode, 0 );
|
2006-02-17 03:40:38 -07:00
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
if (!(path = get_path_from_fd( unix_fd, 0 ))) return 0;
|
2006-02-17 03:40:38 -07:00
|
|
|
wd = inotify_add_dir( path, filter );
|
2023-10-05 05:03:54 -07:00
|
|
|
free( path );
|
2006-02-17 03:40:38 -07:00
|
|
|
if (wd == -1) return 0;
|
|
|
|
|
|
|
|
inode_set_wd( inode, wd );
|
|
|
|
|
2006-02-05 04:19:56 -07:00
|
|
|
return 1;
|
2006-01-30 10:15:31 -07:00
|
|
|
}
|
|
|
|
|
2006-02-22 08:45:34 -07:00
|
|
|
static char *get_basename( const char *link )
|
|
|
|
{
|
|
|
|
char *buffer, *name = NULL;
|
|
|
|
int r, n = 0x100;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
buffer = malloc( n );
|
2006-03-02 10:03:32 -07:00
|
|
|
if (!buffer) return NULL;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
|
|
|
r = readlink( link, buffer, n );
|
|
|
|
if (r < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (r < n)
|
|
|
|
{
|
|
|
|
name = buffer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free( buffer );
|
|
|
|
n *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
while (r > 0 && name[ r - 1 ] == '/' )
|
|
|
|
r--;
|
|
|
|
name[ r ] = 0;
|
|
|
|
|
|
|
|
name = strrchr( name, '/' );
|
|
|
|
if (name)
|
|
|
|
name = strdup( &name[1] );
|
|
|
|
}
|
|
|
|
|
|
|
|
free( buffer );
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
static void dir_add_to_existing_notify( struct dir *dir )
|
2006-02-22 08:45:34 -07:00
|
|
|
{
|
|
|
|
struct inode *inode, *parent;
|
|
|
|
unsigned int filter = 0;
|
|
|
|
struct stat st, st_new;
|
2023-10-05 05:03:54 -07:00
|
|
|
char *link, *name;
|
|
|
|
int res, wd, unix_fd;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
|
|
|
if (!inotify_fd)
|
2023-10-05 05:03:54 -07:00
|
|
|
return;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
|
|
|
unix_fd = get_unix_fd( dir->fd );
|
|
|
|
|
|
|
|
/* check if it's in the list of inodes we want to watch */
|
2023-10-05 05:03:54 -07:00
|
|
|
if (fstat( unix_fd, &st_new )) return;
|
2024-09-11 08:11:49 -07:00
|
|
|
if (find_inode( st_new.st_dev, st_new.st_ino )) return;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
|
|
|
/* lookup the parent */
|
2023-10-05 05:03:54 -07:00
|
|
|
if (!(link = get_path_from_fd( unix_fd, 3 ))) return;
|
|
|
|
strcat( link, "/.." );
|
|
|
|
res = stat( link, &st );
|
|
|
|
free( link );
|
|
|
|
if (res == -1) return;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there's no parent, stop. We could keep going adding
|
|
|
|
* ../ to the path until we hit the root of the tree or
|
|
|
|
* find a recursively watched ancestor.
|
|
|
|
* Assume it's too expensive to search up the tree for now.
|
|
|
|
*/
|
2023-10-05 05:03:54 -07:00
|
|
|
if (!(parent = find_inode( st.st_dev, st.st_ino ))) return;
|
|
|
|
if (parent->wd == -1) return;
|
2006-02-22 08:45:34 -07:00
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
if (!(filter = filter_from_inode( parent, 1 ))) return;
|
|
|
|
if (!(link = get_path_from_fd( unix_fd, 0 ))) return;
|
|
|
|
if (!(name = get_basename( link )))
|
|
|
|
{
|
|
|
|
free( link );
|
|
|
|
return;
|
|
|
|
}
|
2006-02-22 08:45:34 -07:00
|
|
|
inode = inode_add( parent, st_new.st_dev, st_new.st_ino, name );
|
2023-10-05 05:03:54 -07:00
|
|
|
if (inode)
|
|
|
|
{
|
|
|
|
/* Couldn't find this inode at the start of the function, must be new */
|
|
|
|
assert( inode->wd == -1 );
|
2006-02-22 08:45:34 -07:00
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
wd = inotify_add_dir( link, filter );
|
|
|
|
if (wd != -1)
|
|
|
|
inode_set_wd( inode, wd );
|
|
|
|
}
|
|
|
|
free( name );
|
|
|
|
free( link );
|
2006-02-22 08:45:34 -07:00
|
|
|
}
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
#else
|
|
|
|
|
|
|
|
static int init_inotify( void )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int inotify_adjust_changes( struct dir *dir )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_inode( struct inode *inode )
|
|
|
|
{
|
|
|
|
assert( 0 );
|
|
|
|
}
|
|
|
|
|
2023-10-05 05:03:54 -07:00
|
|
|
static void dir_add_to_existing_notify( struct dir *dir )
|
2006-02-22 08:45:34 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-20 06:14:53 -07:00
|
|
|
#endif /* HAVE_SYS_INOTIFY_H */
|
2006-01-30 10:15:31 -07:00
|
|
|
|
2009-08-17 08:18:54 -07:00
|
|
|
struct object *create_dir_obj( struct fd *fd, unsigned int access, mode_t mode )
|
2006-02-17 03:40:38 -07:00
|
|
|
{
|
|
|
|
struct dir *dir;
|
|
|
|
|
|
|
|
dir = alloc_object( &dir_ops );
|
|
|
|
if (!dir)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list_init( &dir->change_records );
|
|
|
|
dir->filter = 0;
|
|
|
|
dir->notified = 0;
|
|
|
|
dir->want_data = 0;
|
|
|
|
dir->inode = NULL;
|
|
|
|
grab_object( fd );
|
|
|
|
dir->fd = fd;
|
2009-08-17 08:18:54 -07:00
|
|
|
dir->mode = mode;
|
|
|
|
dir->uid = ~(uid_t)0;
|
2016-05-01 22:39:16 -07:00
|
|
|
dir->client_process = NULL;
|
2006-02-17 03:40:38 -07:00
|
|
|
set_fd_user( fd, &dir_fd_ops, &dir->obj );
|
|
|
|
|
2006-02-22 08:45:34 -07:00
|
|
|
dir_add_to_existing_notify( dir );
|
|
|
|
|
2006-02-17 03:40:38 -07:00
|
|
|
return &dir->obj;
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:39:16 -07:00
|
|
|
/* retrieve (or allocate) the client-side directory cache entry */
|
|
|
|
DECL_HANDLER(get_directory_cache_entry)
|
|
|
|
{
|
|
|
|
struct dir *dir;
|
|
|
|
int *free_entries;
|
|
|
|
data_size_t free_size;
|
|
|
|
|
|
|
|
if (!(dir = get_dir_obj( current->process, req->handle, 0 ))) return;
|
|
|
|
|
|
|
|
if (!dir->client_process)
|
|
|
|
{
|
|
|
|
if ((dir->client_entry = alloc_dir_cache_entry( dir, current->process )) == -1) goto done;
|
|
|
|
dir->client_process = (struct process *)grab_object( current->process );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dir->client_process == current->process) reply->entry = dir->client_entry;
|
|
|
|
else set_error( STATUS_SHARING_VIOLATION );
|
|
|
|
|
|
|
|
done: /* allow freeing entries even on failure */
|
|
|
|
free_size = get_reply_max_size();
|
|
|
|
free_entries = get_free_dir_cache_entries( current->process, &free_size );
|
|
|
|
if (free_entries) set_reply_data_ptr( free_entries, free_size );
|
|
|
|
|
|
|
|
release_object( dir );
|
|
|
|
}
|
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
/* enable change notifications for a directory */
|
|
|
|
DECL_HANDLER(read_directory_changes)
|
|
|
|
{
|
|
|
|
struct dir *dir;
|
2007-04-02 11:41:59 -07:00
|
|
|
struct async *async;
|
2006-01-27 04:13:56 -07:00
|
|
|
|
|
|
|
if (!req->filter)
|
|
|
|
{
|
|
|
|
set_error(STATUS_INVALID_PARAMETER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-26 04:17:20 -07:00
|
|
|
dir = get_dir_obj( current->process, req->async.handle, 0 );
|
2006-01-27 04:13:56 -07:00
|
|
|
if (!dir)
|
|
|
|
return;
|
2003-03-26 16:41:43 -07:00
|
|
|
|
2006-02-06 03:58:55 -07:00
|
|
|
/* requests don't timeout */
|
2017-06-26 03:19:28 -07:00
|
|
|
if (!(async = create_async( dir->fd, current, &req->async, NULL ))) goto end;
|
2017-07-04 06:26:35 -07:00
|
|
|
fd_queue_async( dir->fd, async, ASYNC_TYPE_WAIT );
|
2006-02-06 03:58:55 -07:00
|
|
|
|
2006-01-27 04:13:56 -07:00
|
|
|
/* assign it once */
|
|
|
|
if (!dir->filter)
|
|
|
|
{
|
2006-02-17 03:40:38 -07:00
|
|
|
init_inotify();
|
2006-01-27 04:13:56 -07:00
|
|
|
insert_change( dir );
|
|
|
|
dir->filter = req->filter;
|
2006-02-21 00:58:19 -07:00
|
|
|
dir->subtree = req->subtree;
|
2006-02-07 08:50:36 -07:00
|
|
|
dir->want_data = req->want_data;
|
2006-01-27 04:13:56 -07:00
|
|
|
}
|
|
|
|
|
2006-03-28 01:58:07 -07:00
|
|
|
/* if there's already a change in the queue, send it */
|
2007-04-02 03:49:45 -07:00
|
|
|
if (!list_empty( &dir->change_records ))
|
2007-04-02 11:09:29 -07:00
|
|
|
fd_async_wake_up( dir->fd, ASYNC_TYPE_WAIT, STATUS_ALERTED );
|
2006-03-28 01:58:07 -07:00
|
|
|
|
2006-01-30 10:15:31 -07:00
|
|
|
/* setup the real notification */
|
2006-02-05 04:19:56 -07:00
|
|
|
if (!inotify_adjust_changes( dir ))
|
2006-01-30 10:15:31 -07:00
|
|
|
dnotify_adjust_changes( dir );
|
2006-01-27 04:13:56 -07:00
|
|
|
|
|
|
|
set_error(STATUS_PENDING);
|
|
|
|
|
2017-07-04 06:26:35 -07:00
|
|
|
release_object( async );
|
2006-01-27 04:13:56 -07:00
|
|
|
end:
|
|
|
|
release_object( dir );
|
1999-05-15 03:48:19 -07:00
|
|
|
}
|
2006-02-06 03:58:55 -07:00
|
|
|
|
|
|
|
DECL_HANDLER(read_change)
|
|
|
|
{
|
2011-01-18 03:24:33 -07:00
|
|
|
struct change_record *record, *next;
|
2006-02-06 03:58:55 -07:00
|
|
|
struct dir *dir;
|
2011-01-18 03:24:33 -07:00
|
|
|
struct list events;
|
|
|
|
char *data, *event;
|
|
|
|
int size = 0;
|
2006-02-06 03:58:55 -07:00
|
|
|
|
|
|
|
dir = get_dir_obj( current->process, req->handle, 0 );
|
|
|
|
if (!dir)
|
|
|
|
return;
|
|
|
|
|
2011-01-18 03:24:33 -07:00
|
|
|
list_init( &events );
|
|
|
|
list_move_tail( &events, &dir->change_records );
|
|
|
|
release_object( dir );
|
|
|
|
|
|
|
|
if (list_empty( &events ))
|
2006-02-06 03:58:55 -07:00
|
|
|
{
|
|
|
|
set_error( STATUS_NO_DATA_DETECTED );
|
2011-01-18 03:24:33 -07:00
|
|
|
return;
|
|
|
|
}
|
2006-02-06 03:58:55 -07:00
|
|
|
|
2011-01-18 03:24:33 -07:00
|
|
|
LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
|
|
|
|
{
|
|
|
|
size += (offsetof(struct filesystem_event, name[record->event.len])
|
|
|
|
+ sizeof(int)-1) / sizeof(int) * sizeof(int);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size > get_reply_max_size())
|
|
|
|
set_error( STATUS_BUFFER_TOO_SMALL );
|
|
|
|
else if ((data = mem_alloc( size )) != NULL)
|
|
|
|
{
|
|
|
|
event = data;
|
|
|
|
LIST_FOR_EACH_ENTRY( record, &events, struct change_record, entry )
|
|
|
|
{
|
|
|
|
data_size_t len = offsetof( struct filesystem_event, name[record->event.len] );
|
2011-01-18 03:24:49 -07:00
|
|
|
|
|
|
|
/* FIXME: rename events are sometimes reported as delete/create */
|
|
|
|
if (record->event.action == FILE_ACTION_RENAMED_OLD_NAME)
|
|
|
|
{
|
|
|
|
struct list *elem = list_next( &events, &record->entry );
|
|
|
|
if (elem)
|
|
|
|
next = LIST_ENTRY(elem, struct change_record, entry);
|
|
|
|
|
|
|
|
if (elem && next->cookie == record->cookie)
|
|
|
|
next->cookie = 0;
|
|
|
|
else
|
|
|
|
record->event.action = FILE_ACTION_REMOVED;
|
|
|
|
}
|
|
|
|
else if (record->event.action == FILE_ACTION_RENAMED_NEW_NAME && record->cookie)
|
|
|
|
record->event.action = FILE_ACTION_ADDED;
|
|
|
|
|
2011-01-18 03:24:33 -07:00
|
|
|
memcpy( event, &record->event, len );
|
|
|
|
event += len;
|
|
|
|
if (len % sizeof(int))
|
|
|
|
{
|
|
|
|
memset( event, 0, sizeof(int) - len % sizeof(int) );
|
|
|
|
event += sizeof(int) - len % sizeof(int);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set_reply_data_ptr( data, size );
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( record, next, &events, struct change_record, entry )
|
|
|
|
{
|
|
|
|
list_remove( &record->entry );
|
|
|
|
free( record );
|
|
|
|
}
|
2006-02-06 03:58:55 -07:00
|
|
|
}
|