add signal name lookup support
This commit is contained in:
1 parent
247937d0bb
commit
123e14e283
6 files changed
+274
-49
No files matched your search
@@ -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))
|
||||
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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<pids.length; i++) {
|
||||
println("kill " + signum + " " + pids[i]);
|
||||
this.sigar.kill(pids[i], signum);
|
||||
println("kill " + signal + " " + pids[i]);
|
||||
this.sigar.kill(pids[i], signal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user