Overview
The operating system the program runs on, the machine and the process, the user’s standard folders, and signals.
(import (std os))
(: num-lock? (-> (Option bool)))
(defun (num-lock? )
(cond
((windows?) (Some (windows-num-lock?))) ; from a module of its own
((linux?) (linux-num-lock?))
(else None)))
(when (windows-version-at-least? 10 #:build 22000)
(displayln "Windows 11"))
A program is built once and runs on every system, so the choice is made when it runs. Each check is a call of the .NET method itself. .NET’s core library is built for one system, so on that system the method returns a constant: the JIT removes the branch that cannot run, and the check costs nothing.
Code for another system can be in a module of its own, imported
everywhere. A module’s definitions run the first time something in it
is used, so on a system where nothing calls it, it does nothing. Every
module must still compile on the machine that builds it: a .NET method
that exists only on one system, as Console.NumberLock on Windows,
is declared everywhere and throws where it is not supported.
The facts about the machine and the process are .NET’s
RuntimeInformation and Environment. The folders are
Environment.GetFolderPath, which follows the XDG variables on
Linux, and a cache folder, which .NET does not have. A folder is given
whether or not it exists yet: create it before you write to it.
Signals. A program that does nothing about signals ends at
once on Ctrl-C or SIGTERM: no finally runs and no scope releases
what it owns. with-signal-cancel is a scope that the first of its
signals cancels, so the program cleans up and goes on. A second signal
does the default action and ends the program. on-signal calls a
function for each signal that comes, as for SIGHUP to read the
configuration again. signal-subscribe and signal-recv give
the signals as values to a fiber.
;; Where a program keeps its settings.
(def settings (path-combine (config-directory) "my-app" "settings.bjodat"))
(directory-create (path-directory settings))
(defbjo (main args)
(match (with-signal-cancel [Interrupt Terminate]
(serve config))
((Ok _) 0)
((Err s) (displayln (str "stopped by " (signal-name s))) 0)))
See also: windows?, linux?, os-platform?, windows-version-at-least?, os-description, processor-count, config-directory, with-signal-cancel, on-signal, signal-subscribe
Reference
android-version-at-least?android?available-memorybrowser?cache-directoryconfig-directorydata-directorydesktop-directorydocuments-directorydotnet-descriptionfreebsd-version-at-least?freebsd?home-directoryios-version-at-least?ios?linux?mac-catalyst-version-at-least?mac-catalyst?machine-namemacos-version-at-least?macos?make-temp-directoryon-signalos-architectureos-descriptionos-platform-version-at-least?os-platform?os-versionprivileged?process-architectureprocess-idprocess-pathprocessor-countruntime-identifiersignal-cancel-arm!signal-cancel-resultsignal-namesignal-recvsignal-subscribesignal-unsubscribe!temp-directorytvos-version-at-least?tvos?user-namewasi?watchos-version-at-least?watchos?windows-version-at-least?windows?
Types
Signal
union ↑ Types
A POSIX signal: a message from the system or from another process to this process.
Hangup- SIGHUP: the terminal closed. Many daemons read their configuration again on it. On Windows, the console closed.
Interrupt- SIGINT: Ctrl-C in a terminal, also on Windows.
Quit- SIGQUIT: Ctrl-\ in a terminal. On Windows, Ctrl-Break.
Terminate- SIGTERM: a request to stop, as from kill or systemd. On Windows, log off or shut down.
Child- SIGCHLD: a child process stopped or ended.
Continue- SIGCONT: the process continues after a stop.
WindowChanged- SIGWINCH: the terminal changed its size.
TerminalInput- SIGTTIN: a background process read from the terminal.
TerminalOutput- SIGTTOU: a background process wrote to the terminal.
TerminalStop- SIGTSTP: Ctrl-Z in a terminal.
User1- SIGUSR1, for the program's own use: 10 on Linux, 30 on macOS and FreeBSD.
User2- SIGUSR2, for the program's own use: 12 on Linux, 31 on macOS and FreeBSD.
OtherSignal- A signal by its number on this system, as (OtherSignal 34) for SIGRTMIN on Linux.
Windows has only Hangup, Interrupt, Quit and
Terminate, and a subscription to another signal raises there.
Microsoft’s documentation says that Windows ends the process soon
after Hangup and Terminate, whatever the program does.
See also: signal-subscribe, with-signal-cancel, signal-name
Signals
record ↑ Types
A subscription to signals, from signal-subscribe.
See also: signal-subscribe, signal-recv, signal-unsubscribe!
Functions
windows?
function ↑ Functions
(: windows? (-> bool))
Whether the program runs on Windows.
- returns
- #t on Windows.
See also: windows-version-at-least?, os-platform?
linux?
function ↑ Functions
(: linux? (-> bool))
Whether the program runs on Linux. Not on Android, which has android?.
- returns
- #t on Linux.
See also: android?, os-platform?
macos?
function ↑ Functions
(: macos? (-> bool))
Whether the program runs on macOS. Not on Mac Catalyst, which has mac-catalyst?.
- returns
- #t on macOS.
See also: macos-version-at-least?, mac-catalyst?, os-platform?
freebsd?
function ↑ Functions
(: freebsd? (-> bool))
Whether the program runs on FreeBSD.
- returns
- #t on FreeBSD.
See also: freebsd-version-at-least?, os-platform?
android?
function ↑ Functions
(: android? (-> bool))
Whether the program runs on Android.
- returns
- #t on Android.
See also: android-version-at-least?, linux?
ios?
function ↑ Functions
(: ios? (-> bool))
Whether the program runs on iOS. Also true on Mac Catalyst, which runs iOS programs.
- returns
- #t on iOS and Mac Catalyst.
See also: ios-version-at-least?, mac-catalyst?
mac-catalyst?
function ↑ Functions
(: mac-catalyst? (-> bool))
Whether the program runs on Mac Catalyst, iOS on a Mac.
- returns
- #t on Mac Catalyst.
See also: mac-catalyst-version-at-least?, ios?, macos?
tvos?
function ↑ Functions
(: tvos? (-> bool))
Whether the program runs on tvOS.
- returns
- #t on tvOS.
See also: tvos-version-at-least?
watchos?
function ↑ Functions
(: watchos? (-> bool))
Whether the program runs on watchOS.
- returns
- #t on watchOS.
See also: watchos-version-at-least?
browser?
function ↑ Functions
(: browser? (-> bool))
Whether the program runs in a browser, as WebAssembly.
- returns
- #t in a browser.
See also: wasi?
wasi?
function ↑ Functions
(: wasi? (-> bool))
Whether the program runs on WASI, WebAssembly outside a browser.
- returns
- #t on WASI.
See also: browser?
os-platform?
function ↑ Functions
(: os-platform? (-> string bool))
Whether the program runs on the system of this name.
platform- The name, in any case: "Windows", "Linux", "macOS" or "OSX", "FreeBSD", "Android", "iOS", "MacCatalyst", "tvOS", "watchOS", "Browser" or "WASI".
- returns
- #t on that system. A name that is no system is #f.
(os-platform? "linux") ; #t on Linux
As the checks of their own, "iOS" is also true on Mac Catalyst.
See also: os-platform-version-at-least?, windows?, linux?
windows-version-at-least?
function ↑ Functions
(: windows-version-at-least? (-> int (#:minor int) (#:build int) (#:revision int) bool))
(windows-version-at-least? major #:minor 0 #:build 0 #:revision 0)
Whether the program runs on Windows of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
#:revision- The revision. 0 unless given.
- returns
- #t on Windows of that version or later; #f on any other system.
(windows-version-at-least? 10 #:build 22000) ; Windows 11 or later
See also: windows?
macos-version-at-least?
function ↑ Functions
(: macos-version-at-least? (-> int (#:minor int) (#:build int) bool))
(macos-version-at-least? major #:minor 0 #:build 0)
Whether the program runs on macOS of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
- returns
- #t on macOS of that version or later; #f on any other system.
(macos-version-at-least? 14) ; Sonoma or later
See also: macos?
freebsd-version-at-least?
function ↑ Functions
(: freebsd-version-at-least? (-> int (#:minor int) (#:build int) (#:revision int) bool))
(freebsd-version-at-least? major #:minor 0 #:build 0 #:revision 0)
Whether the program runs on FreeBSD of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
#:revision- The revision. 0 unless given.
- returns
- #t on FreeBSD of that version or later; #f on any other system.
See also: freebsd?
android-version-at-least?
function ↑ Functions
(: android-version-at-least? (-> int (#:minor int) (#:build int) (#:revision int) bool))
(android-version-at-least? major #:minor 0 #:build 0 #:revision 0)
Whether the program runs on Android of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
#:revision- The revision. 0 unless given.
- returns
- #t on Android of that version or later; #f on any other system.
See also: android?
ios-version-at-least?
function ↑ Functions
(: ios-version-at-least? (-> int (#:minor int) (#:build int) bool))
(ios-version-at-least? major #:minor 0 #:build 0)
Whether the program runs on iOS, or Mac Catalyst, of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
- returns
- #t on iOS or Mac Catalyst of that version or later; #f on any other system.
See also: ios?
mac-catalyst-version-at-least?
function ↑ Functions
(: mac-catalyst-version-at-least? (-> int (#:minor int) (#:build int) bool))
(mac-catalyst-version-at-least? major #:minor 0 #:build 0)
Whether the program runs on Mac Catalyst of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
- returns
- #t on Mac Catalyst of that version or later; #f on any other system.
See also: mac-catalyst?
tvos-version-at-least?
function ↑ Functions
(: tvos-version-at-least? (-> int (#:minor int) (#:build int) bool))
(tvos-version-at-least? major #:minor 0 #:build 0)
Whether the program runs on tvOS of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
- returns
- #t on tvOS of that version or later; #f on any other system.
See also: tvos?
watchos-version-at-least?
function ↑ Functions
(: watchos-version-at-least? (-> int (#:minor int) (#:build int) bool))
(watchos-version-at-least? major #:minor 0 #:build 0)
Whether the program runs on watchOS of this version or later.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
- returns
- #t on watchOS of that version or later; #f on any other system.
See also: watchos?
os-platform-version-at-least?
function ↑ Functions
(: os-platform-version-at-least? (-> string int (#:minor int) (#:build int) (#:revision int) bool))
(os-platform-version-at-least? platform major #:minor 0 #:build 0 #:revision 0)
Whether the program runs on the system of this name, of this version or later.
platform- The name, as os-platform? takes it.
major- The major version.
#:minor- The minor version. 0 unless given.
#:build- The build number. 0 unless given.
#:revision- The revision. 0 unless given.
- returns
- #t on that system, of that version or later.
The version is the one .NET reads for the system. On Linux it is the kernel’s, as 6.18.
See also: os-platform?
os-description
function ↑ Functions
(: os-description (-> string))
(os-description)
The operating system, as a person reads it.
- returns
- A string, as "Fedora Linux 43" or "Microsoft Windows 10.0.22631".
.NET’s RuntimeInformation.OSDescription. Its form differs from
system to system, so it is for a report or a log, not to be read by a
program: on Linux it is the distribution’s name, from
/etc/os-release.
See also: os-version, os-architecture
os-version
function ↑ Functions
(: os-version (-> (Vec int)))
(os-version)
The version of the operating system, as .NET reads it.
- returns
- The parts of the version, most significant first, as [10 0 22631 0] on Windows.
(os-version) ; [6 18 13 200] on Linux 6.18.13-200
On Linux it is the kernel’s version. A part that .NET does not know is
left out, so there are two to four parts. To compare it with a version,
use the version checks, as windows-version-at-least?.
See also: os-description, windows-version-at-least?, os-platform-version-at-least?
os-architecture
function ↑ Functions
(: os-architecture (-> string))
(os-architecture)
The processor architecture of the operating system.
- returns
- Its name in lower case: "x64", "arm64", "x86", "arm", "armv6", "wasm", "s390x", "loongarch64", "ppc64le" or "riscv64".
The names are .NET’s, as a runtime identifier such as
linux-arm64 writes them. A 32-bit process on a 64-bit system
has another process-architecture.
See also: process-architecture, runtime-identifier
process-architecture
function ↑ Functions
(: process-architecture (-> string))
(process-architecture)
The processor architecture this process runs as.
- returns
- Its name in lower case, as os-architecture gives it.
See also: os-architecture
runtime-identifier
function ↑ Functions
(: runtime-identifier (-> string))
(runtime-identifier)
The runtime identifier of .NET in this process, as “linux-x64” or “win-arm64”.
- returns
- A string.
A .NET built by a Linux distribution can give one of its own, as
fedora.43-x64.
See also: os-architecture, dotnet-description
dotnet-description
function ↑ Functions
(: dotnet-description (-> string))
(dotnet-description)
The .NET that runs the program, as “.NET 10.0.4”.
- returns
- A string, for a report or a log.
See also: runtime-identifier
processor-count
function ↑ Functions
(: processor-count (-> int))
(processor-count)
How many processors this process may use.
- returns
- At least 1.
.NET’s Environment.ProcessorCount: the processors of the
machine, less what a container’s CPU limit or the process’s affinity
leaves out.
See also: available-memory
available-memory
function ↑ Functions
(: available-memory (-> long))
(available-memory)
How much memory this process may use, in bytes.
- returns
- A long.
The limit the GC works to: the machine’s memory, or a smaller limit set for a container or for the process. It is not the memory that is free at the moment.
See also: processor-count
machine-name
function ↑ Functions
(: machine-name (-> string))
(machine-name)
The name of this computer on the network.
- returns
- A string.
See also: user-name
user-name
function ↑ Functions
(: user-name (-> string))
(user-name)
The name of the user the process runs as.
- returns
- A string.
See also: machine-name, privileged?, home-directory
process-id
function ↑ Functions
(: process-id (-> int))
(process-id)
This process’s id, as the operating system gives it.
- returns
- An int.
process-path
function ↑ Functions
(: process-path (-> (Option string)))
(process-path)
The executable that runs this process.
- returns
- (Some path), or None when the system does not tell.
A program run by dotnet is run by the dotnet host, so
this is the host’s path, not the program’s.
See also: process-id
privileged?
function ↑ Functions
(: privileged? (-> bool))
(privileged?)
Whether the process runs with the rights of an administrator.
- returns
- #t as root on Unix, and when elevated on Windows.
See also: user-name
home-directory
function ↑ Functions
(: home-directory (-> string))
(home-directory)
The user’s home directory.
- returns
- Its path: HOME on Unix, the user's profile on Windows.
Raises when the system has none, as when HOME is not set and the user has no home in the password database. The other folders raise in the same way.
See also: config-directory, data-directory, cache-directory
config-directory
function ↑ Functions
(: config-directory (-> string))
(config-directory)
The folder for a program’s configuration, which is the user’s own.
- returns
- Its path. Put a program's files in a folder of its own in it.
XDG_CONFIG_HOME on Linux when it is set to an absolute path,
else ~/.config; the roaming application data, %APPDATA%,
on Windows; ~/Library/Application Support on macOS, the same
folder as data-directory’s. It may not exist yet.
See also: data-directory, cache-directory, home-directory
data-directory
function ↑ Functions
(: data-directory (-> string))
(data-directory)
The folder for the data a program keeps for the user on this machine.
- returns
- Its path. Put a program's files in a folder of its own in it.
XDG_DATA_HOME on Linux when it is set to an absolute path, else
~/.local/share; the local application data,
%LOCALAPPDATA%, on Windows; ~/Library/Application Support
on macOS. It may not exist yet.
See also: config-directory, cache-directory
cache-directory
function ↑ Functions
(: cache-directory (-> string))
(cache-directory)
The folder for files a program can make again, which may be deleted.
- returns
- Its path. Put a program's files in a folder of its own in it.
XDG_CACHE_HOME on Linux and the other Unix systems when it is set
to an absolute path, else ~/.cache; the local application data
on Windows; ~/Library/Caches on macOS. .NET has no such folder,
so this follows the XDG Base Directory specification and the systems’
own conventions. It may not exist yet.
See also: data-directory, temp-directory
documents-directory
function ↑ Functions
(: documents-directory (-> string))
(documents-directory)
The user’s folder for documents.
- returns
- Its path: on Linux the XDG documents folder, as ~/Documents.
See also: desktop-directory, home-directory
desktop-directory
function ↑ Functions
(: desktop-directory (-> string))
(desktop-directory)
The user’s desktop folder.
- returns
- Its path: on Linux the XDG desktop folder, as ~/Desktop.
See also: documents-directory, home-directory
temp-directory
function ↑ Functions
(: temp-directory (-> string))
(temp-directory)
The folder for temporary files.
- returns
- Its path, without a separator at the end.
TMPDIR on Unix when it is set, else /tmp; TMP or
TEMP on Windows. Other users can write to it too: for files of
your own, make a directory with make-temp-directory.
See also: make-temp-directory, cache-directory
make-temp-directory
function ↑ Functions
(: make-temp-directory (-> (#:prefix string) string))
(make-temp-directory #:prefix "")
A new, empty directory in the temp folder.
#:prefix- The start of its name. Empty unless given.
- returns
- Its path. Its name is prefix and characters of its own.
(def work (make-temp-directory #:prefix "my-app-")) ; "/tmp/my-app-x3kQ9z"
;; ...
(directory-delete-tree work)
On Unix only its owner may read, write or enter it. It stays until it is deleted.
See also: temp-directory, directory-delete-tree
signal-subscribe
function ↑ Functions
(: signal-subscribe (-> (Vec Signal) Signals))
(signal-subscribe signals)
Subscribes to signals, so that they come as values from signal-recv.
signals- A Vec of the signals. A signal that is given twice counts once.
- returns
- The subscription. The ambient scope owns it.
- raises
System.PlatformNotSupportedException - When this system does not have one of the signals, or cannot handle it, as SIGKILL.
- raises
System.ArgumentException - For (OtherSignal n) with n below 1.
- raises
Bjolang.Runtime.ScopeEnded - When there is no ambient scope, as in the initialiser of a module-level value.
(with-scope
(def signals (signal-subscribe [Hangup Terminate]))
(let loop ()
(match (sync (signal-recv signals))
(Hangup (read-configuration!) (loop))
(_ unit))))
While the subscription lives, its signals do not do their default
action: SIGTERM does not end the process, and Ctrl-Z does not stop it.
The default action of Child, Continue and
WindowChanged is to do nothing, and .NET’s own handling of them
stays. The subscription ends with its scope, or at
signal-unsubscribe!, and then the default action is back. When
it raises, it has subscribed to nothing.
.NET calls the handler on a thread of its own. The handler puts the signal in an inbox with room for one, and each kind of signal has its own inbox. So signals of one kind that come before the program receives them are merged into one, as in OCaml and Racket. Signals of two kinds are two. Every subscription to a signal receives it.
A signal that is ignored when the program starts stays ignored, and does not come. A shell starts a background job with SIGINT and SIGQUIT ignored.
See also: signal-recv, signal-unsubscribe!, with-signal-cancel
signal-recv
function ↑ Functions
(: signal-recv (-> Signals (Event Signal)))
(signal-recv s)
Returns the CML event of the next signal of a subscription.
s- The subscription.
- returns
- An event that yields the signal.
In a choose that another branch wins, it takes nothing. When
signals of more than one kind wait, which one comes first is not given.
After the subscription ends, it never fires.
See also: signal-subscribe, with-signal-cancel
signal-unsubscribe!
function ↑ Functions
(: signal-unsubscribe! (-> Signals void))
(signal-unsubscribe! s)
Ends a subscription before its scope ends.
s- The subscription.
The signals do their default action again, and the signals that wait are dropped. A second call does nothing.
See also: signal-subscribe
on-signal
function ↑ Functions
(: on-signal (-> (Vec Signal) (-bjo-> Signal void) void))
(on-signal signals handler)
Calls a function for each of the signals that comes, for as long as the scope lives.
signals- A Vec of the signals.
handler- What to call with the signal: an ordinary function or a bjoroutine.
- raises
System.PlatformNotSupportedException - As signal-subscribe.
(defun (main)
(on-signal [Interrupt] (fun (s) (println "Ctrl-C: carrying on")))
(do-the-work)
0)
;; From code deeper down, for the whole program.
(def program (current-scope)) ; in main
...
(in-scope program
(on-signal [Hangup] (fun (s) (read-configuration!))))
A daemon of the ambient scope calls the handler. So the handler has the
parameters and effect handlers of the call to on-signal, and it
runs at the same time as the code that called it: share state with it
through (std atomic) or a channel. Only one call runs at a
time. A signal that comes during a call waits, merged with the others
of its kind.
The signals do not do their default action while the handler is there:
Ctrl-C does not end the program. When the scope ends, the handler and
the subscription end together, and the default action is back. In
main that is the end of the program. To handle a signal for the
whole program from code deeper down, call it with in-scope and
the scope of main.
When the handler raises, the signals do their default action again, and the failure is written to standard error as an unhandled exception of the daemon. Nothing else would receive the signals.
See also: signal-subscribe, with-signal-cancel, in-scope
signal-name
function ↑ Functions
(: signal-name (-> Signal string))
(signal-name s)
The POSIX name of a signal.
s- The signal.
- returns
- As "SIGINT" for Interrupt, and "signal 34" for (OtherSignal 34).
See also: Signal
signal-cancel-arm!
function ↑ Functions
(: signal-cancel-arm! (-> (Vec Signal) (-> CancelReason void) void))
(signal-cancel-arm! signals cancel)
Subscribes a scope to signals, so that the first one cancels it. with-signal-cancel writes calls to it.
signals- The signals.
cancel- The cancel thunk of the scope.
It is exported because the template of with-signal-cancel names
it.
See also: with-signal-cancel
signal-cancel-result
function ↑ Functions
(: signal-cancel-result (-> (Vec Signal) (Result Exception %a) CancelToken (Result Signal %a)))
(signal-cancel-result signals outcome token)
The value of with-signal-cancel, from the outcome of its scope. with-signal-cancel writes calls to it.
signals- The signals.
outcome- The value of the body, or the exception that it left with.
token- The token of the scope.
- returns
- (Ok v) for a value, and (Err s) when the signal s cancelled the scope and the body left because of it.
- raises
System.Exception - The exception of the body, in every other case.
It is exported because the template of with-signal-cancel names
it.
See also: with-signal-cancel
Macros
with-signal-cancel
macro ↑ Macros
(with-signal-cancel signals body ...)
Runs body in a scope that the first of the signals cancels.
signals- A Vec of the signals, as [Interrupt Terminate].
body ...- What to run. The form returns (Ok v) when the body returns v, and (Err s) when the signal s stopped the body.
(defbjo (main args)
(match (with-signal-cancel [Interrupt Terminate]
(serve config))
((Ok _) 0)
((Err Interrupt) 130)
((Err _) 143)))
Without it, Ctrl-C and SIGTERM end the program at once. With it, the
first signal cancels the scope with (Requested "SIGINT") and the
like: every sync in the scope that has to wait raises
Cancelled, the scope waits for its fibers, and it releases what
it owns. Then the form returns (Err s).
At the first signal the form gives the signals back to the system. So a second signal does the default action, and ends the program at once, also while it cleans up.
As with with-deadline, a body that returns has its value, also
when the signal stopped its fibers. A failure of the body or of a fiber
is raised, also after a signal. A cancellation from an outer scope is
raised too. A signal that this system does not have raises as in
signal-subscribe, before the body runs.
See also: signal-subscribe, with-cancel, with-deadline, Signal