1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "tools/android/common/daemon.h"
10 #include <sys/types.h>
13 #include "base/command_line.h"
14 #include "base/logging.h"
18 const char kNoSpawnDaemon
[] = "D";
20 int g_exit_status
= 0;
22 void Exit(int unused
) {
26 void CloseFileDescriptor(int fd
) {
27 int old_errno
= errno
;
36 bool HasHelpSwitch(const CommandLine
& command_line
) {
37 return command_line
.HasSwitch("h") || command_line
.HasSwitch("help");
40 bool HasNoSpawnDaemonSwitch(const CommandLine
& command_line
) {
41 return command_line
.HasSwitch(kNoSpawnDaemon
);
44 void ShowHelp(const char* program
,
45 const char* extra_title
,
46 const char* extra_descriptions
) {
47 printf("Usage: %s [-%s] %s\n"
48 " -%s stops from spawning a daemon process\n%s",
49 program
, kNoSpawnDaemon
, extra_title
, kNoSpawnDaemon
,
53 void SpawnDaemon(int exit_status
) {
54 g_exit_status
= exit_status
;
55 signal(SIGUSR1
, Exit
);
59 sleep(10); // Wait for the child process to finish setsid().
64 setsid(); // Detach the child process from its parent.
65 kill(getppid(), SIGUSR1
); // Inform the parent process to exit.
67 // Close the standard input and outputs, otherwise the process may block
68 // adbd when the shell exits.
69 // Comment out these lines if you want to see outputs for debugging.
70 CloseFileDescriptor(0);
71 CloseFileDescriptor(1);
72 CloseFileDescriptor(2);