Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 25 additions & 66 deletions core/src/main/java/org/jruby/RubySignal.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,36 @@ public static void createSignal(ThreadContext context) {
private static final Map<String, Integer> SIGNAME_MAP;
private static final Map<Integer, String> SIGNUM_MAP;

// Signal names in the order of CRuby's siglist, where signo2signm
// returns the first name for a number.
private static final String[] SIGLIST = {
"EXIT", "HUP", "INT", "QUIT", "ILL", "TRAP", "ABRT", "IOT", "EMT", "FPE",
"KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", "STOP", "TSTP",
"CONT", "CHLD", "CLD", "TTIN", "TTOU", "IO", "XCPU", "XFSZ", "VTALRM", "PROF",
"WINCH", "USR1", "USR2", "LOST", "MSG", "PWR", "POLL", "DANGER", "MIGRATE",
"PRE", "GRANT", "RETRACT", "SOUND", "INFO"
};

static {
HashMap<String, Integer> signals = new HashMap<>(Signal.values().length);
HashMap<Integer, String> signums = new HashMap<>(Signal.values().length);

Signal[] values = Signal.values();
for (Signal s : values) {
// Skip signals not defined on this platform
if (!s.defined()) continue;

String desc = s.description();
if (!desc.startsWith(SIGNAME_PREFIX)) continue;
for (String name : SIGLIST) {
Signal s = jnrSignal(name);

desc = signmWithoutPrefix(desc);
if (!SIGNAME(desc)) continue;
// Skip signals not defined on this platform
if (s == null || !s.defined()) continue;

// replace CLD with CHLD value
int signo = s.intValue();
if (s == Signal.SIGCHLD || s == Signal.SIGCLD) {
// skip these because Ruby does them weirdly
continue;
}
// skip these because Ruby does them weirdly
if (s == Signal.SIGCHLD || s == Signal.SIGCLD) continue;

// omit unsupported signals
int signo = s.intValue();
if (signo >= 20000) continue;

signals.put(desc, signo);
signums.put(signo, desc);
signals.put(name, signo);
// the first name in SIGLIST wins, so an alias doesn't replace the original
signums.putIfAbsent(signo, name);
}

// We always define KILL as 9 on Windows
Expand Down Expand Up @@ -236,55 +239,11 @@ public static String signmWithoutPrefix(String nm) {
return (nm.startsWith(SIGNAME_PREFIX)) ? nm.substring(SIGNAME_PREFIX.length()) : nm;
}

private static boolean SIGNAME(final String name) {
switch (name) {
case "EXIT":
case "HUP" :
case "INT" :
case "QUIT":
case "ILL" :
case "TRAP":
case "IOT" :
case "ABRT":
case "EMT" :
case "FPE" :
case "KILL":
case "BUS" :
case "SEGV":
case "SYS" :
case "PIPE":
case "ALRM":
case "TERM":
case "URG" :
case "STOP":
case "TSTP":
case "CONT":
case "CHLD":
case "CLD" :
case "TTIN":
case "TTOU":
case "IO" :
case "XCPU":
case "XFSZ":
case "PROF":
case "VTALRM":
case "WINCH":
case "USR1":
case "USR2":
case "LOST":
case "MSG" :
case "PWR" :
case "POLL":
case "DANGER":
case "MIGRATE":
case "PRE" :
case "GRANT":
case "RETRACT":
case "SOUND":
case "INFO":
return true;
default:
return false;
private static Signal jnrSignal(String name) {
try {
return Signal.valueOf(SIGNAME_PREFIX + name);
} catch (IllegalArgumentException e) {
return null;
}
}

Expand Down
2 changes: 0 additions & 2 deletions spec/tags/ruby/core/signal/signame_tags.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
fails:Signal.signame returns nil if the argument is an invalid signal number
fails:Signal.signame the original should take precendence over alias when looked up by number
fails:Signal.signame the original should take precedence over alias when looked up by number
Loading