[sr-dev] Patches to get proper status code in daemonize mode

marius zbihlei marius.zbihlei at 1and1.ro
Mon Apr 19 13:18:10 CEST 2010


Iñaki Baz Castillo wrote:
> Hi, I've submitted some patches to the SR tracker in order to improve
> something that I consider buggy (and for sure it is):
>
>   http://sip-router.org/tracker/index.php?do=details&task_id=66
>
>
> As explained in the tracker, in daemonize mode kamailio doesn't return
> the proper status code. If for example the listen address is not local
> or a DB connection uses an invalid password, then the command
> "kamailio" would return 0 (OK) anyway (even if it fails to start).
> This occurs because all those checking (socket, DB connections...) are
> performed *after* invoking daemonize() so the parent process (which
> could be invoked by "/etc/init.d/kamailio start") returns 0 knowing
> nothing about those errors. In fact the only checking done before
> daemonizing is the config file parsing.
>
> The patches I've upload to the tracker improve/fix it by doing the following:
>
> - 'daemonize()' function doesn't exit the parent process, instead the
> parent process gets blocked in 'main()' function reading a pipe.
> - When the main process initializes the modules, sockets and so, it
> writes into the pipe so the parent process exists with 0.
> - If the main process fails to start then the pipe write fd is closed
> with no more processes writting into it so the parent process detects
> it and exists with -1 error.
>
> I've checked it and works ok. You can try to set a wrong MySQL
> password or a wrong listen address and run kamailio in daemonize mode.
> Check the exit status so you will get error code rather than 0 (as
> occurs without these changes).
>
> This is strongly required when working with HeartBeat (and any other
> software managing daemons) because HeartBeat relies on the init script
> returned status codes.
> Imagine for example that a MySQL password is changed. For any reason
> HeartBeat decides to change the service to nodo-2. This is what would
> happen without the proposed changes:
> - HeartBeat invokes "/etc/init.d/kamailio start".
> - It returns 0 even if it fails to start due to MySQL password.
> - HeartBeat  invokes periodically "/etc/init.d/kamailio monitor"
> (similar to "status") which would return 1 (it's not running).
> - Then HeartBeat tries to start it again. And of course, it receives 0 again.
> - Loop...
>
> This is, a init script return codes are *important* and current
> SR/Kamailio doesn't do it well as it fails in daemonize mode.
>
> I will use these patches in production very soon as I'm building some
> platforms of Kamailio+HeartBeat and this is really required. If
> somebody could review than patches it should be great.
>
> Regards.
>
>   
Hello Iñaki ,

I had a look over the patches and they look fine. Of course I think one 
of the core developers should have a look also.

I suggest one thing: Instead of a read() from the read end of the pipe, 
can we use a select()/poll() so we can have timeouts and  prevent 
blocking. For example does it make sense to say that if the child 
process doesn't write something to the pipe in let's  say 1 minute, this 
means that it is blocked somewhere and the main process should exit with 
error (thus the init.d script should return != 0) ?!

Marius








More information about the sr-dev mailing list