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