| 1 | package App::Rad::Plugin::Daemonize; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use File::Temp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | ########## POD Begin ################### | ||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 9 | App::Rad::Plugin::Daemonize | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | It's just an awesome plugin for Rad, that allows you to turn your Rad applications into daemons! | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | start | stop | restart your Rad Application! | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 16 | current version here | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | our $VERSION = '0.1.7'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | If you reached here you probably know Rad, but if you don't, please take a quick look on App::Rad, it is the basis for this plugin, that helps you | ||||||||||||||||||||||||||||||||||||||||||
| 26 | to make daemons very quickly. | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # TODO : get the example simpler than that (not passing configuration parameters and with sample code more simple | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | Let us show you the first test daemon : | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 33 | use App::Rad qw/Daemonize/; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | App::Rad->run; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | sub setup { | ||||||||||||||||||||||||||||||||||||||||||
| 37 | my $c = shift; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | $c->daemonize(\&test, use_cmd_args => 1, stderr_logfile => "./test_err.log", stdout_logfile => "./test_out.log") | ||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | sub test { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | while(sleep 1){ | ||||||||||||||||||||||||||||||||||||||||||
| 42 | print "OUT: ", $count++, $/; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | print { STDERR } "ERR: ", $count, $/; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | warn "WARN: ", $count++, $/; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | That's it!! We have our first example | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | As you see, you have to care with the infinite repetition structure on your sub, it's just write the sub and daemonize it. | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | =head1 Configuring your daemon | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | Here will be described the options you can change on your daemon, like PID file, and log files. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | You also can choose to configure it on the command line, like : #./mydaemon start --option1="value" Or you can configure inside your program. | ||||||||||||||||||||||||||||||||||||||||||
| 57 | The second one is recommended if suits best for you. Now you will learn to pass some parameters to your daemon, note that if you don't set them they will assume the standard value (files on the same dir of the binary) and user root : | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | =head2 Setup built in the code | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | Let us take the first deamon example setup (the only line that matters is the $c->daemonize, so let's analyze it): | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | $c->daemonize(\&test, use_cmd_args => 1, stderr_logfile => "./test_err.log", stdout_logfile => "./test_out.log") | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | * First parameter -> Reference to the sub you want to daemonize. Remember that you have to care about the infinite repetition of the sub on your code. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | All your daemon's code should be there. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | * The rest of the parameters will be listed here one by one, as you can use them in any order, as they're keys/values of a hash | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | - use_cmd_args - This sub isnt here, so i dont remember <revise before CPAN> it sets how your parameters will be set, on the daemonize method directly or on the command line (ex: $./mydaemon.pl start --pid_file="/tmp/mydaemon.pid") : If set to true, it will listen to the method's parameters, if set to one, command-line parameters | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | - pid_file - where will be located the file containing the PID of the running daemon. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | WARNING! If you want to do a coherent logging system for your daemon read carefully these two paremeters : | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | - stdout_logfile - Obviously sets where it will be > all that would be printed on the STDOUT of your daemon goes to that file | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | - stderr_logfile - Analog to stdout_logfile but for STDERR, you're free to put both together on the same file. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | - proc_name - Yes! We thought about you doing a `ps aux | grep mydaemon` and getting the whole ugly command line you gave, so this sets the name of the running daemon, for example, just "mydaemon" (yes, you will see it on ps aux, and just that) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | - user - The user that will run your daemon; If you care about security you will want to avoid exploits that run as root, so setting this you set the user that will own the running process | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | =head2 Setup on the command-line | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | This is more simpler than the before, and will be useful if you wanna do something that the user won't need to open the source files to configure your daemon. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | The options are the same as the listed above. Just that you have to specify as command-line options : | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | #./mydaemon.pl start --pid_file="/tmp/mydaemon.pid" --user="foo" --proc_name="mydaemon" | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | ## continue on the next push | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub after_detach { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
after_detach: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 99 | #my %pars = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $c->write_pidfile($pars{pid_file}); | |||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | $c->change_procname($pars{proc_name}) if $c->check_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $c->chroot($pars{chroot_dir}) if $^O ne "MSWin32" and $c->check_root; # TODO: Make it work on windows | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$^O ne "MSWin32" and $c->check_root
MC/DC0%0/2
$^O ne "MSWin32" and $c->check_root
$^O ne "MSWin32"
$c->check_root
Truth table0%0/3
A and B
A= $^O ne "MSWin32"B= $c->check_root
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | $c->change_user($pars{user}) if exists $pars{user}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | $c->signal_handlers($pars{signal_handlers}); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if(exists $pars{stdout_logfile}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | open STDOUT, '>>', $pars{stdout_logfile}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | close STDOUT; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if(exists $pars{stderr_logfile}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | open STDERR, '>>', $pars{stderr_logfile}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | close STDERR; | |||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | ▶ | sub set_daemonize_pars { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_daemonize_pars: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | $c->stash->{daemonize_pars} = {@_}; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub get_daemonize_pars { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_daemonize_pars: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | my %pars = %{ $c->stash->{daemonize_pars} }; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if(exists $pars{use_cmd_args} and $pars{use_cmd_args}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $pars{"use_cmd_args"} and $pars{"use_cmd_args"}
MC/DC0%0/2
exists $pars{"use_cmd_args"} and $pars{"use_cmd_args"}
exists $pars{"use_cmd_args"}
$pars{"use_cmd_args"}
Truth table0%0/3
A and B
A= exists $pars{"use_cmd_args"}B= $pars{"use_cmd_args"}
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | my %options = %{ $c->options }; | |||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | for my $opt (keys %options) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | next if exists $pars{$opt}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | $pars{$opt} = $options{$opt}; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | %pars; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | sub daemonize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
daemonize: called CC=2 SCAR=15.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | my $func = shift; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | my %pars = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | $c->set_daemonize_pars(%pars); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $c->register("Win32_Daemon", sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $c->after_detach(%pars); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | $func->($c); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | }) if $^O eq "MSWin32"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | $c->register("stop" , sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 150 | Carp::croak "You are not root" | ||||||||||||||||||||||||||||||||||||||||||
| 151 | if ($pars{check_root} and exists $pars{check_root}) | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | and not $c->check_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"} and exists $pars{"check_root"}
MC/DC0%0/3
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"}
exists $pars{"check_root"}
not $c->check_root
Truth table0%0/4
A and B and not(C)
A= $pars{"check_root"}B= exists $pars{"check_root"}C= not $c->check_root
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | Carp::carp "$0 is not running" unless $c->is_running($pars{pid_file}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | "Stopping $0 (" . $c->read_pidfile . "): " . ($c->stop ? "OK$/" : "Failed$/") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | $c->register("restart", sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 159 | Carp::croak "You are not root" | ||||||||||||||||||||||||||||||||||||||||||
| 160 | if ($pars{check_root} and exists $pars{check_root}) | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | and not $c->check_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"} and exists $pars{"check_root"}
MC/DC0%0/3
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"}
exists $pars{"check_root"}
not $c->check_root
Truth table0%0/4
A and B and not(C)
A= $pars{"check_root"}B= exists $pars{"check_root"}C= not $c->check_root
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | $c->execute("stop"); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | sleep 2; | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | $c->execute("start"); | |||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | $c->register("status" , sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 169 | Carp::croak "You are not root" | ||||||||||||||||||||||||||||||||||||||||||
| 170 | if ($pars{check_root} and exists $pars{check_root}) | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | and not $c->check_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"} and exists $pars{"check_root"}
MC/DC0%0/3
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"}
exists $pars{"check_root"}
not $c->check_root
Truth table0%0/4
A and B and not(C)
A= $pars{"check_root"}B= exists $pars{"check_root"}C= not $c->check_root
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | $c->is_running($pars{pid_file}) ? "$0 is running (PID: " | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | . $c->read_pidfile . ")" : "$0 is not running" | ||||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | $c->register("start" , sub{ | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | my %pars = $c->get_daemonize_pars; | |||||||||||||||||||||||||||||||||||||||||
| 178 | Carp::croak "You are not root" | ||||||||||||||||||||||||||||||||||||||||||
| 179 | if ($pars{check_root} and exists $pars{check_root}) | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | and not $c->check_root; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"} and exists $pars{"check_root"}
MC/DC0%0/3
$pars{"check_root"} and exists $pars{"check_root"} and not $c->check_root
$pars{"check_root"}
exists $pars{"check_root"}
not $c->check_root
Truth table0%0/4
A and B and not(C)
A= $pars{"check_root"}B= exists $pars{"check_root"}C= not $c->check_root
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($c->is_running($pars{pid_file})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | Carp::carp "$0 is already running (PID: " . $c->read_pidfile . ")"; | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | "Starting $0 (PID: " . $c->read_pidfile . "): Failed$/"; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | my $daemon_pid = $c->detach unless $pars{no_detach}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | print "Starting $0 (PID: $daemon_pid): OK$/"; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | if($^O ne "MSWin36") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | $c->after_detach(%pars); | |||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | $func->($c); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 1 | 1 | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub detach { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detach: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | ▶ | if($^O ne "MSWin32"){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | umask(0); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $SIG{'HUP'} = 'IGNORE'; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $child = fork(); | |||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if($child < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | Carp::croak "Fork failed ($!)"; | |||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if( $child ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | exit 0; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | close(STDIN); | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | POSIX::setsid || Carp::croak "Can't start a new session: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | return $$ | |||||||||||||||||||||||||||||||||||||||||
| 216 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | require Win32::Process; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $proc; | |||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | Win32::Process::Create( | |||||||||||||||||||||||||||||||||||||||||
| 220 | $proc, | ||||||||||||||||||||||||||||||||||||||||||
| 221 | qq{$^X}, | ||||||||||||||||||||||||||||||||||||||||||
| 222 | "perl $0 Win32_Daemon " | ||||||||||||||||||||||||||||||||||||||||||
| 223 | . ( | ||||||||||||||||||||||||||||||||||||||||||
| 224 | join " ", @ARGV | ||||||||||||||||||||||||||||||||||||||||||
| 225 | ), | ||||||||||||||||||||||||||||||||||||||||||
| 226 | 0, | ||||||||||||||||||||||||||||||||||||||||||
| 227 | Win32::Process->DETACHED_PROCESS, | ||||||||||||||||||||||||||||||||||||||||||
| 228 | ".", | ||||||||||||||||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | return $proc->GetProcessID | |||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | sub chroot { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chroot: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $dir = shift; | |||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | ▶ | $dir = File::Temp::tmpnam unless defined $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | mkdir $dir and $c->stash->{chroot_dir} = $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | chroot $dir; | |||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | ▶ | sub change_user { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
change_user: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | my $user = shift; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my $uid; | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if($user =~ /^\d+$/){ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $uid = $user; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $uid = getpwnam($user); | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | defined $uid or die "User \"$user\" do not exists"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | POSIX::setuid($uid); | |||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | ▶ | sub check_root { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
check_root: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | $< == 0; | |||||||||||||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | sub change_procname { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
change_procname: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | my $name = shift; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | ($name = $0) =~ s{^.*/}{} unless defined $name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $0 = $name; | |||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | sub get_pidfile_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_pidfile_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | my $file; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | if(not exists $c->stash->{pid_file}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ($file = $0) =~ s{^.*/}{}; | |||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | $file =~ s/\./_/g; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | $file = ".$file.pid"; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | $file = $c->path . "/$file"; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | $file = $c->stash->{pid_file}; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | $file | |||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub set_pidfile_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_pidfile_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | ▶ | $c->stash->{pid_file} = $file if defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | ▶ | sub write_pidfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_pidfile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | $c->set_pidfile_name($file); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $file = $c->get_pidfile_name; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | open my $PIDFILE, ">", $file; | |||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $ret = print {$PIDFILE} $$, $/; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | close $PIDFILE; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | return unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | $ret | |||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | sub read_pidfile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_pidfile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | $c->set_pidfile_name($file); | |||||||||||||||||||||||||||||||||||||||||
| 307 | 0 | $file = $c->get_pidfile_name; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 0 | ▶ | return unless -f $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 0 | open my $PIDFILE, "<", $file; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | my $ret = scalar <$PIDFILE>; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | close $PIDFILE; | |||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | chomp $ret; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | $ret | |||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | my $ret; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if($^O ne "MSWin32") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | $ret = $c->kill(15, $file); | |||||||||||||||||||||||||||||||||||||||||
| 323 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | require Win32::Process; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | $ret = Win32::Process::KillProcess($c->read_pidfile($file), 0); | |||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | $ret | |||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | ▶ | sub kill { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kill: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my $signal = shift; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | my $pid = $c->read_pidfile($file); | |||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | return unless defined $pid and $pid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $pid and $pid
MC/DC0%0/2
defined $pid and $pid
defined $pid
$pid
Truth table0%0/3
A and B
A= defined $pidB= $pid
| |||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | kill $signal => $pid; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | sub is_running { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_running: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | my $file = shift; | |||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | my $ret; | |||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if($^O ne "MSWin32") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $ret = $c->kill(0, $file); | |||||||||||||||||||||||||||||||||||||||||
| 347 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | require Win32::Process; | |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | $ret = Win32::Process::Open(my $obj, $c->read_pidfile($file), my $flags); | |||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $ret | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | sub signal_handlers { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
signal_handlers: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 0 | my $c = shift; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | my $funcs; | |||||||||||||||||||||||||||||||||||||||||
| 357 | my $count; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | if(ref $_[0] eq "HASH") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | $funcs = shift; | |||||||||||||||||||||||||||||||||||||||||
| 360 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | 0 | $funcs = { @_ }; | |||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | for my $sig (keys %$funcs) { | |||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | my $func = $funcs->{ $sig }; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | $SIG{ $sig } = sub{$func->($c)} and $count++; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | $count | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | } | |||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||
| 371 | 2 | 42 | |||||||||||||||||||||||||||||||||||||||||