【发布时间】:2014-12-25 08:00:11
【问题描述】:
您好,我正在尝试向 lubuntu 内核添加自定义系统调用。我正在尝试终止此系统调用中的进程。我在原始 ubuntu 内核中尝试了 kill() 系统调用。但是我在这样做时遇到了编译器错误。我不知道如何正确地做到这一点。提前感谢您的回答。
#define _POSIX_SOURCE
#include <linux/syscalls.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/signal.h>
#include <linux/types.h>
asmlinkage long sys_my_process_terminator (pid_t pid , int flag)
{
struct task_struct *task;
struct list_head *list;
struct list_head *siblist;
// firstly check the flag
struct task_struct *myprocess;
struct task_struct *sibchild;
myprocess = find_task_by_vpid(pid);
struct task_struct *pp;
pp =myprocess->parent;
if (flag == 0){
// this loop under this comment will kill all the children of the given process
list_for_each(list, &myprocess->children) {
task = list_entry(list, struct task_struct, sibling);
printk ("%s [%d] \n" , task->comm , task->pid);
kill(task->pid,SIGKILL);
}
}
else if (flag==1) {
list_for_each(list, &pp->children) {
task = list_entry(list, struct task_struct, sibling);
list_for_each(siblist, &task->children) {
sibchild = list_entry(siblist, struct task_struct, sibling);
printk ("%s [%d] \n" , sibchild->comm , sibchild->pid);
kill(sibchild->pid,SIGKILL);
}
if (task->pid !=pid){
printk ("%s [%d] \n" , task->comm , task->pid);
kill(task->pid,
}
【问题讨论】:
-
为什么要添加系统调用来杀死进程?已经有这样的系统调用。根据stackoverflow.com/help/mcve向我们展示您的代码并编译错误。
-
但是,向内核添加系统调用是一项非常有趣的任务。请考虑各种教程,例如,stackoverflow.com/questions/3884172/…
-
您认为的“系统调用”很可能确实是一个包装相应系统调用的 glibc 函数。作为第一步,请仔细阅读本文档:man7.org/linux/man-pages/man2/syscalls.2.html“仔细”是指“花 30 分钟”。
标签: linux operating-system kernel