The printk() is not same as the C library function printf(). When developing a module, we can use printk(),
it just is supplied from kenerel itself.
Generally, library functions run at user status, system calls run at kernel status.
The major number of a device is used to determind which driver is linked to. The minor number of a device is
used to tell a individal device.
There is an example with ioctl function.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#define MESSAGE_LENGTH 80
static char Message[MESSAGE_LENGTH];
static struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "rw_test"
static ssize_t module_output(struct file *filp,
char *buffer,
size_t length,
loff_t *offset){
static int finished = 0;
int i;
char message[MESSAGE_LENGTH + 30];
if (finished){
finished = 0;
return 0;
}
sprintf(message, "Last input:%s", Message);
for (i = 0; i < length && message[i]; i++)
put_user(message[i], buffer + i);
finished = 1;
return i;
}
static ssize_t module_input(struct file *filp, const char *buff, size_t len, loff_t *off){
int i;
for (i = 0; i<MESSAGE_LENGTH - 1 && i < len; i++)
get_user(Message[i], buff + i);
Message[i] = '/0';
return i;
}
static int module_permission(struct inode *inode, int op, struct nameidata *foo){
if (op == 4 || (op == 2 && current->euid==0))
return 0;
return -EACCES;
}
int module_open(struct inode *inode, struct file *file){
try_module_get(THIS_MODULE);
return 0;
}
int module_close(struct inode *inode, struct file *file){
try_module_put(THIS_MODULE);
return 0;
}
int device_ioctl(struct inode *inode, struct file *file,
unsigned int ioctl_num, unsigned long ioctl_param){
int i;
char *temp;
char ch;
switch(ioctl_num){
case IOCTL_SET_MSG: temp = (char *)ioctl_param;
get_user(ch, temp);
for (i = 0; ch && i < BUF_LEN; i++, temp++)
get_user(ch, temp);
device_write(file, (char*)ioctl_param, i, 0);
break;
case IOCTL_GET_MSG: i = device_read(file, (char *)ioctl_param, 99, 0);
put_user('/0', (char*)ioctl_param + i);
break;
case IOCTL_GET_NTH_BYTE: return Message[ioctl_param];
break;
}
return 0;
}
int init_module(){
int rv = 0;
Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
Our_Proc_File->proc_fops = &File_Ops_4_Proc_File;
Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 80;
if (Our_Proc_File == NULL){
rv = -ENOMEM;
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
printk(KERN_INFO, "Error: Could not initialize /proc/test/n");
}
return rv;
}
void cleanup_module(){
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
}
Another example for modifing a system call.
#include <linux/kernel.h>
#include <linux/module.h>
#inlcude <linux/moduleparam.h>
#include <linux/unistd.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
extern void *sys_call_table[];
static int uid
module_param(uid, int, 0644);
asmlinkage int (*originall_call) (const char*, int, int);
asmlinkage int our_sys_open(const *filename, int flags, int mode){
int i = 0;
char ch;
if (uid == current->uid){
printk("Opened file by %d:", uid);
do{
get_user(ch, filename + i);
i++;
printk("%c", ch);
}while(ch != 0);
printk("/n");
}
return original_call(filename, flags, mode);
}
int init_module(){
original_call = sys_call_table[__NR_open];
sys_call_table[__NR_open]=our_sys_open;
}
void cleanup_module(){
if (sys_call_table[__NR_open] != our_sys_open){
printk("an unstable state./n");
}
sys_call_table[__NR_open]=original_call;
}
it just is supplied from kenerel itself.
Generally, library functions run at user status, system calls run at kernel status.
The major number of a device is used to determind which driver is linked to. The minor number of a device is
used to tell a individal device.
There is an example with ioctl function.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
#define MESSAGE_LENGTH 80
static char Message[MESSAGE_LENGTH];
static struct proc_dir_entry *Our_Proc_File;
#define PROC_ENTRY_FILENAME "rw_test"
static ssize_t module_output(struct file *filp,
char *buffer,
size_t length,
loff_t *offset){
static int finished = 0;
int i;
char message[MESSAGE_LENGTH + 30];
if (finished){
finished = 0;
return 0;
}
sprintf(message, "Last input:%s", Message);
for (i = 0; i < length && message[i]; i++)
put_user(message[i], buffer + i);
finished = 1;
return i;
}
static ssize_t module_input(struct file *filp, const char *buff, size_t len, loff_t *off){
int i;
for (i = 0; i<MESSAGE_LENGTH - 1 && i < len; i++)
get_user(Message[i], buff + i);
Message[i] = '/0';
return i;
}
static int module_permission(struct inode *inode, int op, struct nameidata *foo){
if (op == 4 || (op == 2 && current->euid==0))
return 0;
return -EACCES;
}
int module_open(struct inode *inode, struct file *file){
try_module_get(THIS_MODULE);
return 0;
}
int module_close(struct inode *inode, struct file *file){
try_module_put(THIS_MODULE);
return 0;
}
int device_ioctl(struct inode *inode, struct file *file,
unsigned int ioctl_num, unsigned long ioctl_param){
int i;
char *temp;
char ch;
switch(ioctl_num){
case IOCTL_SET_MSG: temp = (char *)ioctl_param;
get_user(ch, temp);
for (i = 0; ch && i < BUF_LEN; i++, temp++)
get_user(ch, temp);
device_write(file, (char*)ioctl_param, i, 0);
break;
case IOCTL_GET_MSG: i = device_read(file, (char *)ioctl_param, 99, 0);
put_user('/0', (char*)ioctl_param + i);
break;
case IOCTL_GET_NTH_BYTE: return Message[ioctl_param];
break;
}
return 0;
}
int init_module(){
int rv = 0;
Our_Proc_File = create_proc_entry(PROC_ENTRY_FILENAME, 0644, NULL);
Our_Proc_File->owner = THIS_MODULE;
Our_Proc_File->proc_iops = &Inode_Ops_4_Our_Proc_File;
Our_Proc_File->proc_fops = &File_Ops_4_Proc_File;
Our_Proc_File->mode = S_IFREG | S_IRUGO | S_IWUSR;
Our_Proc_File->uid = 0;
Our_Proc_File->gid = 0;
Our_Proc_File->size = 80;
if (Our_Proc_File == NULL){
rv = -ENOMEM;
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
printk(KERN_INFO, "Error: Could not initialize /proc/test/n");
}
return rv;
}
void cleanup_module(){
remove_proc_entry(PROC_ENTRY_FILENAME, &proc_root);
}
Another example for modifing a system call.
#include <linux/kernel.h>
#include <linux/module.h>
#inlcude <linux/moduleparam.h>
#include <linux/unistd.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
extern void *sys_call_table[];
static int uid
module_param(uid, int, 0644);
asmlinkage int (*originall_call) (const char*, int, int);
asmlinkage int our_sys_open(const *filename, int flags, int mode){
int i = 0;
char ch;
if (uid == current->uid){
printk("Opened file by %d:", uid);
do{
get_user(ch, filename + i);
i++;
printk("%c", ch);
}while(ch != 0);
printk("/n");
}
return original_call(filename, flags, mode);
}
int init_module(){
original_call = sys_call_table[__NR_open];
sys_call_table[__NR_open]=our_sys_open;
}
void cleanup_module(){
if (sys_call_table[__NR_open] != our_sys_open){
printk("an unstable state./n");
}
sys_call_table[__NR_open]=original_call;
}