NAME
class-shared - start, inspect and stop a Class::Shared backend
SYNOPSIS
class-shared start [-f] [--dir DIR | --socket PATH]
class-shared start --listen tcp:HOST:PORT \
--token-file FILE --tls-cert CERT --tls-key KEY \
--tls-ca CA
class-shared status [--dir DIR | --socket PATH]
class-shared stop [--dir DIR | --socket PATH]
class-shared path [--dir DIR | --socket PATH]
class-shared help
Real-world patterns:
# 1. Start a backend for this user, then look at it, then stop it.
class-shared start
class-shared status
class-shared stop
# 2. Run it in the foreground under a supervisor (systemd, runit, ...).
class-shared start -f --dir /run/myapp
# 3. Serve other machines over TLS, with a token.
class-shared start --listen tcp:0.0.0.0:7000 \
--token-file /etc/class-shared/token \
--tls-cert server.pem --tls-key server-key.pem \
--tls-ca clients-ca.pem
# 4. Ask that remote backend how it is, checking it against a CA.
class-shared status --host classhost.example.com --port 7000 \
--token-file /etc/class-shared/token \
--tls-cert client.pem --tls-key client-key.pem \
--tls-ca ca.pem
# 5. Ask it again, trusting exactly one certificate by its fingerprint.
class-shared status --host 192.0.2.10 --port 7000 \
--token-file /etc/class-shared/token \
--tls-cert client.pem --tls-key client-key.pem \
--tls-fingerprint AB:CD:...:EF
# 6. Find out where the socket would be.
class-shared path
DESCRIPTION
class-shared is the command line face of Class::Shared: it starts the singleton Class::Shared::Backend, asks a running backend what it is doing, and asks it to stop.
The backend it starts has no class of its own: a client names the class it wants in the handshake and the backend loads that module then, so one backend serves whatever its clients ask for.
A Class::Shared::Frontend starts a backend of its own over a unix socket and never needs this program. Reach for it when the backend should be started by something else, such as an init system, or when it listens on TCP, which no frontend will start for you.
COMMANDS
- start
-
Starts the backend and does not return until it stops. Unless
--foregroundis given it forks into the background first, so the command returns once the backend is running. Whatever the backend has to say goes to its log file,--logor the socket path with.logappended, in the foreground as well.The transport is a unix socket unless
--listen,--hostor--portasks for TCP. A TCP listener is a TLS listener with client certificates: it needs--tls-cert,--tls-key,--tls-caand--token-file, and refuses to start without any of them. A unix socket takes none of them, because its mode 0600 file already decides who may connect. - status
-
Pings the backend and prints what it reports: the pid, version, protocol, number of objects, number of calls, uptime, the classes it has loaded, the address it is listening on, the number of clients, the bytes queued against the ceiling they are bounded by, and the log path. Prints nothing and exits non-zero if no backend answers.
- stop
-
Asks the backend to shut down, which destroys its objects and runs their
DESTROYmethods in the backend. Over a unix socket it waits for the socket to disappear afterwards; over TCP there is nothing to wait for. Printsstopped. - path
-
Prints the unix socket path that a backend here would listen on, after
--dirand--socket. Refuses the TCP options, which have no socket path. - help
-
Prints a short usage message and exits successfully.
--helpdoes the same.
OPTIONS
- --dir DIR
-
The directory holding the socket, lock and log. Defaults to what "default_dir" in Class::Shared picks:
$CLASS_SHARED_DIR, then$XDG_RUNTIME_DIR/class-shared, then /run/user/$uid/class-shared, then a per-user directory under the system temporary directory. - --socket PATH
-
An explicit unix socket path, overriding
--dir. Not with the TCP options. - --listen ADDR
-
tcp:HOST:PORTto accept clients over TCP, orunix:PATHto name the unix socket. An alternative to--hostand--port, not something to combine with them. - --host HOST, --port PORT
-
The TCP address to listen on, or, for
statusandstop, the address of the backend to talk to. Give both or neither;127.0.0.1keeps it on this machine. - --token-file FILE
-
The shared secret for a TCP listener, read from
FILE. Every frame in both directions then carries the secret's HMAC, and the backend checks that digest before it thaws anything, so a peer without the secret cannot have the backend run code of its choosing. It is a second check, on top of TLS.A TCP listener requires it, and
statusandstopmust be given the same file or they cannot authenticate to a backend that has one; see "TOKENS". A unix socket takes none -- its mode 0600 file already decides who may connect -- and this command refuses the option for one. - --tls-cert FILE, --tls-key FILE
-
The certificate this side presents and its private key: the backend's for
start, this client's forstatusandstop. Both are required for TCP. The backend's certificate must name the address clients use, because they check the name as well as the chain. See "TLS". - --tls-ca FILE
-
PEM file of the CA the other side's certificate must chain to. For
startthat is the CA that signs clients' certificates, and a client signed by any other CA is dropped during the handshake; forstatusandstopit is the CA that signed the backend's certificate. Required for a TCP listener, and for the client commands unless--tls-fingerprintpins the backend instead. - --tls-fingerprint HEX
-
The SHA-256 fingerprint of the backend's certificate, as
openssl x509 -in server.pem -noout -fingerprint -sha256prints it: colons are optional, as is asha256$prefix, and the case does not matter. It replaces--tls-cainstatusandstop, and accepts that exact certificate whatever its name or signer. A listener cannot use it: clients are verified with--tls-ca, not pinned. - --log PATH
-
Where the backend writes its log. Defaults to the socket path with
.logappended. - --max-memory MIB
-
For
start, on Linux and FreeBSD: the most memory, in MiB, the backend process may use. It guards the machine against a target class that allocates without end. When the backend reaches the limit Perl saysOut of memory!and exits, and its objects are lost with it. Without this option the backend has the limits of the shell that started it; so does a backend that a program starts for itself. - -f, --foreground
-
Stay in the foreground instead of forking into the background (
startonly). Useful under a supervisor that does not want a daemon. - -h, --help
-
Print the usage message and exit.
- -V, --version
-
Print the version and exit.
TOKENS
status and stop verify the backend -- against --tls-ca or the pinned --tls-fingerprint -- and present their own certificate before any of this, so over TCP the TLS handshake comes first and a failure there is reported as such: the command never sends a frame to a backend it could not verify, and a backend that rejects its certificate gives it nothing.
Once connected, they sign their requests when --token-file is given, the way a frontend does, and a backend with a token refuses any frame it cannot authenticate: the wrong secret is reported as a frame that failed authentication, and no --token-file at all leaves the request unsigned, so the backend refuses it and drops the connection.
That last case leaves nothing readable: the backend signs its refusal too, so all the caller holds is a Storable complaint about bytes that are not an image. That makes a poor first line for an error, so the message leads with
handshake failed: this backend may require a token; pass --token-file ...
and keeps the complaint on a detail: line, for when the cause was something else. Class::Shared::Frontend makes the same guess when it has no token.
TLS
TCP has no unencrypted mode. start needs a certificate and key of its own and the CA that signs its clients, and it verifies those clients mandatorily: a connection that presents nothing, or a certificate from another CA, is dropped during the handshake and logged, and never gets as far as a frame.
status and stop need the same three options from the client's side: the certificate and key the backend is configured to accept, and either the backend's CA or its pinned fingerprint. Verification is not optional; with a CA it includes the certificate's name, so connect with the address or hostname the backend's certificate is for, while a pin identifies the certificate itself and asks for nothing else.
EXIT STATUS
0 when the command succeeded, 1 when it failed (no backend, a refused request, a secret that does not authenticate, a socket path that is too long, standard output that cannot be written) and 2 for a usage error. A background start exits 0 before it knows whether the backend started; see "COMMON PITFALLS".
SEE ALSO
Class::Shared, Class::Shared::Frontend, Class::Shared::Backend, Class::Shared::Protocol
AUTHOR
Nigel Horne
ENCODING
- Everything you type is passed on as bytes.
-
The command line, the file names and the options are handed on exactly as the shell gives them. Nothing is decoded or re-encoded.
- The token file: ASCII only.
-
The secret is read from the file as bytes and trimmed at the end. Use only ASCII characters, because a program that holds the same secret as a Perl string may not get the same bytes for a non-ASCII character, and the two sides would then disagree. See "ENCODING" in Class::Shared.
- The fingerprint: hexadecimal digits.
-
Case does not matter; colons and a
sha256$prefix are optional. - Paths: ASCII is safest.
-
A socket path is limited to 100 characters, counted as characters and not bytes.
- What the program prints.
-
Its own messages are plain English (ASCII).
statusprints class names, which are ASCII, and the paths and addresses exactly as they were given. The backend's log may hold text in more than one encoding; see "ENCODING" in Class::Shared::Backend.
COMMON PITFALLS
startreturns before the backend is ready.-
In the background (the default) the command exits with status 0 as soon as the background copy has been started. The socket may not exist for a moment. A script that runs
class-shared startand then at onceclass-shared statuscan fail the first time. Trystatusagain, or use--foregroundunder a supervisor. - A failed
startcan still exit 0. -
The background copy reports its problems to the log, after the command has returned. If another backend is already serving, the new one waits about two seconds for the lock and then writes
fatal: another backend is already servingto the log and exits. The shell has already seen status 0. After starting a backend, runclass-shared statusand read the log if it says nothing. - Give the same location to every command.
-
status,stopandpathlook in the default directory unless you give the same--dir,--socketor--listenthatstartused. For TCP they also need the matching--tls-*options and--token-file. stopstops it for everybody.-
It destroys every client's objects, not just yours.
- A unix socket takes no token and no TLS.
-
--token-fileand the--tls-*options are refused without--listen tcp:...(or--hostand--port), on purpose. --tls-fingerprintis for clients.-
startrefuses it. A listener checks its clients with--tls-ca. - The backend finds classes in the
@INCit was started with. -
Start it with the
PERL5LIBorperl -Ithat your clients need. - Old log lines are thrown away.
-
At 10 MiB the log is copied to LOG.1 and emptied; only that one copy is kept.
FORMAL SPECIFICATION
This section describes what each command does in the Z notation, a way of writing exactly what a program promises using sets and logic. You do not need it to use the program.
Z uses mathematical symbols, so this is the only part of the file that is not plain ASCII.
State
┌─ Host ──────────────────────────────────────────────────
│ backends : LOCATION ⇸ BACKEND at most one backend per location
│ log : LOCATION → seq STRING what each location's log holds
└─────────────────────────────────────────────────────────
start
┌─ Start ─────────────────────────────────────────────────
│ ΔHost ; loc? : LOCATION ; fg? : 𝔹 ; status! : {0, 1, 2}
├─────────────────────────────────────────────────────────
│ options invalid ⇒ ΞHost ∧ status! = 1
│ fg? = false ⇒ status! = 0 whatever happens next
│ ∧ (loc? ¬in; dom backends ⇒ eventually loc? ∈ dom backends')
│ ∧ (loc? ∈ dom backends ⇒ log' loc? ends with "fatal: another backend is already serving"
│ after start_timeout, unless that backend stopped first)
│ fg? = true ∧ loc? ¬in; dom backends ⇒ loc? ∈ dom backends' until it stops, then status! = 0
│ fg? = true ∧ loc? ∈ dom backends (after start_timeout) ⇒ status! = 1
└─────────────────────────────────────────────────────────
status
┌─ Status ────────────────────────────────────────────────
│ ΞHost ; loc? : LOCATION ; text! : seq STRING ; status! : {0, 1}
├─────────────────────────────────────────────────────────
│ loc? ∈ dom backends ∧ handshake succeeds ⇒ status! = 0 ∧ text! = report(backends loc?)
│ otherwise ⇒ text! = ⟨⟩ ∧ status! = 1 ∧ a reason is printed on standard error
└─────────────────────────────────────────────────────────
stop
┌─ Stop ──────────────────────────────────────────────────
│ ΔHost ; loc? : LOCATION ; status! : {0, 1}
├─────────────────────────────────────────────────────────
│ loc? ∈ dom backends ∧ request succeeds ⇒ loc? ¬in; dom backends' ∧ status! = 0 ∧ prints "stopped"
│ loc? ¬in; dom backends ⇒ ΞHost ∧ status! = 1
│ unix ∧ the socket is still there after 5 seconds ⇒ status! = 1
└─────────────────────────────────────────────────────────
path
┌─ Path ──────────────────────────────────────────────────
│ ΞHost ; text! : STRING ; status! : {0, 1}
├─────────────────────────────────────────────────────────
│ TCP options given ⇒ status! = 1
│ path too long ⇒ status! = 1
│ otherwise ⇒ text! = socket path ∧ status! = 0
└─────────────────────────────────────────────────────────
STATE DIAGRAM
The program has no state of its own. The state that matters is the backend's, and each command moves it like this.
start stop
(exits 0 at once; the backend (objects destroyed, socket
starts a moment later) and pid file removed)
+------------+ ------------------> +---------+ ------------------> +------------+
| NO BACKEND | | RUNNING | | NO BACKEND |
+------------+ +---------+ +------------+
| ^
+-----+
status, path, or a second start: no change.
(The second start's copy waits about 2 seconds,
logs "another backend is already serving" and
exits 1. The shell has already seen 0.)
- NO BACKEND to RUNNING
-
Trigger:
start. Action: the command forks, the first process exits 0, and the background copy takes the lock, opens the socket and starts serving. - RUNNING to NO BACKEND
-
Trigger:
stop. Action: the backend replies, destroys every object, removes its socket and pid file and exits.stopwaits up to 5 seconds for the socket to disappear. - RUNNING to RUNNING
-
Trigger:
statusorpath(they change nothing), or a secondstart(which fails in the background and changes nothing). - NO BACKEND, a command that needs one
-
Trigger:
statusorstopwith nothing listening. Action: a message on standard error and exit status 1.
LICENSE
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for the full text.