Skip to content

Instantly share code, notes, and snippets.

@joyhuang9473
Last active August 29, 2015 14:10
Show Gist options
  • Save joyhuang9473/8e8c7b6f35bdd7963510 to your computer and use it in GitHub Desktop.
Save joyhuang9473/8e8c7b6f35bdd7963510 to your computer and use it in GitHub Desktop.
implement custom system call by module way
obj-m += project_mod.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
#include <linux/linkage.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/path.h>
#include <linux/slab.h>
static void print_mem(struct task_struct *task) {
struct mm_struct *mm;
struct vm_area_struct *vma;
struct path base_path;
char* tpath = NULL;
char* ret_ptr = NULL;
mm = task->mm;
// printk("\nThis mm_struct has %d vmas.\n", mm->map_count);
for ( vma = mm->mmap ; vma ; vma = vma->vm_next ) {
printk("0x%lx, 0x%lx", vma->vm_start, vma->vm_end);
if ( vma->vm_file != NULL ) {
tpath = (char*)kmalloc(512, 0);
base_path = vma->vm_file->f_path;
ret_ptr = d_path(&base_path, tpath, 512);
printk(" %s", ret_ptr);
}
printk("\n");
}
}
static int mm_exp_load() {
long pid = 1;
struct task_struct *task;
printk("\nprocess id: %d.\n", pid);
for_each_process(task) {
if ( task->pid == pid ) {
printk("process name: %s\n", task->comm, task->pid);
print_mem(task);
}
}
return 0;
}
static void mm_exp_unload(void) {
printk("\nPrint segment information module exiting.\n");
}
module_init(mm_exp_load);
module_exit(mm_exp_unload);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment