kernel: sulog: Simplify code using vfs_truncate

This commit is contained in:
ShirkNeko
2025-10-23 22:16:06 +08:00
parent 4b86989bf9
commit 6a1e1d788b
4 changed files with 4 additions and 74 deletions

View File

@@ -93,39 +93,3 @@ long ksu_strncpy_from_user_nofault(char *dst, const void __user *unsafe_addr,
return strncpy_from_user_nofault(dst, unsafe_addr, count);
}
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

@@ -94,8 +94,4 @@ static long ksu_copy_from_user_retry(void *to,
return copy_from_user(to, from, count);
}
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

View File

@@ -135,40 +135,11 @@ static void sulog_work_handler(struct work_struct *work)
}
if (fp->f_inode->i_size > SULOG_MAX_SIZE) {
pr_info("sulog: rotating log file, size: %lld\n", fp->f_inode->i_size);
filp_close(fp, 0);
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);
pr_info("sulog: log file exceeds maximum size, clearing...\n");
if (vfs_truncate(&fp->f_path, 0)) {
pr_err("sulog: failed to truncate log file\n");
}
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);
pos = 0;
} else {
pos = fp->f_inode->i_size;
}

View File

@@ -6,7 +6,6 @@
extern struct timezone sys_tz;
#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_ENTRY_MAX_LEN 512
#define SULOG_COMM_LEN 256