Redhat RHEL5下的守护进程网络进程(perl)拒绝网络连接

发布于 2024-10-31 02:35:17 字数 4668 浏览 0 评论 0原文

我编写了一个使用 Perl POE 框架来实现 json Web 服务的程序。 到目前为止,一切都很好。我在 debian 系统下运行该应用程序没有任何问题。但是当我在 RHEL5 下运行我的应用程序时,网络连接被拒绝。我没有 setuid,因此该服务以 root 身份运行,并且端口 (9991) 超出了服务端口范围。

我的解决方法是使用 nohup $CMD & 将应用程序作为非守护进程启动。这比愚蠢还要多。但我完全不知道

我的 init.d 脚本

DEBIAN:

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          jobserver
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start/stop jobserver
### END INIT INFO

APPLICATION_CONFIG="/etc/jobserver/jobserver.conf"

test -f $APPLICATION_CONFIG && . $APPLICATION_CONFIG

set -e
if [ ! -x $APPLICATION_PATH ] ; then
    echo "No jobserver installed"
    exit 0
fi

#load init.d helper functions
. /lib/lsb/init-functions

PIDFILE=$APPLICATION_PIDFILE
CONF=$APPLICATION_CONFIG
DAEMON=$APPLICATION_DAEMON
PARAMETER="--configuration $CONF --daemon"

if [ -z "$PIDFILE" ] ; then
    echo "ERROR: APPLICATION_PIDFILE needs to be defined in application config" >&2
    exit 2
fi

jobserver_start() {
    #log_daemon_msg "Starting Jobserver Daemon"
    log_success_msg "Starting Jobserver Daemon"
    start-stop-daemon --start --quiet --oknodo --make-pidfile --pidfile "$PIDFILE" --exec "$DAEMON" -- $PARAMETER
    #log_end_msg $?
}

jobserver_stop() {
    log_success_msg "Stopping Jobserver Daemon"
    start-stop-daemon --stop --quiet --oknodo --pidfile "${PIDFILE}"
    rm -f "${PIDFILE}"
    #log_end_msg $?
}

case $1 in
    start)
        jobserver_start
    ;;
    stop)
        jobserver_stop
    ;;
    restart)
        jobserver_stop
        jobserver_start
    ;;
    *)
        log_success_msg "Usage: /etc/init.d/jobserver {start|stop|restart}"
        exit 1
    ;;
esac

exit $?;

REDHAT:

# Source function library.
. /etc/rc.d/init.d/functions

APPLICATION_CONFIG="/etc/jobserver/jobserver.conf"
test -f $APPLICATION_CONFIG && . $APPLICATION_CONFIG

PIDFILE=$APPLICATION_PIDFILE
CONF=$APPLICATION_CONFIG
DAEMON=$APPLICATION_DAEMON
PARAMETER="--configuration $CONF --daemon"
RETVAL=0

if [ -z "$PIDFILE" ] ; then
    echo "ERROR: APPLICATION_PIDFILE needs to be defined in application config" >&2
    exit 2
fi

jobserver_start() {
    echo -n $"Starting jobserver daemon: "
    daemon --pidfile=$PIDFILE $DAEMON $PARAMETER
    RETVAL=$?

    if [ $RETVAL -ne 0 ]; then
        failure;
    fi;
    echo
    return $RETVAL
}

jobserver_stop() {
    echo -n $"Stopping jobserver daemon: "
    if [ ! -f $PIDFILE ]; then
        echo -n $"Jobserver daemon is not running: ";
    else
        killproc -p $PIDFILE
        RETVAL=$?
    fi
    echo
    return $RETVAL;
}

case $1 in
    start)
        jobserver_start
    ;;
    stop)
        jobserver_stop
    ;;
    restart)
        jobserver_stop
        jobserver_start
    ;;
    *)
        echo "Usage: /etc/init.d/jobserver {start|stop|restart}"
        exit 1
    ;;
esac

exit $?;

Daemonprocess:

sub daemonize {
    # start a new child process
    if (fork()) {
        exit(0);
    }
    # become process group leader
    unless (POSIX::setsid) {
        die("POSIX setsid failed: $!");
    }
    # change to root dir
    chdir("/");
    foreach (0 .. (POSIX::sysconf(&POSIX::_SC_OPEN_MAX) || 1024)) {
        POSIX::close($_);
    }
    # allow only user based io
    umask(077);

    # reopen pipes
    open(STDIN,  "<", "/dev/null");
    open(STDOUT, ">", "/dev/null");
    open(STDERR, ">", "/dev/null");

    # Advisory. Fork one more time; this is not "necessary" for most toolserver
    # daemons but it is a best practice as there are situations where
    # forking twice is needed to avoid zombies. A second fork also
    # prevents the daemon from ever re-acquiring a terminal, by making
    # the main daemon process not be the process group leader
    if (fork()) {
        exit(0);
    }

    # write pidfile
    my $pidfile = "/var/run/jobserverd.pid";
    open(PIDFILE, ">$pidfile");
    print(PIDFILE "$$");
    close(PIDFILE);
}

Serverfront:

sub listen {
    my $self = shift;

    logInfo("Starting webservice. Listening to port ".$self->{'_port'}, 1);

    # Spawn the webservice
    POE::Component::Server::HTTP->new (
        Port           => $self->{'_port'},
        ContentHandler => {
            "/json/" => \&dispatchJSONService
        },
        Headers        => {
            "Server" => "Perl JobServer version ".$self->{'_version'}
        },
    );

    $poe_kernel->run();
}

I wrote a program that is using the Perl POE Framework to realize a json webservice.
So far so good. I have no problems running that application under debian systems. But when i run my application under RHEL5 the network connection is refused. I have no setuid so the service is running as root and the port (9991) is out of the serviceport-range.

My workaround ist to start the application as nondaemon with nohup $CMD &. Thats more than stupid. But I've absolute no idea

my init.d script

DEBIAN:

#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          jobserver
# Required-Start:    $local_fs $network $syslog
# Required-Stop:     $local_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start/stop jobserver
### END INIT INFO

APPLICATION_CONFIG="/etc/jobserver/jobserver.conf"

test -f $APPLICATION_CONFIG && . $APPLICATION_CONFIG

set -e
if [ ! -x $APPLICATION_PATH ] ; then
    echo "No jobserver installed"
    exit 0
fi

#load init.d helper functions
. /lib/lsb/init-functions

PIDFILE=$APPLICATION_PIDFILE
CONF=$APPLICATION_CONFIG
DAEMON=$APPLICATION_DAEMON
PARAMETER="--configuration $CONF --daemon"

if [ -z "$PIDFILE" ] ; then
    echo "ERROR: APPLICATION_PIDFILE needs to be defined in application config" >&2
    exit 2
fi

jobserver_start() {
    #log_daemon_msg "Starting Jobserver Daemon"
    log_success_msg "Starting Jobserver Daemon"
    start-stop-daemon --start --quiet --oknodo --make-pidfile --pidfile "$PIDFILE" --exec "$DAEMON" -- $PARAMETER
    #log_end_msg $?
}

jobserver_stop() {
    log_success_msg "Stopping Jobserver Daemon"
    start-stop-daemon --stop --quiet --oknodo --pidfile "${PIDFILE}"
    rm -f "${PIDFILE}"
    #log_end_msg $?
}

case $1 in
    start)
        jobserver_start
    ;;
    stop)
        jobserver_stop
    ;;
    restart)
        jobserver_stop
        jobserver_start
    ;;
    *)
        log_success_msg "Usage: /etc/init.d/jobserver {start|stop|restart}"
        exit 1
    ;;
esac

exit $?;

REDHAT:

# Source function library.
. /etc/rc.d/init.d/functions

APPLICATION_CONFIG="/etc/jobserver/jobserver.conf"
test -f $APPLICATION_CONFIG && . $APPLICATION_CONFIG

PIDFILE=$APPLICATION_PIDFILE
CONF=$APPLICATION_CONFIG
DAEMON=$APPLICATION_DAEMON
PARAMETER="--configuration $CONF --daemon"
RETVAL=0

if [ -z "$PIDFILE" ] ; then
    echo "ERROR: APPLICATION_PIDFILE needs to be defined in application config" >&2
    exit 2
fi

jobserver_start() {
    echo -n $"Starting jobserver daemon: "
    daemon --pidfile=$PIDFILE $DAEMON $PARAMETER
    RETVAL=$?

    if [ $RETVAL -ne 0 ]; then
        failure;
    fi;
    echo
    return $RETVAL
}

jobserver_stop() {
    echo -n $"Stopping jobserver daemon: "
    if [ ! -f $PIDFILE ]; then
        echo -n $"Jobserver daemon is not running: ";
    else
        killproc -p $PIDFILE
        RETVAL=$?
    fi
    echo
    return $RETVAL;
}

case $1 in
    start)
        jobserver_start
    ;;
    stop)
        jobserver_stop
    ;;
    restart)
        jobserver_stop
        jobserver_start
    ;;
    *)
        echo "Usage: /etc/init.d/jobserver {start|stop|restart}"
        exit 1
    ;;
esac

exit $?;

Daemonprocess:

sub daemonize {
    # start a new child process
    if (fork()) {
        exit(0);
    }
    # become process group leader
    unless (POSIX::setsid) {
        die("POSIX setsid failed: $!");
    }
    # change to root dir
    chdir("/");
    foreach (0 .. (POSIX::sysconf(&POSIX::_SC_OPEN_MAX) || 1024)) {
        POSIX::close($_);
    }
    # allow only user based io
    umask(077);

    # reopen pipes
    open(STDIN,  "<", "/dev/null");
    open(STDOUT, ">", "/dev/null");
    open(STDERR, ">", "/dev/null");

    # Advisory. Fork one more time; this is not "necessary" for most toolserver
    # daemons but it is a best practice as there are situations where
    # forking twice is needed to avoid zombies. A second fork also
    # prevents the daemon from ever re-acquiring a terminal, by making
    # the main daemon process not be the process group leader
    if (fork()) {
        exit(0);
    }

    # write pidfile
    my $pidfile = "/var/run/jobserverd.pid";
    open(PIDFILE, ">$pidfile");
    print(PIDFILE "$");
    close(PIDFILE);
}

Serverfront:

sub listen {
    my $self = shift;

    logInfo("Starting webservice. Listening to port ".$self->{'_port'}, 1);

    # Spawn the webservice
    POE::Component::Server::HTTP->new (
        Port           => $self->{'_port'},
        ContentHandler => {
            "/json/" => \&dispatchJSONService
        },
        Headers        => {
            "Server" => "Perl JobServer version ".$self->{'_version'}
        },
    );

    $poe_kernel->run();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-11-07 02:35:17

问题解决了。问题是双叉。感谢大家的阅读。永远不要相信建议! ;)

Problem solved. The Problem was the double fork. Thanks to all for reading. Never trust advisories! ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文