Sat 23 Aug 2003 02:31:41 PM UTC, original submission:
Like gdomap and gnustep_sndd, gdnc should close any open file descriptors to properly daemonize.
Try something like that :
--- gnustep-base-1.7.2.orig/Tools/gdnc.m
+++ gnustep-base-1.7.2/Tools/gdnc.m
@@ -23,6 +23,7 @@
#include <stdio.h>
#include <unistd.h>
+#include <fcntl.h>
#ifdef _MINGW_
#include "process.h"
@@ -909,8 +910,10 @@
int
main(int argc, char** argv, char** env)
{
+ int desc;
GDNCServer *server;
NSString *str;
+ BOOL isDaemon = NO;
BOOL shouldFork = YES;
BOOL debugging = NO;
CREATE_AUTORELEASE_POOL(pool);
@@ -948,6 +951,7 @@
#else
if (shouldFork)
{
+ isDaemon = YES;
switch (fork())
{
case -1:
@@ -969,6 +973,43 @@
exit(EXIT_SUCCESS);
}
}
+
+ /*
+ * Ensure we don't have any open file descriptors which may refer
+ * to sockets bound to ports we may try to use.
+ *
+ * Use '/dev/null' for stdin and stdout. Assume stderr is ok.
+ */
+ for (desc = 0; desc < FD_SETSIZE; desc++)
+ {
+ if (isDaemon /|| (desc != 2)/)
+ {
+ (void)close(desc);
+ }
+ }
+ if (open("/dev/null", O_RDONLY) != 0)
+ {
+ NSLog(@"gdnc - failed to open stdin from /dev/null (%s)",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (open("/dev/null", O_WRONLY) != 1)
+ {
+ NSLog(@"gdnc - failed to open stdout from /dev/null (%s)",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (isDaemon && open("/dev/null", O_WRONLY) != 2)
+ {
+ NSLog(@"gdnc - failed to open stderr from /dev/null (%s)",
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+ if (0)
+ {
+ NSLog(@"gdnc - Closed descriptors");
+ }
+
#endif /* !MINGW */
{
-----------
P.S.
Is it better to start gdnc at boot time (as recommended by the GNUstep Unix Build Guide ) or at login time (as suggested by the gdnc manpage) ?
Eric Heintzmann and the Debian GNUstep maintainers
|