2007-01-31 15:24:59 +03:00
/*
Unix SMB / CIFS implementation .
Copyright ( C ) Andrew Tridgell 2006
2009-06-27 13:31:53 +04:00
2007-01-31 15:24:59 +03:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2007-01-31 15:24:59 +03:00
( at your option ) any later version .
2009-06-27 13:31:53 +04:00
2007-01-31 15:24:59 +03:00
This program 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 General Public License for more details .
2009-06-27 13:31:53 +04:00
2007-01-31 15:24:59 +03:00
You should have received a copy of the GNU General Public License
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2007-01-31 15:24:59 +03:00
*/
2007-01-21 19:05:49 +03:00
/*
2007-01-31 15:24:59 +03:00
notify implementation using inotify
*/
2007-01-21 19:05:49 +03:00
# include "includes.h"
2010-05-05 03:39:16 +04:00
# include "../librpc/gen_ndr/notify.h"
2011-03-22 18:57:01 +03:00
# include "smbd/smbd.h"
2014-11-19 17:41:42 +03:00
# include "lib/sys_rw_data.h"
2007-01-31 16:47:37 +03:00
2008-10-30 22:30:04 +03:00
# include <sys/inotify.h>
2012-04-09 22:16:12 +04:00
/* glibc < 2.5 headers don't have these defines */
2007-01-31 15:24:59 +03:00
# ifndef IN_ONLYDIR
# define IN_ONLYDIR 0x01000000
# endif
# ifndef IN_MASK_ADD
# define IN_MASK_ADD 0x20000000
# endif
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
struct inotify_private {
struct sys_notify_context * ctx ;
int fd ;
2007-02-01 13:59:35 +03:00
struct inotify_watch_context * watches ;
2007-01-31 15:24:59 +03:00
} ;
2007-01-21 19:05:49 +03:00
2007-02-01 13:59:35 +03:00
struct inotify_watch_context {
struct inotify_watch_context * next , * prev ;
2007-01-31 15:24:59 +03:00
struct inotify_private * in ;
int wd ;
2007-01-31 16:47:37 +03:00
void ( * callback ) ( struct sys_notify_context * ctx ,
void * private_data ,
struct notify_event * ev ) ;
2007-01-31 15:24:59 +03:00
void * private_data ;
uint32_t mask ; /* the inotify mask */
uint32_t filter ; /* the windows completion filter */
const char * path ;
2007-01-21 19:05:49 +03:00
} ;
2007-01-31 15:24:59 +03:00
/*
destroy the inotify private context
*/
static int inotify_destructor ( struct inotify_private * in )
2007-01-21 19:05:49 +03:00
{
2007-01-31 15:24:59 +03:00
close ( in - > fd ) ;
2007-01-21 19:05:49 +03:00
return 0 ;
}
2007-01-31 15:24:59 +03:00
/*
see if a particular event from inotify really does match a requested
notify event in SMB
*/
2007-10-19 04:40:25 +04:00
static bool filter_match ( struct inotify_watch_context * w ,
2007-02-01 13:59:35 +03:00
struct inotify_event * e )
2007-01-31 15:24:59 +03:00
{
2014-10-26 12:13:41 +03:00
bool ok ;
2007-01-31 16:47:37 +03:00
DEBUG ( 10 , ( " filter_match: e->mask=%x, w->mask=%x, w->filter=%x \n " ,
e - > mask , w - > mask , w - > filter ) ) ;
2007-01-31 15:24:59 +03:00
if ( ( e - > mask & w - > mask ) = = 0 ) {
/* this happens because inotify_add_watch() coalesces watches on the same
path , oring their masks together */
return False ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
/* SMB separates the filters for files and directories */
if ( e - > mask & IN_ISDIR ) {
2014-10-26 12:13:41 +03:00
ok = ( ( w - > filter & FILE_NOTIFY_CHANGE_DIR_NAME ) ! = 0 ) ;
return ok ;
}
if ( ( e - > mask & IN_ATTRIB ) & &
( w - > filter & ( FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_EA |
FILE_NOTIFY_CHANGE_SECURITY ) ) ) {
return True ;
}
if ( ( e - > mask & IN_MODIFY ) & &
( w - > filter & FILE_NOTIFY_CHANGE_ATTRIBUTES ) ) {
return True ;
2007-01-21 19:05:49 +03:00
}
2014-10-26 12:13:41 +03:00
ok = ( ( w - > filter & FILE_NOTIFY_CHANGE_FILE_NAME ) ! = 0 ) ;
return ok ;
2007-01-31 15:24:59 +03:00
}
2009-06-27 13:31:53 +04:00
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
/*
dispatch one inotify event
2009-06-27 13:31:53 +04:00
2007-01-31 15:24:59 +03:00
the cookies are used to correctly handle renames
*/
static void inotify_dispatch ( struct inotify_private * in ,
struct inotify_event * e ,
2014-12-05 18:38:45 +03:00
int prev_wd ,
2007-01-31 15:24:59 +03:00
uint32_t prev_cookie ,
struct inotify_event * e2 )
2007-01-21 19:05:49 +03:00
{
2007-02-01 13:59:35 +03:00
struct inotify_watch_context * w , * next ;
2007-01-31 15:24:59 +03:00
struct notify_event ne ;
2007-01-31 16:47:37 +03:00
DEBUG ( 10 , ( " inotify_dispatch called with mask=%x, name=[%s] \n " ,
e - > mask , e - > len ? e - > name : " " ) ) ;
2007-01-31 15:24:59 +03:00
/* ignore extraneous events, such as unmount and IN_IGNORED events */
if ( ( e - > mask & ( IN_ATTRIB | IN_MODIFY | IN_CREATE | IN_DELETE |
IN_MOVED_FROM | IN_MOVED_TO ) ) = = 0 ) {
return ;
}
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
/* map the inotify mask to a action. This gets complicated for
renames */
if ( e - > mask & IN_CREATE ) {
ne . action = NOTIFY_ACTION_ADDED ;
} else if ( e - > mask & IN_DELETE ) {
ne . action = NOTIFY_ACTION_REMOVED ;
} else if ( e - > mask & IN_MOVED_FROM ) {
2014-12-05 18:38:45 +03:00
if ( e2 ! = NULL & & e2 - > cookie = = e - > cookie & &
e2 - > wd = = e - > wd ) {
2007-01-31 15:24:59 +03:00
ne . action = NOTIFY_ACTION_OLD_NAME ;
} else {
ne . action = NOTIFY_ACTION_REMOVED ;
}
} else if ( e - > mask & IN_MOVED_TO ) {
2014-12-05 18:38:45 +03:00
if ( ( e - > cookie = = prev_cookie ) & & ( e - > wd = = prev_wd ) ) {
2007-01-31 15:24:59 +03:00
ne . action = NOTIFY_ACTION_NEW_NAME ;
} else {
ne . action = NOTIFY_ACTION_ADDED ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
} else {
ne . action = NOTIFY_ACTION_MODIFIED ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
ne . path = e - > name ;
2007-01-21 19:05:49 +03:00
2007-01-31 16:47:37 +03:00
DEBUG ( 10 , ( " inotify_dispatch: ne.action = %d, ne.path = %s \n " ,
ne . action , ne . path ) ) ;
2007-01-31 15:24:59 +03:00
/* find any watches that have this watch descriptor */
for ( w = in - > watches ; w ; w = next ) {
next = w - > next ;
if ( w - > wd = = e - > wd & & filter_match ( w , e ) ) {
2014-11-05 14:44:42 +03:00
ne . dir = w - > path ;
2007-01-31 15:24:59 +03:00
w - > callback ( in - > ctx , w - > private_data , & ne ) ;
}
2007-01-21 19:05:49 +03:00
}
2014-12-04 19:01:17 +03:00
if ( ( ne . action = = NOTIFY_ACTION_NEW_NAME ) & &
( ( e - > mask & IN_ISDIR ) = = 0 ) ) {
2007-01-21 19:05:49 +03:00
2014-12-04 19:01:17 +03:00
/*
* SMB expects a file rename to generate three events , two for
* the rename and the other for a modify of the
* destination . Strange !
*/
2009-06-27 13:31:53 +04:00
2014-12-04 19:01:17 +03:00
ne . action = NOTIFY_ACTION_MODIFIED ;
e - > mask = IN_ATTRIB ;
for ( w = in - > watches ; w ; w = next ) {
next = w - > next ;
if ( w - > wd = = e - > wd & & filter_match ( w , e ) & &
! ( w - > filter & FILE_NOTIFY_CHANGE_CREATION ) ) {
ne . dir = w - > path ;
w - > callback ( in - > ctx , w - > private_data , & ne ) ;
}
2007-01-31 15:24:59 +03:00
}
2007-01-21 19:05:49 +03:00
}
}
2007-01-31 15:24:59 +03:00
/*
called when the kernel has some events for us
*/
2013-02-18 13:24:12 +04:00
static void inotify_handler ( struct tevent_context * ev , struct tevent_fd * fde ,
2007-01-31 15:24:59 +03:00
uint16_t flags , void * private_data )
2007-01-21 19:05:49 +03:00
{
2007-01-31 15:24:59 +03:00
struct inotify_private * in = talloc_get_type ( private_data ,
struct inotify_private ) ;
int bufsize = 0 ;
struct inotify_event * e0 , * e ;
uint32_t prev_cookie = 0 ;
2014-12-05 18:38:45 +03:00
int prev_wd = - 1 ;
2014-11-19 17:41:42 +03:00
ssize_t ret ;
2007-01-21 19:05:49 +03:00
/*
we must use FIONREAD as we cannot predict the length of the
filenames , and thus can ' t know how much to allocate
otherwise
*/
2007-01-31 15:24:59 +03:00
if ( ioctl ( in - > fd , FIONREAD , & bufsize ) ! = 0 | |
bufsize = = 0 ) {
2007-01-21 19:05:49 +03:00
DEBUG ( 0 , ( " No data on inotify fd?! \n " ) ) ;
2009-06-27 14:20:18 +04:00
TALLOC_FREE ( fde ) ;
2007-01-21 19:05:49 +03:00
return ;
}
2010-04-20 00:42:55 +04:00
e0 = e = ( struct inotify_event * ) TALLOC_SIZE ( in , bufsize + 1 ) ;
2007-01-31 15:24:59 +03:00
if ( e = = NULL ) return ;
2010-04-20 00:42:55 +04:00
( ( uint8_t * ) e ) [ bufsize ] = ' \0 ' ;
2007-01-21 19:05:49 +03:00
2014-11-19 17:41:42 +03:00
ret = read_data ( in - > fd , e0 , bufsize ) ;
if ( ret ! = bufsize ) {
DEBUG ( 0 , ( " Failed to read all inotify data - %s \n " ,
strerror ( errno ) ) ) ;
2007-01-31 15:24:59 +03:00
talloc_free ( e0 ) ;
2009-09-05 18:18:12 +04:00
/* the inotify fd will now be out of sync,
* can ' t keep reading data off it */
TALLOC_FREE ( fde ) ;
2007-01-21 19:05:49 +03:00
return ;
}
2007-01-31 15:24:59 +03:00
/* we can get more than one event in the buffer */
2008-10-30 02:28:57 +03:00
while ( e & & ( bufsize > = sizeof ( * e ) ) ) {
2007-01-31 15:24:59 +03:00
struct inotify_event * e2 = NULL ;
bufsize - = e - > len + sizeof ( * e ) ;
if ( bufsize > = sizeof ( * e ) ) {
e2 = ( struct inotify_event * ) ( e - > len + sizeof ( * e ) + ( char * ) e ) ;
}
2014-12-05 18:38:45 +03:00
inotify_dispatch ( in , e , prev_wd , prev_cookie , e2 ) ;
prev_wd = e - > wd ;
2007-01-31 15:24:59 +03:00
prev_cookie = e - > cookie ;
e = e2 ;
}
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
talloc_free ( e0 ) ;
}
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
/*
setup the inotify handle - called the first time a watch is added on
this context
*/
2014-10-27 16:09:44 +03:00
static int inotify_setup ( struct sys_notify_context * ctx )
2007-01-31 15:24:59 +03:00
{
struct inotify_private * in ;
2014-10-27 16:07:03 +03:00
struct tevent_fd * fde ;
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
in = talloc ( ctx , struct inotify_private ) ;
2014-10-27 16:09:44 +03:00
if ( in = = NULL ) {
return ENOMEM ;
}
2007-01-31 15:24:59 +03:00
in - > fd = inotify_init ( ) ;
if ( in - > fd = = - 1 ) {
2014-10-27 16:09:44 +03:00
int ret = errno ;
DEBUG ( 0 , ( " Failed to init inotify - %s \n " , strerror ( ret ) ) ) ;
2007-01-31 15:24:59 +03:00
talloc_free ( in ) ;
2014-10-27 16:09:44 +03:00
return ret ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
in - > ctx = ctx ;
in - > watches = NULL ;
ctx - > private_data = in ;
talloc_set_destructor ( in , inotify_destructor ) ;
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
/* add a event waiting for the inotify fd to be readable */
2014-10-27 16:07:03 +03:00
fde = tevent_add_fd ( ctx - > ev , in , in - > fd , TEVENT_FD_READ ,
inotify_handler , in ) ;
if ( fde = = NULL ) {
ctx - > private_data = NULL ;
TALLOC_FREE ( in ) ;
2014-10-27 16:09:44 +03:00
return ENOMEM ;
2014-10-27 16:07:03 +03:00
}
2014-10-27 16:09:44 +03:00
return 0 ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
/*
map from a change notify mask to a inotify mask . Remove any bits
which we can handle
*/
static const struct {
uint32_t notify_mask ;
uint32_t inotify_mask ;
} inotify_mapping [ ] = {
{ FILE_NOTIFY_CHANGE_FILE_NAME , IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO } ,
{ FILE_NOTIFY_CHANGE_DIR_NAME , IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO } ,
{ FILE_NOTIFY_CHANGE_ATTRIBUTES , IN_ATTRIB | IN_MOVED_TO | IN_MOVED_FROM | IN_MODIFY } ,
{ FILE_NOTIFY_CHANGE_LAST_WRITE , IN_ATTRIB } ,
{ FILE_NOTIFY_CHANGE_LAST_ACCESS , IN_ATTRIB } ,
{ FILE_NOTIFY_CHANGE_EA , IN_ATTRIB } ,
{ FILE_NOTIFY_CHANGE_SECURITY , IN_ATTRIB }
2007-01-21 19:05:49 +03:00
} ;
2012-03-26 15:00:53 +04:00
static uint32_t inotify_map ( uint32_t * filter )
2007-01-31 15:24:59 +03:00
{
int i ;
uint32_t out = 0 ;
for ( i = 0 ; i < ARRAY_SIZE ( inotify_mapping ) ; i + + ) {
2012-03-26 15:00:53 +04:00
if ( inotify_mapping [ i ] . notify_mask & * filter ) {
2007-01-31 15:24:59 +03:00
out | = inotify_mapping [ i ] . inotify_mask ;
2012-03-26 15:00:53 +04:00
* filter & = ~ inotify_mapping [ i ] . notify_mask ;
2007-01-31 15:24:59 +03:00
}
}
return out ;
}
/*
destroy a watch
*/
2007-02-01 13:59:35 +03:00
static int watch_destructor ( struct inotify_watch_context * w )
2007-01-21 19:05:49 +03:00
{
2007-01-31 15:24:59 +03:00
struct inotify_private * in = w - > in ;
int wd = w - > wd ;
DLIST_REMOVE ( w - > in - > watches , w ) ;
for ( w = in - > watches ; w ; w = w - > next ) {
2014-10-27 16:20:15 +03:00
if ( w - > wd = = wd ) {
/*
* Another inotify_watch_context listens on this path ,
* leave the kernel level watch in place
*/
return 0 ;
2007-01-31 16:47:37 +03:00
}
2014-10-27 16:20:15 +03:00
}
2009-06-27 13:31:53 +04:00
2014-10-27 16:20:15 +03:00
DEBUG ( 10 , ( " Deleting inotify watch %d \n " , wd ) ) ;
if ( inotify_rm_watch ( in - > fd , wd ) = = - 1 ) {
DEBUG ( 1 , ( " inotify_rm_watch returned %s \n " , strerror ( errno ) ) ) ;
2007-01-31 15:24:59 +03:00
}
return 0 ;
}
/*
add a watch . The watch is removed when the caller calls
talloc_free ( ) on * handle
*/
2014-11-05 15:18:31 +03:00
int inotify_watch ( TALLOC_CTX * mem_ctx ,
struct sys_notify_context * ctx ,
2014-10-27 16:26:35 +03:00
const char * path ,
uint32_t * filter ,
uint32_t * subdir_filter ,
void ( * callback ) ( struct sys_notify_context * ctx ,
void * private_data ,
struct notify_event * ev ) ,
void * private_data ,
void * handle_p )
2007-01-31 15:24:59 +03:00
{
struct inotify_private * in ;
uint32_t mask ;
2007-02-01 13:59:35 +03:00
struct inotify_watch_context * w ;
2012-03-26 14:46:11 +04:00
uint32_t orig_filter = * filter ;
2007-01-31 15:24:59 +03:00
void * * handle = ( void * * ) handle_p ;
/* maybe setup the inotify fd */
if ( ctx - > private_data = = NULL ) {
2014-10-27 16:09:44 +03:00
int ret ;
ret = inotify_setup ( ctx ) ;
if ( ret ! = 0 ) {
2014-10-27 16:26:35 +03:00
return ret ;
2014-10-27 16:09:44 +03:00
}
2007-01-31 15:24:59 +03:00
}
2007-01-21 19:05:49 +03:00
2007-01-31 15:24:59 +03:00
in = talloc_get_type ( ctx - > private_data , struct inotify_private ) ;
2007-01-21 19:05:49 +03:00
2012-03-26 14:46:11 +04:00
mask = inotify_map ( filter ) ;
2007-01-31 15:24:59 +03:00
if ( mask = = 0 ) {
/* this filter can't be handled by inotify */
2014-10-27 16:26:35 +03:00
return EINVAL ;
2007-01-21 19:05:49 +03:00
}
2007-01-31 15:24:59 +03:00
/* using IN_MASK_ADD allows us to cope with inotify() returning the same
2012-03-26 15:11:02 +04:00
watch descriptor for multiple watches on the same path */
2007-01-31 15:24:59 +03:00
mask | = ( IN_MASK_ADD | IN_ONLYDIR ) ;
2014-11-05 15:18:31 +03:00
w = talloc ( mem_ctx , struct inotify_watch_context ) ;
2007-01-31 15:24:59 +03:00
if ( w = = NULL ) {
2012-03-26 14:46:11 +04:00
* filter = orig_filter ;
2014-10-27 16:26:35 +03:00
return ENOMEM ;
2007-01-31 15:24:59 +03:00
}
w - > in = in ;
w - > callback = callback ;
w - > private_data = private_data ;
w - > mask = mask ;
2012-03-26 15:09:33 +04:00
w - > filter = orig_filter ;
2012-03-20 00:57:50 +04:00
w - > path = talloc_strdup ( w , path ) ;
2007-01-31 15:24:59 +03:00
if ( w - > path = = NULL ) {
2012-03-26 14:46:11 +04:00
* filter = orig_filter ;
2013-07-18 03:29:39 +04:00
TALLOC_FREE ( w ) ;
2014-10-27 16:26:35 +03:00
return ENOMEM ;
2007-01-31 15:24:59 +03:00
}
2014-10-27 16:15:12 +03:00
/* get a new watch descriptor for this path */
w - > wd = inotify_add_watch ( in - > fd , path , mask ) ;
if ( w - > wd = = - 1 ) {
int err = errno ;
* filter = orig_filter ;
TALLOC_FREE ( w ) ;
DEBUG ( 1 , ( " inotify_add_watch returned %s \n " , strerror ( err ) ) ) ;
2014-10-27 16:26:35 +03:00
return err ;
2014-10-27 16:15:12 +03:00
}
DEBUG ( 10 , ( " inotify_add_watch for %s mask %x returned wd %d \n " ,
path , mask , w - > wd ) ) ;
2007-01-31 15:24:59 +03:00
( * handle ) = w ;
DLIST_ADD ( in - > watches , w ) ;
/* the caller frees the handle to stop watching */
talloc_set_destructor ( w , watch_destructor ) ;
2009-06-27 13:31:53 +04:00
2014-10-27 16:26:35 +03:00
return 0 ;
2007-01-21 19:05:49 +03:00
}