0

Hello guys I need a little help here. After many hours of study and research I gave up I couldn't do it. I'm new in kernel programming and I have this task to do. I am asked to modify the exit() system call code so that it terminates all the children processes of calling process and then terminate the process. As much as I know exit() system call gives the children to the init process after parent terminates. I thought I can terminate each children by using children id and calling:

kill (child_pid, SIGTERM);

also I know that we can access calling process task_struct using current global variable. Anyone know can I get all the children PID from the current variable? Is there any other solution you know?

UPDATE: I found a way how to traverse the children of current process. Here is my modified code.

void do_exit(long code)
{

struct task_struct *tsk = current;



//code added by me
int nice=current->static_prio-120;

if(tsk->myFlag==1 && nice>10){
    struct task_struct *task; 
    struct list_head *list;
    list_for_each(list, &current->children) { 
    task = list_entry(list, struct task_struct, sibling); 
    //kill child
    kill(task->pid,SIGKILL);

    }


}

Will this even work?

Arnold Asllani
  • 155
  • 1
  • 12

1 Answers1

0

SIGTERM is catchable and in particular can be ignored. You want to send SIGKILL instead. You can't just use the 'kill' system call either. Instead, once you grab the pointer to the child, you send the signal to that. An example how to do it is, well, in the implementation of the kill syscall.

An example code which has to modify children list (add an element) would be clone. An example code which is very likely to traverse the list (and it likely does in your version) would be the wait* family, e.g. waitid.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • I didnt get it can u get more clear please? I updated my question I found a way how to traverse children but would this work? – Arnold Asllani Nov 08 '16 at 23:45
  • 1
    I believe the point of the exercise is that you read the actual code (like samples I mentioned) and learn from there as opposed to googling crap samples without proper context. In particular, the loop you added is wrong as it does not lock tasklist_lock or anything else guaranteeting stability of the list. With kill(task->pid, ..) you take the pid and ask the func to look up the task struct based on the pid. The very task struct you took the pid from. Again, read the code. –  Nov 09 '16 at 04:29
  • And that didn't answered my question. Don just give me a hint. I already have all the hints I need but I can't realize the big picture. Could somebody just show it to me and same me some precious time? – Arnold Asllani Nov 09 '16 at 19:02