New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subprocess-signal. #961

Open
wants to merge 2 commits into
from

Conversation

Projects
None yet
2 participants
@kach

kach commented Jun 12, 2015

This PR creates a new primitive, subprocess-signal, which allows users to send arbitrary POSIX signals to a subprocess. There are many, many use-cases for this, such as communicating with obscure processes that accept SIGUSR*, suspending and resuming process with SIGSTOP and SIGCONT, and even implementing subprocess-kill within Racket rather than as a separate C primitive.

A simple test case and example is:

#lang racket
(define-values (p stdout stdin stderr) (subprocess #f #f #f "/usr/bin/python" "signal.py"))
(display (read-line stdout))
(newline)
(subprocess-signal p 'SIGTERM) ; or any other signal name like SIGUSR1
(display (read-line stdout))
(newline)

In file signal.py, we have:

import signal
import sys
import time

print "Bananas!"
sys.stdout.flush()

def beep(num, stack):
    print "Got signal:", num
    sys.stdout.flush()

signal.signal(signal.SIGHUP, beep)
signal.signal(signal.SIGINT, beep)
signal.signal(signal.SIGQUIT, beep)
signal.signal(signal.SIGILL, beep)
signal.signal(signal.SIGTRAP, beep)
signal.signal(signal.SIGABRT, beep)
signal.signal(signal.SIGEMT, beep)
signal.signal(signal.SIGFPE, beep)
#signal.signal(signal.SIGKILL, beep)
signal.signal(signal.SIGBUS, beep)
signal.signal(signal.SIGSEGV, beep)
signal.signal(signal.SIGSYS, beep)
signal.signal(signal.SIGPIPE, beep)
signal.signal(signal.SIGALRM, beep)
signal.signal(signal.SIGTERM, beep)
signal.signal(signal.SIGURG, beep)
#signal.signal(signal.SIGSTOP, beep)
signal.signal(signal.SIGTSTP, beep)
#signal.signal(signal.SIGCONT, beep)
signal.signal(signal.SIGCHLD, beep)
signal.signal(signal.SIGTTIN, beep)
signal.signal(signal.SIGTTOU, beep)
signal.signal(signal.SIGIO, beep)
signal.signal(signal.SIGXCPU, beep)
signal.signal(signal.SIGXFSZ, beep)
signal.signal(signal.SIGVTALRM, beep)
signal.signal(signal.SIGPROF, beep)
signal.signal(signal.SIGWINCH, beep)
signal.signal(signal.SIGINFO, beep)
signal.signal(signal.SIGUSR1, beep)
signal.signal(signal.SIGUSR2, beep)

while True:
    time.sleep(1)

This feature is UNIX-only, though a small subset of the signals have Windows equivalents which I can try to implement later.

@kach

This comment has been minimized.

Show comment
Hide comment
@kach

kach Jun 12, 2015

Note: The reason it fails on Travis/Linux is because Linux defines a different set of signal constants than OSX does. We should use a smaller set of signals which is guaranteed to be defined everywhere.

EDIT: the offending signals are SIGINFO and SIGEMT

kach commented Jun 12, 2015

Note: The reason it fails on Travis/Linux is because Linux defines a different set of signal constants than OSX does. We should use a smaller set of signals which is guaranteed to be defined everywhere.

EDIT: the offending signals are SIGINFO and SIGEMT

@mflatt

This comment has been minimized.

Show comment
Hide comment
@mflatt

mflatt Jun 20, 2015

Member

Thanks for the patch! I like the idea of subprocess-signal, but I'd like to look more into how the set of supported signals can be generally portable and complete (where the latter might mean accepting numerical values as well as symbols). I can't get to it just now, but I'll hopefully be able to follow up soon.

Member

mflatt commented Jun 20, 2015

Thanks for the patch! I like the idea of subprocess-signal, but I'd like to look more into how the set of supported signals can be generally portable and complete (where the latter might mean accepting numerical values as well as symbols). I can't get to it just now, but I'll hopefully be able to follow up soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment