Hi all -
I've enhanced the daemonizing code in slpd_main.c - my changes are committed
in rev 1692. Basically, I moved the call to Daemonize up above all the
initialization so we don't open all our socket handles before daemonizing -
this is generally considered standard procedure.
I then added the loop that Robert wanted that closes the first 8k file
handles. A better way of doing this is to freopen the standard handles to
/dev/null so that someone attempting to write to the handle doesn't cause
flush to hang on shutdown. Luckily, we don't use printf in our production
code, so this isn't really an issue for slpd.
Finally, I moved the setuid code out of Daemonize into its own routine,
DropPrivileges, and places a call to that function where the call to
Daemonize used to be. The reason Daemonize was called so late was so we
could open our privileged sockets before we drop privs. Another standard
procedure is to daemonize early and drop privs as soon as we can, but no
sooner.
I've built and tested this code on Ubuntu 12.04. It's a *nix-only source
file, so I don't think these changes will have any effect on Windows, but
others should give it a try on other Unix platforms. I don't expect any
problems, but you never know.
Hope this helps,
John
From: Nick Wagner [mailto:ne...@wingedbeast.org]
Sent: Wednesday, October 17, 2012 9:45 AM
To: Robert Hegner
Cc: openslp-us...@lists.sourceforge.net; openslp-devel@lists.sourceforge.net
Subject: Re: [Openslp-users] slpd seems to hijack the port I'm using in my
application
Someone on the devel list with more experience working with linux daemons
will need to make this fix. And they should patch the debian start-stop
script as well as earlier explained in this email chain. We should do this
before the code freeze.
Robert, thanks for finding these issues!
--Nick
On Wed, Oct 17, 2012 at 10:30 AM, Robert Hegner <rheg...@hsr.ch> wrote:
Ok I found out what's wrong. And I would consider this as a bug in OpenSLP.
I'm a Linux newbie but The Linux Programming Interface by Michael
Kerrisk (a great book, by the way) helped me understand what's going on.
You can see in my first post that when slpd listens to the same port as
my application, it also uses the same file descriptor.
As I wrote earlier, I use system() to run the start/stop script of slpd.
According to Kerrisk's book, system() is typically implemented using a
combination of fork() (which copies all open file descriptors) and
exec() (which does not close any file descriptors). So slpd inherits my
open file descriptors when I start it from within my application.
In chapter 37 of his book, Kerrisk also describes the seven steps for
daemonizing a program. Step 6 is:
"Close all open file descriptors that the daemon has inherited from its
parent. (A daemon may need to keep certain inherited file descriptors
open, [...]"
And he also provides some example code showing how to accomplish this:
maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd == -1)
maxfd = BD_MAX_CLOSE; // 8192 in his example
for (fd = 0; fd < maxfd; fd++)
close(fd);
I had a look at the OpenSLP source code and I found that Daemonize() (in
slpd_main.c) does not close all open file descriptors (it only closes
descriptors 0 to 2 under some condition).
I tried to close all file descriptors in Daemonize() but it seemed to
cause some problems. I guess that slpd has already opened some of its
own sockets at that time, which are then being closed again.
But adding
for (i = 3; i < 8192; i++)
close(i);
at the beginning of main() solves all my problems :)
Something like this should be in Daemonize() and not at the beginning of
main(), but I guess someone who is familiar with the initialization code
of slpd should have a look at this.
I hope this change will make it into Release of version 2.0
Robert
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_sfd2d_oct
_______________________________________________
Openslp-devel mailing list
Openslp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openslp-devel