From: <cr...@us...> - 2008-04-24 11:36:44
|
Revision: 4009 http://jnode.svn.sourceforge.net/jnode/?rev=4009&view=rev Author: crawley Date: 2008-04-24 04:36:41 -0700 (Thu, 24 Apr 2008) Log Message: ----------- Converted KillCommand. Note that 'kill' is currently failing due to a SecurityException. This is probably a good thing ... Modified Paths: -------------- trunk/shell/descriptors/org.jnode.shell.command.xml trunk/shell/src/shell/org/jnode/shell/command/KillCommand.java Modified: trunk/shell/descriptors/org.jnode.shell.command.xml =================================================================== --- trunk/shell/descriptors/org.jnode.shell.command.xml 2008-04-23 18:50:23 UTC (rev 4008) +++ trunk/shell/descriptors/org.jnode.shell.command.xml 2008-04-24 11:36:41 UTC (rev 4009) @@ -144,6 +144,12 @@ <repeat><argument argLabel="arg"/></repeat> </sequence> </syntax> + <syntax alias="kill" description="Kill a thread"> + <sequence> + <repeat maxCount="1"><option argLabel="debug" longName="debug"/></repeat> + <argument argLabel="threadId"/> + </sequence> + </syntax> <syntax alias="run" description="Run a command file"> <argument argLabel="file"/> </syntax> Modified: trunk/shell/src/shell/org/jnode/shell/command/KillCommand.java =================================================================== --- trunk/shell/src/shell/org/jnode/shell/command/KillCommand.java 2008-04-23 18:50:23 UTC (rev 4008) +++ trunk/shell/src/shell/org/jnode/shell/command/KillCommand.java 2008-04-24 11:36:41 UTC (rev 4009) @@ -26,73 +26,95 @@ import org.jnode.shell.AbstractCommand; import org.jnode.shell.CommandLine; -import org.jnode.shell.help.Help; -import org.jnode.shell.help.Parameter; -import org.jnode.shell.help.ParsedArguments; -import org.jnode.shell.help.Syntax; -import org.jnode.shell.help.argument.IntegerArgument; +import org.jnode.shell.syntax.*; /** * @author Andreas H\u00e4nel + * @author cr...@jn... */ public class KillCommand extends AbstractCommand { - - static IntegerArgument ARG_THREADID = new IntegerArgument("id", "the id of the thread to kill"); - - public static Help.Info HELP_INFO = new Help.Info( - "kill", new Syntax[] { - new Syntax("kill the Thread with given id", - new Parameter[] { - new Parameter(ARG_THREADID, Parameter.MANDATORY) - } - ) - }); - + + private final IntegerArgument ARG_THREADID = + new IntegerArgument("threadId", Argument.MANDATORY, "the id of the thread to be kill"); + private final FlagArgument FLAG_DEBUG = + new FlagArgument("debug", Argument.OPTIONAL, "if set, print debug information"); + + public KillCommand() { + super("kill the thread with the id supplied"); + registerArguments(ARG_THREADID, FLAG_DEBUG); + } + public static void main(String[] args) throws Exception { new KillCommand().execute(args); } - public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) throws Exception { - ParsedArguments parsedArguments = HELP_INFO.parse(commandLine); - out.print("going to kill Thread with id : "); - out.println(ARG_THREADID.getInteger(parsedArguments)); - // kill Thread - ThreadGroup grp = Thread.currentThread().getThreadGroup(); - while (grp.getParent() != null) { - grp = grp.getParent(); - } - kill(grp,ARG_THREADID.getInteger(parsedArguments),out); - + @SuppressWarnings("deprecation") + public void execute(CommandLine commandLine, InputStream in, PrintStream out, PrintStream err) + throws Exception { + boolean debug = FLAG_DEBUG.isSet(); + int threadId = ARG_THREADID.getValue(); + if (debug) { + out.print("preparing to kill thread with id " + threadId); + } + // In order to kill the thread, we need to traverse the thread group tree, looking + // for the thread whose 'id' matches the supplied one. First, find the tree root. + ThreadGroup grp = Thread.currentThread().getThreadGroup(); + while (grp.getParent() != null) { + grp = grp.getParent(); + } + // Next search the tree + Thread t = findThread(grp, threadId); + // Finally, kill the thread if we found one. + if (t != null) { + if (debug) { + out.print("found the thread: "); + } + out.println(threadId); + // FIXME ... this is bad. Killing a thread this way could in theory bring down the + // entire system if we do it at a point where the application thread is executing + // a method that is updating OS data structures. + t.stop(new ThreadDeath()); + out.println("Killed thread " + threadId); + } + else { + out.println("Thread " + threadId + " not found"); + exit(1); + } } - - @SuppressWarnings("deprecation") - private void kill(ThreadGroup grp,int id,PrintStream out){ - final int max = grp.activeCount() * 2; - final Thread[] ts = new Thread[max]; - grp.enumerate(ts); - - for (int i = 0; i < max; i++) { - final Thread t = ts[i]; - if (t != null) { - if (t.getId()==id){ - out.print("found the thread : "); - out.println(id); - t.stop(new ThreadDeath()); - out.println("killed it..."); - } - } - } - final int gmax = grp.activeGroupCount() * 2; - final ThreadGroup[] tgs = new ThreadGroup[gmax]; - grp.enumerate(tgs); - for (int i = 0; i < gmax; i++) { - final ThreadGroup tg = tgs[i]; - if (tg != null) { - kill(tg, id, out); - } - } - + /** + * Search thread group 'grp' and its dependents groups for a given thread. + * @param grp the thread group to search + * @param id the id of the thread we are looking for + * @return the Thread found or <code>null</code> + */ + private Thread findThread(ThreadGroup grp, int id) { + // Search the current thread group + final int max = grp.activeCount() * 2; + final Thread[] ts = new Thread[max]; + grp.enumerate(ts); + for (int i = 0; i < max; i++) { + final Thread t = ts[i]; + if (t != null) { + if (t.getId() == id) { + return t; + } + } + } + // Recursively search the child thread groups + final int gmax = grp.activeGroupCount() * 2; + final ThreadGroup[] tgs = new ThreadGroup[gmax]; + grp.enumerate(tgs); + for (int i = 0; i < gmax; i++) { + final ThreadGroup tg = tgs[i]; + if (tg != null) { + Thread t = findThread(tg, id); + if (t != null) { + return t; + } + } + } + // Didn't find it here + return null; } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |