I have given the permission to the file and the right path, and yet it fails to write.
Error: Failed to write the file: -22
Kernel version: 2.6.9-89.ELsmp
static int writeMsgToFile(const char *data) {
struct file *file;
mm_segment_t old_fs;
loff_t pos;
int ret;
static loff_t file_size = 0; // Global variable to store the file size
// Open the file for writing
file = filp_open(SLID_FILE_PATH, O_WRONLY | O_CREAT | O_APPEND , 0666);
if (IS_ERR(file)) {
printk(KERN_ERR "Failed to open the file: %ld\n", PTR_ERR(file));
return PTR_ERR(file);
}
old_fs = get_fs();
set_fs(get_ds());
ret = file->f_op->write(file, data, strlen(data), &pos); //Write is failing here
if (ret < 0) {
printk(KERN_ERR "Failed to write the file: %d\n", ret);
} else {
printk(KERN_INFO "Values written to the file successfully\n");
}
set_fs(old_fs);
file->f_pos = pos;
filp_close(file, NULL);
return ret;
}
This code is written in kernel level.
I also have a few more requirement to add to this code
- need the file to be on 10Kb
- Once the data is written to the file and it exceeds 10 kb, it should create a new file.old (let's call it file.old) and write all the data from file to file.old
- After it is written to file.old, we should delete the content in file and make room for the next data to be loaded.
I'm new to programing in kernel level, thank you!