LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-03-2016, 10:21 PM   #1
s@linux
LQ Newbie
 
Registered: May 2016
Posts: 2

Rep: Reputation: Disabled
System call killall (that kill all user processes)


i have used the code of sys_kill written in signal.c and make my own function and placed it also in signal.c make the appropriate changes in table , make it. but when i call it it returns successfully but no process is closed. i am stuck and ave no idea whats happening.
this is my function that i placed in signal.c after endif line

asmlinkage long sys_killothers(void)
{

struct task_struct *temp;

temp = current;


while(temp->pid >=1000)
{
temp = temp->real_parent;
}



struct list_head *mylist = &temp->children;
struct list_head *p;
struct task_struct *block;

list_for_each(p , mylist)
{
block = list_entry(p, struct task_struct , sibling);
pid_t pid;
pid = block->pid;
int sig;

char* toCompare = block->comm;


if(pid != current->pid)
{
sig = 9; //for sigkill
struct siginfo info;

info.si_signo = sig;
info.si_errno = 0;
info.si_code = SI_USER;
info.si_pid = task_tgid_vnr(current);
info.si_uid = from_kuid_munged(current_user_ns(), current_uid());

int ret;

if (pid > 0) {
rcu_read_lock();
ret = kill_pid_info(sig, &info, find_vpid(pid));
rcu_read_unlock();
return ret;
}

read_lock(&tasklist_lock);
if (pid != -1) {
ret = __kill_pgrp_info(sig, &info,
pid ? find_vpid(-pid) : task_pgrp(current));
} else {
int retval = 0, count = 0;
struct task_struct * p;

for_each_process(p) {
if (task_pid_vnr(p) > 1 &&
!same_thread_group(p, current)) {
int err = group_send_sig_info(sig, &info, p);
++count;
if (err != -EPERM)
retval = err;
}
}
ret = count ? retval : -ESRCH;
}
read_unlock(&tasklist_lock);
sys_kill(pid, SIGKILL);
}

}

return 0;
}
 
Old 05-04-2016, 07:49 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Firstly use [code] tags to enclose any code so that it is readable and able to be quoted by others in reply.

Secondly, have you started with a program to kill just one process as opposed to attempting to kill a bunch of processes on your system of which you may or may not know what they are and what they are doing?

The simplest call you can make it to use the kill() function because that is how you send a signal to another process, via C program:
Code:
kill(pid_you_wish_to_kill, SIGKILL);
Consider also using SIGTERM.
 
Old 05-04-2016, 07:59 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Also note, not all user processes are pids > 1000....

User processes are usually identified by UID, not pid.

You should also know that other processes will not terminate until a context switch to them has occured. The resulting race condition gives more than enough time that a fork bomb can still lock the system... or that the system itself will start/restart user processes.

This is why killall is a user mode utility, not a kernel function.

Last edited by jpollard; 05-04-2016 at 08:01 AM.
 
Old 05-05-2016, 07:21 AM   #4
s@linux
LQ Newbie
 
Registered: May 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
we know kill function but this is our assignment and we are told not to use the kill function instead use its code.
so please rectify the problem in this code
the issue is when we use sys_kill(pid_t pid , int sig) directly it works.
the code then looks like this:

[asmlinkage long sys_killothers(void)
{

struct task_struct *temp;

temp = current;


while(temp->pid >=1000)
{
temp = temp->real_parent;
}



struct list_head *mylist = &temp->children;
struct list_head *p;
struct task_struct *block;

list_for_each(p , mylist)
{
block = list_entry(p, struct task_struct , sibling);
pid_t pid;
pid = block->pid;
int sig;
//char *Position = strstr(block->comm , "gnome");
char* toCompare = block->comm;

if(strcmp(toCompare,"gnome") != 0 && pid != current->pid)
{
sig = 9; //for sigkill

sys_kill(pid,sig);

}
}
return 0;
}]

but when the code of sys_kill is used it doesn't works like i posted earlier, although it compiles properly but doesn't kill any process.

}

Last edited by s@linux; 05-05-2016 at 07:26 AM.
 
Old 05-05-2016, 07:42 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by s@linux View Post
we know kill function but this is our assignment and we are told not to use the kill function instead use its code.
so please rectify the problem in this code
Obviously, since this is h-o-m-e-w-o-r-k, you need to be doing this yourself.

One suggestion that I would make here is that your logic might be a little cleaner if you first gather the list of pids to be killed, up to some reasonable size, and then loop through that list to kill the processes ... using existing system calls (internally) to do so.

Look carefully at the implementation of "kill()" to see exactly how it works. As others have noted, it basically marks the process for death: it does not, at that moment, dismantle it. All murders are accomplished by means of signals, which are asynchronous. It is correct for your code to return before the processes cease to exist.

"In the real world," there's a reason why kill-all is not implemented as a single system call. If the mass-murder is being done from user space, the assassin is regularly giving up control and being re-dispatched. There won't be too many corpses lying around at any particular instant.

However, this is a valid academic exercise. A good one, in fact.
 
Old 05-05-2016, 08:35 AM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Not being certain, but I think your selection of what to kill is flawed.

You process the current process tree walking back until the pid is <= 1000 - which is likely to mean GDM (assuming a windowing environment). The children will all likely to be Gnome (which you are checking for), and skipped, thus any
children of Gnome will also be skipped (they are in a tree belonging to Gnome). I expect this would be a single user workstation with only one login active - and only one Gnome process, thus there is only one child being looked at.

Could be wrong, but that is what it looks like to me.

One possibility you can test with is to have the system call return the number of processes signalled... It would confirm that no signals were sent.
 
Old 05-05-2016, 10:17 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I'm sure the assignment is specific and it just says, "all process IDs above 1000"

Bear in mind OP that you're supposed to work on your homework yourself and demonstrate a bit more than what you've shown. Sundialsvcs has some excellent suggestions for organization and I agree that you do need to organize your code better.

Further, in most systems there are services which if they are killed, they may be automatically restarted. Hence, some fundamental service may have a PID in excess of 1000. Heck, my PIDs have probably rolled over a few times, but right now I'm in the 11,000's for a new terminal.

If you are this lost, you should seek the advice of your instructor. They need to know that you, and possibly other students are struggling.

Last edited by rtmistler; 05-05-2016 at 10:20 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] kill all idle user processes jasonws Linux - General 3 02-19-2015 04:45 AM
LXer: How to kill a process in Linux - kill, killall, pkill, xkill LXer Syndicated Linux News 0 04-02-2013 12:01 PM
Using killall to kill mysql processes? SirTristan Linux - Newbie 3 08-14-2009 03:25 PM
Safe to kill all user: 'nobody' processes? lagu2653 Linux - Security 4 11-06-2005 06:28 PM
How can I kill all processes form a certain user ? Menestrel Linux - Newbie 6 07-05-2005 09:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration