Skip to content

Commit

Permalink
Successfully tested install_signal_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-adams committed May 2, 2016
1 parent 18e1bad commit 8af8076
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.ibc
*.ibc
*~
*#
6 changes: 6 additions & 0 deletions posix.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package posix
sourcedir = src
modules = System.Posix.Directory
, System.Posix.Time
, System.Posix.Signal
, System.Posix.Syslog
, System.Posix.Test.Signal
objs = dirent_accessors.c
, stat_wrappers.c
, time_wrappers.c
, signal_wrappers.c

tests = System.Posix.Test.Signal.test_install_handler
161 changes: 161 additions & 0 deletions src/System/Posix/Signal.idr
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
1 change: 1 addition & 0 deletions src/System/Posix/Syslog.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module System.Posix.Syslog
22 changes: 22 additions & 0 deletions src/System/Posix/Test/Signal.idr
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?"

15 changes: 15 additions & 0 deletions src/signal_wrappers.c
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;
}

0 comments on commit 8af8076

Please sign in to comment.