diff --git a/bindings/java/src/jni/javasigar.c b/bindings/java/src/jni/javasigar.c index 9114efa3..05a4a0a2 100644 --- a/bindings/java/src/jni/javasigar.c +++ b/bindings/java/src/jni/javasigar.c @@ -318,6 +318,24 @@ JNIEXPORT void SIGAR_JNIx(kill) } } +JNIEXPORT jint SIGAR_JNIx(getSigNum) +(JNIEnv *env, jclass cls_obj, jstring jname) +{ + jboolean is_copy; + const char *name; + int num; + + name = JENV->GetStringUTFChars(env, jname, &is_copy); + + num = sigar_signum_get((char *)name); + + if (is_copy) { + JENV->ReleaseStringUTFChars(env, jname, name); + } + + return num; +} + #define SetStringField(env, obj, fieldID, val) \ SetObjectField(env, obj, fieldID, JENV->NewStringUTF(env, val)) diff --git a/bindings/java/src/org/hyperic/sigar/Sigar.java b/bindings/java/src/org/hyperic/sigar/Sigar.java index a803c8e2..af9becb2 100644 --- a/bindings/java/src/org/hyperic/sigar/Sigar.java +++ b/bindings/java/src/org/hyperic/sigar/Sigar.java @@ -241,6 +241,37 @@ public class Sigar implements SigarProxy { */ public native void kill(long pid, int signum) throws SigarException; + /** + * Send a signal to a process. + * + * @param pid The process id. + * @param signum The signal name. + * @exception SigarException on failure. + */ + public void kill(long pid, String signame) throws SigarException { + int signum; + + if (Character.isDigit(signame.charAt(0))) { + try { + signum = Integer.parseInt(signame); + } catch (NumberFormatException e) { + signum = -1; + } + } + else { + signum = getSigNum(signame); + } + + if (signum < 0) { + throw new SigarException(signame + + ": invalid signal specification"); + } + + kill(pid, signum); + } + + private static native int getSigNum(String name); + /** * Send a signal to a process. * diff --git a/bindings/java/src/org/hyperic/sigar/cmd/Kill.java b/bindings/java/src/org/hyperic/sigar/cmd/Kill.java index 0df39dfd..694750b0 100644 --- a/bindings/java/src/org/hyperic/sigar/cmd/Kill.java +++ b/bindings/java/src/org/hyperic/sigar/cmd/Kill.java @@ -50,18 +50,13 @@ public class Kill extends SigarCommandBase { } public void output(String[] args) throws SigarException { - int signum = 15; //SIGTERM + String signal = "SIGTERM"; long[] pids; String query; if (args.length == 2) { + signal = args[0]; query = args[1]; - try { - signum = Integer.parseInt(args[0]); - } catch (NumberFormatException e) { - //XXX convert SIGFOO to number - throw new SigarException(e.getMessage()); - } } else { query = args[0]; @@ -70,8 +65,8 @@ public class Kill extends SigarCommandBase { pids = this.shell.findPids(new String[] { query }); for (int i=0; i #include "sigar.h" @@ -24,10 +23,6 @@ #include "sigar_util.h" #include "sigar_os.h" -#ifndef WIN32 -#include -#endif - SIGAR_DECLARE(int) sigar_open(sigar_t **sigar) { int status = sigar_os_open(sigar); @@ -67,41 +62,6 @@ SIGAR_DECLARE(sigar_pid_t) sigar_pid_get(sigar_t *sigar) } #endif -SIGAR_DECLARE(int) sigar_proc_kill(sigar_pid_t pid, int signum) -{ -#ifdef WIN32 - int status = -1; - HANDLE proc = - OpenProcess(PROCESS_ALL_ACCESS, - TRUE, (DWORD)pid); - - if (proc) { - switch (signum) { - case 0: - status = SIGAR_OK; - break; - default: - if (TerminateProcess(proc, signum)) { - status = SIGAR_OK; - } - break; - } - - CloseHandle(proc); - - if (status == SIGAR_OK) { - return SIGAR_OK; - } - } - return GetLastError(); -#else - if (kill(pid, signum) == -1) { - return errno; - } - return SIGAR_OK; -#endif -} - static char *sigar_error_string(int err) { switch (err) { diff --git a/src/sigar_signal.c b/src/sigar_signal.c new file mode 100644 index 00000000..eb0e58d1 --- /dev/null +++ b/src/sigar_signal.c @@ -0,0 +1,219 @@ +/* + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. + * This file is part of SIGAR. + * + * SIGAR is free software; you can redistribute it and/or modify + * it under the terms version 2 of the GNU General Public License as + * published by the Free Software Foundation. This program is distributed + * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +#include "sigar.h" +#include "sigar_private.h" +#include "sigar_util.h" + +#ifdef WIN32 +#include +#else +#include +#endif + +#include + +SIGAR_DECLARE(int) sigar_proc_kill(sigar_pid_t pid, int signum) +{ +#ifdef WIN32 + int status = -1; + HANDLE proc = + OpenProcess(PROCESS_ALL_ACCESS, + TRUE, (DWORD)pid); + + if (proc) { + switch (signum) { + case 0: + status = SIGAR_OK; + break; + default: + if (TerminateProcess(proc, signum)) { + status = SIGAR_OK; + } + break; + } + + CloseHandle(proc); + + if (status == SIGAR_OK) { + return SIGAR_OK; + } + } + return GetLastError(); +#else + if (kill(pid, signum) == -1) { + return errno; + } + return SIGAR_OK; +#endif +} + +SIGAR_DECLARE(int) sigar_signum_get(char *name) +{ + if (strnEQ(name, "SIG", 3)) { + name += 3; + } + + switch (*name) { + case 'A': +#ifdef SIGABRT + if (strEQ(name, "ABRT")) return SIGABRT; +#endif +#ifdef SIGALRM + if (strEQ(name, "ALRM")) return SIGALRM; +#endif + break; + case 'B': +#ifdef SIGBUS + if (strEQ(name, "BUS")) return SIGBUS; +#endif + break; + case 'C': +#ifdef SIGCONT + if (strEQ(name, "CONT")) return SIGCONT; +#endif +#ifdef SIGCHLD + if (strEQ(name, "CHLD")) return SIGCHLD; +#endif +#ifdef SIGCLD + if (strEQ(name, "CLD")) return SIGCLD; +#endif + break; + case 'E': +#ifdef SIGEMT + if (strEQ(name, "EMT")) return SIGEMT; +#endif + break; + case 'F': +#ifdef SIGFPE + if (strEQ(name, "FPE")) return SIGFPE; +#endif + break; + case 'H': +#ifdef SIGHUP + if (strEQ(name, "HUP")) return SIGHUP; +#endif + break; + case 'I': +#ifdef SIGINT + if (strEQ(name, "INT")) return SIGINT; +#endif +#ifdef SIGILL + if (strEQ(name, "ILL")) return SIGILL; +#endif +#ifdef SIGIOT + if (strEQ(name, "IOT")) return SIGIOT; +#endif +#ifdef SIGIO + if (strEQ(name, "IO")) return SIGIO; +#endif +#ifdef SIGINFO + if (strEQ(name, "INFO")) return SIGINFO; +#endif + break; + case 'K': +#ifdef SIGKILL + if (strEQ(name, "KILL")) return SIGKILL; +#endif + break; + case 'P': +#ifdef SIGPOLL + if (strEQ(name, "POLL")) return SIGPOLL; +#endif +#ifdef SIGPIPE + if (strEQ(name, "PIPE")) return SIGPIPE; +#endif +#ifdef SIGPROF + if (strEQ(name, "PROF")) return SIGPROF; +#endif +#ifdef SIGPWR + if (strEQ(name, "PWR")) return SIGPWR; +#endif + break; + case 'Q': +#ifdef SIGQUIT + if (strEQ(name, "QUIT")) return SIGQUIT; +#endif + break; + case 'S': +#ifdef SIGSEGV + if (strEQ(name, "SEGV")) return SIGSEGV; +#endif +#ifdef SIGSYS + if (strEQ(name, "SYS")) return SIGSYS; +#endif +#ifdef SIGSTOP + if (strEQ(name, "STOP")) return SIGSTOP; +#endif +#ifdef SIGSTKFLT + if (strEQ(name, "STKFLT")) return SIGSTKFLT; +#endif + break; + case 'T': +#ifdef SIGTRAP + if (strEQ(name, "TRAP")) return SIGTRAP; +#endif +#ifdef SIGTERM + if (strEQ(name, "TERM")) return SIGTERM; +#endif +#ifdef SIGTSTP + if (strEQ(name, "TSTP")) return SIGTSTP; +#endif +#ifdef SIGTTIN + if (strEQ(name, "TTIN")) return SIGTTIN; +#endif +#ifdef SIGTTOU + if (strEQ(name, "TTOU")) return SIGTTOU; +#endif + break; + case 'U': +#ifdef SIGURG + if (strEQ(name, "URG")) return SIGURG; +#endif +#ifdef SIGUSR1 + if (strEQ(name, "USR1")) return SIGUSR1; +#endif +#ifdef SIGUSR2 + if (strEQ(name, "USR2")) return SIGUSR2; +#endif + break; + case 'V': +#ifdef SIGVTALRM + if (strEQ(name, "VTALRM")) return SIGVTALRM; +#endif + break; + case 'W': +#ifdef SIGWINCH + if (strEQ(name, "WINCH")) return SIGWINCH; +#endif + break; + case 'X': +#ifdef SIGXCPU + if (strEQ(name, "XCPU")) return SIGXCPU; +#endif +#ifdef SIGXFSZ + if (strEQ(name, "XFSZ")) return SIGXFSZ; +#endif + break; + default: + break; + } + + return -1; +} +