From 24ef8601c203a28b85a5e9168e5bada713e6a8f1 Mon Sep 17 00:00:00 2001 From: Adrian Salido Date: Tue, 20 Dec 2016 15:52:15 -0800 Subject: [PATCH] init/service.cpp: fix access check for console Commit 9596d2b95d3137c476be4b7c15c47c62ef5cbe09 changes how availability of console is checked by only checking access bits for the console device. However, in cases where there is no console it defaults to /dev/console. This device is always enumerated by tty driver (i.e. file and access bits may be correct), but it doesn't always map to an underlying console driver. Because the lookup for the underlying console driver happens during the open system call, checking only the access bits is not sufficient and need to make sure open system call is successful, we can safely close the FD afterwards to avoid FD leaks. Test: boot device and check console svc doesn't continuously restart Bug: 33691649 Change-Id: Ia51a8a2f56c345b70db55e95f61a057a98b52895 --- init/service.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/init/service.cpp b/init/service.cpp index 7cff348d6..0f7f62fe9 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -582,12 +582,15 @@ bool Service::Start() { console_ = default_console; } - bool have_console = (access(console_.c_str(), R_OK | W_OK) != -1); - if (!have_console) { - PLOG(ERROR) << "service '" << name_ << "' cannot gain read/write access to console '" << console_ << "'"; + // Make sure that open call succeeds to ensure a console driver is + // properly registered for the device node + int console_fd = open(console_.c_str(), O_RDWR | O_CLOEXEC); + if (console_fd < 0) { + PLOG(ERROR) << "service '" << name_ << "' couldn't open console '" << console_ << "'"; flags_ |= SVC_DISABLED; return false; } + close(console_fd); } struct stat sb;