| 1 | package Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 95 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 95 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 95 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 95 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 95 | ▶ | use Config::Abstraction 0.38; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 95 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 95 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 95 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 95 | ▶ | use Log::Abstraction 0.41; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 95 | ▶ | use mro; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 95 | ▶ | use Params::Get 0.13; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 95 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 95 | ▶ | use Return::Set; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 95 | ▶ | use List::Util qw(any first); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 95 | ▶ | use Scalar::Util qw(blessed reftype weaken); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 95 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 95 | ▶ | use File::stat; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 95 | ▶ | use POSIX qw(WNOHANG); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 95 | ▶ | use Storable (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Avoid magic literals scattered across hot paths and signal handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # Centralising here makes global search-replace safe and self-documents intent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 95 | Readonly my $OS_WINDOWS => 'MSWin32'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 95 | Readonly my $LOGGER_NULL => 'NULL'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 95 | Readonly my $SIG_DEFAULT => 'DEFAULT'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 95 | Readonly my $SIG_IGNORE => 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 95 | Readonly my $POLL_SLEEP => 0.1; # seconds between waitpid polls in disable_hot_reload | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 95 | Readonly my $KILL_TIMEOUT => 5; # seconds before SIGKILL escalation after SIGTERM | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 95 | Readonly my $DEFAULT_INTERVAL => 10; # default hot-reload poll interval in seconds | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # register_object() drops dead weak references from a class's registry list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # whenever the list reaches this size, or twice its size after the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # clean-up, whichever is larger: amortised O(1) per registration, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # list never holds more than twice the live objects (plus this floor). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 95 | Readonly my $REGISTRY_PRUNE_MIN => 64; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # Cache entries carry this format number; an entry with any other is ignored, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # so a persistent cache filled by an older release cannot be misread. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 95 | Readonly my $CACHE_FORMAT => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # Environment variables, besides the section's own prefix, that can change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # what Config::Abstraction reads for a config file: the environment name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # (selects base.ENV.* / local.ENV.*), the encryption key, and the variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # its default search directories and script-name lookups are built from. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 95 | Readonly my @CACHE_ENV_VARS => qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | PLACK_ENV NODE_ENV ENCRYPTION_KEY ENCRYPTION_KEY_FILE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | HOME DOCUMENT_ROOT CONFIG_DIR SCRIPT_NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # Largest config file, in bytes, that will be read. Real config files are a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | # few hundred bytes; 8K is generous, and 16K leaves headroom. A bigger file is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # almost certainly a mistake (a log or data file named by accident) or an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # attempt to make every load and every hot reload parse a huge document. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 95 | Readonly my $MAX_CONFIG_FILE_SIZE => 16 * 1024; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # Matches any path segment that is exactly ".." (anchored start, slash, or end). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # Catches: ../foo foo/../bar foo/.. and leading ../ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Used through _is_path_traversal() so every guard stays in sync. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 95 | Readonly my $RE_PATH_TRAVERSAL => qr{(?:\A|/)\.\.(?:/|\z)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Windows also accepts \ as a separator, and strips trailing dots and spaces | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # from every path component, so "...", ".. " and ". .." all open "..". Any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # component made only of dots and spaces that holds two or more dots is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # therefore a way up the tree there. (On Unix those are ordinary names.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 95 | Readonly my $RE_PATH_TRAVERSAL_WIN32 => qr{(?:\A|[/\\])(?=[. ]*\.[. ]*\.)[. ]+(?:[/\\]|\z)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # Extensions tried, in order, when looking for an ancestor's config file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # The primary file's own extension is always tried first (see _config_candidates). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 95 | Readonly my @CONFIG_EXTENSIONS => qw(.conf .yml .yaml .json); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | # File::stat fields compared by the watcher. mtime alone misses a file that is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # replaced by an older copy (cp -p, rsync -t, a VCS checkout) and an edit made | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | # within the same second; size and inode catch most of those cases. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 95 | Readonly my @WATCHED_STAT_FIELDS => qw(mtime size ino); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # Global registry - intentionally package-level so that the END block and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # signal handlers installed in one call site share state with all others. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # This is a deliberate singleton design; see LIMITATIONS for the trade-offs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 95 | our %_object_registry = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 95 | our %_config_watchers = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 95 | our %_config_file_stats = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 95 | our %_registry_prune_at = (); # class => list size that triggers the next clean-up | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | # Memoization caches - keyed by inputs; valid for the process lifetime because | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | # Perl @ISA hierarchies and filesystem layouts are stable after module load. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | # Both caches include undef sentinels (use exists, not defined, to check hits). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 95 | our %_chain_cache = (); # class => [UNIVERSAL, ..., Class] (base-first) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 95 | our %_find_cache = (); # "\0"-joined key => file path or undef | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Saved before we install our SIGUSR1 handler so we can chain and restore it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 95 | our $_original_usr1_handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # While our SIGUSR1 handler is installed, this holds an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # Object::Configure::_Usr1Chain guard. Releasing it (at END, or by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # restore_signal_handlers) runs its DESTROY, which puts the previous handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # back if ours is still the live one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 95 | our $_usr1_guard; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # Config::Abstraction objects kept between hot reloads, one per loaded file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | # and section ("FILE\0SECTION" => object). Filled and pruned only by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | # reload_config(), so it holds parsers for files that registered objects use, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # and nothing for programs that never reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 95 | our %_config_sources = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | # Config::Abstraction gained reload() in 0.44. With an older one, each | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | # reload builds a new parser instead. A package variable (not Readonly) so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # tests can exercise the fallback on a newer Config::Abstraction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 95 | ▶ | our $_config_abstraction_can_reload = Config::Abstraction->can('reload') ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | Object::Configure - Runtime Configuration for an Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0.27 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 95 | our $VERSION = '0.27'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | C<Object::Configure> lets the users of your Perl classes change how an object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | behaves by editing a settings file or setting an environment variable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | without changing any code. It also gives every object a logger. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | It is a thin layer on top of two other modules: L<Config::Abstraction>, which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | reads settings files and environment variables, and L<Log::Abstraction>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | which writes log messages. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | You call C<configure($class, \%params)> at the start of your class's C<new()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | method. It: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =item 1. Finds the settings file for your class, and for every class your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | class inherits from (its parent classes, through C<@ISA>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =item 2. Merges those files, then applies any environment variables named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | C<ClassName__key>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =item 3. Creates a L<Log::Abstraction> logger and puts it in the C<logger> key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | =item 4. Returns a hash reference. You pass it to C<bless> to make your object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | The module can also do I<hot reload>: a background process watches the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | settings files, and when one changes, every registered object is updated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | while the program keeps running. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | B<Hot reload is not supported on Windows> (Windows has no C<SIGUSR1> signal). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | =head2 Words used in this document | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | =item * B<class> -- a Perl package that makes objects, such as C<My::Module>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =item * B<config file> (configuration file, settings file) -- a text file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | usually YAML, that holds settings. For example C<timeout: 30>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | =item * B<section> -- the part of a config file that belongs to one class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | Its name is the class name with C<::> replaced by C<__> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | (C<My::Module> becomes C<My__Module>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =item * B<merge> -- combine two sets of settings into one. When both have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | the same key, the one with higher priority wins. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | L</Configuration resolution order>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =item * B<ancestor> -- a parent class, or a parent of a parent, and so on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | C<UNIVERSAL> is the ancestor of every Perl class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | =item * B<environment variable> -- a named value set outside the program, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | for example with C<export NAME=value> in a Unix shell. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | =item * B<logger> -- an object with methods such as C<info()>, C<warn()> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | C<error()> that records messages. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | =item * B<signal> -- a short message the operating system delivers to a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | running program. C<SIGUSR1> is a signal with no fixed meaning; programs may | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | use it for anything. This module uses it to mean "reload your settings". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =item * B<signal handler> -- the code Perl runs when a signal arrives. It is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | stored in C<$SIG{USR1}>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | =item * B<watcher> -- a small background process, started by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | C<enable_hot_reload()>, that checks the config files and sends C<SIGUSR1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | when one changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =item * B<registry> -- the list of objects that hot reload updates. An | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | object is added with C<register_object()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | =item * B<weak reference> -- a reference that does not keep an object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | alive. The registry uses them, so registering an object never stops it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | from being freed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | =item * B<cache> -- an object that remembers results so that they do not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | have to be worked out again. See L</Caching parsed config files>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | =head2 Before you start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | =item * Perl 5.26 or later. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | =item * A parser for each config file format you use. For YAML (C<.yml>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | C<.yaml>) install L<YAML::XS>. Without the right parser, a file is skipped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | with a warning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =item * For hot reload: a Unix-like system (Linux, macOS, the BSDs). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | =item * For the optional cache: a cache module such as L<CHI>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | All functions are called with their full name, for example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | C<Object::Configure::configure(...)>. The module exports nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | =head2 Example 1: Add configurable logging to your own class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | package My::Module; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # configure() reads config files + env vars, sets up a logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | # and returns a hashref ready to bless. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | my $params = Object::Configure::configure($class, \%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | return bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | sub do_work { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | $self->{logger}->info('Starting do_work'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | # Usage -- reads /etc/myapp/my-module.yml (and any ancestor files beside it): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my $obj = My::Module->new(config_file => '/etc/myapp/my-module.yml'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | $obj->do_work; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | =head2 Example 2: Configure a third-party class you cannot modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # Wrap LWP::UserAgent so it reads its settings from a YAML file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | my $ua = Object::Configure::instantiate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | class => 'LWP::UserAgent', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | config_file => '/etc/myapp/lwp.yml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | timeout => 30, # fallback if the file has no timeout key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | $ua->get('https://example.com'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =head2 Example 3: Multi-level inheritance -- config files merge automatically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # ~/.conf/my-base-class.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | # My__Base__Class: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | # timeout: 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # retries: 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # ~/.conf/my-child-class.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # My__Child__Class: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # timeout: 60 # overrides base; retries:3 is inherited | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | package My::Child::Class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | our @ISA = ('My::Base::Class'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | # Walks @ISA, merges base config then child config. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # Result: timeout=60, retries=3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | my $params = Object::Configure::configure($class, \%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | return bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | =head2 Example 4: Hot reload -- objects update when the config file changes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | package My::Service; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | my $params = Object::Configure::configure($class, \%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | my $self = bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Register so reload_config() updates $self in-place on file change. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | Object::Configure::register_object($class, $self) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | if $params->{_config_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | return $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | package main; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | my $svc = My::Service->new(config_file => '/etc/myapp/service.yml'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Fork a background watcher; it sends SIGUSR1 when the file changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | Object::Configure::enable_hot_reload( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | interval => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | callback => sub { print "Config reloaded at ", scalar localtime, "\n" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | while (1) { sleep 1 } # event loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | Object::Configure::disable_hot_reload(); # clean shutdown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | =head2 Example 5: Override settings with environment variables (no file needed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | # Shell: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # export My__Worker__log_level=debug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | # export My__Worker__timeout=120 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | package My::Worker; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # configure() picks up My__Worker__* env vars automatically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | my $params = Object::Configure::configure($class, \%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | return bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | my $obj = My::Worker->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | # $obj->{log_level} eq 'debug' $obj->{timeout} == 120 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | =head2 Example 6: Make constructors faster with a cache | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | use CHI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | # One cache for the whole program. Each config file is then parsed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | # once, not once per object, until it (or the environment) changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | our $cache = CHI->new(driver => 'Memory', global => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | package My::Request; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | my $params = Object::Configure::configure($class, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | config_file => '/etc/myapp/my-request.yml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | return bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | =head2 Example 7: Capture log messages in a test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | use Test::More; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | my @log; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | my $obj = My::Module->new(logger => \@log); # messages go into @log | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | $obj->do_work; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | ok(scalar(@log), 'do_work logged something'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | # Or turn logging off completely: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | my $quiet = My::Module->new(logger => 'NULL'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | =head2 Example 8: Keep your own SIGUSR1 handler working | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # Install your handler first ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | $SIG{USR1} = sub { print "USR1 received\n" }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | # ... then register objects (My::Service from Example 4 calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # register_object()). Object::Configure installs its own handler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | # which reloads the objects and then calls yours. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | my $svc = My::Service->new(config_file => '/etc/myapp/service.yml'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # When the program ends, your handler is put back in $SIG{USR1}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | =head2 Config file naming | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | The config file name is derived from the class name by lowercasing it and replacing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | C<::> with hyphens (C<->): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | My::Parent::Class => my-parent-class.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | The file is searched for in the directory of the C<config_file> argument, then in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | any directories listed in C<config_dirs>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | =head2 Config file size limit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | Config files must be small. Any config file larger than 16K (16384 bytes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | is refused before it is parsed: C<configure()> croaks, and a hot reload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | warns and keeps the object's current settings. This applies to every file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | that would be read, including ancestor class files and C<universal.yml>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | =head2 Caching parsed config files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | Every C<configure()> call normally parses every config file again: with a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | two-level hierarchy that is most of the time a constructor takes. Pass a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | cache object to skip the parse when nothing has changed: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | use CHI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | my $cache = CHI->new(driver => 'Memory', global => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | my $params = Object::Configure::configure($class, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | config_file => 'my-app.yml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | Any object with C<get($key)> and C<set($key, $value)> will do (L<CHI>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | L<Cache::Cache>, L<Cache::Memcached>, or your own). What is cached is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | settings read from each file for each class, as plain data; the key covers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | everything that can change them: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | =item * the file itself (device, inode, size and modification time, to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | sub-second precision where the filesystem keeps it); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =item * every other file Config::Abstraction read for it (C<base.*>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | C<local.*> and the like), which is checked again on every hit | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | (Config::Abstraction 0.44 or later; with an older one only the file itself); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | =item * the class's C<CLASS__*> environment variables (matched in any case, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | as Config::Abstraction reads them), the environment name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | (C<CLASS__ENV>, C<PLACK_ENV>, C<NODE_ENV>), the encryption key settings, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | C<HOME>, C<DOCUMENT_ROOT>, C<CONFIG_DIR> and C<SCRIPT_NAME>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | =item * C<--CLASS__key=value> arguments in C<@ARGV>, the current directory, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | C<$0>, and the versions of Object::Configure and Config::Abstraction. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | So an edited file, or a changed environment variable, is simply a cache miss. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | Each entry is used through a private copy, so caches that return references | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | (such as CHI's C<RawMemory> driver) cannot be corrupted through an object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | The cache is only consulted for config files: the environment-only path, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | hot reload (which must re-read), do not use it. If the cache dies, a warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | is given and the file is parsed normally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | Things the key cannot see, and which therefore need the cache's own expiry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | (for example C<CHI-E<gt>new(..., expires_in =E<gt> '10 min')>) if they matter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | to you: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | =item * a config file that did not exist when the entry was cached and has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | appeared since (such as a new C<local.yaml>); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | =item * a new encryption key given in the same key file with the same size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | and timestamp. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | B<Security:> the cached settings are the decrypted values. If your config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | files hold encrypted secrets, use a cache only your program can read (an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | in-memory one), not a shared or file-based one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | =head2 Section key naming inside the config file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | Inside the YAML (or JSON or conf) file, the section key uses double underscores in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | place of C<::>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | # my-parent-class.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | My__Parent__Class: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | timeout: 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | retries: 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | =head2 Configuration resolution order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | The following sources are merged from I<lowest> to I<highest> priority. A value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | from a higher-priority source always wins over a lower-priority one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | =item 1. B<Caller-supplied params> -- the hashref you pass to C<configure()>. Despite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | the name "defaults", these are the I<lowest> priority and are overridden by everything | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | else. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | =item 2. B<UNIVERSAL section> -- the C<UNIVERSAL:> block in C<universal.yml> (or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | C<universal.conf>, C<universal.json>) in your config directories, if the file exists. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | =item 3. B<Ancestor class config files> -- walked base-first through C<@ISA> using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | the class's Method Resolution Order (MRO). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =item 4. B<Primary config file> -- the file named by the C<config_file> argument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | =item 5. B<Environment variables> -- named C<ClassName__key=value>, where C<::> in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | the class name is replaced by C<__>. These are the I<highest> priority. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | =head2 UNIVERSAL configuration | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | If you create C<universal.yml> in your config directory with a C<UNIVERSAL:> section, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | those settings apply to every class that uses C<Object::Configure> unless a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | more-specific source overrides them: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | # ~/.conf/universal.yml | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | UNIVERSAL: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | timeout: 30 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | logger: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | level: warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | =head2 Logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | C<configure()> always sets C<$params-E<gt>{logger}> to a L<Log::Abstraction> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | instance. You can control it by passing a C<logger> key: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | # Arrayref: messages are captured into @log (protected from merge override) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | configure($class, { logger => \@log }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | # Hashref: options forwarded to Log::Abstraction::new() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | configure($class, { logger => { level => 'debug', file => '/var/log/app.log' } }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # String 'NULL': disables all logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | configure($class, { logger => 'NULL' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | # Existing Log::Abstraction object: used as-is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | configure($class, { logger => $my_logger }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | B<Note:> a concrete logger -- C<'NULL'>, an arrayref, a logger object or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | coderef -- is set aside before the config merge and always wins. A hashref is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | treated as options and merges like any other config, so a C<logger:> section | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | in a config file overrides individual keys. See L</COMMON PITFALLS>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | =head2 Environment variable format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | Environment variable names are constructed as: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | ClassName__key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | where C<::> in the class name is replaced by two underscores. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | export My__Module__timeout=60 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | export My__Module__logger__level=debug | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | =head1 HOT RELOAD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | Hot reload lets you edit a config file and have all live objects update themselves | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | without restarting the program. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | =head2 How it works | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | =item 1. Your constructor calls C<register_object($class, $self)> to opt in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | =item 2. Your main program calls C<enable_hot_reload()> to fork a background watcher. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | =item 3. The watcher polls config files every C<interval> seconds and sends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | C<SIGUSR1> to the parent when it detects a change. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | =item 4. The C<SIGUSR1> handler calls C<reload_config()>, which re-reads files and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | updates every registered object in-place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | =item 5. Your main program calls C<disable_hot_reload()> on shutdown. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | Steps 1 and 2 may happen in either order: C<enable_hot_reload()> installs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | the C<SIGUSR1> handler itself if no object has been registered yet. (Before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | version 0.27, starting the watcher first meant the first config edit ended | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | the program, because nothing handled the signal.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | =head2 Sharing SIGUSR1 with other code (daisy chaining) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | Object::Configure does not take C<SIGUSR1> away from code that already uses it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | When the first object is registered, whatever C<$SIG{USR1}> holds is saved and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | Object::Configure's handler is installed in its place. On every signal, that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | handler: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | =item 1. reloads the registered objects, then runs the C<enable_hot_reload()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | callback, if any (a failure in either is reported with C<carp>, never thrown); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | =item 2. calls the saved handler with the same arguments Perl passed (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | signal name), before returning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | The saved handler may be a coderef or a sub I<name>, as C<%SIG> allows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | (C<'My::Pkg::on_usr1'>, or C<'on_usr1'>, which means C<main::on_usr1>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | C<DEFAULT> and C<IGNORE> need no call. Anything the saved handler throws | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | propagates as it would have without Object::Configure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | When the program ends, the previous handler is put back (in the C<DESTROY> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | of a small guard object that lives exactly as long as the installation). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | This is only done if Object::Configure's handler is still the one in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | C<$SIG{USR1}>: if other code installed its own handler afterwards, that one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | is left in place, and if it chains to ours the signal still reaches the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | original handler. Call C<restore_signal_handlers()> to put the saved handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | back immediately and unconditionally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | Private keys (those whose names start with C<_>) are never overwritten during | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | reload, so internal bookkeeping is safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | B<Hot reload is not supported on Windows.> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | =head1 COMMON PITFALLS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | =head2 Nested settings are merged; lists are replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | When two sources set the same key, what happens depends on the value: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | =item * B<Two hashes> (nested settings) are merged key by key, at every level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | =item * B<Anything else> -- a string, a number, a list (array) or C<undef> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | replaces the lower-priority value completely. Lists are never joined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | # Passed by the caller: # In the config file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | # nested => { b => 2 } # nested: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | # list => [9, 9, 9] # a: 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # # list: [1, 2] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | # Result: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | # nested => { a => 1, b => 2 } (merged) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | # list => [1, 2] (replaced, not [9, 9, 9, 1, 2]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | =head2 undef is a value, not "no value" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | An empty value in a config file (C<key: ~> or C<key:> in YAML, C<null> in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | JSON) sets the key to C<undef>, and C<undef> replaces whatever a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | lower-priority source gave, including the caller's value. To keep the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | lower-priority value, leave the key out of the file completely. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | Passing C<undef> as the whole second argument is fine: it is treated like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | C<{}>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | =head2 The section name must match the class exactly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | Inside a config file, settings must be under a section named after the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | class, with C<__> in place of C<::> (C<My__Module:>, not C<My::Module:>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | With any other name there is B<no warning>: the class's settings are simply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | not applied, and the whole section may appear on the object as a single | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | key with the section's name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | =head2 Do not set $SIG{USR1} to 'DEFAULT' while hot reload is running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | While the watcher runs, it sends C<SIGUSR1> whenever a config file changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | Object::Configure's handler deals with it, but if other code later sets | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | C<$SIG{USR1} = 'DEFAULT'>, the next signal B<ends the program>, because that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | is Perl's default action for C<SIGUSR1>. To turn hot reload off, call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | C<restore_signal_handlers()> or C<disable_hot_reload()> instead: they stop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | the watcher. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | =head2 A hashref logger is merged; any other logger always wins | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | A concrete logger choice cannot be merged with config, so it is kept exactly as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | given, whatever C<universal.yml> or a site-local file (such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | C<~/.conf/local.yaml>) says: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # Kept as given -- never overridden by config files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | configure($class, { logger => 'NULL' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | configure($class, { logger => \@captured }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | configure($class, { logger => $existing_obj }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | A hashref is options for L<Log::Abstraction> and follows the normal precedence | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | rules: a C<logger:> section in a config file overrides the keys you pass. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # level may be replaced by a logger: section in a config file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | configure($class, { logger => { level => 'debug' } }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | Before 0.27 only arrayrefs were protected, so a site file could silently replace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | an explicit C<'NULL'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | =head2 Caller-supplied keys are the LOWEST priority | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | Despite being called "defaults", keys you pass directly to C<configure()> are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | overridden by config files and environment variables: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | # My__Module__timeout=60 is set in the shell. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | # $params->{timeout} will be 60, not 30. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | configure('My::Module', { timeout => 30 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | Use caller-supplied keys only as a last-resort fallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | =head2 register_object() pushes; it does not replace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | Calling C<register_object()> twice for the same class registers B<two> entries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | Both objects receive updates on every reload. The second call does not remove the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | Object::Configure::register_object('My::Class', $obj_a); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | Object::Configure::register_object('My::Class', $obj_b); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | # reload_config() now updates BOTH $obj_a and $obj_b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | =head2 Private keys are never updated on hot reload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | Any key whose name begins with C<_> is skipped during C<reload_config()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | A config key named C<_my_setting> in your YAML file will be ignored at reload time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | =head2 The 'class' key appears on objects created by instantiate() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | C<instantiate()> intentionally leaves the C<class> key in the hashref passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | C<$class-E<gt>new()> as a debugging aid. Your object will have a C<class> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | attribute set to the class name: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | my $obj = Object::Configure::instantiate(class => 'My::Thing', ...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | print $obj->{class}; # prints 'My::Thing' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | =head2 Memoization caches are not invalidated during a run | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | C<_get_inheritance_chain()> and C<_find_class_config_file()> cache their results | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | for the lifetime of the process. If you alter C<@ISA> or add config files after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | the first C<configure()> call for a given class, the cache returns stale results. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | =head2 disable_hot_reload() blocks for up to five seconds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | It waits for the watcher process to exit (SIGTERM first, then SIGKILL). Do not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | call it from inside a signal handler or a timing-sensitive loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | =head2 config_file must be developer-controlled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | The C<config_file> path is validated against directory traversal (C<../>) but is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | not otherwise restricted. On Windows, C<\> also counts as a separator, and any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | path component made only of dots and spaces with two or more dots (C<...>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | C<.. >) is refused too, because Windows strips trailing dots and spaces and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | would open C<..>. It must always be a developer-supplied path, never raw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | user input. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | =head2 The cache cannot see brand-new files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | With the C<cache> argument, a cached result is checked against every file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | that was read to make it. A file that did not exist then, and has been | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | created since (such as a new C<local.yaml>), is not noticed until the entry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | expires. Give the cache an expiry time if this matters. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | L</Caching parsed config files>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | =head1 ENCODING | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | Which text can contain non-ASCII characters (accented letters, other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | scripts, emoji), and in what form you get it back: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | =item * B<Values in YAML and JSON config files> -- full UTF-8 is supported, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | including emoji. Save the file as UTF-8. The values you get back are Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | I<character> strings: C<length()> counts characters, not bytes. Other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | formats depend on the parser L<Config::Abstraction> uses for them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | =item * B<Values in environment variables> -- any bytes are accepted, but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | they are B<not decoded>. You get the raw bytes, so C<"caf\x{e9}"> set as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | UTF-8 arrives as 5 bytes, not 4 characters. Decode them yourself if you | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | need characters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | use Encode qw(decode); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | my $name = decode('UTF-8', $obj->{name}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | =item * B<Class names> -- ASCII only: letters, digits and C<_>, separated by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | C<::>, and each part must start with a letter or C<_>. Anything else is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | refused with an error. Environment variable names are built from the class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | name, so they are ASCII too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | =item * B<Config file paths> (C<config_file>, C<config_dirs>) -- passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | the operating system unchanged, as bytes. On Unix, non-ASCII names work if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | you pass them encoded the same way the file system stores them (usually | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | UTF-8). On Windows, use ASCII paths. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | =item * B<Keys> (setting names) -- use ASCII. Environment variables can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | only set ASCII keys, and hot reload compares keys exactly. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | =item * B<Log messages> -- passed to L<Log::Abstraction> unchanged; see its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | documentation for how it writes them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | No public routine changes the caller's C<$@>, C<$!>, C<$?> or C<$_>, or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | disturbs a pending C<alarm()>. (C<$?> matters at program end, when it is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | the exit status: stopping the watcher must not change it.) This matters because C<reload_config()> runs from a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | C<SIGUSR1> handler, at whatever point the program happened to be. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | =head2 configure($class, \%params) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | Merge configuration for C<$class> from all available sources and return a hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | ready to pass to C<bless>. This is the core function; call it at the start of your | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | C<new()> method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | =item * C<$class> (Required, string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | The fully-qualified Perl class name to configure (e.g., C<'My::Module'>). Must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | start with a letter or underscore; each C<::>-separated component must also start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | with a letter or underscore. Digits, newlines, and shell metacharacters are rejected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | =item * C<$params> (Optional, hash reference; C<undef> means C<{}>) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | Your own values for the object's settings, plus the special keys listed in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | L</Keys of $params>. Your values have the I<lowest> priority: config files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | and environment variables override them. The hash you pass is copied, never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | changed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | =head3 Keys of $params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | Any key may be used. These keys have a special meaning: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | =item * C<config_file> (string, optional) -- path to the primary YAML/JSON/conf file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | =item * C<config_dirs> (arrayref of strings, optional) -- additional directories to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | search when C<config_file> is a bare filename with no directory component. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | directory that cannot be searched is skipped. An empty list counts as none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | =item * C<logger> (various, optional) -- see L</Logging>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | =item * C<carp_on_warn> (boolean, optional, default 0) -- if true, the logger uses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | C<Carp::carp> instead of C<warn>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | =item * C<croak_on_error> (boolean, optional) -- passed to L<Log::Abstraction> when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | set, so that C<$logger-E<gt>error()> croaks. When absent, Log::Abstraction's own | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | default applies (C<error()> does not croak). Ignored when you pass a ready-made | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | logger object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | =item * C<cache> (object, optional) -- a cache for parsed config files, such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | a L<CHI> object. It must provide C<get($key)> and C<set($key, $value)>. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | L</Caching parsed config files>. Like any object argument it is also left in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | the returned hash, so it reaches the object's constructor too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | Code references and objects you pass (other than C<logger>) are never read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | as settings: they are returned exactly as given. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | A hashref containing all merged configuration keys plus: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | =item * C<logger> -- a L<Log::Abstraction> instance, or the string C<'NULL'>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | =item * C<_config_file> -- path of the primary config file that was actually read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | (the full path when it was found through C<config_dirs>). Absent if no file was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | loaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | =item * C<_config_files> -- arrayref of every config file that was loaded (only if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | at least one was loaded), base class first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | =item * C<_config_sections> -- arrayref running parallel to C<_config_files>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | the section each file was read with (C<UNIVERSAL>, C<My__Base>, ...). Hot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | reload uses it to re-merge every file exactly as C<configure()> did. Set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | together with C<_config_files>, and only by C<configure()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | =head3 Error messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | =item * C<Object::Configure: configure: what class do you want to configure?> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | C<$class> was C<undef> or an empty string. Pass C<ref($self)> or C<__PACKAGE__>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | =item * C<Object::Configure: configure: invalid class name (must be a valid Perl package name): CLASS> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | C<$class> contains characters not allowed in a Perl package name (for example, a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | digit as the first character of a C<::>-component, a newline, or a semicolon). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | =item * C<CLASS: config_file contains path traversal sequences: FILE> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | C<config_file> contains a C<..> segment (e.g., C<../../etc/passwd>), or on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | Windows any segment Windows would treat as C<..> (C<...>, C<.. >, and C<\> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | separators). The C<config_file> argument must always be a developer-controlled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | =item * C<CLASS: FILE: OS-ERROR> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | C<config_file> is not readable and no C<config_dirs> (or an empty list) were supplied. OS-ERROR is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | the locale's text for C<ENOENT> when the file does not exist, and for C<EACCES> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | when it exists but cannot be read. Check the path and permissions, or add | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | C<config_dirs>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | =item * C<CLASS: FILE: config file is too large (SIZE bytes; the limit is 16384 bytes)> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | a config file that would be read (the primary file, an ancestor's file or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | C<universal.yml>) is larger than 16K. See L</Config file size limit>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | =item * C<Warning: Can't load configuration from FILE: DETAIL> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | L<Config::Abstraction> rejected the file (typically a YAML/JSON syntax error). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | This is a warning, not fatal; C<configure()> continues with an empty config. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | =item * C<Warning: Can't load configuration from FILE: no settings could be read (is a parser for this format installed?)> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | the file was found but nothing could read it, typically because the optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | parser L<Config::Abstraction> needs for its format is not installed (for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | example L<YAML::XS> for C<.yml>, or L<Config::IniFiles> for an INI-style | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | C<.conf>). Also a warning; that file's settings are skipped and the caller's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | values are kept. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | =item * C<Object::Configure: config_path contains path traversal sequences: PATH> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | An environment variable set a C<config_path> value containing C<..>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | =item * C<Object::Configure: configure: params must be a hash reference> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | the second argument was not a hashref. Call C<configure($class, \%args)>, not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | C<configure($class, %args)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | =item * C<Object::Configure: configure: config_dirs must be an array reference> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | C<config_dirs> was a string or other non-arrayref. Wrap a single directory in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | brackets: C<config_dirs =E<gt> ['/etc/myapp']>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | =item * C<Object::Configure: configure: cache must be an object with get() and set() methods> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | C<cache> was not a blessed object, or lacks C<get> or C<set>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | =item * C<Object::Configure: cache get failed: ERROR> / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | C<Object::Configure: cache set failed: ERROR> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | warnings: the cache itself died. The file is parsed as if there were no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | cache, so a cache outage slows C<configure()> down but never breaks it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | class => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | optional => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | matches => qr/\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | description => 'Fully-qualified Perl class name', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | params => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | description => 'Caller-supplied values (lowest priority); undef means {}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | =head4 Keys of params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | config_file => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | description => 'Primary config file path', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | config_dirs => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | description => 'Extra directories to search for config files', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | logger => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | type => [qw(undef string arrayref hashref object)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | description => 'Logger spec -- see CONFIGURATION/Logging', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | carp_on_warn => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | type => 'boolean', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | description => 'Use Carp::carp for logger warnings', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | croak_on_error => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | type => 'boolean', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | description => 'Use Carp::croak for logger errors', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | cache => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | can => [ qw(get set) ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | description => 'Cache for parsed config files (e.g. CHI)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | description => 'Merged configuration hashref, ready to bless', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | schema => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | logger => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | type => [qw(object string)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | description => 'Log::Abstraction instance or the string "NULL"', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | _config_file => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | description => 'Path of the primary config file that was loaded', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | _config_files => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | description => 'All config file paths that were loaded, in load order', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | _config_sections => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | type => 'arrayref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | description => 'Section each file in _config_files was read with', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | 869 | ▶ | sub configure { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
configure: called CC=61 SCAR=41.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 869 | my $class = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | 869 | ▶ | my $params = $_[1] // {}; # caller's defaults; config file values override them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[1] // {}
MC/DC100%1/1
$_[1] // {}
$_[1]
Truth table100%2/2
A
A= $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | 869 | my $caller_logger; # stash for a concrete caller logger (see below) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | 869 | ▶ | croak(__PACKAGE__, ': configure: what class do you want to configure?') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $class or $class eq ""
MC/DC100%2/2
not defined $class or $class eq ""
not defined $class
$class eq ""
Truth table100%3/3
not(A) or B
A= not defined $classB= $class eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | if !defined($class) || $class eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | # SECURITY: validate $class is a syntactically valid Perl package name before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | # it propagates into env_prefix, croak messages, and %_chain_cache keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | # Exploit mechanism: a class name containing \n, ;, or shell metacharacters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | # can poison log lines, carp output, and env-variable lookups if not rejected here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | # Under Perl taint mode (-T) an unvalidated external value would also fail the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | # taint check inside Config::Abstraction when used as an env_prefix substring. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | 851 | ▶ | croak(__PACKAGE__, ': configure: invalid class name (must be a valid Perl package name): ', $class) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | unless $class =~ /\A[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | # A common slip is configure($class, %args); fail with a clear message rather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | # than the opaque "Can't use string as a HASH ref" from the loop below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | 782 | ▶ | croak(__PACKAGE__, ': configure: params must be a hash reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | unless ref($params) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | # A plain string here used to die deep inside the search with Perl's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | # "Can't use string as an ARRAY ref". Checked before the coderef stash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | # below, which would otherwise hide a coderef config_dirs from this test. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | croak(__PACKAGE__, ': configure: config_dirs must be an array reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | 769 | ▶ | if defined($params->{'config_dirs'}) && ref($params->{'config_dirs'}) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params->{"config_dirs"} and ref $params->{"config_dirs"} ne "ARRAY"
MC/DC100%2/2
defined $params->{"config_dirs"} and ref $params->{"config_dirs"} ne "ARRAY"
defined $params->{"config_dirs"}
ref $params->{"config_dirs"} ne "ARRAY"
Truth table100%3/3
A and B
A= defined $params->{"config_dirs"}B= ref $params->{"config_dirs"} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | # The cache is used only through get() and set(), as CHI, Cache::Cache and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | # Cache::Memcached all provide; anything else would only fail later, deep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | # inside the file loop. It is stashed below like any other object, so it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | # is also passed on to the object being configured. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | 756 | my $cache = $params->{'cache'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 756 | ▶ | croak(__PACKAGE__, ': configure: cache must be an object with get() and set() methods') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
defined $cache and not builtin::blessed($cache) && $cache->can("get") && $cache->can("set")
builtin::blessed($cache) && $cache->can('get') && $cache->can('set')
builtin::blessed($cache) and $cache->can("get")
MC/DC75%3/4
defined $cache and not builtin::blessed($cache) && $cache->can("get") && $cache->can("set")
defined $cache
builtin::blessed($cache)
$cache->can("get")
$cache->can('set')
Truth table80%4/5
A and not(B and C && D)
A= defined $cacheB= builtin::blessed($cache)C= $cache->can("get")D= $cache->can('set')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | if defined($cache) && !(blessed($cache) && $cache->can('get') && $cache->can('set')); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | # Work on a shallow copy: the stashing below deletes keys, and on the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | # config-file path those keys were never put back into the caller's hash, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | # so a caller re-using its args hash silently lost coderefs and loggers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 753 | $params = { %{$params} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | # Config::Abstraction, Log::Abstraction, and Return::Set all use eval internally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | # Protect the caller's $@ from being clobbered by our internal eval blocks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | # The -r/-f probes below set $! on every miss; keep the caller's $! too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | 753 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | 753 | local $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | # Config::Abstraction treats unknown scalar values as config file paths and will | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | # attempt to read them, corrupting coderefs and object references. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | # Stash them here and restore after merging so callers never need this pattern. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | 753 | my %stashed_values; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | 753 | foreach my $key (keys %$params) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | 1746 | ▶ | next if $key eq 'logger'; # logger has its own path through _build_logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 1632 | my $value = $params->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | 1632 | ▶ | if(ref($value) eq 'CODE' || blessed($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value eq "CODE" or builtin::blessed($value)
MC/DC100%2/2
ref $value eq "CODE" or builtin::blessed($value)
ref $value eq "CODE"
builtin::blessed($value)
Truth table100%3/3
A or B
A= ref $value eq "CODE"B= builtin::blessed($value)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | 41 | $stashed_values{$key} = delete $params->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | # A concrete logger choice ('NULL', an arrayref, an object, a coderef) cannot be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | # merged with config, so it is set aside and always wins. Previously only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | # arrayrefs were, and a site file such as ~/.conf/local.yaml could replace a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | # caller's explicit 'NULL'. A hashref is options, and merges like any config. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 753 | ▶ | if(defined($params->{'logger'}) && ref($params->{'logger'}) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params->{"logger"} and ref $params->{"logger"} ne "HASH"
MC/DC100%2/2
defined $params->{"logger"} and ref $params->{"logger"} ne "HASH"
defined $params->{"logger"}
ref $params->{"logger"} ne "HASH"
Truth table100%3/3
A and B
A= defined $params->{"logger"}B= ref $params->{"logger"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | 104 | $caller_logger = delete $params->{'logger'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | 753 | my $original_class = $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | 753 | $class =~ s/::/__/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 753 | my $config_file = $params->{'config_file'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | 753 | my $config_dirs = $params->{'config_dirs'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | # SECURITY (S1 - path traversal): reject config_file paths containing ".." segments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | # before they reach Config::Abstraction. The pen-test suite confirmed C::A parses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | # /etc/passwd as a colon-delimited conf file, injecting every user account as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | # config key. The ".." guard blocks the traversal vector; direct absolute paths to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | # system files remain the caller's responsibility (document: config_file must be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | # developer-controlled, never raw user input). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | 753 | ▶ | if(defined($config_file) && _is_path_traversal($config_file)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $config_file and _is_path_traversal($config_file)
MC/DC100%2/2
defined $config_file and _is_path_traversal($config_file)
defined $config_file
_is_path_traversal($config_file)
Truth table100%3/3
A and B
A= defined $config_fileB= _is_path_traversal($config_file)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | 23 | croak("$original_class: config_file contains path traversal sequences: $config_file"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | # _get_inheritance_chain returns [UNIVERSAL, ..., Base, Child] (base-first). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | # The discovery loop below walks it child-first (so a file shared by two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | # classes goes to the more specific one) and unshifts, which puts the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | # list back in base-first order without a sort: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | # Premise 1: walking child-first and unshifting each hit leaves the hits | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | # in chain order, base first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | # Premise 2: the primary file belongs to the class itself, the last | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | # class in the chain, and is pushed after every ancestor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | # Conclusion: the list is already in load order. (The old sort by chain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | # index gave the same order; its "// 999" fallback could never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | # fire, because every class in the list comes from the chain.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 730 | my @inheritance_chain = _get_inheritance_chain($original_class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | 730 | my @config_files_to_load = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 730 | my %tracked_files = (); # de-duplicates ancestor hits only | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | 730 | my $primary_file; # the path actually read for config_file, if any | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | 730 | ▶ | if($config_file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # Fail early with a locale-correct OS error string. -r only checks mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | # bits on a file that exists and sets no errno, so the message used to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | # show whatever stale $! happened to hold. Name the cause ourselves: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | # -e sets ENOENT for a missing file; an existing one is EACCES. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | # An empty config_dirs searches nowhere, so it counts as none: without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | # this, config_dirs => [] silently loaded nothing instead of croaking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | 562 | ▶ | if(!($config_dirs && @{$config_dirs}) && !-r $config_file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$config_dirs and @{$config_dirs;} or -r $config_file
$config_dirs and @{$config_dirs;}
MC/DC100%3/3
$config_dirs and @{$config_dirs;} or -r $config_file
$config_dirs
@{$config_dirs;}
-r $config_file
Truth table80%4/5
A and B or C
A= $config_dirsB= @{$config_dirs;}C= -r $config_file
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | 21 | ▶ | $! = POSIX::EACCES() if -e $config_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | 21 | croak("$original_class: ", $config_file, ": $!"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | 541 | foreach my $ancestor_class (reverse @inheritance_chain) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | 1130 | my $ancestor_config_file = _find_class_config_file( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | $ancestor_class, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | $config_file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | $config_dirs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | # Primary file is added separately at the end (highest priority) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | 1130 | ▶ | next if $ancestor_config_file && $ancestor_config_file eq $config_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ancestor_config_file and $ancestor_config_file eq $config_file
MC/DC100%2/2
$ancestor_config_file and $ancestor_config_file eq $config_file
$ancestor_config_file
$ancestor_config_file eq $config_file
Truth table100%3/3
A and B
A= $ancestor_config_fileB= $ancestor_config_file eq $config_file
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | # -r is not redundant with the -r test inside _find_class_config_file: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | # that result is cached, and the file may have gone since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | 744 | ▶ | if($ancestor_config_file && -r $ancestor_config_file && !$tracked_files{$ancestor_config_file}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$ancestor_config_file and -r $ancestor_config_file and not $tracked_files{$ancestor_config_file}
$ancestor_config_file and -r $ancestor_config_file
MC/DC66%2/3
$ancestor_config_file and -r $ancestor_config_file and not $tracked_files{$ancestor_config_file}
$ancestor_config_file
-r $ancestor_config_file
not $tracked_files{$ancestor_config_file}
Truth table75%3/4
A and B and not(C)
A= $ancestor_config_fileB= -r $ancestor_config_fileC= not $tracked_files{$ancestor_config_file}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | 83 | unshift @config_files_to_load, { file => $ancestor_config_file, class => $ancestor_class }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | # The class's own file stands in as the primary (e.g. config_file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | # found only via config_dirs); a readable config_file below overrides it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | 83 | ▶ | $primary_file = $ancestor_config_file if $ancestor_class eq $original_class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | 83 | $tracked_files{$ancestor_config_file} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | 83 | ▶ | $_config_file_stats{$ancestor_config_file} = stat($ancestor_config_file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | if -f $ancestor_config_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # Premise 1: the loop above skips any path equal to $config_file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | # Premise 2: %tracked_files is only written inside that loop. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | # Conclusion: $config_file is never in %tracked_files, so only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | # readability needs testing here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | 541 | ▶ | if(-r $config_file) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | 454 | push @config_files_to_load, { file => $config_file, class => $original_class }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | 454 | $primary_file = $config_file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | 454 | ▶ | $_config_file_stats{$config_file} = stat($config_file) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | if -f $config_file; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | 541 | ▶ | if(!scalar(@config_files_to_load)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | 62 | foreach my $dir (@{$config_dirs}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | 74 | my $candidate = File::Spec->catfile($dir, $config_file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | 74 | ▶ | if(-r $candidate) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | 50 | push @config_files_to_load, { file => $candidate, class => $original_class }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | 50 | $primary_file = $candidate; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | # Track it so hot reload notices edits; without this a file found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | # only through config_dirs was never watched | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | 50 | ▶ | $_config_file_stats{$candidate} = stat($candidate) if -f $candidate; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | 50 | last; # stop at first readable hit; later dirs are lower priority | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 709 | ▶ | if(@config_files_to_load) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | # Already base-first: see the note above the discovery loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | 529 | my $merged_params = { %$params }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | 529 | foreach my $config_info (@config_files_to_load) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | 586 | my $cfg_file = $config_info->{file}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | 586 | my $cfg_class = $config_info->{class}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | # Refuse an oversized file before any parser sees it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | 586 | ▶ | if(my $size = _config_too_large($cfg_file)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | 3 | croak("$original_class: $cfg_file: config file is too large ($size bytes; the limit is $MAX_CONFIG_FILE_SIZE bytes)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | 583 | my $section_name = $cfg_class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | 583 | $section_name =~ s/::/__/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | # Opt-in cache: a valid entry replaces the whole parse below | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | 583 | my $cache_key; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | 583 | ▶ | if($cache) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | 16 | $cache_key = _cache_key($cfg_file, $section_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | 16 | ▶ | if(my $hit = _cache_fetch($cache, $cache_key)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | 7 | $merged_params = _deep_merge($merged_params, $hit); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | 7 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | # Load only the specific file; do not re-pass config_dirs to avoid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | # re-scanning directories and picking up the wrong file for this class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 576 | my $config = Config::Abstraction->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | config_file => $cfg_file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | env_prefix => "${section_name}__" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | 575 | ▶ | if(!$config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | 8 | ▶ | carp("Warning: Can't load configuration from $cfg_file: $@") if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | } elsif(my $this_config = _section_settings($config, $section_name, {})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | # Only successful reads are cached; a failure is retried next time | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | 550 | ▶ | _cache_store($cache, $cache_key, $config, $this_config) if $cache; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | 550 | $merged_params = _deep_merge($merged_params, $this_config); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | # The file was found but nothing could read it (typically the parser | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | # for its format is not installed). Merging the non-hash result used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | # to replace every setting, and then crash; warn and keep going. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | 17 | carp("Warning: Can't load configuration from $cfg_file: no settings could be read (is a parser for this format installed?)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 525 | $params = $merged_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | } elsif(my $config = Config::Abstraction->new(env_prefix => "${class}__")) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | # No config file: honour environment variables across the full ancestor chain. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 15 | my $merged_config = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | # Iterate base-first so that each more-specific class overrides the more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | # general one: UNIVERSAL -> GrandParent -> Parent. The class itself is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | # left out, saving one Config::Abstraction->new() per call (each one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | # searches every default config directory): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | # Premise 1: the class is always the last element of the chain, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | # $config above is the parser that loop step would build. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | # Premise 2: its section is merged again below, over everything the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | # loop produced, by the same parser. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | # Conclusion: merging it in the loop as well changes nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | 15 | foreach my $ancestor_class (@inheritance_chain[0 .. $#inheritance_chain - 1]) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | 17 | my $section_name = $ancestor_class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | 17 | $section_name =~ s/::/__/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | 17 | my $ancestor_env_config = Config::Abstraction->new(env_prefix => "${section_name}__"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 17 | ▶ | if($ancestor_env_config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | 2 | ▶ | if(my $ancestor_config = _section_settings($ancestor_env_config, $section_name, {})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | 2 | $merged_config = _deep_merge($merged_config, $ancestor_config); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | # Keep the caller's values if nothing usable came back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | 15 | ▶ | $params = _section_settings($config, $class, $params) // $params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
_section_settings($config, $class, $params) // $params
MC/DC0%0/2
_section_settings($config, $class, $params) // $params
_section_settings($config, $class, $params)
$params
Truth table33%1/3
A // B
A= _section_settings($config, $class, $params)B= $params
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | 15 | $params = _deep_merge($merged_config, $params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | # SECURITY (S1 extension - config_path traversal + taint): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | # config_path arrives from Config::Abstraction, which reads %ENV. Under | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | # taint mode (-T) the value is tainted; any syscall with a tainted argument | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | # is fatal. Apply the same traversal guard as config_file (above) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | # before any filesystem probe, so the two guards stay in sync. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | # Exploit: ClassName__config_path=../../etc/shadow causes the hot-reload watcher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | # to stat() and track an arbitrary system file, leaking mtime changes via SIGUSR1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | 15 | ▶ | if($params->{config_path}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | croak(__PACKAGE__, ': config_path contains path traversal sequences: ', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | $params->{config_path}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 3 | ▶ | if _is_path_traversal($params->{config_path}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | $_config_file_stats{ $params->{config_path} } = stat($params->{config_path}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | 1 | ▶ | if -f $params->{config_path}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | # croak_on_error was previously computed and then thrown away. It is now | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | # forwarded, but only when set, so loggers keep Log::Abstraction's default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | # (do not croak) unless the caller or a config file asks otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 703 | my $croak_on_error = $params->{'croak_on_error'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | # Passed as-is: absent and undef both mean "off", and _build_logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | # already turns undef into 0, so a second default here would be redundant | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | 703 | my $carp_on_warn = $params->{'carp_on_warn'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | # A concrete caller logger always wins. Otherwise use the merged spec: the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | # caller's hashref options overlaid by any logger: section from config files. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | 703 | ▶ | my $logger_spec = defined($caller_logger) ? $caller_logger : $params->{'logger'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | 703 | $params->{'logger'} = _build_logger($logger_spec, $carp_on_warn, $croak_on_error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | # Record the path that was really read (which for a config_dirs hit is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | # full path, not the bare name given), and nothing at all if no file was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | # loaded, so instantiate() does not register objects for a missing file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | $params->{_config_file} = $primary_file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | 701 | ▶ | if defined($primary_file) && !exists($params->{_config_file}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $primary_file and not exists $params->{"_config_file"}
MC/DC100%2/2
defined $primary_file and not exists $params->{"_config_file"}
defined $primary_file
not exists $params->{"_config_file"}
Truth table100%3/3
A and not(B)
A= defined $primary_fileB= not exists $params->{"_config_file"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | # _config_sections runs parallel to _config_files: the section each file was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | # read with, so a hot reload can re-merge every file exactly as here. Both | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | # are set together, or not at all, so the two lists always line up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | 701 | ▶ | if(@config_files_to_load && !exists($params->{_config_files})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@config_files_to_load and not exists $params->{"_config_files"}
MC/DC100%2/2
@config_files_to_load and not exists $params->{"_config_files"}
@config_files_to_load
not exists $params->{"_config_files"}
Truth table100%3/3
A and not(B)
A= @config_files_to_loadB= not exists $params->{"_config_files"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | 523 | $params->{_config_files} = [ map { $_->{file} } @config_files_to_load ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | 523 | $params->{_config_sections} = [ map { $_->{class} =~ s/::/__/gr } @config_files_to_load ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | # Re-attach stashed coderefs/objects. An empty slice assignment changes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | # nothing, so no "if %stashed_values" guard is needed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 701 | @{$params}{ keys %stashed_values } = values %stashed_values; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | 701 | return Return::Set::set_return($params, { 'type' => 'hashref' }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | =head2 instantiate(%params) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | Configure and instantiate a third-party class without modifying the class itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | C<instantiate> is a convenience wrapper: it calls C<configure>, passes the merged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | hashref to C<$class-E<gt>new(...)>, and optionally registers the result for hot reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | Use it when you need runtime configuration for a class whose source you cannot change. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | Takes a flat hash (not a hashref). Recognized keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | =item * C<class> (Required, string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | The fully-qualified class name to instantiate (e.g., C<'LWP::UserAgent'>). The class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | must already be loaded and must have a C<new> method that accepts a hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | =item * All other keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | Passed through to C<configure()> as C<\%params>. See L</configure($class, \%params)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | for the full list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | A blessed object of type C<$class>, or whatever C<$class-E<gt>new(...)> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | returned if it was not a blessed reference (for example C<undef> from a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | constructor that signals failure that way). Such a value is returned as-is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | and is never registered for hot reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | B<Note:> The returned object's hash will contain a C<class> key holding the class name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | This is intentional -- it is left in the hash as a debugging aid so you can always | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | see which class an object came from. Do not depend on its absence. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | =item * Calls C<configure($class, \%params)> -- see its side effects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | =item * Calls C<$class-E<gt>new(\%merged_params)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | =item * If the config produced a C<_config_file> and C<new()> returned a blessed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | object, calls C<register_object($class, $obj)> so the object participates in hot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | =head3 Error messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | Same as C<configure()>. In addition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | =item * C<Usage: Params::Get-E<gt>Object::Configure::instantiate(class =E<gt> $val)> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | C<instantiate()> was called with no arguments at all. This message comes from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | L<Params::Get>. (With other arguments but no C<class>, the C<configure()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | message about the missing class is given instead.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | =item * Any exception thrown by C<$class-E<gt>new(...)> propagates unchanged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | =head3 Usage Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | my $ua = Object::Configure::instantiate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | class => 'LWP::UserAgent', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | config_file => 'lwp.yml', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | config_dirs => ['/etc/myapp'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | class => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | optional => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | description => 'Fully-qualified class name; must respond to new(hashref)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | All other keys are passed to C<configure()>; see L</Keys of $params>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | description => 'Blessed instance of $class, with class key present in hash; ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | . 'otherwise the non-blessed value new() returned, unregistered', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | notes => 'class key intentionally left in hash as a debugging aid', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | sub instantiate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | 55 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
instantiate: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | # The third-party constructor may set $! as a side effect; keep the caller's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | 55 | local $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | 55 | my $params = Params::Get::get_params('class', @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | # 'class' is read into $class then left in $params, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | # 'class' propagates into configure() as a spurious config key and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | # ends up in the blessed object's hash, allowing the caller to see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | # that has come from what, helping debugging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | 53 | my $class = $params->{'class'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | 53 | $params = configure($class, $params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | 47 | my $obj = $class->new($params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | # A constructor may signal failure by returning undef (or return an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | # unblessed ref); hand that back rather than dying in register_object() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | # with an internal usage message that hides the real failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | 42 | ▶ | register_object($class, $obj) if $params->{_config_file} && blessed($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params->{"_config_file"} and builtin::blessed($obj)
MC/DC100%2/2
$params->{"_config_file"} and builtin::blessed($obj)
$params->{"_config_file"}
builtin::blessed($obj)
Truth table100%3/3
A and B
A= $params->{"_config_file"}B= builtin::blessed($obj)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | 42 | return $obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | =head1 HOT RELOAD FEATURES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | =head2 enable_hot_reload(%opts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | Fork a background watcher that sends SIGUSR1 to the parent whenever a tracked | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | configuration file changes on disk. Objects registered via C<register_object()> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | then have their configuration reloaded automatically. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | B<UNIX only.> On Windows this function is a silent no-op (SIGUSR1 does not exist): it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | forks nothing and returns empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | Takes a flat hash. All keys are optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | =item * C<interval> (integer E<gt>= 1, default: 10) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | Seconds between file-modification checks. Lower values detect changes faster | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | but use more CPU. Fractional values are truncated to whole seconds in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | watcher, and anything that ends up below 1 (including zero and negative values) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | is silently replaced with the default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | =item * C<callback> (coderef, optional) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | Called in the parent process after each config reload. Useful for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | logging or flushing caches. If it dies, the error is reported with C<carp> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | (C<Object::Configure: hot reload callback failed: ...>) rather than propagated, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | because it runs inside a signal handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | The PID of the watcher child process (integer E<gt> 0), or C<undef>/empty if hot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | reload was already active (idempotent: a second call returns immediately without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | forking again). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | =item * Installs Object::Configure's C<SIGUSR1> handler, if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | C<register_object()> has not already done so (see | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | L</register_object($class, $obj)>), so the watcher's signals are always | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | handled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | =item * Forks a child process. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | =item * The child polls C<%_config_file_stats> and sends C<SIGUSR1> to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | parent when a file's modification time, size or inode changes, or the file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | appears or disappears. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | =item * The child exits by itself within one C<interval> if the parent dies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | without stopping it (for example through C<POSIX::_exit> or C<SIGKILL>), so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | it never signals whatever process adopts it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | =item * The child is ended at once by C<SIGTERM> or C<SIGINT> (their default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | action, set before either can reach it), so C<disable_hot_reload()> does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | wait, and none of the parent's C<END> blocks or destructors run in it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | =item * Stores C<{pid =E<gt> $pid, callback =E<gt> $cb}> in C<%_config_watchers>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | =head3 Error messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | =item * C<Failed to fork config watcher: OS-ERROR> -- C<fork()> returned C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | Check system resource limits (C<ulimit -u>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | =item * C<Object::Configure::enable_hot_reload: callback must be a code reference> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | C<callback> was given but is not a coderef. It is checked here because it would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | otherwise only fail later, inside the C<SIGUSR1> handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | =head3 Usage Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | Object::Configure::enable_hot_reload( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | interval => 5, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | callback => sub { warn "Config reloaded at " . localtime . "\n" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | while (1) { sleep 1 } # watcher runs in the background | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | interval => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | default => 10, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | min => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | description => 'Poll interval in seconds', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | callback => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | type => 'coderef', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | description => 'Called in parent after each reload', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | optional => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | min => 2, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | description => 'PID of the watcher child; undef/empty if hot reload was ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | . 'already active, or on Windows', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | 71 | ▶ | sub enable_hot_reload { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enable_hot_reload: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | 71 | my %params = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | # SECURITY (S4): use defined-or (//) not truth-or (||) so that interval=>0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | # is not silently replaced. Then guard: a zero or negative interval would make | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | # the child busy-loop, consuming 100% CPU (internal DoS vector). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | 71 | ▶ | my $interval = $params{interval} // $DEFAULT_INTERVAL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$params{"interval"} // $DEFAULT_INTERVAL
MC/DC100%2/2
$params{"interval"} // $DEFAULT_INTERVAL
$params{"interval"}
$DEFAULT_INTERVAL
Truth table100%3/3
A // B
A= $params{"interval"}B= $DEFAULT_INTERVAL
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | 71 | ▶ | $interval = $DEFAULT_INTERVAL unless $interval > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | 71 | my $callback = $params{callback}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | # The callback is invoked from the SIGUSR1 handler; a non-coderef would only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | # fail there, asynchronously and far from the mistake, so reject it now. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | 71 | ▶ | croak(__PACKAGE__, '::enable_hot_reload: callback must be a code reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $callback and ref $callback ne "CODE"
MC/DC100%2/2
defined $callback and ref $callback ne "CODE"
defined $callback
ref $callback ne "CODE"
Truth table100%3/3
A and B
A= defined $callbackB= ref $callback ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | if defined($callback) && ref($callback) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | # Windows has no SIGUSR1, so a watcher could never notify us: the POD promises | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | # a no-op there rather than a pseudo-process polling forever. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | # A second call while already watching must not fork another watcher. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | 58 | ▶ | return if $^O eq $OS_WINDOWS || %_config_watchers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$^O eq $OS_WINDOWS or %Object::Configure::_config_watchers
MC/DC100%2/2
$^O eq $OS_WINDOWS or %Object::Configure::_config_watchers
$^O eq $OS_WINDOWS
%Object::Configure::_config_watchers
Truth table100%3/3
A or B
A= $^O eq $OS_WINDOWSB= %Object::Configure::_config_watchers
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | # The watcher's only way to report a change is SIGUSR1. Without our | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | # handler, Perl's default action for SIGUSR1 ends the process, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | # program that enabled hot reload before registering any object was | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | # killed by its first config edit. Install the handler first: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | # Premise 1: the watcher sends nothing until after the fork below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | # Premise 2: the handler is in $SIG{USR1} before the fork. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | # Conclusion: every signal the watcher sends is handled. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | 46 | _install_usr1_handler(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | # Handle fork failure ourselves so the documented croak message is used | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | 95 | ▶ | no autodie qw(fork); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | # Captured before the fork: the child cannot learn it reliably later | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | 46 | my $parent_pid = $$; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | # Hold back SIGTERM and SIGINT until the child has set them to their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | # default action (see _run_config_watcher); the parent's mask is put | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | # back straight after the fork | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | 46 | my $saved_mask = POSIX::SigSet->new(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | 46 | POSIX::sigprocmask(POSIX::SIG_BLOCK(), _stop_signals(), $saved_mask); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | 46 | my $pid = fork(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | 46 | my $fork_error = $!; # before sigprocmask can change it | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | 46 | ▶ | POSIX::sigprocmask(POSIX::SIG_SETMASK(), $saved_mask) if $pid || !defined($pid); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pid or not defined $pid
MC/DC0%0/2
$pid or not defined $pid
$pid
not defined $pid
Truth table66%2/3
A or not(B)
A= $pidB= not defined $pid
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | 46 | ▶ | if($pid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | 45 | $_config_watchers{pid} = $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | 45 | $_config_watchers{callback} = $callback; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | 45 | return $pid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | } elsif(defined $pid) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | # Child: run forever, signal parent on change. _exit, not exit: the child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | # is a copy of the parent, and exit() would run the parent's END blocks and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | # DESTROY methods here too (closing shared DB handles, removing temp files). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | 0 | _run_config_watcher($interval, $callback, $parent_pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | 0 | POSIX::_exit(0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | 1 | croak("Failed to fork config watcher: $fork_error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | =head2 disable_hot_reload() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | Stop the background watcher and clear hot-reload state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | Safe to call when hot reload is not active (no-op). After this call, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | configuration files are no longer monitored and C<%_config_watchers> is empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | Nothing (void). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | =item * Sends SIGTERM to the watcher child. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | =item * Polls for up to C<$KILL_TIMEOUT> seconds (default: 5); escalates to SIGKILL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | if the child has not exited by then. (The watcher ends at once on SIGTERM, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | this only happens if something has interfered with it.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | =item * Leaves C<$?> unchanged, although it waits for the child: when this runs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | at program end, C<$?> is the program's exit status. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | =item * Calls C<waitpid> to reap the child. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | =item * Clears C<%_config_watchers>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | B<Blocking>: this function may take up to five seconds if the watcher ignores SIGTERM. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | {} # no arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | type => 'void' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | 201 | ▶ | sub disable_hot_reload { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
disable_hot_reload: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | # The watcher may already have exited, so a failed kill() is expected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | # and must not stop %_config_watchers from being cleared | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | 95 | ▶ | no autodie qw(kill); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | # waitpid() below sets $? to the watcher's wait status. At program end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | # (this runs from END) $? is the exit status Perl is about to use, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | # without this a program that ended normally exited with 15 (SIGTERM) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | # 9 (SIGKILL), whichever stopped the watcher. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | 201 | local $?; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | ## MUTANT_SKIP_BEGIN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | 201 | ▶ | if(my $pid = $_config_watchers{pid}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | # SECURITY (S3 - PID safety): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | # $pid > 1 excludes PID 0 (sends SIGTERM to whole process group - DoS) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | # and PID 1 (init - catastrophic as root). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | # $pid != $$ prevents self-signaling if %_config_watchers is corrupted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | # Exploit mechanism: if global state is poisoned with pid=0 or pid=1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | # kill('TERM', 0) signals every process in the group; kill('TERM', 1) kills | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | # init as root. Both are now rejected at the comparison level. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | 61 | ▶ | if($pid =~ /\A[0-9]+\z/ && $pid > 1 && $pid != $$) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$pid =~ /\A[0-9]+\z/ and $pid > 1 and $pid != $$
$pid =~ /\A[0-9]+\z/ and $pid > 1
MC/DC100%3/3
$pid =~ /\A[0-9]+\z/ and $pid > 1 and $pid != $$
$pid =~ /\A[0-9]+\z/
$pid > 1
$pid != $$
Truth table100%4/4
A and B and C
A= $pid =~ /\A[0-9]+\z/B= $pid > 1C= $pid != $$
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | 50 | kill('TERM', $pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | # Poll up to KILL_TIMEOUT seconds; escalate to SIGKILL if SIGTERM is ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | # SIGKILL cannot be caught or deferred so the subsequent waitpid is always safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | # One test per pass: stop as soon as the child is reaped (waitpid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | # returns its pid) or gone (-1), or the deadline has passed. The old | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | # do/while asked "still running and time left?" twice per pass. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | 50 | my $deadline = time() + $KILL_TIMEOUT; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | 50 | my $kid; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | # (De Morgan: "until reaped or out of time" is "while running and time left") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | 50 | ▶ | while(($kid = waitpid($pid, WNOHANG)) == 0 && time() < $deadline) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
($kid = waitpid($pid, 1)) == 0 and &Object::Configure::time() < $deadline
MC/DC100%2/2
($kid = waitpid($pid, 1)) == 0 and &Object::Configure::time() < $deadline
($kid = waitpid($pid, 1)) == 0
&Object::Configure::time() < $deadline
Truth table100%3/3
A and B
A= ($kid = waitpid($pid, 1)) == 0B= &Object::Configure::time() < $deadline
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | 98 | select undef, undef, undef, $POLL_SLEEP; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | # Premise: the loop only ends with $kid == 0 when the deadline passed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | # Conclusion: the child ignored SIGTERM, so SIGKILL it and reap it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | 50 | ▶ | if($kid == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | 3 | kill('KILL', $pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | 3 | waitpid($pid, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | # Cleared even when the pid is missing, 0 or rejected above: leftover state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | # would make enable_hot_reload() believe a watcher is running and never start one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | 201 | %_config_watchers = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | ## MUTANT_SKIP_END | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | 201 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | =head2 reload_config() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | Immediately reload configuration from disk for every registered object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | Normally called automatically by the SIGUSR1 handler. You may call it manually | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | to force a reload (e.g., in tests or on a custom signal). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | An integer E<gt>= 0: the count of objects whose configuration was successfully | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | reloaded. Objects with no config file, or whose file could not be parsed, are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | not counted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | =item * Re-reads every file an object was configured from (C<universal.yml>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | ancestor files and its own files) and merges them base-first, as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | C<configure()> did, so an edit to any of them takes effect. A file that has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | been deleted is skipped: a key only it set keeps its last value, and a key it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | shared with another file takes that file's value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | =item * Each file is parsed once per reload, however many objects use it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | The parsers are kept between reloads and refreshed with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | L<Config::Abstraction>'s C<reload()> (0.44 and later); with an older | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | Config::Abstraction a new parser is built each time. Parsers no live | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | registered object uses are discarded at the end of each reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | =item * An object without C<_config_sections> (built by hand rather than by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | C<configure()>) re-reads only its most specific file, with its own section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | =item * Updates non-private keys (those not starting with C<_>) in-place on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | each live object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | =item * Logs C<Configuration reloaded for CLASS> through the object's logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | best effort: if the logger dies, the reload still counts as a success. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | =item * Prunes dead weak references from C<%_object_registry>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | =item * Emits a C<carp> warning (not a croak) if reload fails for any individual | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | object; other objects are still processed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | =head3 Error messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | All are warnings; none stops the remaining objects being reloaded, and an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | object that cannot be reloaded is not counted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | =item * C<Warning: Can't load configuration from FILE: DETAIL> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | a kept parser's C<reload()> failed (Config::Abstraction keeps the previous | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | settings and reports why). That file adds nothing to this reload, and is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | parsed afresh next time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | =item * C<Failed to reload config for object: ERROR> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | re-reading an object's file died (for example, the parser threw). ERROR is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | exception text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | =item * C<Object::Configure: _reload_object_config: refusing path with traversal sequences: PATH> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | the object's C<_config_files>, C<_config_file> or C<config_file> key contains a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | C<..> segment, for example because the object was tampered with. The file is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | not read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | =item * C<Object::Configure: _reload_object_config: FILE: config file is too large (SIZE bytes; the limit is 16384 bytes)> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | the object's config file has grown past 16K since it was loaded. It is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | read and the object keeps its current settings. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | L</Config file size limit>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | {} # no arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | type => 'integer', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | description => 'Count of objects successfully reloaded', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | condition => 'value >= 0', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | 105 | ▶ | sub reload_config { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reload_config: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | # Re-reading files sets $! on misses; this runs from a signal handler, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | # it must not change $! under whatever code the signal interrupted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | 105 | local $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | 105 | my $reloaded_count = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | # Files refreshed during this pass: each one is re-read once, however | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | # many registered objects use it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | 105 | my %pass; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | 105 | foreach my $class_key (keys %_object_registry) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | 98 | my $objects = $_object_registry{$class_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1802 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1803 | 98 | @$objects = grep { defined $$_ } @$objects; # prune garbage-collected weak refs (check referent, not the ref-to-scalar itself) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1805 | 98 | foreach my $obj_ref (@$objects) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1806 | 168 | ▶ | if(my $obj = $$obj_ref) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1807 | # Protect the caller's $@ from being clobbered by our internal eval blocks. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1808 | 168 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1809 | 168 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1810 | # Only count objects that actually had a config file to re-read, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1811 | # so the return value matches its documented meaning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1812 | 168 | ▶ | $reloaded_count++ if _reload_object_config($obj, \%pass); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1813 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1814 | 168 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1815 | 5 | carp("Failed to reload config for object: $@"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1816 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1817 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1818 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1819 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1820 | 98 | ▶ | delete $_object_registry{$class_key} unless @$objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1821 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1822 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1823 | # A parser that no live object asked for in this pass is no longer needed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1824 | 105 | delete @_config_sources{ grep { !$pass{$_} } keys %_config_sources }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1825 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1826 | 105 | return $reloaded_count; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1827 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1828 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1829 | =head2 register_object($class, $obj) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1830 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1831 | Register a blessed object so it receives configuration updates when files change. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1832 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1833 | B<Push semantics>: each call I<appends> a new entry to the registry for C<$class>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1834 | It does not replace a previous entry. Multiple objects of the same class are all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1835 | tracked and all reloaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1837 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1838 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1839 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1840 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1841 | =item * C<$class> (Required, string) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1842 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1843 | The class name used to organise the registry. Typically C<ref($self)> or the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1844 | calling package name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1845 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1846 | =item * C<$obj> (Required, blessed reference) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1847 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1848 | The object to register. B<Must be a blessed reference.> Passing an unblessed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1849 | hashref or any other unblessed value causes an immediate C<croak>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1851 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1853 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1854 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1855 | Nothing (void). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1856 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1857 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1858 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1859 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1860 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1861 | =item * Pushes a weak reference to C<$obj> onto C<$_object_registry{$class}>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1863 | =item * Removes references to objects that no longer exist from that list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1864 | whenever it has doubled in size since the last clean-up (and has at least 64 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1865 | entries). This keeps the registry bounded in programs that register many | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1866 | short-lived objects and rarely or never receive C<SIGUSR1>, at an amortised | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1867 | cost of O(1) per call. Live objects are never removed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1869 | =item * If Object::Configure's handler is not installed yet (by an earlier | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1870 | call, or by C<enable_hot_reload()>): saves the current C<$SIG{USR1}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1871 | and installs Object::Configure's handler. On UNIX, the handler calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1872 | C<reload_config()> and the hot-reload callback, then daisy chains to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1873 | prior handler (coderef or sub name), which is always called, even when the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1874 | reload or callback fails. See L</Sharing SIGUSR1 with other code (daisy chaining)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1875 | It also creates a guard whose C<DESTROY> (run at program end, or by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1876 | C<restore_signal_handlers()>) puts the prior handler back if ours is still | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1877 | installed. On Windows, signal installation is skipped but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1878 | C<$_original_usr1_handler> is still set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1879 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1880 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1881 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1882 | =head3 Error messages | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1883 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1884 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1885 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1886 | =item * C<Object::Configure::register_object: Usage ($class, $obj)> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1887 | either C<$class> or C<$obj> was C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1888 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1889 | =item * C<Object::Configure::register_object: $obj must be a blessed reference> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1890 | C<$obj> was defined but not blessed. This guard prevents DoS via registry flooding | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1891 | (reloading thousands of unblessed entries on every SIGUSR1). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1892 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1893 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1894 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1895 | The C<SIGUSR1> handler installed here may also warn (it never dies, since an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1896 | exception there would surface at whatever code the signal interrupted): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1897 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1898 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1899 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1900 | =item * C<Object::Configure: hot reload failed: ERROR> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1901 | C<reload_config()> itself died. The callback and the previous handler are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1902 | still called. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1903 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1904 | =item * C<Object::Configure: hot reload callback failed: ERROR> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1905 | the C<callback> given to C<enable_hot_reload()> died. The reload itself has | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1906 | already happened and the previous handler is still called. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1907 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1908 | =item * C<Object::Configure: Cannot chain to non-code USR1 handler: HANDLER> -- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1909 | C<$SIG{USR1}> held a string other than C<DEFAULT> or C<IGNORE> that does not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1910 | name a defined sub (unqualified names are looked up in C<main::>, as Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1911 | does). Nothing is called. Define the sub, or install a coderef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1912 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1913 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1914 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1915 | =head3 Usage Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1916 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1917 | package My::Module; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1918 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1919 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1920 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1921 | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1922 | my $params = Object::Configure::configure($class, \%args); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1923 | my $self = bless $params, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1924 | Object::Configure::register_object($class, $self) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1925 | if $self->{_config_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1926 | return $self; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1927 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1928 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1929 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1930 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1931 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1932 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1933 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1934 | class => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1935 | type => 'string', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1936 | optional => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1937 | description => 'Class name for registry key', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1938 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1939 | obj => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1940 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1941 | optional => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1942 | description => 'Blessed object to register; unblessed refs are rejected', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1943 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1944 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1945 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1946 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1947 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1948 | type => 'void' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1949 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1950 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1951 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1952 | sub register_object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1953 | 11575 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_object: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1954 | 11575 | my ($class, $obj) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1955 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1956 | 11575 | ▶ | croak(__PACKAGE__, '::register_object: Usage ($class, $obj)') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $class and defined $obj
MC/DC100%2/2
defined $class and defined $obj
defined $class
defined $obj
Truth table100%3/3
A and B
A= defined $classB= defined $obj
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1957 | unless defined($class) && defined($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1958 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1959 | # SECURITY: enforce the API contract (POD: "$obj must be a blessed reference"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1960 | # Accepting unblessed refs silently would allow DoS via registry flooding: an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1961 | # adversary or buggy caller can push thousands of unblessed entries; reload_config() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1962 | # iterates every entry on every SIGUSR1, degrading throughput proportionally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1963 | 11553 | ▶ | croak(__PACKAGE__, '::register_object: $obj must be a blessed reference') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1964 | unless blessed($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1965 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1966 | 11541 | my $obj_ref = \$obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1967 | 11541 | weaken($$obj_ref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1968 | 11541 | ▶ | my $list = ($_object_registry{$class} //= []); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$Object::Configure::_object_registry{$class} //= []
MC/DC100%1/1
$Object::Configure::_object_registry{$class} //= []
$Object::Configure::_object_registry{$class}
Truth table100%2/2
A
A= $Object::Configure::_object_registry{$class}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1969 | 11541 | push @{$list}, $obj_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1970 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1971 | # Without this, a program that registers short-lived objects (one per | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1972 | # request, say) and never receives SIGUSR1 kept a dead entry for every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1973 | # object it ever made: reload_config() was the only clean-up. Pruning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1974 | # when the list doubles costs O(1) amortised per call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1975 | 11541 | ▶ | if(@{$list} >= ($_registry_prune_at{$class} // $REGISTRY_PRUNE_MIN)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$Object::Configure::_registry_prune_at{$class} // $REGISTRY_PRUNE_MIN
MC/DC100%2/2
$Object::Configure::_registry_prune_at{$class} // $REGISTRY_PRUNE_MIN
$Object::Configure::_registry_prune_at{$class}
$REGISTRY_PRUNE_MIN
Truth table100%3/3
A // B
A= $Object::Configure::_registry_prune_at{$class}B= $REGISTRY_PRUNE_MIN
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1976 | 180 | @{$list} = grep { defined $$_ } @{$list}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1977 | 180 | my $next = 2 * @{$list}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1978 | 180 | ▶ | $_registry_prune_at{$class} = ($next > $REGISTRY_PRUNE_MIN) ? $next : $REGISTRY_PRUNE_MIN; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1979 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1980 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1981 | 11541 | _install_usr1_handler(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1982 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1983 | 11541 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1984 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1985 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1986 | =head2 restore_signal_handlers() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1987 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1988 | Turn hot reload off: stop the watcher, if one is running, then restore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1989 | C<$SIG{USR1}> to the handler that was in place before Object::Configure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1990 | installed its own, and clear C<$_original_usr1_handler>. The watcher is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1991 | stopped first because its next signal would otherwise reach the restored | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1992 | handler, and if that is C<DEFAULT>, end the program. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1993 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1994 | This explicit call restores unconditionally, even if other code has since | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1995 | installed a handler of its own. The automatic restore at program end is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1996 | more careful: it only puts the previous handler back if Object::Configure's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1997 | handler is still installed. See | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1998 | L</Sharing SIGUSR1 with other code (daisy chaining)>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1999 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2000 | Safe to call even when Object::Configure never installed a handler (no-op). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2001 | On Windows this function has no effect (SIGUSR1 does not exist there). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2003 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2004 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2005 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2006 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2007 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2008 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2009 | Nothing (void). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2010 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2011 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2012 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2013 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2015 | =item * Stops the watcher if one is running, exactly as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2016 | C<disable_hot_reload()> does (so it may wait up to five seconds). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2017 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2018 | =item * Sets C<$SIG{USR1}> back to its saved value (UNIX only). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2019 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2020 | =item * Sets C<$_original_usr1_handler> to C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2021 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2022 | =item * Releases the installation guard (its C<DESTROY> then has nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2023 | left to do). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2024 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2025 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2026 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2027 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2028 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2029 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2030 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2031 | {} # no arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2032 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2033 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2034 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2035 | type => 'void' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2036 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2037 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2038 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2039 | sub restore_signal_handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2040 | 129 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
restore_signal_handlers: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2041 | # Without our handler, the watcher's next SIGUSR1 would meet the saved | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2042 | # one, and Perl's default action for SIGUSR1 ends the process. So the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2043 | # watcher goes first; with nothing running this does nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2044 | 129 | ▶ | disable_hot_reload() if %_config_watchers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2045 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2046 | 129 | ▶ | if(defined $_original_usr1_handler) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2047 | 35 | ▶ | $SIG{USR1} = $_original_usr1_handler unless $^O eq $OS_WINDOWS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2048 | 35 | $_original_usr1_handler = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2049 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2050 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2051 | # $SIG{USR1} is no longer ours, so the guard's DESTROY has nothing to do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2052 | 129 | undef $_usr1_guard; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2053 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2054 | 129 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2055 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2056 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2057 | =head2 get_signal_handler_info() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2058 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2059 | Return a snapshot of the current signal-handler and hot-reload state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2060 | This is a debugging aid; normal application code does not need to call it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2061 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2062 | =head3 Arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2063 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2064 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2065 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2066 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2067 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2068 | A hashref with these keys: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2069 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2070 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2071 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2072 | =item * C<original_usr1> -- the C<$SIG{USR1}> value that existed before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2073 | Object::Configure installed its handler, or C<undef> if no handler was saved yet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2074 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2075 | =item * C<current_usr1> -- the currently installed C<$SIG{USR1}> handler (coderef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2076 | C<'DEFAULT'>, C<'IGNORE'>, or C<undef>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2077 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2078 | =item * C<hot_reload_active> -- C<1> if Object::Configure's C<SIGUSR1> handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2079 | is installed (C<$_original_usr1_handler> is defined), C<''> otherwise. Always | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2080 | C<''> on Windows, which has no C<SIGUSR1>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2081 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2082 | =item * C<watcher_pid> -- the PID of the background watcher child, or C<undef> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2083 | if C<enable_hot_reload()> has not been called (or the watcher has been stopped). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2084 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2085 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2087 | =head3 Usage Example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2088 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2089 | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2090 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2091 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2092 | # register_object() installs the SIGUSR1 handler; enable_hot_reload() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2093 | # starts the watcher. Both are needed for every field below to be set. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2094 | Object::Configure::register_object(ref($obj), $obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2095 | Object::Configure::enable_hot_reload(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2096 | print Dumper(Object::Configure::get_signal_handler_info()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2097 | # { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2098 | # original_usr1 => 'DEFAULT', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2099 | # current_usr1 => sub { ... }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2100 | # hot_reload_active => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2101 | # watcher_pid => 12345, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2102 | # } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2104 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2106 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2108 | {} # no arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2110 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2112 | type => 'hashref', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2113 | description => 'Snapshot of signal-handler and watcher state', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2114 | schema => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2115 | original_usr1 => { type => 'string', optional => 1 }, # or a coderef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2116 | current_usr1 => { type => 'string', optional => 1 }, # or a coderef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2117 | hot_reload_active => { type => 'boolean' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2118 | watcher_pid => { type => 'integer', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2120 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2121 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2123 | 142 | ▶ | sub get_signal_handler_info { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_signal_handler_info: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2124 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2125 | original_usr1 => $_original_usr1_handler, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2126 | current_usr1 => $SIG{USR1}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2127 | # Windows has no SIGUSR1: the previous handler is saved but nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2128 | # is installed, so hot reload is never active there. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2129 | hot_reload_active => ($^O ne $OS_WINDOWS) && defined($_original_usr1_handler), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2130 | watcher_pid => $_config_watchers{pid}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2131 | 142 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$^O ne $OS_WINDOWS && defined $Object::Configure::_original_usr1_handler
MC/DC100%2/2
$^O ne $OS_WINDOWS && defined $Object::Configure::_original_usr1_handler
$^O ne $OS_WINDOWS
defined $Object::Configure::_original_usr1_handler
Truth table100%3/3
A && B
A= $^O ne $OS_WINDOWSB= defined $Object::Configure::_original_usr1_handler
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2134 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2135 | # Private helpers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2136 | # All routines below are implementation details; callers must not rely on them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2137 | # ---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2139 | # Purpose: Install Object::Configure's SIGUSR1 handler, once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2140 | # Entry: None. Called by register_object() and enable_hot_reload(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2141 | # Exit: Returns 1 if this call installed it, else nothing/0 (already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2142 | # installed, or Windows, where only the previous handler is saved). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2143 | # Side: Saves $SIG{USR1} in $_original_usr1_handler, sets $SIG{USR1} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2144 | # and creates the _Usr1Chain guard. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2145 | 11587 | ▶ | sub _install_usr1_handler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_install_usr1_handler: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2146 | # Install SIGUSR1 handler exactly once. We save the previous handler so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2147 | # we can chain to it (another module may have installed one) and restore it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2148 | # on shutdown. On Windows SIGUSR1 does not exist so we skip the signal work | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2149 | # but still save $_original_usr1_handler so restore_signal_handlers is safe. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2150 | 11587 | ▶ | return if defined $_original_usr1_handler; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2152 | 92 | ▶ | $_original_usr1_handler = $SIG{USR1} || $SIG_DEFAULT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$SIG{'USR1'} || $SIG_DEFAULT
MC/DC0%0/2
$SIG{'USR1'} || $SIG_DEFAULT
$SIG{'USR1'}
$SIG_DEFAULT
Truth table66%2/3
A || B
A= $SIG{'USR1'}B= $SIG_DEFAULT
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2153 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2154 | 92 | ▶ | return 0 if($^O eq $OS_WINDOWS); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2156 | # Daisy chain: our work runs first, then the handler that was there | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2157 | # before us. Neither our reload nor the callback may stop the chain: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2158 | # Premise 1: anything that dies inside the handler is caught here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2159 | # Premise 2: the previous handler is called after both evals. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2160 | # Conclusion: the previous handler always runs, once per signal. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2161 | 90 | my $previous = $_original_usr1_handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2162 | 35 | ▶ | my $handler = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2163 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2164 | 35 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2165 | 35 | ▶ | eval { reload_config(); 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2166 | or carp(__PACKAGE__, ': hot reload failed: ', $@); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2168 | # A dying callback must not escape the signal handler: it would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2169 | # surface at whatever statement the program happened to be running | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2170 | 35 | ▶ | if(my $cb = $_config_watchers{callback}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2171 | 15 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2172 | 15 | ▶ | eval { $cb->(); 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2173 | or carp(__PACKAGE__, ': hot reload callback failed: ', $@); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2176 | # The package variable is read at signal time (so the chain target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2177 | # can be inspected and changed); once it is cleared by a restore, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2178 | # the handler saved at install time is still honoured, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2179 | # handler that other code installed on top of ours keeps working | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2180 | 35 | ▶ | _chain_usr1_handler($_original_usr1_handler // $previous, @_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$Object::Configure::_original_usr1_handler // $previous
MC/DC0%0/2
$Object::Configure::_original_usr1_handler // $previous
$Object::Configure::_original_usr1_handler
$previous
Truth table33%1/3
A // B
A= $Object::Configure::_original_usr1_handlerB= $previous
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2181 | 90 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2183 | 90 | $SIG{USR1} = $handler; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2185 | # Created after $SIG{USR1} is set: assigning it frees any older guard, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2186 | # whose DESTROY then sees that its own handler is no longer live and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2187 | # leaves ours in place | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2188 | 90 | $_usr1_guard = Object::Configure::_Usr1Chain->new($handler, $previous); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2190 | 90 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2193 | # Purpose: The signals that stop the watcher, as a POSIX::SigSet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2194 | # Entry: None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2195 | # Exit: Returns a new SigSet holding SIGTERM and SIGINT. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2196 | # Side: None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2197 | 46 | ▶ | sub _stop_signals { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stop_signals: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2198 | 46 | return POSIX::SigSet->new(POSIX::SIGTERM(), POSIX::SIGINT()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2199 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2201 | # Purpose: Call the SIGUSR1 handler that was installed before ours. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2202 | # Entry: $previous is what $SIG{USR1} held then: a coderef, 'DEFAULT', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2203 | # 'IGNORE', or a sub name ('main::handler', or 'handler', which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2204 | # Perl, like us, looks up in package main). @args is what Perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2205 | # passed to our handler (the signal name). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2206 | # Exit: Returns whatever the previous handler returns, else nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2207 | # Exceptions from the previous handler propagate, exactly as they | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2208 | # would have had Object::Configure never been loaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2209 | # Side: Warns "Cannot chain to non-code USR1 handler" for a name that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2210 | # does not resolve to a defined sub. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2211 | 44 | ▶ | sub _chain_usr1_handler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_chain_usr1_handler: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2212 | 44 | my ($previous, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2213 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2214 | # reftype, not ref: a blessed coderef is still callable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2215 | 44 | ▶ | return $previous->(@args) if (reftype($previous) // '') eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($previous) // ""
MC/DC100%1/1
builtin::reftype($previous) // ""
builtin::reftype($previous)
Truth table100%2/2
A
A= builtin::reftype($previous)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2217 | # DEFAULT for USR1 would terminate, but we have already handled the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2218 | # signal; IGNORE means discard. Either way, nothing more to do. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2219 | 24 | ▶ | return if !defined($previous) || $previous eq $SIG_DEFAULT || $previous eq $SIG_IGNORE; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $previous or $previous eq $SIG_DEFAULT or $previous eq $SIG_IGNORE
not defined $previous or $previous eq $SIG_DEFAULT
MC/DC100%3/3
not defined $previous or $previous eq $SIG_DEFAULT or $previous eq $SIG_IGNORE
not defined $previous
$previous eq $SIG_DEFAULT
$previous eq $SIG_IGNORE
Truth table100%4/4
not(A) or B or C
A= not defined $previousB= $previous eq $SIG_DEFAULTC= $previous eq $SIG_IGNORE
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2221 | 9 | ▶ | my $name = ($previous =~ /::/) ? $previous : "main::$previous"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2222 | 95 | ▶ | my $code = do { no strict 'refs'; defined(&{$name}) ? \&{$name} : undef }; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2223 | 9 | ▶ | return $code->(@args) if $code; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2225 | 6 | carp(__PACKAGE__, ": Cannot chain to non-code USR1 handler: $previous"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2226 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2227 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2229 | # Purpose: Consolidate all logger-creation paths into one place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2230 | # Called from configure() and _reconfigure_logger() to eliminate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2231 | # the duplication that existed between the two. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2232 | # Entry: $spec may be: undef (want default), the string 'NULL' (no logging), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2233 | # an ARRAY ref (log-capture array), a HASH ref (options for Log::Abstraction), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2234 | # a pre-built Log::Abstraction instance (pass through), or any other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2235 | # scalar (treated as a logger name / file path). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2236 | # $carp_on_warn is a boolean controlling Carp::carp integration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2237 | # $croak_on_error is optional; when defined it is passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2238 | # Log::Abstraction, when undef Log::Abstraction's default applies. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2239 | # Exit: Returns a Log::Abstraction instance, or the string 'NULL'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2240 | # A pre-built logger is returned untouched (the options are not applied). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2241 | # Side: May allocate a new Log::Abstraction object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2242 | 742 | ▶ | sub _build_logger { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_logger: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2243 | 742 | my ($spec, $carp_on_warn, $croak_on_error) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2245 | # Options common to every newly built logger; keys in a hashref spec win | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2246 | 742 | ▶ | my %opts = (carp_on_warn => $carp_on_warn // 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$carp_on_warn // 0
MC/DC100%1/1
$carp_on_warn // 0
$carp_on_warn
Truth table100%2/2
A
A= $carp_on_warn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2247 | 742 | ▶ | $opts{croak_on_error} = $croak_on_error if defined $croak_on_error; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2249 | 742 | ▶ | return Log::Abstraction->new(\%opts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2250 | unless defined $spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2252 | 157 | ▶ | return $LOGGER_NULL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $spec and $spec eq $LOGGER_NULL
MC/DC100%2/2
not ref $spec and $spec eq $LOGGER_NULL
not ref $spec
$spec eq $LOGGER_NULL
Truth table100%3/3
not(A) and B
A= not ref $specB= $spec eq $LOGGER_NULL
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2253 | if !ref($spec) && $spec eq $LOGGER_NULL; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2255 | 70 | ▶ | return $spec | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($spec) and $spec->isa("Log::Abstraction")
MC/DC100%2/2
builtin::blessed($spec) and $spec->isa("Log::Abstraction")
builtin::blessed($spec)
$spec->isa("Log::Abstraction")
Truth table100%3/3
A and B
A= builtin::blessed($spec)B= $spec->isa("Log::Abstraction")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2256 | if blessed($spec) && $spec->isa('Log::Abstraction'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2258 | 65 | ▶ | if(ref($spec) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2259 | 23 | return Log::Abstraction->new({ %opts, array => $spec }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2260 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2262 | 42 | ▶ | if(ref($spec) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2263 | 34 | return Log::Abstraction->new({ %opts, %$spec }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2266 | # Scalar: a logger name, file path, or other string identifier passed to L::A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2267 | 8 | return Log::Abstraction->new({ %opts, logger => $spec }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2270 | # Purpose: Build the ancestor chain needed for config-file discovery and env merging. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2271 | # Uses the class's own MRO (DFS or C3) via mro::get_linear_isa, which is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2272 | # more correct than a hardcoded DFS walk and handles diamond inheritance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2273 | # UNIVERSAL is added explicitly because mro::get_linear_isa does not include | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2274 | # it unless it appears in @ISA, yet Object::Configure supports universal.yml. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2275 | # Entry: $class is a fully-qualified class name that has already been loaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2276 | # Exit: Returns a list in base-first order: (UNIVERSAL, ..., GrandParent, Parent, Class). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2277 | # Result is memoized in %_chain_cache; mro::get_linear_isa is not re-invoked | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2278 | # for classes already seen in this process. The cache is valid for the process | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2279 | # lifetime because @ISA is stable after module load in normal Perl programs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2280 | # Algorithmic cost: O(N) first call; O(1) amortised on subsequent calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2281 | 753 | ▶ | sub _get_inheritance_chain { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_inheritance_chain: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2282 | 753 | my ($class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2284 | # Cache hit: return a copy of the stored list (caller may modify the returned list). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2285 | 753 | ▶ | return @{ $_chain_cache{$class} } if $_chain_cache{$class}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2286 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2287 | 538 | my @mro = @{ mro::get_linear_isa($class) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2289 | # mro::get_linear_isa returns child-first; reverse to get base-first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2290 | # UNIVERSAL is implicit in Perl's type system but not always in the MRO list, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2291 | # so append it when absent to ensure universal.yml is picked up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2292 | # List::Util::any is XS and short-circuits on first match -- faster than grep. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2293 | 538 | ▶ | push @mro, 'UNIVERSAL' unless any { $_ eq 'UNIVERSAL' } @mro; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2294 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2295 | 538 | my @chain = reverse @mro; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2296 | 538 | $_chain_cache{$class} = \@chain; # store arrayref; return list copy below | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2297 | 538 | return @chain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2298 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2300 | # Purpose: Find a config file for a specific ancestor class using the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2301 | # naming convention as the primary config file (directory + extension). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2302 | # Entry: $class is a fully-qualified class name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2303 | # $base_config_file is the primary config file path (provides dir and ext). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2304 | # $config_dirs is an optional arrayref of additional search directories. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2305 | # Does NOT modify elements of $config_dirs (trailing-slash removal uses a copy). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2306 | # Exit: Returns a readable file path, or undef if nothing found. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2307 | # Result (including undef for "not found") is memoized in %_find_cache, keyed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2308 | # by (class, base_config_file, config_dirs-elements) joined with NUL bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2309 | # Subsequent calls with identical arguments skip all filesystem probes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2310 | # Algorithmic cost: O(extensions) syscalls first call; O(1) amortised. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2311 | 1155 | ▶ | sub _find_class_config_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_class_config_file: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2312 | 1155 | my ($class, $base_config_file, $config_dirs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2314 | # Build a NUL-separated cache key. NUL cannot appear in Linux file paths (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2315 | # pen-test suite confirms this: Perl's -r warns and returns undef for NUL paths). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2316 | # Premise 1: configure(), the only public caller, croaks unless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2317 | # config_dirs is undef or an arrayref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2318 | # Premise 2: undef and [] both mean "no extra directories". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2319 | # Conclusion: dereferencing $config_dirs // [] is safe, and undef and [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2320 | # share one cache entry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2321 | 1155 | ▶ | my @dirs = @{ $config_dirs // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$config_dirs // []
MC/DC100%1/1
$config_dirs // []
$config_dirs
Truth table100%2/2
A
A= $config_dirs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2323 | 1155 | my $cache_key = join("\0", $class, $base_config_file, @dirs); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2324 | 1155 | ▶ | return $_find_cache{$cache_key} if exists $_find_cache{$cache_key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2325 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2326 | 949 | my $class_file = lc($class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2327 | 949 | $class_file =~ s/::/-/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2329 | 949 | my ($base_vol, $base_dir_part, $base_name_ext) = File::Spec->splitpath($base_config_file); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2330 | 949 | my (undef, $base_ext) = $base_name_ext =~ /^(.*?)(\.[^.]+)?$/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2331 | 949 | ▶ | $base_ext //= ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$base_ext //= ""
MC/DC100%1/1
$base_ext //= ""
$base_ext
Truth table100%2/2
A
A= $base_ext
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2332 | 949 | my $base_dir = File::Spec->catpath($base_vol, $base_dir_part, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2334 | # The primary file's directory is searched first, then each config_dirs entry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2335 | 949 | my @search_dirs = ($base_dir, @dirs); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2337 | # First readable regular file wins; List::Util::first stops probing at the hit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2338 | 3477 | ▶ | my $found = first { -r $_ && -f $_ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2339 | 949 | map { _config_candidates($_, $class_file, $base_ext) } @search_dirs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2341 | # Cache stores undef for "not found"; callers must use exists, not defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2342 | 949 | return ($_find_cache{$cache_key} = $found); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2344 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2345 | # Purpose: List the paths at which one directory may hold a class's config file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2346 | # most-preferred first. Shared by the primary-directory and config_dirs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2347 | # searches in _find_class_config_file so the two cannot drift apart. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2348 | # Entry: $dir is a directory, or '' meaning "relative to the current directory" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2349 | # (what splitpath gives for a bare config_file such as 'app.yml'). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2350 | # $class_file is the class name in file form (e.g. 'my-base-class'). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2351 | # $ext is the primary file's extension, possibly ''. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2352 | # Exit: Returns a list of distinct paths: $ext first, then @CONFIG_EXTENSIONS. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2353 | # For $dir eq '' the paths are bare file names. This matters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2354 | # File::Spec->catfile('', 'x.yml') is '/x.yml', which searched the root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2355 | # directory, and a bare name keeps the primary file comparable with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2356 | # the config_file string the caller passed in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2357 | # Side: None (no filesystem access). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2358 | 1185 | ▶ | sub _config_candidates { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_config_candidates: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2359 | 1185 | my ($dir, $class_file, $ext) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2361 | 1185 | my %seen; # $ext may itself be .yml etc; skip the duplicate probe | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2362 | 1185 | my @names = grep { !$seen{$_}++ } map { "${class_file}$_" } ($ext, @CONFIG_EXTENSIONS); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2364 | 1185 | ▶ | return ($dir eq '') ? @names : map { File::Spec->catfile($dir, $_) } @names; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2365 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2367 | # Purpose: Run as the forked watcher child. Polls %_config_file_stats and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2368 | # sends SIGUSR1 to the parent when any file changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2369 | # Entry: $interval >= 1 (seconds). $callback is unused in the child (it runs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2370 | # in the parent's SIGUSR1 handler). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2371 | # Exit: Never returns; terminates via SIGTERM/SIGINT handlers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2372 | # Side: Modifies %_config_file_stats entries in the child's address space only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2373 | 0 | ▶ | sub _run_config_watcher { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_config_watcher: not called CC=11 SCAR=46.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2374 | 0 | my ($interval, $callback, $parent_pid) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2376 | # The process to notify. enable_hot_reload() passes it ($$ before the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2377 | # fork), because by the time the child runs the parent may already have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2378 | # gone, and getppid() would then name init or a subreaper instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2379 | 0 | ▶ | $parent_pid //= getppid(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$parent_pid //= getppid
MC/DC0%0/2
$parent_pid //= getppid
$parent_pid
getppid
Truth table0%0/3
A //= B
A= $parent_pidB= getppid
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2381 | # If the parent has gone, signalling it fails; don't die noisily in the child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2382 | 95 | ▶ | no autodie qw(kill); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2384 | # SECURITY (S5): re-validate $interval in the child process. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2385 | # If the parent's fork() fires before enable_hot_reload() applies its own guard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2386 | # (race) or state is corrupted between fork and here, sleep(0) or sleep(-1) would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2387 | # cause a busy-loop that saturates the CPU. int() also prevents floating-point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2388 | # values like 0.001 from reaching the kernel as a near-zero sleep. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2389 | 0 | ▶ | $interval = int($interval // $DEFAULT_INTERVAL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$interval // $DEFAULT_INTERVAL
MC/DC0%0/2
$interval // $DEFAULT_INTERVAL
$interval
$DEFAULT_INTERVAL
Truth table0%0/3
A // B
A= $intervalB= $DEFAULT_INTERVAL
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2390 | 0 | ▶ | $interval = $DEFAULT_INTERVAL unless $interval > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2392 | # Let the kernel end the watcher on SIGTERM and SIGINT (their default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2393 | # action), so the parent's END blocks and destructors never run here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2394 | # This used to be a Perl handler calling _exit, but Perl runs those only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2395 | # between statements: a SIGTERM that arrived just before sleep() was not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2396 | # handled until the sleep ended, so disable_hot_reload() waited its full | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2397 | # five seconds and then used SIGKILL (about one stop in ten). A default | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2398 | # action takes effect at once, even in the middle of a system call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2399 | 0 | local $SIG{TERM} = $SIG_DEFAULT; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2400 | 0 | local $SIG{INT} = $SIG_DEFAULT; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2402 | # enable_hot_reload() blocks both around the fork, so that neither can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2403 | # reach the child before the lines above; deliver any that are pending | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2404 | 0 | ▶ | POSIX::sigprocmask(POSIX::SIG_UNBLOCK(), _stop_signals()) unless $^O eq $OS_WINDOWS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2406 | 0 | while(1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2407 | 0 | sleep($interval); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2409 | # Once the parent has gone, getppid() changes (to init or a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2410 | # subreaper). Stop then: without this an orphaned watcher ran | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2411 | # forever, kept any pipe it inherited open (so whatever waits on | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2412 | # that pipe, such as a test harness, never finished), and sent its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2413 | # next SIGUSR1 to whatever process had adopted it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2414 | 0 | ▶ | POSIX::_exit(0) if getppid() != $parent_pid; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2416 | 0 | my $changes_detected = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2418 | 0 | foreach my $config_file (keys %_config_file_stats) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2419 | # A missing file is recorded as undef rather than dropped from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2420 | # watch list, so a file that an editor deletes and re-creates is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2421 | # still noticed when it comes back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2422 | 0 | ▶ | my $current_stat = -f $config_file ? stat($config_file) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2424 | 0 | ▶ | if(_file_changed($_config_file_stats{$config_file}, $current_stat)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2425 | 0 | $_config_file_stats{$config_file} = $current_stat; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2426 | 0 | $changes_detected = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2428 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2430 | 0 | ▶ | if($changes_detected && $^O ne $OS_WINDOWS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$changes_detected and $^O ne $OS_WINDOWS
MC/DC0%0/2
$changes_detected and $^O ne $OS_WINDOWS
$changes_detected
$^O ne $OS_WINDOWS
Truth table0%0/3
A and B
A= $changes_detectedB= $^O ne $OS_WINDOWS
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2431 | 0 | kill('USR1', $parent_pid); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2432 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2433 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2434 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2436 | # Purpose: Decide whether a watched file differs from its last snapshot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2437 | # Split out of _run_config_watcher (which never returns) so the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2438 | # comparison can be unit-tested without forking. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2439 | # Entry: $old and $new are File::stat objects, or undef for "file absent". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2440 | # Exit: Returns 1 if the file appeared, vanished, or any field in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2441 | # @WATCHED_STAT_FIELDS differs; 0 otherwise (including absent->absent). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2442 | # Side: None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2443 | 13 | ▶ | sub _file_changed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_changed: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2444 | 13 | my ($old, $new) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2446 | # Both present: compare fields. Otherwise it changed iff exactly one is present. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2447 | my $changed = ($old && $new) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2448 | 15 | ▶ | ? any { $old->$_ != $new->$_ } @WATCHED_STAT_FIELDS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2449 | 13 | ▶ | : !$old != !$new; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$old and $new
MC/DC100%2/2
$old and $new
$old
$new
Truth table100%3/3
A and B
A= $oldB= $new
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2450 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2451 | 13 | ▶ | return $changed ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2452 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2454 | # Purpose: Reload a single object's configuration from disk and update its fields. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2455 | # Private properties (prefix '_') are intentionally skipped to avoid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2456 | # clobbering internal bookkeeping set at construction time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2457 | # Entry: $obj must be a blessed reference with a {_config_file} or {_config_files} key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2458 | # Exit: Returns 1 if a config file was re-read and applied, false otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2459 | # (not blessed, no file, traversal refused, or unparsable file). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2460 | # reload_config() uses this to count real reloads. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2461 | # Side: Reads from disk. Calls $obj->_on_config_reload if the method exists. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2462 | 196 | ▶ | sub _reload_object_config { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reload_object_config: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2463 | 196 | my ($obj, $pass) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2465 | 196 | ▶ | return unless blessed($obj); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2466 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2467 | 189 | my $original_class = ref($obj); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2468 | 189 | (my $class = $original_class) =~ s/::/__/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2470 | 189 | my $sources = _reload_sources($obj, $class); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2472 | # SECURITY (S1 - path traversal): an attacker who modifies $obj->{_config_file} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2473 | # (e.g., via a deserialization gadget or a malicious config merge) can redirect | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2474 | # hot-reload to read arbitrary system files. Reject paths with ".." segments here | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2475 | # so that even a corrupted object cannot force a traversal read. One bad path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2476 | # refuses the whole reload: the object has been tampered with. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2477 | # Must come BEFORE the -f check so traversal paths for non-existent files are also rejected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2478 | 189 | ▶ | if(my $bad = first { _is_path_traversal($_->[0]) } @{$sources}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2479 | 8 | carp(__PACKAGE__, ': _reload_object_config: refusing path with traversal sequences: ', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2480 | $bad->[0]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2481 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2482 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2483 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2484 | # A file deleted since it was loaded is skipped; its values stay on the object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2485 | 181 | my @present = grep { -f $_->[0] } @{$sources}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2486 | 181 | ▶ | return unless @present; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2487 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2488 | # This runs from a signal handler, so an oversized file is reported, not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2489 | # fatal; the object keeps its current settings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2490 | 152 | foreach my $source (@present) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2491 | 186 | ▶ | if(my $size = _config_too_large($source->[0])) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2492 | 2 | carp(__PACKAGE__, ": _reload_object_config: $source->[0]: config file is too large ($size bytes; the limit is $MAX_CONFIG_FILE_SIZE bytes)"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2493 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2494 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2495 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2497 | # Re-merge base-first, as configure() did, so edits to an ancestor's file or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2498 | # universal.yml take effect too. A file that cannot be read adds nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2499 | 150 | my $new_params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2500 | 150 | foreach my $source (@present) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2501 | 184 | ▶ | my $settings = _source_settings(@{$source}, $pass) or next; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2502 | 172 | $new_params = _deep_merge($new_params, $settings); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2503 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2504 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2505 | # A non-hash result means nothing could be read: leave the object alone | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2506 | 148 | ▶ | return 0 unless $new_params; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2507 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2508 | 141 | foreach my $key (keys %$new_params) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2509 | 217 | ▶ | next if $key =~ /^_/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2511 | 213 | my $val = $new_params->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2513 | # Only the exact 'logger' key is a logger spec ('logger.file' is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2514 | # plain value). An undef logger is stored as-is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2515 | # Premise 1: _build_logger returns 'NULL' unchanged for 'NULL'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2516 | # Premise 2: it builds a default logger for undef, which is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2517 | # what an explicit "logger: ~" in the file asks for. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2518 | # Conclusion: only undef needs its own branch; 'NULL' does not. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2519 | 213 | ▶ | if($key ne 'logger') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2520 | 206 | $obj->{$key} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2521 | } elsif(defined $val) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2522 | 6 | _reconfigure_logger($obj, $key, $val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2523 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2524 | 1 | $obj->{$key} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2525 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2526 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2528 | 141 | ▶ | $obj->_on_config_reload($new_params) if $obj->can('_on_config_reload'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2530 | # Best effort: the new settings are already in place, so a broken log | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2531 | # sink must not turn a successful reload into a reported failure. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2532 | # blessed() first: the logger may be the string 'NULL', and calling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2533 | # ->can on a string would look up a package of that name. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2534 | 141 | ▶ | if(blessed($obj->{logger}) && $obj->{logger}->can('info')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($obj->{"logger"}) and $obj->{"logger"}->can("info")
MC/DC50%1/2
builtin::blessed($obj->{"logger"}) and $obj->{"logger"}->can("info")
builtin::blessed($obj->{"logger"})
$obj->{"logger"}->can("info")
Truth table66%2/3
A and B
A= builtin::blessed($obj->{"logger"})B= $obj->{"logger"}->can("info")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2535 | 62 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2536 | 62 | eval { $obj->{logger}->info("Configuration reloaded for $original_class"); 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2537 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2538 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2539 | # Every failure returned 0 above, so reaching here means success | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2540 | 141 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2541 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2543 | # Purpose: List the files a hot reload re-reads for one object, each with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2544 | # the section it is read with. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2545 | # Entry: $obj is a blessed hashref; $class is its class in section form. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2546 | # Exit: Returns an arrayref of [FILE, SECTION] pairs, base-first; empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2547 | # when the object names no file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2548 | # With _config_sections from configure() (same length as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2549 | # _config_files), every file is re-read with its own section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2550 | # Without it (an object built by hand, or before 0.27), only the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2551 | # most specific file is re-read, with the object's own section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2552 | # Side: None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2553 | 195 | ▶ | sub _reload_sources { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reload_sources: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2554 | 195 | my ($obj, $class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2556 | 195 | my ($files, $sections) = @{$obj}{qw(_config_files _config_sections)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2558 | # ref() eq 'ARRAY' already implies a true value, so no separate truth test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2559 | 195 | ▶ | if(ref($files) eq 'ARRAY' && @{$files}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $files eq "ARRAY" and @{$files;}
MC/DC100%2/2
ref $files eq "ARRAY" and @{$files;}
ref $files eq "ARRAY"
@{$files;}
Truth table100%3/3
A and B
A= ref $files eq "ARRAY"B= @{$files;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2560 | 76 | ▶ | if(ref($sections) eq 'ARRAY' && @{$sections} == @{$files}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $sections eq "ARRAY" and @{$sections;} == @{$files;}
MC/DC100%2/2
ref $sections eq "ARRAY" and @{$sections;} == @{$files;}
ref $sections eq "ARRAY"
@{$sections;} == @{$files;}
Truth table100%3/3
A and B
A= ref $sections eq "ARRAY"B= @{$sections;} == @{$files;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2561 | 73 | return [ grep { $_->[0] } map { [ $files->[$_], $sections->[$_] ] } 0 .. $#{$files} ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2562 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2563 | 3 | ▶ | return $files->[-1] ? [ [ $files->[-1], $class ] ] : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2564 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2566 | 119 | ▶ | my $file = $obj->{_config_file} || $obj->{config_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$obj->{'_config_file'} || $obj->{'config_file'}
MC/DC100%2/2
$obj->{'_config_file'} || $obj->{'config_file'}
$obj->{'_config_file'}
$obj->{'config_file'}
Truth table100%3/3
A || B
A= $obj->{'_config_file'}B= $obj->{'config_file'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2567 | 119 | ▶ | return $file ? [ [ $file, $class ] ] : []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2568 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2570 | # Purpose: Read one file's settings for one section during a hot reload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2571 | # Entry: $file exists; $section is the section name (My__Class). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2572 | # $pass is the hashref reload_config() shares between all objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2573 | # in one pass, or undef for a one-off call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2574 | # Exit: Returns the settings hashref, or undef if nothing usable was read. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2575 | # Side: With $pass: keeps the parser in %_config_sources and refreshes it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2576 | # at most once per pass (reload() on Config::Abstraction >= 0.44, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2577 | # otherwise a new parser), so N objects sharing a file read it once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2578 | # Without $pass: always a new parser, and %_config_sources is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2579 | # touched. A die from Config::Abstraction->new propagates (the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2580 | # caller reports it); a failed reload() is reported here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2581 | 197 | ▶ | sub _source_settings { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_source_settings: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2582 | 197 | my ($file, $section, $pass) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2583 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2584 | 197 | my $config; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2585 | 197 | ▶ | if($pass) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2586 | 181 | my $key = join("\0", $file, $section); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2587 | 181 | $config = $_config_sources{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2588 | 181 | ▶ | if(!$pass->{$key}++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2589 | 69 | $config = _refresh_source($config, $file, $section); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2590 | 67 | ▶ | if($config) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2591 | 64 | $_config_sources{$key} = $config; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2592 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2593 | 3 | delete $_config_sources{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2594 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2595 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2596 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2597 | 16 | $config = _refresh_source(undef, $file, $section); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2598 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2599 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2600 | 195 | ▶ | return $config ? _section_settings($config, $section, {}) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2601 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2602 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2603 | # Purpose: Bring a parser up to date with its file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2604 | # Entry: $config is the parser kept from an earlier pass, or undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2605 | # Exit: Returns an up-to-date Config::Abstraction object, or undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2606 | # An existing parser is refreshed in place with reload() when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2607 | # Config::Abstraction has it; otherwise a new parser is built. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2608 | # reload() keeps the old settings and dies if the file is now | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2609 | # broken: that is reported, and undef returned, so the file adds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2610 | # nothing to this reload and is parsed afresh next time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2611 | # Side: Reads $file; may carp. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2612 | 85 | ▶ | sub _refresh_source { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_refresh_source: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2613 | 85 | my ($config, $file, $section) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2615 | 85 | ▶ | if($config && $_config_abstraction_can_reload) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$config and $Object::Configure::_config_abstraction_can_reload
MC/DC100%2/2
$config and $Object::Configure::_config_abstraction_can_reload
$config
$Object::Configure::_config_abstraction_can_reload
Truth table100%3/3
A and B
A= $configB= $Object::Configure::_config_abstraction_can_reload
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2616 | 10 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2617 | 10 | ▶ | return $config if eval { $config->reload(); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2618 | 3 | carp("Warning: Can't load configuration from $file: $@"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2619 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2620 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2621 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2622 | 75 | return Config::Abstraction->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2623 | config_file => $file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2624 | env_prefix => "${section}__" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2625 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2626 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2627 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2628 | # Purpose: The one path-traversal test, shared by every guard. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2629 | # Entry: $path is a defined string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2630 | # Exit: True if $path could reach a parent directory on this platform. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2631 | # $^O is read at call time (not load time) so the Windows rules | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2632 | # can be tested on any platform by localising $^O. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2633 | # Side: None. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2634 | 829 | ▶ | sub _is_path_traversal { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_path_traversal: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2635 | 829 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2636 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2637 | 829 | ▶ | return $path =~ ($^O eq $OS_WINDOWS ? $RE_PATH_TRAVERSAL_WIN32 : $RE_PATH_TRAVERSAL); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2638 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2639 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2640 | # Purpose: Identify a file's current contents cheaply, for the cache. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2641 | # Entry: $path is a file name (it may not exist). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2642 | # Exit: Returns "DEV:INODE:SIZE:MTIME" (MTIME to sub-second precision | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2643 | # where the filesystem keeps it), or 'absent'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2644 | # Side: One stat() call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2645 | 89 | ▶ | sub _file_signature { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_file_signature: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2646 | 89 | my ($path) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2647 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2648 | 89 | ▶ | my @st = defined($path) ? Time::HiRes::stat($path) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2649 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2650 | 89 | ▶ | return @st ? join(':', @st[0, 1, 7, 9]) : 'absent'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2651 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2652 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2653 | # Purpose: Build the cache key for one config file read with one section. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2654 | # Entry: $file is the path configure() is about to parse; $section is the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2655 | # section name (My__Class), which is also the env_prefix stem. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2656 | # Exit: Returns a 40-character hex SHA-1 of every input that can change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2657 | # what Config::Abstraction returns for that file: both module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2658 | # versions, the current directory and $0 (they decide which extra | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2659 | # base.*, local.* and script-named files are read), the file's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2660 | # signature, every SECTION__* environment variable (any case), the variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2661 | # in @CACHE_ENV_VARS, the encryption key file's signature, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2662 | # --SECTION__key=value arguments in @ARGV, in order. Hashing keeps | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2663 | # keys short and keeps secrets (such as an encryption key) out of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2664 | # the cache's key space. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2665 | # Side: Reads %ENV, @ARGV and the filesystem; changes nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2666 | 32 | ▶ | sub _cache_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_key: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2667 | 32 | my ($file, $section) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2668 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2669 | 32 | my $prefix = "${section}__"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2670 | 32 | ▶ | my $key_file = $ENV{"${prefix}ENCRYPTION_KEY_FILE"} // $ENV{'ENCRYPTION_KEY_FILE'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$ENV{"${prefix}ENCRYPTION_KEY_FILE"} // $ENV{"ENCRYPTION_KEY_FILE"}
MC/DC0%0/2
$ENV{"${prefix}ENCRYPTION_KEY_FILE"} // $ENV{"ENCRYPTION_KEY_FILE"}
$ENV{"${prefix}ENCRYPTION_KEY_FILE"}
$ENV{"ENCRYPTION_KEY_FILE"}
Truth table33%1/3
A // B
A= $ENV{"${prefix}ENCRYPTION_KEY_FILE"}B= $ENV{"ENCRYPTION_KEY_FILE"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2671 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2672 | my @parts = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2673 | $CACHE_FORMAT, $VERSION, $Config::Abstraction::VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2674 | Cwd::getcwd(), $0, $file, $section, _file_signature($file), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2675 | # Case-insensitive, as Config::Abstraction matches them. On Windows | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2676 | # %ENV reports every name in upper case, so a case-sensitive match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2677 | # found none, and a changed variable left a stale entry in use. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2678 | 23 | (map { "$_=$ENV{$_}" } sort grep { /\A\Q$prefix\E/i } keys %ENV), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2679 | 256 | ▶ | (map { "$_=" . ($ENV{$_} // '') } @CACHE_ENV_VARS), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ENV{$_} // ""
MC/DC100%1/1
$ENV{$_} // ""
$ENV{$_}
Truth table100%2/2
A
A= $ENV{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2680 | _file_signature($key_file), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2681 | 32 | (grep { /\A--\Q$prefix\E[^=]*=/ } @ARGV), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2682 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2683 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2684 | 32 | ▶ | return Digest::SHA::sha1_hex(join("\0", map { $_ // '' } @parts)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2685 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2686 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2687 | # Purpose: Return a cached section's settings, if the entry is still valid. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2688 | # Entry: $cache is the caller's get/set object; $key from _cache_key(). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2689 | # Exit: Returns a private copy of the settings hashref, or undef for a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2690 | # miss, a malformed or older-format entry, or an entry for which | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2691 | # any file the original load read has changed since. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2692 | # Side: Calls $cache->get. A cache that dies is reported, not fatal: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2693 | # configure() then parses the file as if there were no cache. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2694 | 25 | ▶ | sub _cache_fetch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_fetch: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2695 | 25 | my ($cache, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2696 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2697 | 25 | my $entry; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2698 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2699 | 25 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2700 | 25 | ▶ | if(!eval { $entry = $cache->get($key); 1 }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2701 | 3 | carp(__PACKAGE__, ': cache get failed: ', $@); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2702 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2703 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2704 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2706 | return unless ref($entry) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2707 | && ($entry->{format} // 0) == $CACHE_FORMAT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2708 | && ref($entry->{settings}) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2709 | 22 | ▶ | && ref($entry->{files}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition81%9/11
ref $entry eq "HASH" and ($entry->{"format"} // 0) == $CACHE_FORMAT and ref $entry->{"settings"} eq "HASH" and ref $entry->{"files"} eq "ARRAY"
ref $entry eq "HASH" and ($entry->{"format"} // 0) == $CACHE_FORMAT and ref $entry->{"settings"} eq "HASH"
ref $entry eq "HASH" and ($entry->{"format"} // 0) == $CACHE_FORMAT
$entry->{"format"} // 0
MC/DC50%2/4
ref $entry eq "HASH" and ($entry->{"format"} // 0) == $CACHE_FORMAT and ref $entry->{"settings"} eq "HASH" and ref $entry->{"files"} eq "ARRAY"
ref $entry eq "HASH"
$entry->{"format"}
ref $entry->{"settings"} eq "HASH"
ref $entry->{"files"} eq "ARRAY"
Truth table60%3/5
A and B and C and D
A= ref $entry eq "HASH"B= $entry->{"format"}C= ref $entry->{"settings"} eq "HASH"D= ref $entry->{"files"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2710 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2711 | # The key covers the primary file; these are the other files that load | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2712 | # read (base.*, local.* and the like), each with its signature then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2713 | 10 | ▶ | return if any { _file_signature($_->[0]) ne $_->[1] } @{ $entry->{files} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2714 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2715 | # A copy: the merged result is handed to the caller's object, and must not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2716 | # share structure with an entry that some caches return by reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2717 | 9 | return Storable::dclone($entry->{settings}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2718 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2719 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2720 | # Purpose: Save a successfully read section to the cache. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2721 | # Entry: $cache and $key as for _cache_fetch(); $config is the parser that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2722 | # produced $settings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2723 | # Exit: Returns nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2724 | # Side: Calls $cache->set with a copy of $settings and the signature of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2725 | # every file the parser read (Config::Abstraction::found(), 0.44 and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2726 | # later; with an older one only the primary file, which the key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2727 | # already covers, is checked). A cache that dies is reported. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2728 | 11 | ▶ | sub _cache_store { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cache_store: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2729 | 11 | my ($cache, $key, $config, $settings) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2730 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2731 | 11 | ▶ | my @found = $config->can('found') ? @{ $config->found() // [] } : (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$config->found // []
MC/DC0%0/1
$config->found // []
$config->found
Truth table50%1/2
A
A= $config->found
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2732 | my $entry = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2733 | format => $CACHE_FORMAT, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2734 | settings => Storable::dclone($settings), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2735 | 11 | files => [ map { [ $_, _file_signature($_) ] } @found ], | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2736 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2737 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2738 | 11 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2739 | 11 | ▶ | eval { $cache->set($key, $entry); 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2740 | or carp(__PACKAGE__, ': cache set failed: ', $@); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2741 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2742 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2743 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2745 | # Purpose: Enforce $MAX_CONFIG_FILE_SIZE before a config file is parsed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2746 | # Entry: $file is a path that exists (it has already passed -r or -f). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2747 | # Exit: Returns the file's size in bytes if it exceeds the limit, else 0. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2748 | # Character devices and directories report a small or zero size | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2749 | # (-s on /dev/urandom is 0) and so are not refused here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2750 | # Side: One stat() call; may set $! (callers localise it). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2751 | 772 | ▶ | sub _config_too_large { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_config_too_large: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2752 | 772 | my ($file) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2753 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2754 | 772 | my $size = -s $file; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2755 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2756 | 772 | ▶ | return (defined($size) && $size > $MAX_CONFIG_FILE_SIZE) ? $size : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $size and $size > $MAX_CONFIG_FILE_SIZE
MC/DC50%1/2
defined $size and $size > $MAX_CONFIG_FILE_SIZE
defined $size
$size > $MAX_CONFIG_FILE_SIZE
Truth table66%2/3
A and B
A= defined $sizeB= $size > $MAX_CONFIG_FILE_SIZE
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2757 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2758 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2759 | # Purpose: Ask Config::Abstraction for one section's settings, and insist on a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2760 | # hashref. Shared by every configure()/reload path so none of them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2761 | # can merge a bad result: with no parser for a file's format, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2762 | # Config::Abstraction 0.43 returns '' and _deep_merge, which returns a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2763 | # non-hash overlay unchanged, would replace every setting with it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2764 | # Entry: $config is a Config::Abstraction object; $section is the class name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2765 | # in section form (My__Class); $defaults is a hashref (may be {}). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2766 | # Exit: Returns the settings hashref, or undef for any non-hash result. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2767 | # Side: None beyond Config::Abstraction's own (it may read %ENV). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2768 | 771 | ▶ | sub _section_settings { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_section_settings: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2769 | 771 | my ($config, $section, $defaults) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2770 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2771 | 771 | my $settings = $config->merge_defaults( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2772 | defaults => $defaults, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2773 | section => $section, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2774 | merge => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2775 | deep => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2776 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2778 | 771 | ▶ | return (ref($settings) eq 'HASH') ? $settings : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2779 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2780 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2781 | # Purpose: Replace the logger on an already-constructed object with one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2782 | # built from a new config value (typically a YAML hashref). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2783 | # Delegates to _build_logger so logger-creation logic lives in one place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2784 | # Entry: $obj is a blessed hashref. $key is the hash key to update (usually 'logger'). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2785 | # $logger_config is the new spec from the config file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2786 | # Exit: Returns nothing; updates $obj->{$key} in-place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2787 | # Side: May allocate a new Log::Abstraction instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2788 | sub _reconfigure_logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2789 | 16 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_reconfigure_logger: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2790 | 16 | my ($obj, $key, $logger_config) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2791 | 16 | ▶ | my $carp_on_warn = $obj->{carp_on_warn} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$obj->{'carp_on_warn'} || 0
MC/DC100%1/1
$obj->{'carp_on_warn'} || 0
$obj->{'carp_on_warn'}
Truth table100%2/2
A
A= $obj->{'carp_on_warn'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2792 | 16 | $obj->{$key} = _build_logger($logger_config, $carp_on_warn, $obj->{croak_on_error}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2793 | 16 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2794 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2795 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2796 | # Purpose: Right-precedence deep merge of two hash references. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2797 | # Scalar/arrayref values in $overlay replace those in $base entirely; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2798 | # nested hashrefs are merged recursively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2799 | # Entry: Both args should be hashrefs (or undef/non-ref, handled gracefully). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2800 | # Exit: Returns a new hashref; neither input is modified. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2801 | 848 | ▶ | sub _deep_merge { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deep_merge: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2802 | 848 | my ($base, $overlay) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2803 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2804 | 848 | ▶ | return $overlay unless ref($base) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2805 | 699 | ▶ | return $overlay unless ref($overlay) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2806 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2807 | 690 | my $result = { %$base }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2808 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2809 | 690 | foreach my $key (keys %$overlay) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2810 | 1415 | ▶ | if(ref($overlay->{$key}) eq 'HASH' && ref($result->{$key}) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $overlay->{$key} eq "HASH" and ref $result->{$key} eq "HASH"
MC/DC100%2/2
ref $overlay->{$key} eq "HASH" and ref $result->{$key} eq "HASH"
ref $overlay->{$key} eq "HASH"
ref $result->{$key} eq "HASH"
Truth table100%3/3
A and B
A= ref $overlay->{$key} eq "HASH"B= ref $result->{$key} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2811 | 61 | $result->{$key} = _deep_merge($result->{$key}, $overlay->{$key}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2812 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2813 | 1354 | $result->{$key} = $overlay->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2814 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2815 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2816 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2817 | 690 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2818 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2819 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2820 | # Clean up the watcher child and restore signal state on interpreter exit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2821 | # With a guard, the restore happens in its DESTROY, which leaves alone a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2822 | # handler that other code installed on top of ours. Without one (Windows, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2823 | # or state set up by hand) fall back to the explicit restore. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2824 | 94 | ▶ | END { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2825 | 94 | disable_hot_reload(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2826 | 94 | ▶ | if($_usr1_guard) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2827 | 13 | undef $_usr1_guard; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2828 | 13 | $_original_usr1_handler = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2829 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2830 | 81 | restore_signal_handlers(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2831 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2832 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2833 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2834 | # Guard for our SIGUSR1 handler. It exists exactly while the handler is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2835 | # installed; its DESTROY undoes the installation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2836 | 95 | package Object::Configure::_Usr1Chain { ## no critic (ProhibitMultiplePackages) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2837 | # Entry: $handler is the coderef just put in $SIG{USR1}; $previous is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2838 | # what was there before it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2839 | 93 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2840 | 93 | my ($class, $handler, $previous) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2841 | 93 | return bless { handler => $handler, previous => $previous }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2842 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2843 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2844 | # Put the previous handler back, but only if ours is still live. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2845 | # Premise 1: if $SIG{USR1} is no longer our coderef, other code replaced | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2846 | # it after us, and may be chaining to us. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2847 | # Premise 2: overwriting that would silently cut its handler off. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2848 | # Conclusion: restore only when $SIG{USR1} is exactly our handler. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2849 | # Runs at END and during global destruction, so it must not disturb $@, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2850 | # $! or $? (the program's exit status). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2851 | 93 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2852 | 93 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2853 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2854 | 93 | local ($@, $!, $?); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2855 | 93 | my $live = $SIG{USR1}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2856 | 93 | ▶ | if(ref($live) eq 'CODE' && ref($self->{handler}) eq 'CODE' && $live == $self->{handler}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
ref $live eq "CODE" and ref $self->{"handler"} eq "CODE" and $live == $self->{"handler"}
ref $live eq "CODE" and ref $self->{"handler"} eq "CODE"
MC/DC66%2/3
ref $live eq "CODE" and ref $self->{"handler"} eq "CODE" and $live == $self->{"handler"}
ref $live eq "CODE"
ref $self->{"handler"} eq "CODE"
$live == $self->{"handler"}
Truth table75%3/4
A and B and C
A= ref $live eq "CODE"B= ref $self->{"handler"} eq "CODE"C= $live == $self->{"handler"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2857 | 44 | $SIG{USR1} = $self->{previous}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2858 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2859 | 93 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2860 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2861 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2862 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2863 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2864 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2865 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2866 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2867 | =item * L<Config::Abstraction> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2869 | =item * L<Log::Abstraction> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2870 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2871 | =item * L<Test Dashboard|https://nigelhorne.github.io/Object-Configure/coverage/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2872 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2873 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2875 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2876 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2877 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2878 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2879 | =item * B<Global singleton state.> C<%_object_registry>, C<%_config_watchers>, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2880 | C<%_config_file_stats> are package globals. Two independent subsystems in the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2881 | process share one hot-reload registry and one SIGUSR1 handler. There is no | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2882 | instance-level isolation. A proper fix would wrap state in an object and allow | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2883 | multiple independent C<Object::Configure> instances, but that would break the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2884 | existing constructor-call API (C<configure($class, \%params)>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2885 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2886 | =item * B<Hot reload is UNIX-only.> SIGUSR1 does not exist on Windows. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2887 | All signal-related paths are guarded with C<$^O ne 'MSWin32'>, so the module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2888 | loads on Windows but silently skips hot-reload registration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2889 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2890 | =item * B<configure() is a God function.> At about 280 lines it handles arg validation, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2891 | config-file discovery, MRO walking, multi-file merging, caching, env-var merging, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2892 | logger creation, and hot-reload bookkeeping. Future versions should decompose this into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2893 | smaller, independently testable units. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2894 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2895 | =item * B<_deep_merge reimplements CPAN.> L<Hash::Merge::Simple> or L<Hash::Merge> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2896 | provide tested, feature-complete deep merge. The internal C<_deep_merge> is 15 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2897 | lines and correct for the current use, but does not handle arrayrefs (they are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2898 | replaced wholesale, not merged). If array-merge semantics are ever needed, switch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2899 | to a CPAN module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2900 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2901 | =item * B<No encapsulation enforcement.> Private helpers (C<_build_logger>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2902 | C<_get_inheritance_chain>, etc.) are accessible to any caller. L<Sub::Private> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2903 | (enforce mode) would make accidental external use a compile-time error. It is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2904 | added here to avoid a smoker dependency on a less-common module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2905 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2906 | =item * B<configure() signature is positional, instantiate() is named.> The two | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2907 | public constructors have inconsistent calling conventions. Normalising them to named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2908 | args would require a deprecation cycle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2909 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2910 | =item * B<mro::get_linear_isa and UNIVERSAL.> Perl's C<mro::get_linear_isa> does | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2911 | not include C<UNIVERSAL> in its output unless C<UNIVERSAL> appears explicitly in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2912 | C<@ISA>. This module appends C<UNIVERSAL> manually so that C<universal.yml> is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2913 | always discovered. If a future Perl version changes this behaviour the guard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2914 | (C<any { $_ eq 'UNIVERSAL' }>) remains correct. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2915 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2916 | =item * B<Hot reload does not re-apply caller values or environment-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2917 | settings.> It re-merges the object's config files (each still honours its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2918 | environment variables), but the values passed to C<configure()> and settings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2919 | that came only from environment variables, with no file, are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2920 | re-evaluated. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2921 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2922 | =item * B<Hot reload keeps one parser per file in memory.> For as long as a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2923 | registered object uses a file, its parsed contents stay in memory, so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2924 | later reloads can refresh it in place. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2925 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2926 | =item * B<The config file cache cannot see new files.> A C<cache> entry is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2927 | checked against every file its load read, but not against files that did not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2928 | exist then. See L</Caching parsed config files>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2929 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2930 | =item * B<Keys removed from a config file stay on the object.> Reload only adds | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2931 | and overwrites keys; it never deletes a key that has disappeared from the file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2932 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2933 | =item * B<The watcher sees only files known when it was forked.> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2934 | C<%_config_file_stats> is copied into the child by C<fork()>. Files first loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2935 | by a C<configure()> call made after C<enable_hot_reload()> are not watched until | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2936 | you call C<disable_hot_reload()> and C<enable_hot_reload()> again. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2937 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2938 | =item * B<Change detection is stat-based.> The watcher compares mtime, size and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2939 | inode. An edit that keeps all three the same (same size, within the same | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2940 | second, rewritten in place) is not detected. Content hashing would close this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2941 | gap at the cost of reading every file on every poll. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2942 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2943 | =item * B<File formats depend on optional parsers.> L<Config::Abstraction> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2944 | loads the parser for each format only if it is installed. Without one, a file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2945 | in that format is skipped with a warning (see L</configure($class, \%params)>); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2946 | nothing crashes, but its settings are not applied. Install L<YAML::XS> for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2947 | YAML files. (With L<Config::Abstraction> 0.43, C<.json> files are also misread | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2948 | when L<YAML::XS> is missing.) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2949 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2950 | =item * B<Messages are English only.> Error and warning texts are fixed strings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2951 | there is no i18n layer. Callers matching on them should match the stable prefix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2952 | (for example C<path traversal> or C<must be a hash reference>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2953 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2954 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2955 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2956 | =head1 DESIGN NOTES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2957 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2958 | Several checks were removed because earlier code already guarantees their | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2959 | outcome. Each rests on a short argument, repeated next to the code: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2960 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2961 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2962 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2963 | =item * B<No sort of the files to load.> If the class's ancestors are found | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2964 | child-first and each one is put at the I<front> of the list, then the list is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2965 | base-first. The primary file belongs to the class itself and is added last. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2966 | So the list is already in load order. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2967 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2968 | =item * B<The primary file is never checked for duplicates.> If the search | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2969 | for ancestor files skips any path equal to C<config_file>, then C<config_file> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2970 | can never already be in the list. So only its readability is tested. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2971 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2972 | =item * B<config_dirs is not re-checked by the private helpers.> If | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2973 | C<configure()> refuses anything but C<undef> or an arrayref, then the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2974 | helpers can treat C<config_dirs // []> as a list without testing its type. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2975 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2976 | =item * B<On reload, only an undef logger is special.> The logger builder | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2977 | already returns C<'NULL'> unchanged, so only C<undef> needs its own branch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2978 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2979 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2980 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2981 | These are checked one partition at a time by F<t/logic.t>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2982 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2983 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2984 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2985 | Please report bugs and feature requests at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2986 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2987 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2988 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2989 | =item * RT (CPAN bug tracker): L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-Configure> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2990 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2991 | or by e-mail: C<bug-object-configure at rt.cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2992 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2993 | =item * GitHub issues: L<https://github.com/nigelhorne/Object-Configure/issues> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2994 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2995 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2996 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2997 | You will be notified automatically of progress on your report. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2998 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2999 | perldoc Object::Configure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3000 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3001 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3002 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3003 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3004 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3005 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3006 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3007 | You do not need this section to use the module. It describes each public | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3008 | function precisely, in a notation based on Z, for people who want to check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3009 | the implementation or write tests against it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3010 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3011 | How to read it: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3012 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3013 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3014 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3015 | =item * C<Pre-condition> must be true when the function is called; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3016 | C<Post-condition> is then true when it returns. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3017 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3018 | =item * C<x@pre> is the value of C<x> before the call; C<x@post> (or plain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3019 | C<x> in a post-condition) is its value after. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3020 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3021 | =item * C<(+)> is the merge described in L</Nested settings are merged; lists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3022 | are replaced>: the right-hand side wins. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3023 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3024 | =item * C<#s> is the number of elements in C<s>; C<dom> and C<ran> are the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3025 | keys and the values of a mapping. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3026 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3027 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3028 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3029 | =head2 configure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3030 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3031 | configure: Class x Params -> ConfigHash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3032 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3033 | Given: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3034 | - C: set of all class names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3035 | - P: set of all parameter hashes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3036 | - F: set of all file paths | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3037 | - H: set of all configuration hashes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3038 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3039 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3040 | - ConfigFiles: F -> H (maps file paths to configuration content) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3041 | - EnvVars: String -> String (environment variables) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3042 | - InheritanceChain: C -> seq C (ordered sequence of ancestor classes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3043 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3044 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3045 | forall class in C, params in P: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3046 | class != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3047 | (params.config_file != empty => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3048 | (exists dir in params.config_dirs: readable(dir/params.config_file)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3049 | OR readable(params.config_file)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3050 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3051 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3052 | forall result in H: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3053 | result = params | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3054 | (+) (merge f in InheritanceConfigFiles(class): ConfigFiles(f)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3055 | (+) (merge v in RelevantEnvVars(class): v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3056 | result.logger in Log::Abstraction | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3057 | (forall k in dom params: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3058 | (params(k) in CodeRef OR blessed(params(k))) => result(k) = params(k)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3059 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3060 | where (+) denotes hash merge with right-precedence, and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3061 | InheritanceConfigFiles(class) is ordered | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3062 | universal.yml, ancestors base-first, primary config_file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3063 | (so the order of priority, lowest first, is: params, UNIVERSAL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3064 | ancestors, the primary file, environment variables) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3065 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3066 | With params.cache = c (an object with get and set): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3067 | for each file f read with section s: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3068 | let k = SHA1(Inputs(f, s)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3069 | Settings(f, s) = copy(c.get(k).settings) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3070 | if c.get(k) is a valid entry and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3071 | forall (g, sig) in c.get(k).files: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3072 | signature(g) = sig | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3073 | = parse(f, s), then c.set(k, entry) otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3074 | so result is the same as without c, unless a file that did not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3075 | exist when k was stored has appeared since (see LIMITATIONS). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3076 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3077 | =head2 instantiate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3078 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3079 | instantiate: Params -> Object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3080 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3081 | Given: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3082 | - P: set of all parameter hashes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3083 | - C: set of all class names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3084 | - O: set of all objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3085 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3086 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3087 | forall params in P: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3088 | params.class in C | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3089 | params.class.can('new') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3090 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3091 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3092 | forall result in O: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3093 | exists config in H: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3094 | config = configure(params.class, params) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3095 | result = params.class.new(config) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3096 | (config._config_file != empty AND blessed(result) != empty => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3097 | result in _object_registry(params.class)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3098 | (blessed(result) = empty => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3099 | result NOT in _object_registry(params.class)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3101 | =head2 enable_hot_reload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3103 | enable_hot_reload: Interval x Callback -> PID | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3105 | Given: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3106 | - I: set of positive integers (intervals in seconds) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3107 | - CB: set of code references | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3108 | - PID: set of process identifiers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3110 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3111 | - _config_watchers: {pid: PID, callback: CB} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3112 | - _config_file_stats: F -> Stat | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3114 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3115 | forall interval in I, callback in CB union {empty}: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3116 | interval >= 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3117 | _config_watchers = empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3118 | OS != 'MSWin32' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3120 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3121 | forall result in PID: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3122 | result > 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3123 | _config_watchers.pid = result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3124 | _config_watchers.callback = callback | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3125 | $SIG{USR1} = reload_config_handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3126 | -- installed as by register_object() if it was not already | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3127 | (forall t in Time: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3128 | (t mod interval = 0) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3129 | (exists f in dom _config_file_stats: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3130 | (mtime(f), size(f), inode(f)) != | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3131 | (mtime, size, ino)(_config_file_stats(f)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3132 | OR exists(f) != exists@fork(f) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3133 | send_signal(SIGUSR1, parent_process))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3134 | (parent_process = $$@pre, fixed at the fork; once | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3135 | getppid() != parent_process the watcher exits, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3136 | no signal is ever sent to any other process) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3138 | On Windows: result = empty and the state is unchanged. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3139 | If _config_watchers != empty@pre: result = empty and the state is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3140 | unchanged (idempotent). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3142 | =head2 disable_hot_reload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3144 | disable_hot_reload: () -> () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3146 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3147 | - _config_watchers: {pid: PID, callback: CB} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3148 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3149 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3150 | true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3152 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3153 | _config_watchers = empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3154 | (forall p in PID: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3155 | p = _config_watchers.pid@pre => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3156 | NOT alive(p)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3158 | =head2 reload_config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3160 | reload_config: () -> N | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3162 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3163 | - _object_registry: C -> seq ObjectRef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3164 | - ConfigFiles: F -> H | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3166 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3167 | true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3169 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3170 | forall result in N: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3171 | result = |{obj in flatten(ran _object_registry) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3172 | obj != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3173 | Merged(obj) != empty}| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3174 | (forall obj in flatten(ran _object_registry): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3175 | obj != empty AND Merged(obj) != empty => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3176 | (forall k in dom Merged(obj): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3177 | k NOT in PrivateKeys => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3178 | obj(k)@post = Merged(obj)(k))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3179 | dom _config_sources@post = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3180 | {(f, s) | exists obj live: (f, s) in Sources(obj), f exists} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3182 | where PrivateKeys = {k | k starts with '_'} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3183 | Sources(obj) = zip(obj._config_files, obj._config_sections) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3184 | if both present and of equal length, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3185 | else <(last(obj._config_files) or obj._config_file, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3186 | section(class(obj)))> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3187 | Merged(obj) = (+) over (f, s) in Sources(obj), f exists, base-first: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3188 | Section(ConfigFiles(f), s) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3189 | and each (f, s) is read at most once per call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3191 | =head2 register_object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3192 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3193 | register_object: C x O -> () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3195 | Given: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3196 | - C: set of class names | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3197 | - O: set of blessed objects | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3198 | - OR: C -> seq WeakRef(O) (object registry) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3200 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3201 | - _object_registry: OR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3202 | - _original_usr1_handler: SignalHandler union {empty} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3203 | - $SIG{USR1}: SignalHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3205 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3206 | forall class in C, obj in O: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3207 | class != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3208 | obj != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3209 | blessed(obj) != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3211 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3212 | forall class in C, obj in O: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3213 | exists ref in _object_registry(class): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3214 | weak(ref) = obj | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3215 | (forall live o registered under class: o in ran _object_registry(class)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3216 | #_object_registry(class) <= max(64, 2 * #live(class)) + 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3217 | -- dead entries are pruned when the list doubles (amortised O(1)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3218 | (_original_usr1_handler = empty@pre => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3219 | (_original_usr1_handler@post = $SIG{USR1}@pre | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3220 | $SIG{USR1}@post = reload_config_handler)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3222 | Daisy chain (each delivery of SIGUSR1 to reload_config_handler h): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3223 | let p = _original_usr1_handler (or, once cleared, $SIG{USR1}@install) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3224 | h(sig) = reload_config(); callback(); call(p, sig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3225 | where call(p, sig) = p(sig) if p in CodeRef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3226 | = (main::p)(sig) if p names a defined sub | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3227 | = skip if p in {DEFAULT, IGNORE} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3228 | = warn otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3229 | and failures in reload_config() and callback() are caught, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3230 | call(p, sig) happens exactly once per delivery. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3232 | Teardown (guard DESTROY, at END): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3233 | $SIG{USR1}@pre = h => $SIG{USR1}@post = $SIG{USR1}@install | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3234 | $SIG{USR1}@pre != h => $SIG{USR1}@post = $SIG{USR1}@pre | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3236 | =head2 restore_signal_handlers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3238 | restore_signal_handlers: () -> () | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3240 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3241 | - _original_usr1_handler: SignalHandler union {empty} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3242 | - $SIG{USR1}: SignalHandler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3244 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3245 | true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3247 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3248 | _config_watchers@post = empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3249 | (forall p: p = _config_watchers.pid@pre => NOT alive(p)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3250 | $SIG{USR1}@post = _original_usr1_handler@pre | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3251 | _original_usr1_handler@post = empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3252 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3253 | =head2 get_signal_handler_info | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3255 | get_signal_handler_info: () -> InfoHash | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3257 | Given: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3258 | - IH: set of all info hashes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3260 | State: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3261 | - _original_usr1_handler: SignalHandler union {empty} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3262 | - $SIG{USR1}: SignalHandler union {empty} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3263 | - _config_watchers: {pid: PID, callback: CB} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3264 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3265 | Pre-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3266 | true | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3268 | Post-condition: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3269 | forall result in IH: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3270 | result.original_usr1 = _original_usr1_handler | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3271 | result.current_usr1 = $SIG{USR1} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3272 | result.hot_reload_active = (_original_usr1_handler != empty | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3273 | and $^O != 'MSWin32') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3274 | result.watcher_pid = _config_watchers.pid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3276 | =head1 STATE DIAGRAM | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3278 | Object::Configure keeps one set of hot-reload state for the whole process. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3279 | It depends on two things: whether Object::Configure's C<SIGUSR1> handler is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3280 | installed (the "handler"), and whether the background process started by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3281 | C<enable_hot_reload()> is running (the "watcher"). The watcher only runs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3282 | while the handler is installed, so there are three states, plus a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3283 | short-lived RELOADING state while a signal is being handled. Use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3284 | C<get_signal_handler_info()> to see the current state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3286 | register_object() (first call) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3287 | +--------------+ ----------------------------> +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3288 | | S0 IDLE | | S1 HANDLER | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3289 | | no handler | <---------------------------- | no watcher | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3290 | | no watcher | restore_signal_handlers() | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3291 | +--------------+ +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3292 | | ^ | ^ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3293 | | | enable_ | | | SIGUSR1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3294 | | | restore_signal_handlers() hot_ | | | (sent by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3295 | | | (stops the watcher too) reload()| | | kill) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3296 | | | v | v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3297 | | | +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3298 | | +--------------------------------- | S2 WATCHING | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3299 | +-----------------------------------------> | handler and | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3300 | enable_hot_reload() (installs the | watcher | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3301 | handler, then forks the watcher) +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3302 | | ^ disable_hot_reload() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3303 | a config file changes: | | goes back to S1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3304 | the watcher sends SIGUSR1 v | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3305 | +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3306 | | R RELOADING | handler returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3307 | | (in handler) | back to S1 or S2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3308 | +--------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3310 | =head2 Transitions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3312 | FROM TRIGGER TO ACTIONS / SIDE EFFECTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3313 | ------- ------------------------------- ------- -------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3314 | S0 register_object(), first call S1 saves $SIG{USR1}; installs the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3315 | handler; creates the guard; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3316 | adds a weak reference to the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3317 | registry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3318 | S1, S2 register_object() same adds a weak reference; removes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3319 | dead ones when the class's list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3320 | has doubled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3321 | S0 enable_hot_reload() S2 installs the handler as above | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3322 | (no object needed), then forks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3323 | the watcher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3324 | S1 enable_hot_reload() S2 forks the watcher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3325 | S2 enable_hot_reload() S2 nothing (already watching) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3326 | S2 disable_hot_reload() S1 SIGTERM to the watcher; SIGKILL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3327 | after 5 seconds; waits for it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3328 | S0, S1 disable_hot_reload() same nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3329 | S1 restore_signal_handlers() S0 puts the saved handler back in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3330 | $SIG{USR1}; releases the guard | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3331 | S2 restore_signal_handlers() S0 stops the watcher (as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3332 | disable_hot_reload()), then as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3333 | from S1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3334 | S1, S2 SIGUSR1 (watcher or kill) R see RELOADING below | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3335 | R handler finishes S1/S2 back to the state it came from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3336 | S1, S2 other code assigns $SIG{USR1} same* *our handler is no longer live: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3337 | reloads stop unless that code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3338 | calls ours, and the watcher's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3339 | signals go to that code (if it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3340 | sets 'DEFAULT', a signal ends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3341 | the program); at program end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3342 | its handler is kept | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3343 | any reload_config(), called directly same step 1 of RELOADING only: every | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3344 | registered object is re-read; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3345 | the callback and the previous | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3346 | handler are not called | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3347 | any configure() reads a file same the file is remembered; a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3348 | watcher started LATER checks it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3349 | S2 the program dies without END (end) the watcher notices within one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3350 | (e.g. POSIX::_exit, SIGKILL) interval and exits by itself | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3351 | any program END (end) watcher stopped as by | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3352 | disable_hot_reload(); then the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3353 | saved handler is put back, but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3354 | only if ours is still installed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3356 | On Windows there is no C<SIGUSR1>, so the process stays in S0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3357 | C<register_object()> only saves C<$SIG{USR1}> (so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3358 | C<restore_signal_handlers()> is safe), C<enable_hot_reload()> returns without | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3359 | starting a watcher, and C<get_signal_handler_info()> reports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3360 | C<hot_reload_active> as false. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3362 | RELOADING runs these steps, in this order, once per signal: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3364 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3366 | =item 1. C<reload_config()>: re-read every registered object's files and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3367 | update the object's settings. A failure is reported with C<carp>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3369 | =item 2. The C<callback> given to C<enable_hot_reload()>, if any. A | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3370 | failure is reported with C<carp>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3372 | =item 3. The handler that was in C<$SIG{USR1}> before Object::Configure's, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3373 | if it is code or the name of a sub. This step always runs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3375 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3377 | =head2 Life cycle of a registered object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3379 | [ made by configure() + bless, or by instantiate() ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3380 | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3381 | | register_object() (instantiate() calls it for you when | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3382 | v a config file was loaded) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3383 | +------------------+ reload_config() +--------------------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3384 | | REGISTERED | ------------------> | UPDATED IN PLACE | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3385 | | weak reference | <------------------ | keys not starting with _ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3386 | | in the registry | (returns) | set from its files | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3387 | +------------------+ +--------------------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3388 | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3389 | | the program drops its last reference to the object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3390 | v | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3391 | +------------------+ next reload_config(), or a register_object() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3392 | | FREED | for the same class once its list has doubled | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3393 | | dead reference | ---------------------------------------------> removed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3394 | +------------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3396 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3398 | Copyright 2025-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3400 | Usage is subject to GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3401 | If you use it, please let me know. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3403 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3404 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3405 | 95 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||