>>Т.е. ты хочешь из ядра открыть и прочитать файл. Так?
>
>вроде так
Poprobuy etot code:
/* copy_file.c */
#include <linux/module.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>// get_fs(), set_fs(), KERNEL_DS
#include <linux/file.h> // fput()
#include <linux/mm.h> // GFP_KERNEL
#include <linux/slab.h> // kmalloc
void copy_file(const char * fread, const char * fwrite)
{
int length_read, length_write;
char buffer[255];
struct file * f = NULL;
struct inode *inode;
mm_segment_t orig_fs;
f = filp_open(fread, O_RDONLY, 00);
if (!f || !f->f_op || !f->f_op->read) {
printk(KERN_INFO "File (read) object is a null pointer!\n");
return;
}
//f->f_pos = 2;
inode = f->f_dentry->d_inode;
printk(KERN_INFO "File size - %u\n", (unsigned int)inode->i_size);
orig_fs = get_fs();
set_fs(KERNEL_DS);
length_read = f->f_op->read(f, buffer, 4, &f->f_pos);
fput(f);
f = filp_open(fwrite, O_WRONLY | O_CREAT, 0600);
if (!f || !f->f_op || !f->f_op->write) {
printk(KERN_INFO "File (write) object is a null pointer!\n");
return;
}
//f->f_pos = 4;
orig_fs = get_fs();
set_fs(KERNEL_DS);
length_write = f->f_op->write(f, buffer, 4, &f->f_pos);
fput(f);
return;
}
static int __init copymodule_init(void)
{
unsigned char *file_to_read = kmalloc(20, GFP_KERNEL);
unsigned char *file_to_write = kmalloc(20, GFP_KERNEL);
file_to_read = "/home/readwrite/mark";
file_to_write = "/home/readwrite/mike";
copy_file(file_to_read, file_to_write);
return 0;
}
static void __exit copymodule_exit(void)
{
return;
}
module_init(copymodule_init);
module_exit(copymodule_exit);
MODULE_LICENSE("GPL");
/* end of copy_file.c */
#Makefile:
CC = gcc
CFLAGS = -O2 -Wall
LINUX = /usr/src/linux
MODFLAGS = -D__KERNEL__ -DMODULE -I$(LINUX)/include
copy_file.o: copy_file.c
$(CC) $(CFLAGS) $(MODFLAGS) -c copy_file.c
#end of Makefile
Posle compilyacii vvodi comandu:
insmod copy_file.o
Tolko pravil`no zaday imena faylov (file_to_read i file_to_write).