| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # Miscellaneous administrivia. | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Bot::Pastebot::Administrivia; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 884 | use warnings; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 31 |  | 
| 6 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 5 | use Carp qw(croak); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 9 | 1 |  |  | 1 |  | 4 | use Bot::Pastebot::Conf qw( get_names_by_type get_items_by_name ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 54 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 1 |  |  | 1 |  | 5 | use base qw(Exporter); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 83 |  | 
| 12 |  |  |  |  |  |  | our @EXPORT_OK = qw(get_pid write_pidfile uses_pidfile); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | # Return this module's configuration. | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 1 |  |  | 1 |  | 5 | use Bot::Pastebot::Conf qw(SCALAR REQUIRED); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 395 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | my %conf = ( | 
| 19 |  |  |  |  |  |  | administrivia => { | 
| 20 |  |  |  |  |  |  | name      => SCALAR | REQUIRED, | 
| 21 |  |  |  |  |  |  | pidfile   => SCALAR, | 
| 22 |  |  |  |  |  |  | }, | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 0 |  |  | 0 | 0 |  | sub get_conf { return %conf } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # return the name used in the config for this section. | 
| 28 |  |  |  |  |  |  | sub _get_name { | 
| 29 | 0 |  |  | 0 |  |  | my $name; | 
| 30 | 0 |  |  |  |  |  | eval { ($name) = get_names_by_type('administrivia') }; | 
|  | 0 |  |  |  |  |  |  | 
| 31 | 0 | 0 |  |  |  |  | return $name unless $@; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Do we have a pidfile configured? | 
| 35 |  |  |  |  |  |  | sub uses_pidfile { | 
| 36 | 0 |  |  | 0 | 0 |  | _get_name(); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | # Examine the PID file to see if there's a session running already. | 
| 40 |  |  |  |  |  |  | sub get_pid { | 
| 41 | 0 |  |  | 0 | 0 |  | my $name = _get_name(); | 
| 42 | 0 |  |  |  |  |  | my %conf = get_items_by_name($name); | 
| 43 | 0 |  |  |  |  |  | my $pidfile = $conf{pidfile}; | 
| 44 | 0 | 0 | 0 |  |  |  | return unless $pidfile && -e $pidfile; | 
| 45 | 0 |  |  |  |  |  | my $pid = do { | 
| 46 | 0 |  |  |  |  |  | local $/; | 
| 47 | 0 | 0 |  |  |  |  | open my $fh, '<', $pidfile or die "open($pidfile): $!"; | 
| 48 | 0 |  |  |  |  |  | <$fh>; | 
| 49 |  |  |  |  |  |  | }; | 
| 50 | 0 |  |  |  |  |  | my $is_running = kill 0, $pid; | 
| 51 | 0 | 0 |  |  |  |  | return $pid if $is_running; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | # We don't seem to be running, so write our PID file. | 
| 55 |  |  |  |  |  |  | sub write_pidfile { | 
| 56 | 0 |  |  | 0 | 0 |  | my $name = _get_name(); | 
| 57 | 0 |  |  |  |  |  | my %conf = get_items_by_name($name); | 
| 58 | 0 |  |  |  |  |  | my $pidfile = $conf{pidfile}; | 
| 59 | 0 | 0 |  |  |  |  | return unless $pidfile; | 
| 60 | 0 | 0 |  |  |  |  | open my $fh, '>', $pidfile or die "open($pidfile): $!"; | 
| 61 | 0 |  |  |  |  |  | print $fh $$; | 
| 62 | 0 |  |  |  |  |  | close $fh; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | 1; |