| 1 | title: Starting, ending, and signals |
| 2 | part: Standing alone |
| 3 | summary: What runs before main, what runs after, and how to wait for a signal. |
| 4 | --- |
| 5 | With no run time, the life of a program is short enough to say in a few lines. What happens, in order: |
| 6 | |
| 7 | 1. The entry code sets up a stack for signal handlers and installs the handlers for `SIGSEGV` and `SIGFPE`. A program that uses C starts through C instead, so that C's own set-up runs. |
| 8 | 2. The bodies of the modules run, each once, imported modules first. |
| 9 | 3. The main module's `main` runs. |
| 10 | 4. `Rt.exit` runs the procedures registered with `Rt.atExit`, the last registered first, and ends the program with `main`'s status. |
| 11 | |
| 12 | ## Bodies, then main |
| 13 | |
| 14 | The first tour showed bodies running before `main`. They are the place to set a module up, and they can end early with `return;`. |
| 15 | |
| 16 | ```raz Order run |
| 17 | module Order; |
| 18 | import Out; |
| 19 | |
| 20 | var steps*: INT; |
| 21 | |
| 22 | proc note(what: []const U8); |
| 23 | begin |
| 24 | INC(steps); |
| 25 | Out.int(steps); Out.str(". "); Out.str(what); Out.ln; |
| 26 | end; |
| 27 | |
| 28 | begin |
| 29 | note("the body runs before main"); |
| 30 | if steps > 5 then return; end; |
| 31 | note("and carries on if it isn't ended early"); |
| 32 | end Order. |
| 33 | ``` |
| 34 | |
| 35 | That file is a module with a body and no `main`: it can be imported, or its body run as a program. Add the `main` and see both: |
| 36 | |
| 37 | ```raz Both run |
| 38 | module Both; |
| 39 | import Out, Order; |
| 40 | |
| 41 | proc main*; |
| 42 | begin |
| 43 | Out.str("main: "); Out.int(Order.steps); Out.str(" steps happened first\n"); |
| 44 | end; |
| 45 | |
| 46 | end Both. |
| 47 | ``` |
| 48 | |
| 49 | ## Things to do at the end |
| 50 | |
| 51 | `Rt.atExit(p)` registers a procedure with no parameters to run when the program ends by `Rt.exit`: at the end of `main`, by a call to `Rt.exit`, or by a failed check, after its message. It is where a program restores a terminal or removes a lock file. At most 32 are registered, and each runs at most once. Out's buffer is flushed this way, which is why a program that ends in a failure still shows what it printed. |
| 52 | |
| 53 | ```raz Cleanup run |
| 54 | module Cleanup; |
| 55 | import Out, Rt; |
| 56 | |
| 57 | proc first; |
| 58 | begin |
| 59 | Out.str("registered first, runs last\n"); |
| 60 | end; |
| 61 | |
| 62 | proc second; |
| 63 | begin |
| 64 | Out.str("registered second, runs first\n"); |
| 65 | end; |
| 66 | |
| 67 | proc main*; |
| 68 | begin |
| 69 | _ := Rt.atExit(first); |
| 70 | _ := Rt.atExit(second); |
| 71 | Out.str("main is done\n"); |
| 72 | end; |
| 73 | |
| 74 | end Cleanup. |
| 75 | ``` |
| 76 | |
| 77 | ## Two ways to leave in a hurry |
| 78 | |
| 79 | `Rt.exit(status)` ends the program after the `atExit` procedures. `Sys.exit(status)` ends it at once, without them. A failed check and `Rt.exit` also skip the `defer` statements still pending in the calls under way. `Sys.exit` is for a child process that must not run what it inherited, and for nothing else. |
| 80 | |
| 81 | ```raz Hurry |
| 82 | module Hurry; |
| 83 | import Out, Rt, Sys; |
| 84 | |
| 85 | proc bye; |
| 86 | begin |
| 87 | Out.str("atExit ran\n"); |
| 88 | end; |
| 89 | |
| 90 | proc main*; |
| 91 | begin |
| 92 | _ := Rt.atExit(bye); |
| 93 | defer Out.str("a deferred statement ran\n"); |
| 94 | Out.str("leaving\n"); |
| 95 | Out.flush; |
| 96 | Sys.exit(3); |
| 97 | end; |
| 98 | |
| 99 | end Hurry. |
| 100 | ``` |
| 101 | |
| 102 | ```sh |
| 103 | $ raz Hurry.raz |
| 104 | $ ./Hurry; echo "status $?" |
| 105 | ``` |
| 106 | |
| 107 | Neither the deferred statement nor `bye` ran. That's the rule, and the reason to keep cleanup that matters (a terminal, a lock file) in `atExit` and use `Rt.exit` for ending. |
| 108 | |
| 109 | ## Signals |
| 110 | |
| 111 | A program is stopped by a signal it doesn't catch, with a message that names the place for `SIGSEGV` and `SIGFPE`, and without one for the others, and ends with status 2. A program that wants to handle one, say `SIGTERM` for a clean shutdown, doesn't write a handler that runs at an arbitrary moment. It asks for the signals to arrive as data: `Sys.catch` takes a list of signals and gives a descriptor that is ready to read when one comes, and `Sys.caught` says which. So the program waits for signals the way it waits for input. |
| 112 | |
| 113 | ```raz Terms run |
| 114 | module Terms; |
| 115 | import Out, Sys; |
| 116 | |
| 117 | proc main*; |
| 118 | var fd, sig: INT; |
| 119 | begin |
| 120 | fd := Sys.catch([Sys.sigTerm, Sys.sigWinch]); |
| 121 | Out.str("asking myself to stop\n"); |
| 122 | _ := Sys.kill(Sys.pid(), Sys.sigTerm); |
| 123 | if Sys.ready(fd, 1000) then |
| 124 | sig := Sys.caught(fd); |
| 125 | if sig = Sys.sigTerm then Out.str("caught SIGTERM, stopping cleanly\n"); end; |
| 126 | end; |
| 127 | sig := Sys.caught(fd); |
| 128 | Out.int(sig); Out.str(" waiting after that\n"); (* 0: none *) |
| 129 | end; |
| 130 | |
| 131 | end Terms. |
| 132 | ``` |
| 133 | |
| 134 | `Sys.ready(fd, ms)` waits up to `ms` milliseconds for something to read; with `Sys.readyOf` it waits on several descriptors, so one loop serves a socket, a terminal and a stop signal together. `Sys.ignore(sig, true)` makes a signal do nothing, as a shell ignores `SIGTTOU`. |