From e3c62328abbd548bb0da42fdbad954b3ce4f7102 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 24 Jun 2013 16:34:38 +0200 Subject: simple fork and pidfile aptwebserver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forking only after being ready to accept clients avoids running races with the tests which sometimes failed on the first 'apt-get update' (or similar) with the previous background-start and hope for the best… The commit fixes also some oversight output-order changes in regards to Description-md5 and (I-M-S) race conditions in various tests. Git-Dch: Ignore --- test/interactive-helper/aptwebserver.cc | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test/interactive-helper/aptwebserver.cc') diff --git a/test/interactive-helper/aptwebserver.cc b/test/interactive-helper/aptwebserver.cc index 05b875673..a8d191d0e 100644 --- a/test/interactive-helper/aptwebserver.cc +++ b/test/interactive-helper/aptwebserver.cc @@ -387,6 +387,41 @@ int main(int const argc, const char * argv[]) return 2; } + FileFd pidfile; + if (_config->FindB("aptwebserver::fork", false) == true) + { + std::string const pidfilename = _config->Find("aptwebserver::pidfile", "aptwebserver.pid"); + int const pidfilefd = GetLock(pidfilename); + if (pidfilefd < 0 || pidfile.OpenDescriptor(pidfilefd, FileFd::WriteOnly) == false) + { + _error->Errno("aptwebserver", "Couldn't acquire lock on pidfile '%s'", pidfilename.c_str()); + _error->DumpErrors(std::cerr); + return 3; + } + + pid_t child = fork(); + if (child < 0) + { + _error->Errno("aptwebserver", "Forking failed"); + _error->DumpErrors(std::cerr); + return 4; + } + else if (child != 0) + { + // successfully forked: ready to serve! + std::string pidcontent; + strprintf(pidcontent, "%d", child); + pidfile.Write(pidcontent.c_str(), pidcontent.size()); + if (_error->PendingError() == true) + { + _error->DumpErrors(std::cerr); + return 5; + } + std::cout << "Successfully forked as " << child << std::endl; + return 0; + } + } + std::clog << "Serving ANY file on port: " << port << std::endl; listen(sock, 1); @@ -502,5 +537,7 @@ int main(int const argc, const char * argv[]) << " on socket " << sock << std::endl; close(client); } + pidfile.Close(); + return 0; } -- cgit v1.2.3