Skip to content

Commit b597541

Browse files
rsuntkTwinbornPlate75
authored andcommitted
kernelsu: pkg_observer: Add backward compatibility
Co-authored-by: TwinbornPlate75 <[email protected]> Signed-off-by: Pranav Vashi <[email protected]>
1 parent fd36865 commit b597541

File tree

2 files changed

+79
-14
lines changed

2 files changed

+79
-14
lines changed

drivers/kernelsu/pkg_observer.c

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
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

2223
static 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

4039
static 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+
4454
static 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

123146
void 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
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This header should not be used outside of pkg_observer.c!
2+
3+
#include <linux/version.h>
4+
5+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
6+
typedef const struct qstr *ksu_fname_t;
7+
#define ksu_fname_len(f) ((f)->len)
8+
#define ksu_fname_arg(f) ((f)->name)
9+
#else
10+
typedef const unsigned char *ksu_fname_t;
11+
#define ksu_fname_len(f) (strlen(f))
12+
#define ksu_fname_arg(f) (f)
13+
#endif
14+
15+
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 9, 0)
16+
#define KSU_DECL_FSNOTIFY_OPS(name) \
17+
int name(struct fsnotify_mark *mark, u32 mask, struct inode *inode, \
18+
struct inode *dir, const struct qstr *file_name, u32 cookie)
19+
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 2, 0)
20+
#define KSU_DECL_FSNOTIFY_OPS(name) \
21+
int name(struct fsnotify_group *group, struct inode *inode, u32 mask, \
22+
const void *data, int data_type, ksu_fname_t file_name, \
23+
u32 cookie, struct fsnotify_iter_info *iter_info)
24+
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 18, 0)
25+
#define KSU_DECL_FSNOTIFY_OPS(name) \
26+
int name(struct fsnotify_group *group, struct inode *inode, u32 mask, \
27+
const void *data, int data_type, ksu_fname_t file_name, \
28+
u32 cookie, struct fsnotify_iter_info *iter_info)
29+
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
30+
#define KSU_DECL_FSNOTIFY_OPS(name) \
31+
int name(struct fsnotify_group *group, struct inode *inode, \
32+
struct fsnotify_mark *inode_mark, \
33+
struct fsnotify_mark *vfsmount_mark, u32 mask, \
34+
const void *data, int data_type, ksu_fname_t file_name, \
35+
u32 cookie, struct fsnotify_iter_info *iter_info)
36+
#else
37+
#define KSU_DECL_FSNOTIFY_OPS(name) \
38+
int name(struct fsnotify_group *group, struct inode *inode, \
39+
struct fsnotify_mark *inode_mark, \
40+
struct fsnotify_mark *vfsmount_mark, u32 mask, void *data, \
41+
int data_type, ksu_fname_t file_name, u32 cookie)
42+
#endif

0 commit comments

Comments
 (0)