Daemonise

 view release on metacpan or  search on metacpan

examples/dungeonkeeper  view on Meta::CPAN

sub start {
    my (@daemons) = @_;

    # TODO: handle starting of individual bunnies

    $d->is_worker(1);

    # create PID dir and ensure correct owner/group/permissions
    mkdir($d->pid_dir, 0755) unless (-d $d->pid_dir);
    my (undef, undef, $uid) = getpwnam($d->user);
    my (undef, undef, $gid) = getgrnam($d->group);
    chown($uid, $gid, $d->pid_dir) if ($uid or $gid);

    die("not a directory: " . $d->bin_dir) unless (-d $d->bin_dir);

    $d->start(\&main);

    return;
}

sub stop {
    my (@daemons) = @_;

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

    isa => 'Int',
);


has 'group' => (
    is      => 'rw',
    default => sub { 'root' },
);


has 'gid' => (is => 'rw');


has 'pid_file' => (
    is        => 'rw',
    predicate => 'has_pid_file',
);


has 'running' => (
    is        => 'rw',

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

    if ($self->foreground) {
        $self->running($$);
        $self->log('staying in foreground');
        return;
    }

    $self->phase('starting');
    $self->check_pid_file if $self->has_pid_file;

    $self->uid($self->_get_uid);
    $self->gid($self->_get_gid);
    $self->gid((split /\s+/, $self->gid)[0]);

    # turn off logging to STDOUT when running in background
    $self->print_log(0);

    my $pid = $self->async;

    ### parent process should do the pid file and exit
    if ($pid) {
        $pid && exit;
        ### child process will continue on
    }
    else {
        $self->_create_pid_file if $self->has_pid_file;

        ### make sure we can remove the file later
        chown($self->uid, $self->gid, $self->pid_file)
            if $self->has_pid_file;

        ### become another user and group
        $self->_set_user;

        ### close all input/output and separate
        ### from the parent process group
        open(STDIN, '<', '/dev/null')
            or die "Can't open STDIN from /dev/null: [$!]";

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

    }
    else {
        $uid = getpwnam($self->user);
    }

    die 'No such user "' . $self->user . '"' unless defined $uid;

    return $uid;
}

sub _get_gid {
    my ($self) = @_;

    my @gid = ();
    foreach my $group (split(/[, ]+/, join(" ", $self->group))) {
        if ($group =~ /^\d+$/) {
            push @gid, $group;
        }
        else {
            my $id = getgrnam($group);
            die "No such group \"$group\"" unless defined $id;
            push @gid, $id;
        }
    }

    die "No group found in arguments." unless @gid;

    return join(" ", $gid[0], @gid);
}

sub _create_pid_file {
    my ($self) = @_;

    # child should also know its PID
    $self->running($$);

    ### see if the pid_file is already there
    $self->check_pid_file;

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN


    die "Pid_file \"" . $self->pid_file . "\" not created.\n"
        unless -e $self->pid_file;

    return 1;
}

sub _set_user {
    my ($self) = @_;

    $self->_set_gid || return;
    $self->_set_uid || return;

    return 1;
}

sub _set_uid {
    my ($self) = @_;

    my $uid = $self->_get_uid;

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

        # try again - needed by some 5.8.0 linux systems (rt #13450)
        local $< = local $> = $uid;
        if ($< != $uid) {
            die "Couldn't become uid \"$uid\": $!\n";
        }
    }

    return 1;
}

sub _set_gid {
    my ($self) = @_;

    my $gids = $self->_get_gid;
    my $gid = (split /\s+/, $gids)[0];

    # store all the gids - this is really sort of optional
    eval { local $) = $gids };

    POSIX::setgid($gid);

    # look for any valid gid in the list
    if (!grep { $gid == $_ } split /\s+/, $() {
        die "Couldn't become gid \"$gid\": $!\n";
    }

    return 1;
}

1;

__END__

=pod

lib/Daemonise/Plugin/Daemon.pm  view on Meta::CPAN

    }

=head1 ATTRIBUTES

=head2 user

=head2 uid

=head2 group

=head2 gid

=head2 pid_file

=head2 running

=head2 phase

=head2 logfile

=head2 foreground



( run in 0.809 second using v1.01-cache-2.11-cpan-5735350b133 )