kernel: sulog: Simplify code using vfs_truncate

This commit is contained in:
ShirkNeko
2025-10-23 22:15:44 +08:00
parent 0cf65d71b6
commit 163424673f
4 changed files with 4 additions and 75 deletions

View File

@@ -238,40 +238,3 @@ long ksu_copy_from_user_nofault(void *dst, const void __user *src, size_t size)
return 0; return 0;
#endif #endif
} }
int ksu_vfs_unlink(struct inode *dir, struct dentry *dentry)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 6, 0)
struct inode *delegated_inode = NULL;
return vfs_unlink(&nop_mnt_idmap, dir, dentry, &delegated_inode);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
struct inode *delegated_inode = NULL;
return vfs_unlink(&init_user_ns, dir, dentry, &delegated_inode);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
struct inode *delegated_inode = NULL;
return vfs_unlink(dir, dentry, &delegated_inode);
#else
return vfs_unlink(dir, dentry);
#endif
}
int ksu_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry)
{
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 12, 0)
struct renamedata rd = {
.old_dir = old_dir,
.old_dentry = old_dentry,
.new_dir = new_dir,
.new_dentry = new_dentry,
.delegated_inode = NULL,
.flags = 0,
};
return vfs_rename(&rd);
#elif LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
struct inode *delegated_inode = NULL;
return vfs_rename(old_dir, old_dentry, new_dir, new_dentry, &delegated_inode, 0);
#else
return vfs_rename(old_dir, old_dentry, new_dir, new_dentry);
#endif
}

View File

@@ -117,8 +117,4 @@ static long ksu_copy_from_user_retry(void *to,
#define ksu_access_ok(addr, size) access_ok(VERIFY_READ, addr, size) #define ksu_access_ok(addr, size) access_ok(VERIFY_READ, addr, size)
#endif #endif
extern int ksu_vfs_unlink(struct inode *dir, struct dentry *dentry);
extern int ksu_vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
struct inode *new_dir, struct dentry *new_dentry);
#endif #endif

View File

@@ -135,40 +135,11 @@ static void sulog_work_handler(struct work_struct *work)
} }
if (fp->f_inode->i_size > SULOG_MAX_SIZE) { if (fp->f_inode->i_size > SULOG_MAX_SIZE) {
pr_info("sulog: rotating log file, size: %lld\n", fp->f_inode->i_size); pr_info("sulog: log file exceeds maximum size, clearing...\n");
filp_close(fp, 0); if (vfs_truncate(&fp->f_path, 0)) {
pr_err("sulog: failed to truncate log file\n");
struct path old_path;
if (!kern_path(SULOG_OLD_PATH, 0, &old_path)) {
ksu_vfs_unlink(old_path.dentry->d_parent->d_inode, old_path.dentry);
path_put(&old_path);
} }
pos = 0;
struct path current_path, parent_path;
if (!kern_path(SULOG_PATH, 0, &current_path)) {
parent_path = current_path;
path_get(&parent_path);
parent_path.dentry = current_path.dentry->d_parent;
struct dentry *old_dentry = lookup_one_len("sulog.log.old",
parent_path.dentry, strlen("sulog.log.old"));
if (!IS_ERR(old_dentry)) {
ksu_vfs_rename(parent_path.dentry->d_inode, current_path.dentry,
parent_path.dentry->d_inode, old_dentry);
dput(old_dentry);
}
path_put(&current_path);
path_put(&parent_path);
}
fp = ksu_filp_open_compat(SULOG_PATH, O_WRONLY | O_CREAT | O_EXCL, 0640);
if (IS_ERR(fp)) {
pr_err("sulog: failed to create new log file: %ld\n", PTR_ERR(fp));
goto cleanup;
}
const char *rotate_msg = "=== Log file rotated, old log saved as sulog.log.old ===\n";
ksu_kernel_write_compat(fp, rotate_msg, strlen(rotate_msg), &pos);
} else { } else {
pos = fp->f_inode->i_size; pos = fp->f_inode->i_size;
} }

View File

@@ -6,7 +6,6 @@
extern struct timezone sys_tz; extern struct timezone sys_tz;
#define SULOG_PATH "/data/adb/ksu/log/sulog.log" #define SULOG_PATH "/data/adb/ksu/log/sulog.log"
#define SULOG_OLD_PATH "/data/adb/ksu/log/sulog.log.old"
#define SULOG_MAX_SIZE (128 * 1024 * 1024) // 128MB #define SULOG_MAX_SIZE (128 * 1024 * 1024) // 128MB
#define SULOG_ENTRY_MAX_LEN 512 #define SULOG_ENTRY_MAX_LEN 512
#define SULOG_COMM_LEN 256 #define SULOG_COMM_LEN 256