Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 27ec05ea3e
Fetching contributors…

Cannot retrieve contributors at this time

file 247 lines (221 sloc) 7.943 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
%% Copyright (c) 2012, Michael Santos <michael.santos@gmail.com>
%% All rights reserved.
%%
%% Redistribution and use in source and binary forms, with or without
%% modification, are permitted provided that the following conditions
%% are met:
%%
%% Redistributions of source code must retain the above copyright
%% notice, this list of conditions and the following disclaimer.
%%
%% Redistributions in binary form must reproduce the above copyright
%% notice, this list of conditions and the following disclaimer in the
%% documentation and/or other materials provided with the distribution.
%%
%% Neither the name of the author nor the names of its contributors
%% may be used to endorse or promote products derived from this software
%% without specific prior written permission.
%%
%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
%% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
%% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
%% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
%% COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
%% INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
%% BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
%% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
%% CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
%% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(perc_signal).
-behaviour(gen_server).

-include_lib("perc/include/perc.hrl").
-include_lib("perc/include/perc_signal.hrl").

-export([
    define/1,
    signalfd_siginfo/1
    ]).
-export([
    start/1,
    stop/1,
    getfd/1,

    start_link/1
    ]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
    terminate/2, code_change/3]).

-record(state, {
        port,
        pid,
        fd
        }).



%%--------------------------------------------------------------------
%%% Exports
%%--------------------------------------------------------------------
start(Signals) when is_list(Signals) ->
    start_link(Signals).

stop(Ref) when is_pid(Ref) ->
    gen_server:call(Ref, stop).

getfd(Ref) when is_pid(Ref) ->
    gen_server:call(Ref, getfd).

start_link(Signals) when is_list(Signals) ->
    case perc:sigaddset(Signals) of
        {ok, Mask} ->
            Pid = self(),
            gen_server:start_link(?MODULE, [Pid, Mask], []);
        Err ->
            Err
    end.


%%--------------------------------------------------------------------
%%% Callbacks
%%--------------------------------------------------------------------
init([Pid, Mask]) ->
    process_flag(trap_exit, true),

    case perc:signalfd(Mask) of
        {ok, FD} ->
            Port = erlang:open_port({fd, FD, FD}, [stream, binary]),
            {ok, #state{
                port = Port,
                pid = Pid,
                fd = FD
            }};
        Err ->
            Err
    end.


handle_call(getfd, _From, #state{fd = FD} = State) ->
    {reply, FD, State};

handle_call(stop, _From, State) ->
    {stop, normal, ok, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

%% Signal received
handle_info({Port, {data, Data}}, #state{port = Port, pid = Pid} = State) ->
    [ Pid ! {signal, self(), signalfd_siginfo(Signal)} ||
        <<Signal:128/binary>> <= Data ],
    {noreply, State};

% WTF?
handle_info(Info, State) ->
    error_logger:error_report([wtf, Info]),
    {noreply, State}.

terminate(_Reason, #state{fd = FD, port = Port}) ->
    catch erlang:port_close(Port),
    perc:close(FD),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%--------------------------------------------------------------------
%%% Utility functions
%%--------------------------------------------------------------------

% Signal definitions: linux
define(sighup) -> 1;
define(sigint) -> 2;
define(sigquit) -> 3;
define(sigill) -> 4;
define(sigtrap) -> 5;
define(sigabrt) -> 6;
define(sigbus) -> 7;
define(sigfpe) -> 8;
define(sigkill) -> 9;
define(sigusr1) -> 10;
define(sigsegv) -> 11;
define(sigusr2) -> 12;
define(sigpipe) -> 13;
define(sigalrm) -> 14;
define(sigterm) -> 15;
define(sigstkflt) -> 16;
define(sigchld) -> 17;
define(sigcont) -> 18;
define(sigstop) -> 19;
define(sigtstp) -> 20;
define(sigttin) -> 21;
define(sigttou) -> 22;
define(sigurg) -> 23;
define(sigxcpu) -> 24;
define(sigxfsz) -> 25;
define(sigvtalrm) -> 26;
define(sigprof) -> 27;
define(sigwinch) -> 28;
define(sigio) -> 29;
define(sigpwr) -> 30;
define(sigsys) -> 31;
define(sigrtmin) -> 34;
define(sigrtmax) -> 64.

signalfd_siginfo(<<
        ?UINT32(Signo), % Signal number
        ?INT32(Errno), % Error number (unused)
        ?INT32(Code), % Signal code
        ?UINT32(Pid), % PID of sender
        ?UINT32(Uid), % Real UID of sender
        ?INT32(Fd), % File descriptor (SIGIO)
        ?UINT32(Tid), % Kernel timer ID (POSIX timers)
        ?UINT32(Band), % Band event (SIGIO)
        ?UINT32(Overrun), % POSIX timer overrun count
        ?UINT32(Trapno), % Trap number that caused signal
        ?INT32(Status), % Exit status or signal (SIGCHLD)
        ?INT32(Int), % Integer sent by sigqueue(2)
        ?UINT64(Ptr), % Pointer sent by sigqueue(2)
        ?UINT64(Utime), % User CPU time consumed (SIGCHLD)
        ?UINT64(Stime), % System CPU time consumed (SIGCHLD)
        ?UINT64(Addr), % Address that generated signal (for hardware-generated signals)
        Pad/binary % Pad size to 128 bytes (allow for additional fields in the future)
    >> = Data) when byte_size(Data) =:= 128 ->
    #signalfd_siginfo{
        ssi_signo = Signo,
        ssi_errno = Errno,
        ssi_code = Code,
        ssi_pid = Pid,
        ssi_uid = Uid,
        ssi_fd = Fd,
        ssi_tid = Tid,
        ssi_band = Band,
        ssi_overrun = Overrun,
        ssi_trapno = Trapno,
        ssi_status = Status,
        ssi_int = Int,
        ssi_ptr = Ptr,
        ssi_utime = Utime,
        ssi_stime = Stime,
        ssi_addr = Addr,
        pad = Pad
        };
signalfd_siginfo(#signalfd_siginfo{
        ssi_signo = Signo,
        ssi_errno = Errno,
        ssi_code = Code,
        ssi_pid = Pid,
        ssi_uid = Uid,
        ssi_fd = Fd,
        ssi_tid = Tid,
        ssi_band = Band,
        ssi_overrun = Overrun,
        ssi_trapno = Trapno,
        ssi_status = Status,
        ssi_int = Int,
        ssi_ptr = Ptr,
        ssi_utime = Utime,
        ssi_stime = Stime,
        ssi_addr = Addr,
        pad = Pad
    }) ->
    <<
        ?UINT32(Signo), % Signal number
        ?INT32(Errno), % Error number (unused)
        ?INT32(Code), % Signal code
        ?UINT32(Pid), % PID of sender
        ?UINT32(Uid), % Real UID of sender
        ?INT32(Fd), % File descriptor (SIGIO)
        ?UINT32(Tid), % Kernel timer ID (POSIX timers)
        ?UINT32(Band), % Band event (SIGIO)
        ?UINT32(Overrun), % POSIX timer overrun count
        ?UINT32(Trapno), % Trap number that caused signal
        ?INT32(Status), % Exit status or signal (SIGCHLD)
        ?INT32(Int), % Integer sent by sigqueue(2)
        ?UINT64(Ptr), % Pointer sent by sigqueue(2)
        ?UINT64(Utime), % User CPU time consumed (SIGCHLD)
        ?UINT64(Stime), % System CPU time consumed (SIGCHLD)
        ?UINT64(Addr), % Address that generated signal (for hardware-generated signals)
        Pad/binary % Pad size to 128 bytes (allow for additional fields in the future)
    >>.


%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
Something went wrong with that request. Please try again.