以下是sample code..
test_read 回傳值表示還有多少需要讀取
test_write 回傳值表示已經處理了幾個byte
#include
#include
#include
#define TESTNAME "foo"
#define MAXLENGTH 64
static int test_read(char *page, char **start, off_t off,
int count, int *eof, void *data)
{
// return value means the remaining bytes need to read
return count;
}
static int test_write(struct file *file, const char *buffer,
unsigned long count, void *data)
{
char tmp[128];
int t1 = 0;
if (!count)
return 0;
if ( count > MAXLENGTH )
return -EINVAL;
if (buffer && !copy_from_user(tmp, buffer, 128)) {
sscanf(tmp, "%x", &t1);
printk("t1 = 0x[%x]\n", t1);
}
else{
return -EFAULT;
}
return count;
}
static int __init test_init(void)
{
struct proc_dir_entry *test_proc;
test_proc = create_proc_entry( TESTNAME, 0644, NULL);
if (test_proc == NULL) {
remove_proc_entry(TESTNAME, NULL);
printk(KERN_ALERT "Error: Could not initialize /proc/%s\n",
VTESTNAME1);
return -ENOMEM;
}
test_proc->read_proc = (read_proc_t *)test_read;
test_proc->write_proc = (write_proc_t *)test_write;
printk("/proc/%s created\n", TESTNAME);
return 0;
}
static void __exit test_exit( void )
{
remove_proc_entry( VTESTNAME, NULL );
}
module_init( test_init );
module_exit( test_exit );
- Dec 19 Thu 2013 22:26
[linux] proc
close
全站熱搜
留言列表
發表留言