Beekeeper
view release on metacpan or search on metacpan
lib/Beekeeper/Logger.pm view on Meta::CPAN
my $file = $self->{service} . '.log';
($user) = ($user =~ m/(\w+)/); # untaint
$self->{log_file} = (-d "$dir/$user") ? "$dir/$user/$file" : "$dir/$file";
}
unless ($self->{foreground}) {
my $log_file = $self->{log_file};
# If running as root temporarily restore uid and gid to allow opening
local $> = $< if ($< == 0);
local $) = $( if ($( == 0);
if (open(my $fh, '>>', $log_file)) {
# Send STDERR and STDOUT to log file
open(STDERR, '>&', $fh) or die "Can't redirect STDERR to $log_file: $!";
open(STDOUT, '>&', $fh) or die "Can't redirect STDOUT to $log_file: $!";
}
else {
# Probably no permissions to open the log file
lib/Beekeeper/WorkerPool/Daemon.pm view on Meta::CPAN
# use the module Unix::SetUser which allows to do that (or think about
# using 'su' to start your daemon as a non root user)
# Only root can swith user
return unless ($> == 0);
my $as_user = $self->{options}->{user} || "nobody";
my $as_group = $self->{options}->{group} || "nogroup";
my $uid = getpwnam($as_user);
my $gid = getgrnam($as_group);
unless (defined $uid) {
die("Cannot switch to a non existent user '$as_user'");
}
unless (defined $gid) {
die("Cannot switch to a non existent group '$as_group'");
}
unless ($uid > 0) {
die("Cannot run daemon as root");
}
# Change the effective gid
$) = $gid or die("Cannot switch to group '$as_group': $!");
# Change the effective uid
$> = $uid or die("Cannot switch to user '$as_user': $!");
}
sub restore_effective_user {
my $self = shift;
# Only root can swith user
return unless ($< == 0);
# Restore the effective uid to the real uid
$> = $<;
# Restore the effective gid to the real gid
$) = $(;
}
#------------------------------------------------------------------------------
# PIDFILE HANDLING
sub pid_file {
my $self = shift;
( run in 0.602 second using v1.01-cache-2.11-cpan-5735350b133 )