Blob


1 // This is free and unencumbered software released into the public domain.
2 //
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
6 // means.
7 //
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.
15 //
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.
24 use errors;
25 use io;
26 use rt;
27 use unix::signal;
28 use unix;
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
33 // loop.
35 fn sigcatch(
36 sig: signal::sig,
37 info: *signal::siginfo,
38 ctx: *opaque
39 ) void = {
40 // We should be able to write up to PIPE_BUF bytes without
41 // blocking.
42 let no = sig: u8;
44 // read, write
45 let pipe: (io::file, io::file) = match(global.sigpipe) {
46 case void =>
47 // this can't happen
48 return;
49 case let pipe: (io::file, io::file) =>
50 yield pipe;
51 };
53 // TODO how to save and restore errno?
54 match (rt::write(pipe.1, &no, size(u8))) {
55 case =>
56 // can't do anything about it.
57 return;
58 };
59 };
61 fn sigdispatch(fd: io::file, ev: event, data: nullable *opaque) void = {
62 let no: u8 = 0;
64 // read, write
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.
70 return;
71 case => yield;
72 };
74 let sigcb = global.sigcb as *fn(signal::sig, nullable *opaque) void;
75 sigcb(no: signal::sig, global.sigdata);
76 };
78 export fn signal(
79 sig: signal::sig,
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"?
85 // read, write
86 let pipe = unix::pipe(
87 unix::pipe_flag::NONBLOCK | unix::pipe_flag::NOCLOEXEC
88 )?;
89 global.sigpipe = pipe;
90 add(pipe.0, READ, &sigdispatch, null);
91 };
93 global.sigcb = cb;
94 global.sigdata = data;
96 signal::handle(sig, &sigcatch);
97 };