shill: Daemon()ize. For real.

Without --forgeground, shill will now daemonize by default. This is
needed to make it work in way we such that we can reuse the flimflam
upstart job (which has an "expect fork") and start flimflam or shill
depending on the presence of a magic file.

BUG=chromium-os:23492
TEST=manually tested that shill deamonizes, and that it works properly
     with the upstart change (https://gerrit.chromium.org/gerrit/12176)

Change-Id: I5de5d56178d7d1c2e15702f3be91206debf44aa9
Reviewed-on: https://gerrit.chromium.org/gerrit/12177
Reviewed-by: Paul Stewart <pstew@chromium.org>
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Ready: Gaurav Shah <gauravsh@chromium.org>
Tested-by: Gaurav Shah <gauravsh@chromium.org>
diff --git a/shill_main.cc b/shill_main.cc
index c3af22b..71ea271 100644
--- a/shill_main.cc
+++ b/shill_main.cc
@@ -3,6 +3,8 @@
 // found in the LICENSE file.
 
 #include <time.h>
+#include <unistd.h>
+
 #include <string>
 #include <vector>
 
@@ -93,6 +95,10 @@
   CommandLine::Init(argc, argv);
   CommandLine* cl = CommandLine::ForCurrentProcess();
 
+  const int nochdir = 0, noclose = 0;
+  if (!cl->HasSwitch(switches::kForeground))
+    PLOG_IF(FATAL, daemon(nochdir, noclose) == -1 ) << "Failed to daemonize";
+
   // If the help flag is set, force log in foreground.
   SetupLogging(cl->HasSwitch(switches::kForeground) ||
                cl->HasSwitch(switches::kHelp));