22#include <linux/module.h>
33#include <linux/fs.h>
44#include <linux/namei.h>
5- #include <linux/fsnotify_backend .h>
5+ #include <linux/fsnotify .h>
66#include <linux/slab.h>
7+ #include <linux/string.h>
78#include <linux/rculist.h>
89#include <linux/version.h>
910#include "klog.h" // IWYU pragma: keep
@@ -21,42 +22,64 @@ struct watch_dir {
2122
2223static struct fsnotify_group * g ;
2324
24- static int ksu_handle_inode_event (struct fsnotify_mark * mark , u32 mask ,
25- struct inode * inode , struct inode * dir ,
26- const struct qstr * file_name , u32 cookie )
25+ #include "pkg_observer_defs.h" // KSU_DECL_FSNOTIFY_OPS
26+ static KSU_DECL_FSNOTIFY_OPS (ksu_handle_generic_event )
2727{
28- if (!file_name )
28+ if (!file_name || ( mask & FS_ISDIR ) )
2929 return 0 ;
30- if (mask & FS_ISDIR )
31- return 0 ;
32- if (file_name -> len == 13 &&
33- !memcmp (file_name -> name , "packages.list" , 13 )) {
30+
31+ if (ksu_fname_len (file_name ) == 13 &&
32+ !memcmp (ksu_fname_arg (file_name ), "packages.list" , 13 )) {
3433 pr_info ("packages.list detected: %d\n" , mask );
3534 track_throne ();
3635 }
3736 return 0 ;
3837}
3938
4039static const struct fsnotify_ops ksu_ops = {
41- .handle_inode_event = ksu_handle_inode_event ,
40+ #if LINUX_VERSION_CODE >= KERNEL_VERSION (5 , 9 , 0 )
41+ .handle_inode_event = ksu_handle_generic_event ,
42+ #else
43+ .handle_event = ksu_handle_generic_event ,
44+ #endif
4245};
4346
47+ static void __maybe_unused m_free (struct fsnotify_mark * m )
48+ {
49+ if (m ) {
50+ kfree (m );
51+ }
52+ }
53+
4454static int add_mark_on_inode (struct inode * inode , u32 mask ,
4555 struct fsnotify_mark * * out )
4656{
4757 struct fsnotify_mark * m ;
58+ int ret ;
4859
4960 m = kzalloc (sizeof (* m ), GFP_KERNEL );
5061 if (!m )
5162 return - ENOMEM ;
5263
64+ #if LINUX_VERSION_CODE < KERNEL_VERSION (4 , 12 , 0 )
65+ fsnotify_init_mark (m , m_free );
66+ m -> mask = mask ;
67+ ret = fsnotify_add_mark (m , g , inode , NULL , 0 );
68+ #else
5369 fsnotify_init_mark (m , g );
5470 m -> mask = mask ;
71+ #if LINUX_VERSION_CODE >= KERNEL_VERSION (4 , 18 , 0 )
72+ ret = fsnotify_add_inode_mark (m , inode , 0 );
73+ #else
74+ ret = fsnotify_add_mark (m , inode , NULL , 0 );
75+ #endif
76+ #endif
5577
56- if (fsnotify_add_inode_mark ( m , inode , 0 ) ) {
78+ if (ret < 0 ) {
5779 fsnotify_put_mark (m );
58- return - EINVAL ;
80+ return ret ;
5981 }
82+
6083 * out = m ;
6184 return 0 ;
6285}
@@ -116,13 +139,13 @@ int ksu_observer_init(void)
116139 return PTR_ERR (g );
117140
118141 ret = watch_one_dir (& g_watch );
119- pr_info ("observer init done\n" );
142+ pr_info ("%s done. \n" , __func__ );
120143 return 0 ;
121144}
122145
123146void ksu_observer_exit (void )
124147{
125148 unwatch_one_dir (& g_watch );
126149 fsnotify_put_group (g );
127- pr_info ("observer exit done\n" );
150+ pr_info ("%s: done. \n" , __func__ );
128151}
0 commit comments