Re: [Openslp-users] slpd seems to hijack the port I'm using in my application
Brought to you by:
jcalcote
From: Nick W. <ne...@wi...> - 2012-10-17 15:45:07
|
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 <rh...@hs...> 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 > > |