Standing alone · lesson 9 of 18

Starting, ending, and signals

With no run time, the life of a program is short enough to say in a few lines. What happens, in order:

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. 2. The bodies of the modules run, each once, imported modules first. 3. The main module's main runs. 4. Rt.exit runs the procedures registered with Rt.atExit, the last registered first, and ends the program with main's status.

Bodies, then main

The first tour showed bodies running before main. They are the place to set a module up, and they can end early with return;.

Order.raz
module Order;
import Out;

var steps*: INT;

proc note(what: []U8);
begin
    INC(steps);
    Out.int(steps); Out.str(". "); Out.str(what); Out.ln;
end;

begin
    note("the body runs before main");
    if steps > 5 then return; end;
    note("and carries on if it isn't ended early");
end Order.
1. the body runs before main
2. and carries on if it isn't ended early

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:

Both.raz
module Both;
import Out, Order;

proc main*;
begin
    Out.str("main: "); Out.int(Order.steps); Out.str(" steps happened first\n");
end;

end Both.
1. the body runs before main
2. and carries on if it isn't ended early
main: 2 steps happened first

Things to do at the end

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.

Cleanup.raz
module Cleanup;
import Out, Rt;

proc first;
begin
    Out.str("registered first, runs last\n");
end;

proc second;
begin
    Out.str("registered second, runs first\n");
end;

proc main*;
begin
    _ := Rt.atExit(first);
    _ := Rt.atExit(second);
    Out.str("main is done\n");
end;

end Cleanup.
main is done
registered second, runs first
registered first, runs last

Two ways to leave in a hurry

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.

Hurry.raz
module Hurry;
import Out, Rt, Sys;

proc bye;
begin
    Out.str("atExit ran\n");
end;

proc main*;
begin
    _ := Rt.atExit(bye);
    defer Out.str("a deferred statement ran\n");
    Out.str("leaving\n");
    Out.flush;
    Sys.exit(3);
end;

end Hurry.
$ raz Hurry.raz
$ ./Hurry; echo "status $?"
leaving
status 3

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.

Signals

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. 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.

Terms.raz
module Terms;
import Out, Sys;

proc main*;
var fd, sig: INT;
begin
    fd := Sys.catch([Sys.sigTerm, Sys.sigWinch]);
    Out.str("asking myself to stop\n");
    _ := Sys.kill(Sys.pid(), Sys.sigTerm);
    if Sys.ready(fd, 1000) then
        sig := Sys.caught(fd);
        if sig = Sys.sigTerm then Out.str("caught SIGTERM, stopping cleanly\n"); end;
    end;
    sig := Sys.caught(fd);
    Out.int(sig); Out.str(" waiting after that\n");     (* 0: none *)
end;

end Terms.
asking myself to stop
caught SIGTERM, stopping cleanly
0 waiting after that

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.

Save a program under its module’s name and run it with raz run Name.raz.