-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Successfully tested install_signal_handler
- Loading branch information
1 parent
18e1bad
commit 8af8076
Showing
6 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
*.ibc | ||
*.ibc | ||
*~ | ||
*# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
module System.Posix.Signal | ||
|
||
%include C "signal_wrappers.c" | ||
|
||
||| Type of signal handlers | ||
||| | ||
||| The actual implementation should be of the form unsafePerformIO $ do ... | ||
||| if it needs to do any IO actions (as always for Idris functions used as C callbacks) | ||
public export | ||
Signal_handler : Type | ||
Signal_handler = (sig_num : Int) -> () | ||
|
||
install_handler : Int -> Ptr -> IO Ptr | ||
install_handler signum handler = foreign FFI_C "install_handler" (Int -> Ptr -> IO Ptr) signum handler | ||
|
||
||| Install a new signal handler for @sig_num | ||
||| | ||
||| @sig_num - the signal to be handled | ||
||| @handler - the wrapper for the Idris handler function (define as foreign FFI_C "%wrapper" ((CfnPtr Signal_handler) -> IO Ptr) (MkCFnPtr name-of-idris-routine) | ||
||| Returns null on error and a pointer to the allocated struct sigaction on success | ||
export | ||
install_signal_handler : (sig_num : Int) -> (handler: IO Ptr) -> IO Ptr | ||
install_signal_handler sig_num handler = do | ||
wr <- handler | ||
install_handler sig_num wr | ||
|
||
export | ||
SIGHUP : Int | ||
SIGHUP = 1 | ||
|
||
export | ||
SIGINT : Int | ||
SIGINT = 2 | ||
|
||
export | ||
SIGQUIT : Int | ||
SIGQUIT = 3 | ||
|
||
export | ||
SIGILL : Int | ||
SIGILL = 4 | ||
|
||
export | ||
SIGTRAP : Int | ||
SIGTRAP = 5 | ||
|
||
export | ||
SIGABORT : Int | ||
SIGABORT = 6 | ||
|
||
export | ||
SIGIOT : Int | ||
SIGIOT = 6 | ||
|
||
export | ||
SIGBUS : Int | ||
SIGBUS = 7 | ||
|
||
export | ||
SIGFPE : Int | ||
SIGFPE = 8 | ||
|
||
export | ||
SIGKILL : Int | ||
SIGKILL = 9 | ||
|
||
export | ||
SIGUSR1 : Int | ||
SIGUSR1 = 10 | ||
|
||
export | ||
SIGSEGV : Int | ||
SIGSEGV = 11 | ||
|
||
export | ||
SIGUSR2 : Int | ||
SIGUSR2 = 12 | ||
|
||
export | ||
SIGPIPE : Int | ||
SIGPIPE = 13 | ||
|
||
export | ||
SIGALRM : Int | ||
SIGALRM = 14 | ||
|
||
export | ||
SIGTERM : Int | ||
SIGTERM = 15 | ||
|
||
export | ||
SIGSTKFLT : Int | ||
SIGSTKFLT = 16 | ||
|
||
export | ||
SIGCHLD : Int | ||
SIGCHLD = 17 | ||
|
||
export | ||
SIGCLD : Int | ||
SIGCLD = 17 | ||
|
||
export | ||
SIGCONT : Int | ||
SIGCONT = 18 | ||
|
||
export | ||
SIGSTOP : Int | ||
SIGSTOP = 19 | ||
|
||
export | ||
SIGTSTP : Int | ||
SIGTSTP = 20 | ||
|
||
export | ||
SIGTTIN : Int | ||
SIGTTIN = 21 | ||
|
||
export | ||
SIGTTOU : Int | ||
SIGTTOU = 22 | ||
|
||
export | ||
SIGURG : Int | ||
SIGURG = 23 | ||
|
||
export | ||
SIGXCPU : Int | ||
SIGXCPU = 24 | ||
|
||
export | ||
SIGXFSZ : Int | ||
SIGXFSZ = 25 | ||
|
||
export | ||
SIGVTALRM : Int | ||
SIGVTALRM = 26 | ||
|
||
export | ||
SIGPROF : Int | ||
SIGPROF = 27 | ||
|
||
export | ||
SIGWINCH : Int | ||
SIGWINCH = 28 | ||
|
||
export | ||
SIGPOLL : Int | ||
SIGPOLL = 29 | ||
|
||
export | ||
SIGIO : Int | ||
SIGIO = 29 | ||
|
||
export | ||
SIGPWR : Int | ||
SIGPWR = 30 | ||
|
||
export | ||
SIGSYS : Int | ||
SIGSYS = 31 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module System.Posix.Syslog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
||| Tests for System.Posix.Signal | ||
module System.Posix.Test.Signal | ||
|
||
import System.Posix.Signal | ||
import System | ||
|
||
my_handler : Signal_handler | ||
my_handler sig_num = unsafePerformIO $ do | ||
putStrLn $ "Signal handler running after receiving signal number " ++ (show sig_num) | ||
|
||
my_handler_wrapper : IO Ptr | ||
my_handler_wrapper = foreign FFI_C "%wrapper" ((CFnPtr Signal_handler) -> IO Ptr) | ||
(MkCFnPtr my_handler) | ||
|
||
export | ||
test_install_handler : IO () | ||
test_install_handler = do | ||
install_signal_handler SIGINT my_handler_wrapper | ||
putStrLn "Now press Ctrl-C to interrupt" | ||
usleep 100000000 | ||
putStrLn "Test completed. Did you see a message indicating signal 2 was handled?" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <signal.h> | ||
|
||
inline struct sigaction * install_handler (int signum, void (*handler)(int)) | ||
{ | ||
int rc; | ||
struct sigaction *act; | ||
act = calloc (1, sizeof (struct sigaction)); | ||
if (act == NULL) | ||
return act; | ||
act->sa_handler = handler; | ||
rc = sigaction (signum, act, NULL); | ||
if (rc == 0) | ||
return act; | ||
return NULL; | ||
} |