@ A resposta de Calandoa não funciona mais para o Kernel +3.10. Combinou o código dele e o código de exemplo que encontrei aqui . Em seguida, melhorou a qualidade do código ...
Código salvo em printk_user.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
static ssize_t write_proc(struct file *filep, const char *buffer, size_t count, loff_t *offsetp)
{
char string[256];
count = count < 255 ? count : 255;
if(copy_from_user(string, buffer, count) != 0) {
return -EFAULT;
}
string[count] = 'TARGET = printk_user
obj-m := $(TARGET).o
KERNEL_VERSION=$(shell uname -r)
KDIR = /lib/modules/$(KERNEL_VERSION)/build
PWD = $(shell pwd)
printk:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
';
printk(string);
return count;
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.write = write_proc,
};
static int proc_init(void) {
struct proc_dir_entry *proc_file;
proc_file = proc_create("printk_user", 0, NULL, &proc_fops);
if(proc_file == NULL) {
return -ENOMEM;
}
return 0;
}
static void proc_cleanup(void) {
remove_proc_entry("printk_user", NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);
Faça usando este Makefile
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/proc_fs.h>
#include <asm/uaccess.h>
static ssize_t write_proc(struct file *filep, const char *buffer, size_t count, loff_t *offsetp)
{
char string[256];
count = count < 255 ? count : 255;
if(copy_from_user(string, buffer, count) != 0) {
return -EFAULT;
}
string[count] = 'TARGET = printk_user
obj-m := $(TARGET).o
KERNEL_VERSION=$(shell uname -r)
KDIR = /lib/modules/$(KERNEL_VERSION)/build
PWD = $(shell pwd)
printk:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) M=$(PWD) clean
';
printk(string);
return count;
}
static const struct file_operations proc_fops = {
.owner = THIS_MODULE,
.write = write_proc,
};
static int proc_init(void) {
struct proc_dir_entry *proc_file;
proc_file = proc_create("printk_user", 0, NULL, &proc_fops);
if(proc_file == NULL) {
return -ENOMEM;
}
return 0;
}
static void proc_cleanup(void) {
remove_proc_entry("printk_user", NULL);
}
MODULE_LICENSE("GPL");
module_init(proc_init);
module_exit(proc_cleanup);