The trivial-signal Reference Manual

This is the trivial-signal Reference Manual, version 0.1.0, generated automatically by Declt version 4.0 beta 2 "William Riker" on Sun Sep 15 06:58:18 2024 GMT+0.

Table of Contents


1 Introduction


2 Systems

The main system appears first, followed by any subsystem dependency.


2.1 trivial-signal

Unix signal handling library.

Author

Eitaro Fukamachi

License

Public Domain

Long Description

# trivial-signal [![Build Status](https://travis-ci.org/guicho271828/trivial-signal.svg)](https://travis-ci.org/guicho271828/trivial-signal)

Trivial-signal is a Common Lisp UNIX signal handling library.

**News** : now equipped with nested signal handling capability &
multithreads! See ‘Threading Policy“ below. (Masataro Asai)

**News** : Maintainer has changed. Thanks Fukamachi! (Masataro Asai)

**News** : It now uses CFFI-grovel to obtain the signal numbers. It now
recognizes much more signals e.g. SIGRTMIN. I hope it works even on SPARK !

~**TODO**~ : exit handlers (with ‘atexit‘) –> Consider [exit-hooks](https://github.com/ailisp/exit-hooks).

**Requirements** :
* [CFFI](http://common-lisp.net/project/cffi/)
* [Bordeaux-threads](http://trac.common-lisp.net/bordeaux-threads/wiki/ApiDocumentation)

**Installation** :
“‘
cd ~/common-lisp/
git clone https://github.com/guicho2.71828/trivial-signal.git
“‘

“‘common-lisp
(ql:quickload :trivial-signal)
“‘

## Usage : Toplevel Handlers

“‘common-lisp
(use-package :trivial-signal)
(defun exit-on-signal (signo)
(format *error-output* "~&received ~A~%" (signal-name signo))
(sb-ext:exit :code 1 :abort t))
(setf (signal-handler :term) #’exit-on-signal) ;; :term can also be :sigterm or 15

(loop (sleep 3))

;; now run ‘kill -15 $PID‘ on the terminal to run ‘exit-on-signal‘
“‘

Above example shows the usage of **toplevel handlers**.
Toplevel handlers are system wide and (in most cases) static.

## Usage: SIGNAL-HANDLER-BIND

The next important usage of trivial-signal is
to establish handlers dynamically by ‘signal-handler-bind‘.
The scope of this kind of signal handlers are thread-local.

When the main process receives a signal,
handlers are called in the same way as in ‘handler-bind‘ : the
top handler in the innermost ‘signal-handler-bind‘ is called first.

“‘COMMON-LISP
(use-package :trivial-signal)

(tagbody
(signal-handler-bind ((15 (lambda (c) (print :first)))
(15 (lambda (c) (print :escaping) (go :escape)))
;; mixing different handlers is ok
(2 (lambda (c) (print :escaping) (go :escape)))
;; once the signal is handled, remaining handlers are not called
(15 (lambda (c) (print :this-should-not-be-called))))
(loop (sleep 3))) ;; now send signal 15 from the terminal
:escape
(print :success!))

(tagbody
;; nested handlers are called from the most recently established ones.
;; If the handler declines, the next innermost one is called.
(signal-handler-bind ((15 (lambda (c) (print :this-should-not-be-called))))
(signal-handler-bind ((15 (lambda (c) (print :escaping) (go :escape))))
(signal-handler-bind ((15 (lambda (c) (print :most-recent))))
(loop (sleep 3)))))
:escape
(print :success!))
“‘

If all these thread-local handlers decline, then the toplevel handlers are called.
If that declines again, then a common-lisp condition ‘unix-signal‘ is
signaled in that context.

# Signal Handling Internal

Signals are handled by C-level posix ‘signal(8)‘ API
with which we set a low-level handler through CFFI.

## Some Signals may not Work Right

Note that, depending on the lisp implementation, some signals may not be
captured. This is related to the implementations’ internal, which may use
signals internally for their own sake (such as thread manipulation).

At least I checked the following signals work:

+ on SBCL x86_64, 4-8, 10-11, 16, 18, 21-22, 30-31, 34-64 (SIGRTMIN-SIGRTMAX)
+ if you set a signal handler on 13 (SIGPIPE), sbcl hangs up
+ on CCL x86_64, 1-3,6,8,10,12-14,16-18,21-24,26-29,31, 34-64 (SIGRTMIN-SIGRTMAX)

To see which signals works on your environment, see [TESTING.org](https://github.com/guicho271828/trivial-signal/blob/master/TESTING.org)

## Threading Policy

The C-level signal handlers call a lisp function, which interrupts each
thread who has thread-local signal handlers established by
‘signal-handler-bind‘.

Signals directly sent to each thread might not be captured by ‘trivial-signal‘.
The behavior is currently undefined.
Our advise is that they should be sent to the main process.

This is again related to the internal behavior of the implementations.
For example, on SBCL, signals sent to the main process are not
distributed to each thread. However, CCL seems to distribute the signals.

# API
## sigspec API

Signals can be either specified by its number or by its name.
In ‘trivial-signal‘, the name can be specified with keywords.
Below examples should be sufficient :

+ 15, :term, :sigterm (additionally, constant ‘+sigterm+‘ is bound to 15)
+ 2, :int, :sigint (additionally, constant ‘+sigint+‘ is bound to 2 )
+ 24, :xcpu, :sigxcpu (additionally, constant ‘+sigxcpu+‘ is bound to 24)

Note that the signal number actually depends on the OS you are using.
These numbers are obtained by ‘cffi-grovel‘, therefore OS level
compatibility is now fixed.

### [Function] signal-name (signo)

This returns the name of ‘SIGNO‘ as a keyword.

“‘common-lisp
(signal-name 15)
;=> :TERM
“‘

### [Function] signal-number (signame)

This returns the number of ‘SIGNAME‘ as an integer.

“‘common-lisp
(signal-number :term)
;=> 15
“‘
## Thread-local handlers API

#### [Macro] signal-handler-bind ([(sigspec handler)]* &body forms)

This executes ‘FORMS‘ in an environment where signal handler bindings are in effect.

“‘common-lisp
(signal-handler-bind ((:term (lambda (signo)
(declare (ignore signo))
(sb-ext:exit :abort t)))
(:int (lambda (signo)
(princ (signal-name signo) *error-output*))))
;; do something.
)
“‘

#### [Function] call-signal-handler-bind (new-signal-handlers fn)

Run FN in a dynamic environment where the signal handler bindings are
in effect. ‘new-signal-handlers‘ is a cons tree of ((signo handler ...) ...).
This is rather an internal function which signal-handler-bind expands into.
Use this function when you want to dynamically alter the signal to be captured.

Note that, trivial-signal only considers the first appearance of (signo handlers...)
with the matching signo in the same layer. For example,

(call-signal-handler-bind
‘((,*signo* ,(lambda (c) (lprint :first))
,(lambda (c) (lprint :escaping) (go :escape))
,(lambda (c) (lprint :this-should-not-be-called))))
(lambda () ...))

is okay but

(call-signal-handler-bind
‘((,*signo* ,(lambda (c) (lprint :first)))
(,*signo* ,(lambda (c) (lprint :escaping) (go :escape)))
(,*signo* ,(lambda (c) (lprint :this-should-not-be-called))))
(lambda () ... ))

is incorrect (the 2nd and 3rd handlers are ignored).
If you want to do it, wrap the main code in the ‘(lambda () ...)‘
with another call-signal-handler-bind.
(Also, the macro ‘signal-handler-bind‘ automatically solve this.)

#### [Macro] with-signal-handler (signal fn &body forms)

(deprecated)
This executes ‘FORMS‘ in an environment where a signal handler ‘FN‘ for a signal ‘SIGNAL‘ is in effect.

“‘common-lisp
(with-signal-handler :term (lambda (signo)
(declare (ignore signo))
(sb-ext:exit :abort t))
;; do something.
)
“‘

## Toplevel handlers

Toplevel handlers are system wide, global handlers that capture the signals
sent to the main process.
The functionality of the toplevel signal handlers are analogous to ‘*debugger-hook*‘.
When the lisp process receives a signal,
it is handled by these toplevel handlers
**unless** some nested signal handlers (described later) handles it.

“‘common-lisp
(use-package :trivial-signal)
(defun exit-on-signal (signo)
(format *error-output* "~&received ~A~%" (signal-name signo))
(sb-ext:exit :code 1 :abort t))

(signal-handler :term) ;=> NIL
(setf (signal-handler :term) #’exit-on-signal)
;=> #<FUNCTION (LAMBDA (SIGNO)) {1005764E3B}>

(signal-handler :term)
;=> #<FUNCTION (LAMBDA (SIGNO)) {1005764E3B}>
; T

;; Removing a signal handler.
(setf (signal-handler :term) nil) ; or: (remove-signal-handler :term) (deprecated)
;=> T

;; Clearing all signal handlers.
(remove-all-signal-handlers)
“‘

#### [Function] signal-handler (signal)

This returns a signal handler for a signal ‘SIGNAL‘.

‘SIGNAL‘ can be either a keyword or an integer.

“‘common-lisp
(signal-handler :term)
(signal-handler 15)
(signal-handler +sigterm+)
“‘

#### [Function] \(setf signal-handler) (fn signal)

This sets a signal handler ‘FN‘ for a signal ‘SIGNAL‘.

‘FN‘ must be a function or a symbol of a function name, which takes one
argument as a signal number.
Otherwise ‘FN‘ should be ‘NIL‘, indicating the handler should be removed.

“‘common-lisp
(setf (signal-handler :term)
#’(lambda (signo)
(princ (signal-name signo) *error-output*)))
“‘

#### [Function] remove-signal-handler (signal)

(deprecated) This removes a signal handler from a signal ‘SIGNAL‘.

#### [Function] remove-all-signal-handlers ()

This clears all signal handlers.

## Author

* Eitaro Fukamachi (e.arrows@gmail.com) (author)
* Masataro Asai (guicho2.71828@gmai.com) (maintainer)

## License

Trivial-signal is free and unencumbered software released into the public domain.

Version

0.1.0

Defsystem Dependency

cffi-grovel (system).

Dependencies
  • cffi (system).
  • bordeaux-threads (system).
Source

trivial-signal.asd.

Child Component

src (module).


3 Modules

Modules are listed depth-first from the system components tree.


3.1 trivial-signal/src

Source

trivial-signal.asd.

Parent Component

trivial-signal (system).

Child Components

4 Files

Files are sorted by type and then listed depth-first from the systems components trees.


4.1 Lisp


4.1.1 trivial-signal/trivial-signal.asd

Source

trivial-signal.asd.

Parent Component

trivial-signal (system).

ASDF Systems

trivial-signal.

Packages

trivial-signal-asd.


4.1.2 trivial-signal/src/packages.lisp

Source

trivial-signal.asd.

Parent Component

src (module).

Packages

4.1.3 trivial-signal/src/grovel.lisp

Dependency

packages.lisp (file).

Source

trivial-signal.asd.

Parent Component

src (module).


4.1.4 trivial-signal/src/signals.lisp

Dependency

grovel.lisp (file).

Source

trivial-signal.asd.

Parent Component

src (module).

Public Interface
Internals

4.1.5 trivial-signal/src/trivial-signal.lisp

Dependency

signals.lisp (file).

Source

trivial-signal.asd.

Parent Component

src (module).

Public Interface
Internals

5 Packages

Packages are listed by definition order.


5.1 trivial-signal

Source

packages.lisp.

Use List
Public Interface
Internals

5.2 trivial-signal-asd

Source

trivial-signal.asd.

Use List
  • asdf/interface.
  • common-lisp.

5.3 trivial-signal.signals

Source

packages.lisp.

Use List

common-lisp.

Used By List

trivial-signal.

Public Interface
Internals

6 Definitions

Definitions are sorted by export status, category, package, and then by lexicographic order.


6.1 Public Interface


6.1.1 Macros

Macro: signal-handler-bind (bindings &body forms)

Execute FORMS in a dynamic environment where thread-local signal handler bindings are in effect.

The syntax is almost identical to cl:handler-bind. Example:

(tagbody
(signal-handler-bind ((15 (lambda (c) (print :first)))
(15 (lambda (c) (print :escaping) (go :escape)))
(2 (lambda (c) (print :escaping) (go :escape)))
(15 (lambda (c) (print :this-should-not-be-printed)))) (loop (sleep 3)))
:escape
(print :success!))

Now send signal 15 to the main lisp process using the terminal. It should
print :FIRST, :ESCAPING and :SUCCESS.

(Note: it does not work on some implementations, due to their
internals. In such cases, try another signal number, e.g. 10 !)

Package

trivial-signal.

Source

trivial-signal.lisp.

Macro: with-signal-handler (signal fn &body forms)

This is only for the backward compatibility

Package

trivial-signal.

Source

trivial-signal.lisp.


6.1.2 Ordinary functions

Function: call-signal-handler-bind (new-signal-handlers fn)

Execute FN in a dynamic environment where signal handler bindings are
in effect. new-signal-handlers is a cons tree of ((signo handler ...) ...)

note that, trivial-signal only considers the first appearance of (signo handlers...) with matching signo in the same layer. For example,

(call-signal-handler-bind
‘((,*signo* ,(lambda (c) (lprint :first))
,(lambda (c) (lprint :escaping) (go :escape))
,(lambda (c) (lprint :this-should-not-be-called))))
(lambda () ...))

is okay but

(call-signal-handler-bind
‘((,*signo* ,(lambda (c) (lprint :first)))
(,*signo* ,(lambda (c) (lprint :escaping) (go :escape)))
(,*signo* ,(lambda (c) (lprint :this-should-not-be-called))))
(lambda () ... ))

is incorrect (2nd and 3rd handlers are ignored).
If you want to do it wrap the main code in (lambda () ...)
with another call-signal-handler-bind.

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: remove-all-signal-handlers ()

Removes all toplevel signal handlers

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: remove-signal-handler (signal)

Deprecated.
To remove the toplevel signal handler, (setf (signal-handler signo) nil) .

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: signal-handler (signal)

Returns the toplevel signal handler for a signal SIGNAL.
Toplevel handlers are system wide and (in most cases) static.
Unlike normal signal handlers, toplevel handlers can hold at most one
handler for the same signal.

Example:

(use-package :trivial-signal)
(defun exit-on-signal (signo)
(format *error-output* "~&received ~A~%" (signal-name signo))
(sb-ext:exit :code 1 :abort t))
(setf (signal-handler :term) #’exit-on-signal) ;; :term can also be :sigterm or 15

(loop (sleep 3))

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: (setf signal-handler) (signal)

Set the toplevel signal handler FN for a signal SIGNAL. Toplevel handlers can hold at most one handler for the same signal.

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: signal-name (signo)

Return the name of SIGNO as a keyword.

Package

trivial-signal.signals.

Source

signals.lisp.

Function: signal-number (sigspec)

Return the integer of SIGSPEC, which follows the sigspec API e.g. +SIGHUP+, :hup or :sighup.

Package

trivial-signal.signals.

Source

signals.lisp.


6.1.3 Generic functions

Generic Reader: signo (condition)
Package

trivial-signal.

Methods
Reader Method: signo ((condition unix-signal))
Source

trivial-signal.lisp.

Target Slot

signo.


6.1.4 Conditions

Condition: unix-signal
Package

trivial-signal.

Source

trivial-signal.lisp.

Direct superclasses

condition.

Direct methods

signo.

Direct slots
Slot: signo
Initargs

:signo

Readers

signo.

Writers

This slot is read-only.


6.2 Internals


6.2.1 Special variables

Special Variable: *currently-enabled-signals*
Package

trivial-signal.

Source

trivial-signal.lisp.

Special Variable: *listener-threads*
Package

trivial-signal.

Source

trivial-signal.lisp.

Special Variable: *listener-threads-lock*
Package

trivial-signal.

Source

trivial-signal.lisp.

Special Variable: *listening-signal-p*

per-thread flag for whether the current thread is listening signals

Package

trivial-signal.

Source

trivial-signal.lisp.

Special Variable: *signal-handler-hierarchy*

Nested cons-tree of signal handlers

Package

trivial-signal.

Source

trivial-signal.lisp.

Special Variable: *signal-keyword*
Package

trivial-signal.signals.

Source

signals.lisp.

Special Variable: *toplevel-signal-handlers*

Toplevel handler

Package

trivial-signal.

Source

trivial-signal.lisp.


6.2.2 Ordinary functions

Function: %disable-all-signal-handlers (signals)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: %disable-signal-handler (signo)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: %enable-all-signal-handlers (signals)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: %enable-signal-handler (signo)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: %inline-bindings (bindings)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: %next-enabled-signals (new-signal-handlers)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: assocdr (item alist)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: canonical-signal-arg (signal)
Package

trivial-signal.

Source

trivial-signal.lisp.

Function: constant->keyword (symbol)

+SIGHUP+ -> :HUP

Package

trivial-signal.signals.

Source

signals.lisp.

Function: constant->sigkeyword (symbol)

+SIGHUP+ -> :SIGHUP

Package

trivial-signal.signals.

Source

signals.lisp.

Function: group-bindings (bindings)

returns an alist of ((name handler1 handler2 ...)=cons ...)

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: invoke-handlers (signo)

Handler-invoking procedure per thread

Package

trivial-signal.

Source

trivial-signal.lisp.

Function: keyword->constant (symbol)

:HUP -> +SIGHUP+

Package

trivial-signal.signals.

Source

signals.lisp.


Appendix A Indexes


A.1 Concepts


A.2 Functions

Jump to:   %   (  
A   C   F   G   I   K   M   R   S   W  
Index Entry  Section

%
%disable-all-signal-handlers: Private ordinary functions
%disable-signal-handler: Private ordinary functions
%enable-all-signal-handlers: Private ordinary functions
%enable-signal-handler: Private ordinary functions
%inline-bindings: Private ordinary functions
%next-enabled-signals: Private ordinary functions

(
(setf signal-handler): Public ordinary functions

A
assocdr: Private ordinary functions

C
call-signal-handler-bind: Public ordinary functions
canonical-signal-arg: Private ordinary functions
constant->keyword: Private ordinary functions
constant->sigkeyword: Private ordinary functions

F
Function, %disable-all-signal-handlers: Private ordinary functions
Function, %disable-signal-handler: Private ordinary functions
Function, %enable-all-signal-handlers: Private ordinary functions
Function, %enable-signal-handler: Private ordinary functions
Function, %inline-bindings: Private ordinary functions
Function, %next-enabled-signals: Private ordinary functions
Function, (setf signal-handler): Public ordinary functions
Function, assocdr: Private ordinary functions
Function, call-signal-handler-bind: Public ordinary functions
Function, canonical-signal-arg: Private ordinary functions
Function, constant->keyword: Private ordinary functions
Function, constant->sigkeyword: Private ordinary functions
Function, group-bindings: Private ordinary functions
Function, invoke-handlers: Private ordinary functions
Function, keyword->constant: Private ordinary functions
Function, remove-all-signal-handlers: Public ordinary functions
Function, remove-signal-handler: Public ordinary functions
Function, signal-handler: Public ordinary functions
Function, signal-name: Public ordinary functions
Function, signal-number: Public ordinary functions

G
Generic Function, signo: Public generic functions
group-bindings: Private ordinary functions

I
invoke-handlers: Private ordinary functions

K
keyword->constant: Private ordinary functions

M
Macro, signal-handler-bind: Public macros
Macro, with-signal-handler: Public macros
Method, signo: Public generic functions

R
remove-all-signal-handlers: Public ordinary functions
remove-signal-handler: Public ordinary functions

S
signal-handler: Public ordinary functions
signal-handler-bind: Public macros
signal-name: Public ordinary functions
signal-number: Public ordinary functions
signo: Public generic functions
signo: Public generic functions

W
with-signal-handler: Public macros