Skip to content

Commit

Permalink
Correct daemon handling for stop too
Browse files Browse the repository at this point in the history
  • Loading branch information
a-schild committed Oct 2, 2017
1 parent 59b2ddb commit e073179
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ for **update** istructions read the **update.txt** document
- v1.0 Initial public version
- v1.1 Added support for speed dial numbers
- v1.2 Added support for text search
- v1.3 Added support to correctly shutdown ldap daemon

(C) Aarboard AG, www.aarboard.ch, 2017

Expand Down
4 changes: 2 additions & 2 deletions install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Prerequisites:
- Install required packages for the ldap server
apt-get update
apt-get install libnet-server-perl libnet-ldap-server-perl libnet-daemon-perl libproc-daemon-perl
apt-get install libclass-dbi-mysql-perl libxml-xpath-perl
apt-get install libclass-dbi-mysql-perl libxml-xpath-perl libfile-pid-perl
apt-get install libfile-cache-perl libcache-cache-perl libdatetime-perl
- Link start script to /etc/init.d
ln -s -t /etc/init.d /var/local/aarldap/inno-ldap
Expand All @@ -39,7 +39,7 @@ Prerequisites:
- Make log directory
mkdir /var/log/innoldap
- Start the service
services inno-ldap start
service inno-ldap start

- Install the MySQL Database
apt-get install mysql-server
Expand Down
40 changes: 20 additions & 20 deletions ldap-server/inno-ldap
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ DESC="Innoldap Gateway"
NAME=inno-ldap.pl
DAEMON=/var/local/aarldap/$NAME
DAEMON_ARGS="--options args"
PIDFILE=/var/run/$NAME.pid
PIDFILE=/var/run/inno-ldap.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
Expand Down Expand Up @@ -62,25 +62,25 @@ do_start()
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
#/usr/bin/killall inno-ldap
#[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}

#
Expand Down
61 changes: 38 additions & 23 deletions ldap-server/inno-ldap.pl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ package Listener;
use InnoLdapServer;
use Data::Dumper;
use DateTime;
use File::Pid;
#use Scalar::Util qw/openhandle/;

my $debug = 0;
my $logpath= "/var/log/innoldap";
my $dt = DateTime->now;
my $logName= $dt->ymd . '_' . $dt->hms('-');

my $handler= 0;
my $continue = 1;
my $roothandler= 0;

sub process_request {
my $self = shift;
Expand All @@ -31,34 +34,15 @@ sub process_request {
my $peer_port = $sock->peerport();
logwarn("Connection accepted from $peer_address : $peer_port");

#print STDERR 'in = ('.openhandle($in).') '.Dumper($in);
#print STDERR 'out = ('.openhandle($out).') '.Dumper($out);
my $handler = InnoLdapServer->new($sock);
#my $handler = InnoLdapServer->new($in,$out);
while (1)
{
my $finished = $handler->handle;
return if $finished;
{
my $finished = $handler->handle;
return if $finished;
}
}


Proc::Daemon::Init;

my $continue = 1;
$SIG{TERM} = sub { $continue = 0 };

open(STDOUT, '>', "$logpath/inno-ldap.$logName.log") or die "Can't open stdout log";
open(STDERR, '>', "$logpath/inno-ldap.$logName.error.log") or die "Can't open error log";

while ($continue) {
package main;


Listener->run(port => 389);
}


sub logmsg {
print (scalar localtime() . " @_\n");
}
Expand All @@ -77,4 +61,35 @@ sub logdebug {
}
}

sub server_close {
logmsg("Server close called");
$continue= 0;
}

# Start daemon
Proc::Daemon::Init();

my $pidfile = File::Pid->new({file => "/var/run/inno-ldap.pid"});
if ($pidfile->running())
{
die "Already running";
}

$pidfile->write();

open(STDOUT, '>', "$logpath/inno-ldap.$logName.log") or die "Can't open stdout log";
select((select(STDOUT), $|=1)[0]); # make the log file "hot" - turn off buffering
open(STDERR, '>', "$logpath/inno-ldap.$logName.error.log") or die "Can't open error log";
select((select(STDERR), $|=1)[0]); # make the log file "hot" - turn off buffering

while ($continue) {
# package main;
$roothandler= Listener->run(
port => 389,
log_level => 4
);
}

$pidfile->remove();

1;
9 changes: 9 additions & 0 deletions upgrade.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
This new version adds support for speel dial functionality

Upgrade from 1.2 to 1.3 version:

- Install the perl pid library
apt-get libfile-pid-perl

To upgrade place these new files on the LinuxPA
- InnoLdapServer.pm (Don't forget to change login config)
- inno-ldap and inno-ldap.pl

Upgrade from initial (1.0) to the 1.1 version:

To upgrade place these new files on the LinuxPA
Expand Down

0 comments on commit e073179

Please sign in to comment.