自用 Github粘贴代码用
抵抗惩罚吧
全部回复
仅看楼主
level 11
wincherst 楼主
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <linux/sched.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#define LEN 21
int len,has_read;
char *msg;
int read_proc(struct file *filp,char *buf,size_t count,loff_t *offp ) {
struct timespec time_ckt;
time_ckt=current_kernel_time();
unsigned long p=time_ckt.tv_sec;
int len_sec=0;
while(p/=10){
len_sec++;
}
len_sec++;
int i;
p=time_ckt.tv_sec;
for(i=0;i<len_sec;i++){
int x=1;
int j;
for(j=i+1;j<len_sec;j++){
x*=10;
}
msg[i]=p/x+'0';
p-=(msg[i]-'0')*x;
}
msg[len_sec]=' ';
p=time_ckt.tv_nsec;
int len_nsec=0;
while(p/=10){
len_nsec++;
}
len_nsec++;
if(len_nsec<9){
for(i=0;i<9-len_nsec;i++){
msg[len_sec+1+i]=' ';
}
}
p=time_ckt.tv_nsec;
for(i=0;i<len_nsec;i++){
int x=1;
int j;
for(j=i+1;j<len_nsec;j++){
x*=10;
}
msg[len_sec+1+9-len_nsec+i]=p/x+'0';
p-=(msg[len_sec+1+9-len_nsec+i]-'0')*x;
}
msg[len]='\0';
if(count+has_read>=len){
count=len-has_read;
}
has_read=has_read+count;
copy_to_user(buf, msg, count);
if(count==0)
has_read=0;
return count;
}
struct file_operations proc_fops = {
read: read_proc
};
//装载模块
int proc_init (void) {
msg=(char*)kmalloc(LEN*sizeof(char),GFP_KERNEL);
if(msg==NULL){
printk(KERN_INFO "分配内存失败");
return 0;
}
len=LEN;
has_read=0;
proc_create("clock",0,NULL,&proc_fops);
return 0;
}
//模块卸载
void proc_cleanup(void) {
remove_proc_entry("clock",NULL);
kfree(msg);
msg=NULL;
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);
2017年12月19日 11点12分 1
level 11
wincherst 楼主
ifneq ($(KERNELRELEASE),)
obj-m := clock.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif
clean:
$(MAKE) -C $(KERNELDIR) M=$(PWD) clean
2017年12月19日 11点12分 2
level 11
wincherst 楼主
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/mm.h>
MODULE_AUTHOR("[email protected].");
MODULE_DESCRIPTION("Kernel study and test.");
void fileread(const char * filename)
{
struct file *filp;
struct inode *inode;
mm_segment_t fs;
off_t fsize;
char *buf;
unsigned long magic;
printk("<1>start....\n");
filp=filp_open(filename,O_RDONLY,0);
inode=filp->f_dentry->d_inode;
magic=inode->i_sb->s_magic;
printk("<1>file system magic:%li \n",magic);
printk("<1>super blocksize:%li \n",inode->i_sb->s_blocksize);
printk("<1>inode %li \n",inode->i_ino);
fsize=inode->i_size;
printk("<1>file size:%i \n",(int)fsize);
buf=(char *) kmalloc(fsize+1,GFP_ATOMIC);
fs=get_fs();
set_fs(KERNEL_DS);
filp->f_op->read(filp,buf,fsize,&(filp->f_pos));
set_fs(fs);
buf[fsize]='\0';
printk("<1>The File Content is:\n");
printk("<1>%s",buf);
filp_close(filp,NULL);
}
void filewrite(char* filename, char* data)
{
struct file *filp;
mm_segment_t fs;
filp = filp_open(filename, O_RDWR|O_APPEND, 0644);
if(IS_ERR(filp))
{
printk("open error...\n");
return;
}
fs=get_fs();
set_fs(KERNEL_DS);
filp->f_op->write(filp, data, strlen(data),&filp->f_pos);
set_fs(fs);
filp_close(filp,NULL);
}
int init_module()
{
char *filename="/root/test1.c";
printk("<1>Read File from Kernel.\n");
fileread(filename);
filewrite(filename, "kernel write test\n");
return 0;
}
void cleanup_module()
{
printk("<1>Good,Bye!\n");
}
2017年12月20日 02点12分 3
level 11
wincherst 楼主
obj-m := os_attack.o
KDIR := /lib/modules/$(uname -r)/build/
PWD := $(shell pwd)
all:module
module:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf *.ko *.mod.c *.o Module.* modules.* .*.cmd .tmp_versions
2017年12月20日 02点12分 4
1