| 1 | /* |
| 2 | Copyright (C) 2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | |
| 17 | */ |
| 18 | package com.mysql.management.util; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.io.PrintStream; |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | public final class ProcessUtil { |
| 26 | |
| 27 | private String pid; |
| 28 | |
| 29 | private PrintStream out; |
| 30 | |
| 31 | private PrintStream err; |
| 32 | |
| 33 | private String killCommand; |
| 34 | |
| 35 | private Utils utils; |
| 36 | |
| 37 | private File installDir; |
| 38 | |
| 39 | public ProcessUtil(String pid, PrintStream out, PrintStream err, |
| 40 | File installDir) { |
| 41 | this(pid, out, err, installDir, new Utils()); |
| 42 | } |
| 43 | |
| 44 | public ProcessUtil(String pid, PrintStream out, PrintStream err, |
| 45 | File installDir, Utils utils) { |
| 46 | this.installDir = installDir; |
| 47 | this.pid = (pid != null) ? pid.trim() : "-1"; |
| 48 | this.out = out; |
| 49 | this.err = err; |
| 50 | this.utils = utils; |
| 51 | if (utils.files().isWindows()) { |
| 52 | this.killCommand = getWindowsKillFile().getPath(); |
| 53 | } else { |
| 54 | this.killCommand = "kill"; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /* called from constructor */ |
| 59 | final File getWindowsKillFile() { |
| 60 | File parent = new File(installDir, "c-mxj-utils"); |
| 61 | File kill = new File(parent, "kill.exe"); |
| 62 | if (!kill.exists()) { |
| 63 | utils.streams().createFileFromResource("kill.exe", kill); |
| 64 | } |
| 65 | return kill; |
| 66 | } |
| 67 | |
| 68 | String pid() { |
| 69 | return pid; |
| 70 | } |
| 71 | |
| 72 | public void kill() { |
| 73 | kill(false); |
| 74 | } |
| 75 | |
| 76 | public void forceKill() { |
| 77 | Exceptions.VoidBlock block = new Exceptions.VoidBlock() { |
| 78 | public void inner() { |
| 79 | kill(true); |
| 80 | } |
| 81 | }; |
| 82 | block.execNotThrowingExceptions(err); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @param force |
| 87 | */ |
| 88 | private void kill(boolean force) { |
| 89 | String threadName = "killing process " + pid; |
| 90 | if (force) { |
| 91 | threadName = "force " + threadName; |
| 92 | } |
| 93 | launchShell(threadName, killArgs(force), 10); |
| 94 | } |
| 95 | |
| 96 | String[] killArgs(boolean force) { |
| 97 | List args = new ArrayList(); |
| 98 | args.add(killCommand); |
| 99 | if (force) { |
| 100 | args.add("-9"); |
| 101 | } |
| 102 | args.add(pid); |
| 103 | return utils.str().toStringArray(args); |
| 104 | } |
| 105 | |
| 106 | public boolean isRunning() { |
| 107 | String threadName = "is_process_" + pid + "_running"; |
| 108 | Shell shell = launchShell(threadName, isRunningArgs(), 5); |
| 109 | if (!shell.hasReturned()) { |
| 110 | return false; |
| 111 | } |
| 112 | return shell.returnCode() == 0; |
| 113 | } |
| 114 | |
| 115 | private Shell launchShell(String threadName, String[] args, int seconds) { |
| 116 | Shell shell = utils.shellFactory().newShell(args, threadName, out, err); |
| 117 | shell.start(); |
| 118 | int fraction = 20; |
| 119 | int loops = (fraction * seconds); |
| 120 | do { |
| 121 | utils.threads().pause((1000 / fraction)); |
| 122 | } while (!shell.hasReturned() && loops-- > 0); |
| 123 | |
| 124 | if (!shell.hasReturned()) { |
| 125 | err.println("Thread \"" + threadName + "\" may be hung"); |
| 126 | err.println("(did not return after " + seconds + " seconds)"); |
| 127 | err.println("command line used: "); |
| 128 | err.println(new ListToString("", " ", "").toString(args)); |
| 129 | } |
| 130 | return shell; |
| 131 | } |
| 132 | |
| 133 | String[] isRunningArgs() { |
| 134 | return new String[] { killCommand, "-0", pid }; |
| 135 | } |
| 136 | |
| 137 | public void killNoThrow() { |
| 138 | Exceptions.VoidBlock block = new Exceptions.VoidBlock() { |
| 139 | public void inner() { |
| 140 | kill(); |
| 141 | } |
| 142 | }; |
| 143 | block.execNotThrowingExceptions(err); |
| 144 | } |
| 145 | } |