cScm Configuration Daemon

cScm – is a tool to convert SCM configuration files into binary format and store its in shared memory for reading by cSvn-ui and cGit-ui CGI scripts

12 Commits   0 Branches   1 Tag
Index: daemon.c
===================================================================
--- daemon.c	(nonexistent)
+++ daemon.c	(revision 5)
@@ -0,0 +1,39 @@
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <fcntl.h>
+#include <paths.h>
+#include <unistd.h>
+
+#include <daemon.h>
+
+int daemon( int nochdir, int noclose )
+{
+  int fd;
+
+  switch( fork() )
+  {
+    case -1:
+      return( -1 );
+    case 0:
+      break;
+    default:
+      _exit( 0 ); /* direct use kernel exit */
+  }
+
+  if( setsid() == -1 ) return( -1 );
+  if( !nochdir ) chdir( "/" );
+  if( noclose ) return( 0 );
+
+  fd = open( _PATH_DEVNULL, O_RDWR, 0 );
+  if( fd != -1 )
+  {
+    dup2( fd, STDIN_FILENO );
+    dup2( fd, STDOUT_FILENO );
+    dup2( fd, STDERR_FILENO );
+    if( fd > 2 ) close( fd );
+  }
+  return( 0 );
+}