This source file includes following definitions.
- SIGCHLDHandler
- GracefulShutdownHandler
- SIGHUPHandler
- SIGINTHandler
- SIGTERMHandler
- ExitWhenPossibleOnUIThread
- Observe
- ShutdownFDReadError
- ShutdownFDClosedError
- ExitPosted
- ThreadMain
- SetFileDescriptorLimit
- PreEarlyInitialization
- PostMainMessageLoopStart
- ShowMissingLocaleMessageBox
#include "chrome/browser/chrome_browser_main_posix.h"
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <sys/resource.h>
#include <unistd.h>
#include <string>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_service.h"
#if defined(TOOLKIT_GTK)
#include "chrome/browser/ui/gtk/chrome_browser_main_extra_parts_gtk.h"
#if defined(ENABLE_PRINTING)
#include "chrome/browser/printing/print_dialog_gtk.h"
#include "chrome/browser/printing/printing_gtk_util.h"
#endif
#endif
using content::BrowserThread;
namespace {
void SIGCHLDHandler(int signal) {
}
pid_t g_pipe_pid = -1;
int g_shutdown_pipe_write_fd = -1;
int g_shutdown_pipe_read_fd = -1;
void GracefulShutdownHandler(int signal) {
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_DFL;
RAW_CHECK(sigaction(signal, &action, NULL) == 0);
RAW_CHECK(g_pipe_pid == getpid());
RAW_CHECK(g_shutdown_pipe_write_fd != -1);
RAW_CHECK(g_shutdown_pipe_read_fd != -1);
size_t bytes_written = 0;
do {
int rv = HANDLE_EINTR(
write(g_shutdown_pipe_write_fd,
reinterpret_cast<const char*>(&signal) + bytes_written,
sizeof(signal) - bytes_written));
RAW_CHECK(rv >= 0);
bytes_written += rv;
} while (bytes_written < sizeof(signal));
}
void SIGHUPHandler(int signal) {
RAW_CHECK(signal == SIGHUP);
GracefulShutdownHandler(signal);
}
void SIGINTHandler(int signal) {
RAW_CHECK(signal == SIGINT);
GracefulShutdownHandler(signal);
}
void SIGTERMHandler(int signal) {
RAW_CHECK(signal == SIGTERM);
GracefulShutdownHandler(signal);
}
class ExitHandler : public content::NotificationObserver {
public:
static void ExitWhenPossibleOnUIThread();
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
private:
ExitHandler();
virtual ~ExitHandler();
static void Exit();
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ExitHandler);
};
void ExitHandler::ExitWhenPossibleOnUIThread() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (SessionRestore::IsRestoringSynchronously()) {
new ExitHandler();
} else {
Exit();
}
}
void ExitHandler::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (!SessionRestore::IsRestoringSynchronously()) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&ExitHandler::Exit));
delete this;
}
}
ExitHandler::ExitHandler() {
registrar_.Add(
this, chrome::NOTIFICATION_SESSION_RESTORE_DONE,
content::NotificationService::AllBrowserContextsAndSources());
}
ExitHandler::~ExitHandler() {
}
void ExitHandler::Exit() {
#if defined(OS_CHROMEOS)
chrome::ExitCleanly();
#else
chrome::AttemptExit();
#endif
}
class ShutdownDetector : public base::PlatformThread::Delegate {
public:
explicit ShutdownDetector(int shutdown_fd);
virtual void ThreadMain() OVERRIDE;
private:
const int shutdown_fd_;
DISALLOW_COPY_AND_ASSIGN(ShutdownDetector);
};
ShutdownDetector::ShutdownDetector(int shutdown_fd)
: shutdown_fd_(shutdown_fd) {
CHECK_NE(shutdown_fd_, -1);
}
NOINLINE void ShutdownFDReadError() {
asm("");
sleep(UINT_MAX);
}
NOINLINE void ShutdownFDClosedError() {
asm("");
sleep(UINT_MAX);
}
NOINLINE void ExitPosted() {
asm("");
sleep(UINT_MAX);
}
void ShutdownDetector::ThreadMain() {
base::PlatformThread::SetName("CrShutdownDetector");
int signal;
size_t bytes_read = 0;
ssize_t ret;
do {
ret = HANDLE_EINTR(
read(shutdown_fd_,
reinterpret_cast<char*>(&signal) + bytes_read,
sizeof(signal) - bytes_read));
if (ret < 0) {
NOTREACHED() << "Unexpected error: " << strerror(errno);
ShutdownFDReadError();
break;
} else if (ret == 0) {
NOTREACHED() << "Unexpected closure of shutdown pipe.";
ShutdownFDClosedError();
break;
}
bytes_read += ret;
} while (bytes_read < sizeof(signal));
VLOG(1) << "Handling shutdown for signal " << signal << ".";
base::Closure task = base::Bind(&ExitHandler::ExitWhenPossibleOnUIThread);
if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, task)) {
RAW_LOG(WARNING, "No UI thread, exiting ungracefully.");
kill(getpid(), signal);
sleep(3);
RAW_LOG(WARNING, "Still here, exiting really ungracefully.");
_exit(signal | (1 << 7));
}
ExitPosted();
}
void SetFileDescriptorLimit(unsigned int max_descriptors) {
struct rlimit limits;
if (getrlimit(RLIMIT_NOFILE, &limits) == 0) {
unsigned int new_limit = max_descriptors;
if (limits.rlim_max > 0 && limits.rlim_max < max_descriptors) {
new_limit = limits.rlim_max;
}
limits.rlim_cur = new_limit;
if (setrlimit(RLIMIT_NOFILE, &limits) != 0) {
PLOG(INFO) << "Failed to set file descriptor limit";
}
} else {
PLOG(INFO) << "Failed to get file descriptor limit";
}
}
}
ChromeBrowserMainPartsPosix::ChromeBrowserMainPartsPosix(
const content::MainFunctionParams& parameters)
: ChromeBrowserMainParts(parameters) {
}
void ChromeBrowserMainPartsPosix::PreEarlyInitialization() {
ChromeBrowserMainParts::PreEarlyInitialization();
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIGCHLDHandler;
CHECK(sigaction(SIGCHLD, &action, NULL) == 0);
const std::string fd_limit_string =
parsed_command_line().GetSwitchValueASCII(
switches::kFileDescriptorLimit);
int fd_limit = 0;
if (!fd_limit_string.empty()) {
base::StringToInt(fd_limit_string, &fd_limit);
}
#if defined(OS_MACOSX)
if (fd_limit == 0) {
fd_limit = 1024;
}
#endif
if (fd_limit > 0)
SetFileDescriptorLimit(fd_limit);
}
void ChromeBrowserMainPartsPosix::PostMainMessageLoopStart() {
ChromeBrowserMainParts::PostMainMessageLoopStart();
int pipefd[2];
int ret = pipe(pipefd);
if (ret < 0) {
PLOG(DFATAL) << "Failed to create pipe";
} else {
g_pipe_pid = getpid();
g_shutdown_pipe_read_fd = pipefd[0];
g_shutdown_pipe_write_fd = pipefd[1];
#if !defined(ADDRESS_SANITIZER) && !defined(KEEP_SHADOW_STACKS)
const size_t kShutdownDetectorThreadStackSize = PTHREAD_STACK_MIN * 2;
#else
const size_t kShutdownDetectorThreadStackSize = PTHREAD_STACK_MIN * 4;
#endif
if (!base::PlatformThread::CreateNonJoinable(
kShutdownDetectorThreadStackSize,
new ShutdownDetector(g_shutdown_pipe_read_fd))) {
LOG(DFATAL) << "Failed to create shutdown detector task.";
}
}
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIGTERMHandler;
CHECK(sigaction(SIGTERM, &action, NULL) == 0);
action.sa_handler = SIGINTHandler;
CHECK(sigaction(SIGINT, &action, NULL) == 0);
action.sa_handler = SIGHUPHandler;
CHECK(sigaction(SIGHUP, &action, NULL) == 0);
#if defined(TOOLKIT_GTK) && defined(ENABLE_PRINTING)
printing::PrintingContextLinux::SetCreatePrintDialogFunction(
&PrintDialogGtk::CreatePrintDialog);
printing::PrintingContextLinux::SetPdfPaperSizeFunction(
&GetPdfPaperSizeDeviceUnitsGtk);
#endif
}
void ChromeBrowserMainPartsPosix::ShowMissingLocaleMessageBox() {
#if defined(OS_CHROMEOS)
NOTREACHED();
#elif defined(OS_MACOSX)
NOTREACHED();
#elif defined(TOOLKIT_GTK)
ChromeBrowserMainExtraPartsGtk::ShowMessageBox(
chrome_browser::kMissingLocaleDataMessage);
#elif defined(USE_AURA)
NOTREACHED();
#else
#error "Need MessageBox implementation."
#endif
}