This source file includes following definitions.
- Exit
- CloseFileDescriptor
- HasHelpSwitch
- HasNoSpawnDaemonSwitch
- ShowHelp
- SpawnDaemon
#include "tools/android/common/daemon.h"
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "base/command_line.h"
#include "base/logging.h"
namespace {
const char kNoSpawnDaemon[] = "D";
int g_exit_status = 0;
void Exit(int unused) {
_exit(g_exit_status);
}
void CloseFileDescriptor(int fd) {
int old_errno = errno;
close(fd);
errno = old_errno;
}
}
namespace tools {
bool HasHelpSwitch(const CommandLine& command_line) {
return command_line.HasSwitch("h") || command_line.HasSwitch("help");
}
bool HasNoSpawnDaemonSwitch(const CommandLine& command_line) {
return command_line.HasSwitch(kNoSpawnDaemon);
}
void ShowHelp(const char* program,
const char* extra_title,
const char* extra_descriptions) {
printf("Usage: %s [-%s] %s\n"
" -%s stops from spawning a daemon process\n%s",
program, kNoSpawnDaemon, extra_title, kNoSpawnDaemon,
extra_descriptions);
}
void SpawnDaemon(int exit_status) {
g_exit_status = exit_status;
signal(SIGUSR1, Exit);
if (fork()) {
sleep(10);
NOTREACHED();
}
setsid();
kill(getppid(), SIGUSR1);
CloseFileDescriptor(0);
CloseFileDescriptor(1);
CloseFileDescriptor(2);
}
}