Name

Class::Shared - Keep one instance of a class and let other processes call it

Synopsis

The usual way to use this distribution is through Class::Shared::Frontend. Each example below is a different real-world pattern.

# 1. The simplest case: share one class between many processes.
use Class::Shared::Frontend;

my $simple = Class::Shared::Frontend->new('Class::Simple');

$simple->foo('hello');          # runs in the shared backend
print $simple->foo(), "\n";     # prints "hello"

# 2. Give the class its constructor arguments.  They are used once, when
#    the object is made, and must be plain data (no code references).
my $logger = Class::Shared::Frontend->new('My::Logger',
    ctor => [ level => 'debug', file => '/var/log/app.log' ],
);

my @all = $logger->lines();     # list context is kept
my $n   = $logger->lines();     # scalar context is kept too

# 3. Use a backend on another machine, over TLS with client certificates.
my $remote = Class::Shared::Frontend->new('Class::Simple',
    host       => 'classhost.example.com',
    port       => 7000,
    tls_ca     => 'ca.pem',
    tls_cert   => 'client.pem',
    tls_key    => 'client-key.pem',
    token_file => '/etc/class-shared/token',
);

# 4. Look after the backend yourself, from the shell.
#      class-shared start        # start it in the background
#      class-shared status       # ask what it is doing
#      class-shared stop         # ask it to stop

# 5. Use the helper functions to write your own client.
use Class::Shared ();

my $dir = Class::Shared::default_dir();
my ($ok, $why) = Class::Shared::ensure_dir($dir);
die "cannot use $dir: $why" unless $ok;

my $socket = Class::Shared::socket_path($dir);

Description

Perl has no equivalent of a shared library for classes: every process loads its own copy of Class/Simple.pm, runs its own constructors and keeps its own state. For a large class used by many short lived processes that is a lot of memory and a lot of start up time.

Class::Shared moves the code into one process and lets the others keep a small proxy: a Class::Shared::Frontend standing in for one object, held by a Class::Shared::Backend that loaded the class once and does the work.

client process            singleton backend process
--------------            -------------------------
Frontend (object #1)  --socket-->  Class::Shared::Backend
Frontend (object #2)  --socket-->  |-- Class::Simple (code, loaded once)
Frontend (object #3)  --socket-->  |-- object #1
                                   |-- object #2
                                   `-- object #3

So there is one copy of the class in the system and as many objects as there are frontends: Class::Shared::Frontend->new('Class::Simple', ...) is one call to Class::Simple->new(...), in the backend.

The arguments for that constructor go in ctor, as an array reference: ctor => [verbose => 1] is Class::Simple->new(verbose => 1). They are used once, when the frontend connects and asks the backend for the object, and they travel over the socket, so they must be plain data -- a code reference cannot be sent. If the constructor dies, the frontend's new dies too, naming the class and the error the backend reported.

Class::Shared::Frontend lists every option this constructor takes.

The frontend never loads the target class, so it stays cheap. An object lives as long as the connection that made it; when the frontend goes, the backend destroys the object, so its DESTROY runs once, in the backend.

One backend serves every class its clients ask for. A client names the class it wants in its first message, the handshake, and the backend loads that module then, once per class, however many clients ask for it. The backend may be on this machine (a unix socket, see Class::Shared::default_dir) or on another one (TCP).

Class::Shared is for Unix-like systems: unix sockets, POSIX directory permissions and fork are what make a backend reachable, private and startable. See "PLATFORMS".

This page documents the helper functions that the frontend, the backend and the command line program share. Start with Class::Shared::Frontend if you only want to use a shared class.

Words Used in This Documentation

Functions

None is exported. They are used by the frontend and the backend, and may be useful when writing your own client. Call each one with its full name, for example Class::Shared::validate_class($name).

A function either returns its answer or, when it cannot, dies with a message that begins Class::Shared: and ends with a new line. The one exception is ensure_dir, which returns a pair so that the reason can be shown to the user.

The schemas in each API SPECIFICATION follow the conventions of Params::Validate::Strict (for input) and Return::Set (for output). They describe the function for people and for tools. The code does not call those two modules, and this distribution does not depend on them.

Validate_Class

my $class = Class::Shared::validate_class($class);

Checks that $class looks like a Perl package name, and returns it unchanged. Use it before you pass a name to require or build a file name from it.

A valid name is one or more words joined by ::. Each word starts with an English letter or an underscore and then has letters, digits or underscores. So Class::Simple and My_Class2 are valid, and 2Bad, Foo:: and Foo-Bar are not.

Side effects: none.

Dies with Class::Shared: no class name given if $class is undef or empty, and with Class::Shared: 'NAME' is not a valid class name otherwise. The domains and boundaries these two messages divide are listed under API SPECIFICATION below, and t/domain.t tests every one of them.

Api Specification

Input
{
    class => {
        type     => 'string',
        position => 0,
        optional => 0,
        min      => 1,
        matches  => qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/,
    },
}
Domains
valid    one or more ASCII words, each starting with a letter or "_" and
         continuing with letters, digits or "_", joined by "::"
invalid  undef, the empty string, a leading digit in any word, an empty
         word, any other separator, any non-ASCII character

partition                representative           result
-------------------------------------------------------------
one word                 Foo                      accepted
several words            Foo::Bar::Baz            accepted
digits and underscores   My_Class2, A::_2         accepted
length 1                 A, _                     accepted
empty string             ""                       dies: no class name
leading digit            2Foo                     dies
empty word               Foo::, ::Foo, A:::B      dies
hyphen, space or dot     Foo-Bar, Foo Bar         dies
non-ASCII letter         "M" + U+00FC + "ller"    dies
emoji, RTL override      U+1F600, U+202E          dies

Boundary values: length 0 dies, length 1 is the smallest accepted.  There
is no maximum: a 40,000 character name is accepted, because the check does
not repeat a group (perl 5.26 stops a repeated group at 32,766 repeats).
Only the pattern in this specification can match, so the answer is
untainted and safe to use as a module name.
Output
{ type => 'string', matches => qr/\A[A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*\z/ }

Read_Secret

my $secret = Class::Shared::read_secret('/etc/class-shared/token');

Reads a shared secret (a token) from a file and returns it. The whole file is read as bytes. White space at the end, including the final new line, is removed. White space at the start, and any inside the secret, is kept.

Never put a secret on a command line, where other users can see it. Give its file name instead and let this function read it.

Side effects: opens, reads and closes the file.

Dies if no file name is given, if the file cannot be read, if it is larger than 65536 bytes (which also refuses an endless file such as /dev/zero), or if the secret is empty after the end of it has been trimmed.

Api Specification

Input
{
    path => { type => 'string', position => 0, optional => 0, min => 1 },
}
Domains
valid    a path to a readable file whose content, after trailing white
         space is removed, is 1 to 65536 bytes
invalid  undef, the empty path, an unreadable path, a directory, a file
         larger than 65536 bytes, and a file that is empty after trimming

partition            representative        result
-----------------------------------------------------------
1 byte               "a"                   accepted
trailing white space "secret \r\n"         trimmed
leading white space  "  secret"            kept
white space only     "\n\t "               dies: is empty
0 bytes              ""                    dies: is empty
65536 bytes          "a" x 65536           accepted
65537 bytes          "a" x 65537           dies: too large

Boundary values: 0 bytes dies, 1 byte is the smallest accepted, 65536
bytes is the largest accepted, 65537 dies.  The size is counted in bytes,
so the limit is the same for a UTF-8 or a binary secret.  The content is
returned as bytes, unchanged and not decoded, so an embedded NUL and a
multibyte character cost exactly what they occupy in the file.
Output
{ type => 'string', min => 1 }

Normalise_Fingerprint

my $pin = Class::Shared::normalise_fingerprint(
    'AB:CD:...:EF'    # 32 pairs of hex digits, as openssl prints them
);

Turns the SHA-256 fingerprint of a certificate into the one form the TLS layer wants: sha256$ followed by 64 lower case hexadecimal digits.

It accepts 64 hex digits, or 32 pairs of hex digits separated by colons, with or without a leading sha256$. Upper or lower case digits are fine. The prefix itself must be lower case. To get a fingerprint from a certificate file, run openssl x509 -in server.pem -noout -fingerprint -sha256 and copy the part after the equals sign.

Side effects: none.

Dies if no value is given, or if the value is not a SHA-256 fingerprint.

Api Specification

Input
{
    value => {
        type     => 'string',
        position => 0,
        optional => 0,
        matches  => qr/\A(?:sha256\$)?(?:(?:[0-9A-Fa-f]{2}:){31}[0-9A-Fa-f]{2}|[0-9A-Fa-f]{64})\z/,
    },
}
Domains
valid    exactly 64 hexadecimal digits, or 32 pairs of two hexadecimal
         digits separated by ":", with or without a lower case "sha256$"
         prefix
invalid  any other length, any other separator, an upper case prefix, and
         any character that is not a hexadecimal digit

partition             representative         result
----------------------------------------------------------
64 digits             0102...1f20             accepted
32 colon-separated    01:02:...:1f:20          accepted
sha256$ prefix        sha256$0102...           accepted
upper case digits     0102...1F20             accepted, lower cased
upper case prefix     SHA256$0102...           dies
63 or 65 digits       one short, one long      dies
31 or 33 pairs        one short, one long      dies
spaces or hyphens     01 02 03..., 01-02-03    dies
non-ASCII character   U+00FC, U+1F600, U+202E  dies

Boundary values: the plain form is valid at exactly 64 characters (63 and
65 die); the colon form is valid at exactly 95 (94 and 96 die).  The
prefix is removed and every digit is lower cased; all invalid values are
echoed in the message byte for byte, without being decoded.
Output
{ type => 'string', matches => qr/\Asha256\$[0-9a-f]{64}\z/ }

Client_Tls_Options

my %tls = Class::Shared::client_tls_options(
    cert => 'client.pem',
    key  => 'client-key.pem',
    ca   => 'ca.pem',
);
my $socket = IO::Socket::SSL->new(PeerAddr => $host, PeerPort => $port, %tls);

Returns the options a TLS client needs, as a flat list of key and value pairs that you can pass straight to IO::Socket::SSL->new. It exists so that the frontend and the command line program cannot disagree about what "verify the backend" means.

Give cert and key (the certificate this client shows), and then either ca or fingerprint. With ca, the backend's certificate must be signed by that CA and must name the host you connect to. With fingerprint (the output of "normalise_fingerprint"), exactly one certificate is trusted, and its name and its signer do not matter. Verification is always on.

This function does not check that the files exist, and does not check that you gave only one of ca and fingerprint. The caller does that.

Side effects: loads IO::Socket::SSL. Nothing else loads it.

Dies if IO::Socket::SSL is not installed.

Api Specification

Input
{
    cert        => { type => 'string', optional => 0 },
    key         => { type => 'string', optional => 0 },
    ca          => { type => 'string', optional => 1 },
    fingerprint => { type => 'string', optional => 1,
                     matches => qr/\Asha256\$[0-9a-f]{64}\z/ },
}
Domains
valid    cert and key, then either ca or fingerprint (or neither, which
         the caller is expected to have refused already)
invalid  nothing is refused here: this function checks neither that the
         files exist nor that ca and fingerprint are not both given

partition            representative                  result
---------------------------------------------------------------
ca given             ca => "ca.pem"                  SSL_ca_file
fingerprint given    sha256$ + 64 lower hex digits   pin, no CA
both given           ca and fingerprint              the pin wins
neither given        cert and key only               SSL_ca_file undef
multibyte path       /etc/U+00FC/client.pem          passed through

Output boundaries: the flat list has five pairs with a CA and seven with a
pin, so ten and fourteen elements.  SSL_verify_mode is "verify the peer"
in every partition, and SSL_version is the same for both ends.
Output
{
    type   => 'arrayref',
    min    => 10,
    max    => 14,
    description => 'a flat list of option name and value pairs: SSL_version, SSL_cert_file, SSL_key_file, SSL_verify_mode, and either SSL_ca_file or (SSL_fingerprint, SSL_force_fingerprint, SSL_verifycn_scheme)',
}

Dir_of

my $dir = Class::Shared::dir_of('/run/user/1000/class-shared/x.sock');
# '/run/user/1000/class-shared'

Returns the directory part of a path, so that the answer can always be used. A path with no directory part, such as x.sock, gives . (the current directory). A file directly in the top directory, such as /x.sock, gives /.

Side effects: none.

Api Specification

Input
{ path => { type => 'string', position => 0, optional => 0 } }
Domains
partition            representative    result
-------------------------------------------------
bare file name       x.sock            .
file in the root     /x.sock           /
nested file          /a/b/c            /a/b
trailing slash       a/b/              a
doubled separator    a//b              a
empty path           ""                .

The answer is never empty, so it can always be used: a path with no
directory part gives ".".  There is no length boundary.  A multibyte
directory part is returned unchanged, with no encoding or decoding.
Output
{ type => 'string', min => 1 }

Default_Dir

my $dir = Class::Shared::default_dir();
my $dir = Class::Shared::default_dir(dir => '/tmp/mine');

Returns the directory that holds this user's sockets. The first of these that applies wins:

  1. the dir you pass;
  2. the environment variable CLASS_SHARED_DIR;
  3. $ENV{XDG_RUNTIME_DIR}/class-shared, if XDG_RUNTIME_DIR names a directory that exists;
  4. /run/user/UID/class-shared, if /run/user/UID exists (UID is the effective user id);
  5. a per-user directory under the system temporary directory.

The path is kept short on purpose, because it ends up inside a socket address, which has a small size limit. It is per user because the socket is a trust boundary.

Side effects: reads the environment and checks whether directories exist. It does not create anything; use "ensure_dir" for that.

Dies if the directory name contains a NUL byte, which would end the name early in a system call and so name a different place. Under taint mode (-T) the name is untainted here, because everything made in it is checked by "ensure_dir" before it is trusted.

Api Specification

Input
{ dir => { type => 'string', optional => 1, min => 1 } }
Domains
The first partition that applies wins, so they are ordered:

rank  partition                             result
-----------------------------------------------------------------
1     dir given and not empty               the dir, unchanged
2     CLASS_SHARED_DIR set and not empty    that value
3     XDG_RUNTIME_DIR names a directory     that directory/class-shared
4     /run/user/UID exists                  that directory/class-shared
5     none of the above                     tmpdir/class-shared-UID

invalid: a name containing a NUL byte dies at any rank, because a system
call would end the name there and so name a different place.  The empty
string is not invalid: it means "not given" and falls through to the next
rank.  A multibyte name is returned unchanged, not encoded.  Nothing is
created, so a rank may name a directory that does not exist yet.
Output
{ type => 'string', min => 1 }

Ensure_Dir

my ($ok, $why) = Class::Shared::ensure_dir($dir);
die "cannot use $dir: $why" unless $ok;

Makes sure $dir exists and that nobody else can put a socket in it. It returns (1, undef) on success or (0, $why_not) when the directory is not safe. It is refused if

If the directory is missing it is created with mode 0700. Only the last part of the path is created: the parent directory must already exist.

Side effects: may create one directory.

Dies (and does not return a pair) if no directory is given, or on a perl without unix sockets.

Api Specification

Input
{ dir => { type => 'string', position => 0, optional => 0, min => 1 } }
Domains
valid    a path whose last part exists, or can be created, as a directory
         owned by this user with neither the group write bit (0020) nor
         the other write bit (0002) set
invalid  undef, the empty path, a path whose parent is missing, a path
         that is not a directory, a directory owned by another user, and
         a directory with either write bit set

partition              representative    result
-----------------------------------------------------
does not exist         tmp/fresh         (1, undef), mode 0700
exists, mode 0700      tmp/private       (1, undef)
exists, mode 0755      tmp/open          (1, undef)
exists, mode 0777      tmp/writable      (0, reason)
exists, mode 0770      group writable    (0, reason)
exists, mode 0702      other writable    (0, reason)
parent missing         tmp/absent/child  (0, "cannot create ...")
a plain file           tmp/file          (0, "cannot create ...")
empty or undef         ""                dies

Boundary values: the boundary is the write mask 0022, not a single mode:
0700 is the most private mode created and 0777 the most open refused, but
0755 is accepted because others may read and search, not write.  Only the
last part of the path is created, so a missing parent is a refusal, not a
recursive mkdir.  A multibyte name is created as given.
Output
{
    type   => 'arrayref',
    min    => 2,
    max    => 2,
    schema => [
        { type => 'boolean', description => '1 when the directory is safe to use' },
        { type => 'string',  optional => 1, description => 'undef on success, otherwise the reason' },
    ],
}

Socket_Path

my $path = Class::Shared::socket_path();
my $path = Class::Shared::socket_path('/tmp/mine');

Returns the path of the backend socket inside $dir, or inside the default directory (see "default_dir") if none is given. The file name is always class-shared.sock. There is one socket for every class, because the client chooses the class and not the listener.

Side effects: none. The directory and the socket are not created.

Dies on a perl without unix sockets, or if the result is longer than 100 characters (see "check_socket_path").

Api Specification

Input
{ dir => { type => 'string', position => 0, optional => 1, min => 1 } }
Domains
valid    no argument, or a directory name, such that the socket path is
         1 to 100 characters
invalid  a directory name that makes the path longer than 100 characters,
         which dies with the message documented under L</check_socket_path>

partition                     representative      result
------------------------------------------------------------------
default directory             ()                  .../class-shared.sock
a directory                   /tmp                /tmp/class-shared.sock
path exactly 100 characters   dir of 82 chars     accepted
path 101 characters           dir of 83 chars     dies

The file name is always "class-shared.sock", so the boundary is reached
through the directory: with a 17 character file name the largest usable
directory is 82 characters.  Nothing is created and nothing is checked for
existence.
Output
{ type => 'string', min => 1, max => 100 }

Check_Socket_Path

my $path = Class::Shared::check_socket_path($path);

Returns $path if it is short enough to be a socket address, and dies if it is not, or if no path is given at all. The limit is 100 characters, which is less than the operating system's limit so that the code stays portable. Without this check the system would cut a long path short without saying so, and a client and a backend could end up using two different sockets.

The frontend, the backend and class-shared all use it for a socket that you give yourself, as well as for the path that socket_path builds.

Side effects: none.

Dies with Class::Shared: no socket path given if $path is undef or empty, and with Class::Shared: socket path 'PATH' is N characters, more than the 100 allowed; set CLASS_SHARED_DIR or pass dir => to a shorter directory if it is too long.

Api Specification

Input
{ path => { type => 'string', position => 0, optional => 0, min => 1 } }
Domains
valid    a non-empty path of 1 to 100 characters
invalid  undef, the empty path, and any path longer than 100 characters

partition          representative   result
-------------------------------------------------
length 1           /                accepted
length 99          ...              accepted
length 100         ...              accepted, the maximum
length 101         ...              dies
length 0 or undef  ""               dies

Boundary values: 100 is accepted and 101 dies; 0 and undef die.  The limit
is 100 characters and is below the operating system's.  A multibyte path is
counted in characters and returned unchanged.  This function does not check
that a socket can use its characters: see L</ENCODING>.
Output
{ type => 'string', min => 1, max => 100 }

Poll_Pause

Class::Shared::poll_pause();

Sleeps for one polling interval ($Class::Shared::POLL_INTERVAL, 0.02 seconds). Every loop that waits for a socket or a lock uses it, so that none of them spins the processor and all of them wake at the same rate.

Side effects: sleeps. It returns nothing.

Api Specification

Input
{ }
Domains
Takes no input, so it has one partition.  The output is the empty list
(undef in scalar context).  The only observable effect is a sleep of
C<$Class::Shared::POLL_INTERVAL> seconds, 0.02, which is the same for
every loop that waits for a socket or a lock.
Output
{ type => 'undef' }

Detach_Standard_Streams

my ($ok, $why) = Class::Shared::detach_standard_streams('/path/to/log');

Used when a backend goes into the background. It reopens standard input on the null device (/dev/null) and standard output and standard error on the log file, so that the process holds no terminal and its messages land in a file. If the log file cannot be opened, they go to the null device instead, because a backend that cannot write a log should still run.

It returns (1, undef), or (0, $why) if a stream could not be reopened. The caller decides whether that matters: the backend treats it as fatal.

Side effects: replaces STDIN, STDOUT and STDERR of the current process. Do not call it in a program that still needs them.

Api Specification

Input
{ log => { type => 'string', position => 0, optional => 0, min => 1 } }
Domains
valid    any log path: one that can be opened for appending, and one that
         cannot (the streams then go to the null device and the call still
         succeeds, because a backend that cannot log should still run)
invalid  none by value: an unopenable path is a documented fallback, not
         an error

partition            representative        result
---------------------------------------------------------
writable log         /tmp/backend.log      (1, undef), file created
log in a missing dir /absent/backend.log    (1, undef), null device

The refusal path, C<(0, $why)>, exists for a failure to reopen a stream
after the log was opened, such as a descriptor limit; it cannot be
provoked by the value of the path alone.  The call replaces STDIN, STDOUT
and STDERR of the current process, so a test makes it in a child.
Output
{
    type   => 'arrayref',
    min    => 2,
    max    => 2,
    schema => [
        { type => 'boolean' },
        { type => 'string', optional => 1 },
    ],
}

Package Variables

These can be changed with local in a test or a special program. Most users never need to.

Encoding

Class::Shared moves text between processes, so it matters which parts accept more than plain English letters.

Common Pitfalls

Transport Security

Authentication belongs to the transport, and the two transports are deliberately different.

A unix socket is created mode 0600 in a directory only its owner can write to, so the kernel has already decided who may connect: there is no token and no TLS, and no frame carries a digest. The backend, the frontend and class-shared refuse both for a unix socket rather than let either look like a defence.

TCP is the opposite case. A port is reachable by anybody who can route to it, and treating a frame means running what the sender put in it, so a TCP listener is a TLS listener: it needs a certificate and its key to prove which backend it is, and a CA to check client certificates against, and it will not start without all three. Clients must present a certificate that CA signed, and must verify the backend's certificate against the CA that signed it -- name included -- or against a pinned SHA-256 fingerprint of that one certificate, which stands in for both the CA and the name. A client whose certificate the backend cannot verify is dropped during the handshake and never receives a frame.

TLS answers who is on the other end and makes the bytes unreadable. The token is kept on top of it, as a second and independent check: with a token every frame in both directions carries an HMAC-SHA-256 of its body, and the digest is verified before Storable::thaw sees the body. So a client that has a certificate but not the shared secret still cannot make the backend deserialise, and so run, bytes of its choosing. The digest authenticates one frame; TLS authenticates the connection and encrypts it. Both, because either one alone has been enough to lose a system.

See "SECURITY" in Class::Shared::Backend for the backend's side of it and Class::Shared::Protocol for the framing.

Making the Certificates

A TCP listener needs four files: a CA, the listener's certificate and key, and a certificate for every client. OpenSSL makes them in three steps. The CA's key signs certificates and is not needed on the machines that use them.

# 1. A CA of your own.
openssl req -x509 -newkey rsa:2048 -nodes -days 3650 \
    -keyout ca-key.pem -out ca.pem -subj '/CN=Class::Shared CA' \
    -addext 'basicConstraints=critical,CA:TRUE' \
    -addext 'keyUsage=critical,keyCertSign,cRLSign'

The listener's certificate has to name the address clients connect to, in subjectAltName: a DNS: entry for a hostname, an IP: entry for an address, or both.

# 2. The listener's certificate.  Put your own addresses in server.ext.
printf 'subjectAltName = DNS:classhost.example.com, IP:127.0.0.1\n' > server.ext
printf 'extendedKeyUsage = serverAuth\n' >> server.ext
openssl req -new -newkey rsa:2048 -nodes -subj '/CN=classhost.example.com' \
    -keyout server-key.pem -out server.csr
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca-key.pem \
    -CAcreateserial -days 3650 -out server.pem -extfile server.ext

Each client needs a certificate of its own. The backend cannot tell one from another beyond this, so give each client, or each pair of them, their own.

# 3. A client certificate.
printf 'extendedKeyUsage = clientAuth\n' > client.ext
openssl req -new -newkey rsa:2048 -nodes -subj '/CN=class-shared client' \
    -keyout client-key.pem -out client.csr
openssl x509 -req -in client.csr -CA ca.pem -CAkey ca-key.pem \
    -CAcreateserial -days 3650 -out client.pem -extfile client.ext

The listener is given three of the files:

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

and every client the other three:

my $simple = Class::Shared::Frontend->new('Class::Simple',
    host       => 'classhost.example.com',
    port       => 7000,
    tls_ca     => 'ca.pem',
    tls_cert   => 'client.pem',
    tls_key    => 'client-key.pem',
    token_file => '/etc/class-shared/token',
);

A frontend that pins the listener's certificate instead of trusting the CA skips tls_ca and passes tls_fingerprint the digest that openssl x509 -in server.pem -noout -fingerprint -sha256 prints. That works with a self-signed certificate too, and then the CA is only needed for the clients. t/certs/make-certs.sh in the distribution generates this set, plus a second CA and impostor certificates, for the tests.

Limitations

Platforms

Unix-like systems only. A backend needs three things this distribution cannot do without:

A perl without unix domain sockets can run none of it, so that is the question the check asks, rather than a list of operating systems: a Cygwin perl has the sockets and works. Windows has had the sockets since Windows 10, but an MSWin32 perl still cannot run a backend: its stat reports the same mode bits for every directory, so none there can be shown to be owner-only and Class::Shared::ensure_dir refuses them all.

The refusal names what is missing: Class::Shared::ensure_dir and Class::Shared::socket_path die with that explanation on a perl without the sockets, and Class::Shared::ensure_dir refuses the directories it cannot show to be owner-only.

It installs there anyway, and Makefile.PL says so as it runs. Refusing to install would take away Class::Shared::Protocol, which is pure Perl and can talk to a backend from a machine that cannot run one, and would leave such a platform with nothing to report but a failed install.

The wire protocol, Class::Shared::Protocol, is pure Perl and loads on any platform, so it can be used to talk to a backend from a machine that cannot run one.

See Also

Class::Shared::Frontend, Class::Shared::Backend, Class::Shared::Protocol, class-shared.

Hacking

README.md is generated from the POD in this file and must never be edited by hand. After changing the POD, run make readme, which calls maint/regen-readme; it converts the POD with Pod::Github's pod2github, the converter whose ## headings, fenced Perl blocks and MetaCPAN links are the style the committed front page uses. pod2markdown does not reproduce it.

Pod::Github is a development requirement, so cpanm --installdeps \--with-develop . installs everything needed to regenerate the front page.

Author

Nigel Horne, <njh at nigelhorne.com>

Formal Specification

This section describes what each function 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 module. Read the plain text above first.

Z uses mathematical symbols, so this is the only part of the file that is not plain ASCII. A short key:

x? input        x! output        x' the value of x after the operation
=>  implies     /\  and          \/  or          ~  not
|->  maps to    <->  relation    +->  partial function (each input at most one answer)
seq  a sequence (an ordered list)    # the length of a sequence

In the Z text below the symbols are the usual Unicode ones.

Basic Types

[CHAR]                                  all characters
STRING   == seq CHAR
STRING⊥  == STRING ∪ {⊥}                a string, or undef
PATH     == STRING
MAXPATH  == 100
RESULT[X] ::= value⟨⟨X⟩⟩ | dies⟨⟨STRING⟩⟩    a function returns X, or dies with a message
IDENT    == the strings that start with a letter or _ and continue with letters, digits or _
CLASSNAME == { c : STRING | ∃ parts : seq₁ IDENT • c = join(⟨"::"⟩, parts) }
HEX64    == the strings of exactly 64 hexadecimal digits
FP       == { f : STRING | f ∈ HEX64 ∨ f is 32 pairs of hex digits separated by ":" }

Validate_Class

┌─ ValidateClass ─────────────────────────────────────────
│ class? : STRING⊥
│ out!   : RESULT[STRING]
├─────────────────────────────────────────────────────────
│ class? ∈ CLASSNAME ⇒ out! = value(class?)
│ class? ∉ CLASSNAME ⇒ ∃ m : STRING • out! = dies(m)
└─────────────────────────────────────────────────────────

Read_Secret

┌─ ReadSecret ────────────────────────────────────────────
│ path?  : STRING⊥
│ files  : PATH ⇸ STRING                  the file system, as bytes
│ out!   : RESULT[STRING]
├─────────────────────────────────────────────────────────
│ path? ∈ dom files ∧ trim(files(path?)) ≠ ⟨⟩
│     ⇒ out! = value(trim(files(path?)))
│ path? ∉ dom files ∨ trim(files(path?)) = ⟨⟩ ∨ path? = ⊥
│     ⇒ ∃ m : STRING • out! = dies(m)
│ files' = files                          nothing is changed
└─────────────────────────────────────────────────────────

where trim removes white space from the end only.

Normalise_Fingerprint

┌─ NormaliseFingerprint ──────────────────────────────────
│ value? : STRING⊥
│ out!   : RESULT[STRING]
├─────────────────────────────────────────────────────────
│ value? = ⟨"sha256$"⟩ ⌢ f ∨ value? = f, for some f ∈ FP
│     ⇒ out! = value(⟨"sha256$"⟩ ⌢ lower(strip_colons(f)))
│ otherwise ⇒ ∃ m : STRING • out! = dies(m)
└─────────────────────────────────────────────────────────

Client_Tls_Options

┌─ ClientTlsOptions ──────────────────────────────────────
│ cert?, key? : STRING
│ ca?, fingerprint? : STRING⊥
│ out! : RESULT[seq (STRING × VALUE)]
├─────────────────────────────────────────────────────────
│ fingerprint? ≠ ⊥ ⇒ out! = value(base ⌢ pin)
│ fingerprint? = ⊥ ⇒ out! = value(base ⌢ ⟨("SSL_ca_file", ca?)⟩)
│ SSL_verify_mode ∈ base is always "verify the peer"
└─────────────────────────────────────────────────────────

Dir_of

┌─ DirOf ─────────────────────────────────────────────────
│ path? : STRING
│ dir!  : STRING
├─────────────────────────────────────────────────────────
│ dir! = dirname(path?)       ("." when path? has no directory part)
│ dir! ≠ ⟨⟩
└─────────────────────────────────────────────────────────

Default_Dir

┌─ DefaultDir ────────────────────────────────────────────
│ dir? : STRING⊥ ; env : STRING ⇸ STRING ; isdir : ℙ PATH ; euid : ℕ
│ dir! : STRING
├─────────────────────────────────────────────────────────
│ dir? ≠ ⊥ ∧ dir? ≠ ⟨⟩ ⇒ dir! = dir?
│ else "CLASS_SHARED_DIR" ∈ dom env ∧ env(..) ≠ ⟨⟩ ⇒ dir! = env("CLASS_SHARED_DIR")
│ else env("XDG_RUNTIME_DIR") ∈ isdir ⇒ dir! = env("XDG_RUNTIME_DIR") / "class-shared"
│ else "/run/user/" ⌢ euid ∈ isdir ⇒ dir! = that / "class-shared"
│ else dir! = tmpdir / ("class-shared-" ⌢ euid)
│ isdir' = isdir                          nothing is created
└─────────────────────────────────────────────────────────

Ensure_Dir

┌─ EnsureDir ─────────────────────────────────────────────
│ dir?  : STRING⊥
│ fs    : PATH ⇸ (owner : ℕ × mode : ℕ)   the directories that exist
│ euid  : ℕ
│ out!  : RESULT[𝔹 × STRING⊥]
├─────────────────────────────────────────────────────────
│ dir? = ⊥ ∨ dir? = ⟨⟩ ∨ ¬ UnixSockets ⇒ ∃ m • out! = dies(m)
│ dir? ∉ dom fs ∧ parent(dir?) ∈ dom fs ⇒ fs' = fs ⊕ {dir? ↦ (euid, 8#700)}
│ dir? ∈ dom fs' ∧ owner(fs'(dir?)) = euid ∧ mode(fs'(dir?)) ∧ 8#022 = 0
│     ⇒ out! = value(true, ⊥)
│ otherwise (and no dying) ⇒ ∃ why : STRING • out! = value(false, why)
└─────────────────────────────────────────────────────────

Socket_Path

┌─ SocketPath ────────────────────────────────────────────
│ dir? : STRING⊥
│ out! : RESULT[PATH]
├─────────────────────────────────────────────────────────
│ ¬ UnixSockets ⇒ ∃ m • out! = dies(m)
│ let p = default_dir(dir?) / "class-shared.sock" in
│     UnixSockets ∧ #p ≤ MAXPATH ⇒ out! = value(p)
│     UnixSockets ∧ #p > MAXPATH ⇒ ∃ m • out! = dies(m)
└─────────────────────────────────────────────────────────

Check_Socket_Path

┌─ CheckSocketPath ───────────────────────────────────────
│ path? : STRING⊥
│ out!  : RESULT[PATH]
├─────────────────────────────────────────────────────────
│ path? ≠ ⊥ ∧ 0 < #path? ≤ MAXPATH ⇒ out! = value(path?)
│ path? = ⊥ ∨ #path? = 0           ⇒ ∃ m • out! = dies(m)
│ path? ≠ ⊥ ∧ #path? > MAXPATH     ⇒ ∃ m • out! = dies(m)
└─────────────────────────────────────────────────────────

Poll_Pause

┌─ PollPause ─────────────────────────────────────────────
│ clock, clock' : ℝ
├─────────────────────────────────────────────────────────
│ clock' ≥ clock + POLL_INTERVAL          it returns nothing
└─────────────────────────────────────────────────────────

Detach_Standard_Streams

┌─ DetachStandardStreams ─────────────────────────────────
│ log? : PATH ; stdin, stdout, stderr : STREAM ; writable : ℙ PATH
│ out! : 𝔹 × STRING⊥
├─────────────────────────────────────────────────────────
│ stdin' = null
│ log? ∈ writable ⇒ stdout' = stderr' = file(log?) ∧ out! = (true, ⊥)
│ log? ∉ writable ⇒ stdout' = stderr' = null      ∧ out! = (true, ⊥)
│ any reopen fails ⇒ ∃ why • out! = (false, why)
└─────────────────────────────────────────────────────────

The Unix Socket Probe (Internal)

┌─ UnixSocketProbe ───────────────────────────────────────
│ cache, cache' : {unknown, yes, no}
├─────────────────────────────────────────────────────────
│ cache = unknown ⇒ cache' ∈ {yes, no}    asked once, by naming a socket and
│                                         trying socketpair; both must work
│ cache ≠ unknown ⇒ cache' = cache        the answer never changes in a run
└─────────────────────────────────────────────────────────

State Diagram

The functions in this file keep almost no state. The one piece they do keep is the answer to "can this perl make a unix socket?", which is worked out once, the first time it is needed, and then never changes.

              both tries work
+---------+ -------------------------> +---------+ <---+
| UNKNOWN |                            | UNIX_OK |     | every later call
+---------+ ---+                       +---------+ ----+ (no change)
               |  try fails
               |                       +---------+ <---+
               +---------------------> | NO_UNIX |     | every later call
                                       +---------+ ----+ (calls die)

A directory, as seen by "ensure_dir", moves through these states. The function does not remember them; they describe what it finds and what it does.

+---------+   mkdir 0700 works    +---------+
| MISSING | --------------------> | PRESENT |   (it existed, or was just made)
+---------+                       +---------+
     |                              |      |
     | mkdir fails                  |      | I own it, and group and
     | (parent missing, no          |      | other cannot write to it
     |  permission, ...)            |      v
     |                              |   +------+
     |                              |   | SAFE |   returns (1, undef)
     |                              |   +------+
     |                              | someone else owns it, or
     |                              | group or other can write to it
     v                              v
  +------------------------------------+
  | REFUSED   returns (0, $why)        |
  +------------------------------------+

Copyright 2026 Nigel Horne.

Class::Shared 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.

Class::Shared 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.

If you use it, please let me know.