From 9596d2b95d3137c476be4b7c15c47c62ef5cbe09 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Sat, 10 Dec 2016 12:17:32 -0800 Subject: [PATCH] init/service.cpp: fix FD leak for services with consoles When init starts a service with a console, it tests for the presence of a readable/writable console device. The test results in a leaked file descriptor. Use access() instead of open() to avoid leaking file descriptors. Bug introduced in 70daa67062c016eea1a30be2e1de0dcba1d23a13. Test: compiles and device boots Change-Id: I4efcfa0bb2cdb09f0455bc04a3a91e784bda7962 --- init/service.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init/service.cpp b/init/service.cpp index a7eaf6650..e967a7c31 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -567,9 +567,9 @@ bool Service::Start() { console_ = default_console; } - bool have_console = (open(console_.c_str(), O_RDWR | O_CLOEXEC) != -1); + bool have_console = (access(console_.c_str(), R_OK | W_OK) != -1); if (!have_console) { - PLOG(ERROR) << "service '" << name_ << "' couldn't open console '" << console_ << "'"; + PLOG(ERROR) << "service '" << name_ << "' cannot gain read/write access to console '" << console_ << "'"; flags_ |= SVC_DISABLED; return false; }