diff -Nru mongodb-src-r2.0.2.orig/db/cmdline.cpp mongodb-src-r2.0.2/db/cmdline.cpp --- mongodb-src-r2.0.2.orig/db/cmdline.cpp 2011-12-14 12:18:48.000000000 -0500 +++ mongodb-src-r2.0.2/db/cmdline.cpp 2012-01-15 01:15:01.985803346 -0500 @@ -263,11 +263,34 @@ cout.flush(); cerr.flush(); + int pipes[2]; + if (pipe(pipes) < 0) { + cout << "Can't create pipe while forking server process: " << strerror(errno) << endl; + ::exit(-1); + } + pid_t c = fork(); if ( c ) { + char buf[1]; + // parent; here, we close out the write side of the pipe and + // block on a single byte read from the final child to tell us + // when it is done + close(pipes[1]); + ssize_t bytes = read(pipes[0], buf, 1); + close(pipes[0]); + if (bytes != 1) { + // in this case, the child failed to start for some reason. + // we assume that the child wrote the error information, so + // all we have to do is exit with an error code + ::exit(-1); + } + _exit(0); } + // in the child, close out the read end of the pipe + close(pipes[0]); + if ( chdir("/") < 0 ) { cout << "Cant chdir() while forking server process: " << strerror(errno) << endl; ::exit(-1); @@ -301,6 +324,21 @@ setupCoreSignals(); setupSignals( true ); + + if ( params.count("pidfilepath")) { + writePidFile( params["pidfilepath"].as() ); + } + + ssize_t bytes = write(pipes[1], "a", 1); + close(pipes[1]); + if (bytes < 0) { + cout << "Can't write to the parent pipe: " << strerror(errno) << endl; + ::exit(-1); + } + else if (bytes != 1) { + cout << "Can't write to the parent pipe: unknown error" << endl; + ::exit(-1); + } } #endif @@ -311,7 +349,9 @@ initLogging( logpath , params.count( "logappend" ) ); } - if ( params.count("pidfilepath")) { + // if this was a forking configuration, the pidfile was written above; + // if not, we have to write it here + if ( !params.count("fork") && params.count("pidfilepath")) { writePidFile( params["pidfilepath"].as() ); }