1 // This is free and unencumbered software released into the public domain.
3 // Anyone is free to copy, modify, publish, use, compile, sell, or
4 // distribute this software, either in source code form or as a compiled
5 // binary, for any purpose, commercial or non-commercial, and by any
8 // In jurisdictions that recognize copyright laws, the author or authors
9 // of this software dedicate any and all copyright interest in the
10 // software to the public domain. We make this dedication for the benefit
11 // of the public at large and to the detriment of our heirs and
12 // successors. We intend this dedication to be an overt act of
13 // relinquishment in perpetuity of all present and future rights to this
14 // software under copyright law.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 // OTHER DEALINGS IN THE SOFTWARE.
30 // In theory we could use TLS to store an event base per-thread and
31 // then we could have a per-thread signal cacher machinery. In
32 // practice, we only provide signal catching for the global event
37 info: *signal::siginfo,
40 // We should be able to write up to PIPE_BUF bytes without
45 let pipe: (io::file, io::file) = match(global.sigpipe) {
49 case let pipe: (io::file, io::file) =>
53 // TODO how to save and restore errno?
54 match (rt::write(pipe.1, &no, size(u8))) {
56 // can't do anything about it.
61 fn sigdispatch(fd: io::file, ev: event, data: nullable *opaque) void = {
65 let pipe = global.sigpipe as (io::file, io::file);
67 match (rt::read(pipe.0, &no, size(u8))) {
68 case let err: rt::errno =>
69 // can't do much about.
74 let sigcb = global.sigcb as *fn(signal::sig, nullable *opaque) void;
75 sigcb(no: signal::sig, global.sigdata);
80 cb: *fn(signal::sig, nullable *opaque) void,
81 data: nullable *opaque
82 ) (void | errors::error) = {
83 if (global.sigpipe is void) {
84 // XXX NOCLOEXEC is O_CLOEXEC or means "do not close-on-exec"?
86 let pipe = unix::pipe(
87 unix::pipe_flag::NONBLOCK | unix::pipe_flag::NOCLOEXEC
89 global.sigpipe = pipe;
90 add(pipe.0, READ, &sigdispatch, null);
94 global.sigdata = data;
96 signal::handle(sig, &sigcatch);