From 71683564c2b383a88fe7f3d14b64e8c22ab619bf Mon Sep 17 00:00:00 2001 From: Alan Huang Date: Tue, 3 Dec 2013 15:35:03 +0800 Subject: [PATCH] Bug 931456 - Check whether b2g and its descendant have opened files only. r=dhylands --- dom/system/gonk/OpenFileFinder.cpp | 50 +++++++++++++++++++++++++++--- dom/system/gonk/OpenFileFinder.h | 18 ++++++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/dom/system/gonk/OpenFileFinder.cpp b/dom/system/gonk/OpenFileFinder.cpp index 32a4f122b319..9a3f7d1b3074 100644 --- a/dom/system/gonk/OpenFileFinder.cpp +++ b/dom/system/gonk/OpenFileFinder.cpp @@ -13,11 +13,14 @@ namespace mozilla { namespace system { -OpenFileFinder::OpenFileFinder(const nsACString& aPath) +OpenFileFinder::OpenFileFinder(const nsACString& aPath, + bool aCheckIsB2gOrDescendant /* = true */) : mPath(aPath), mProcDir(nullptr), mFdDir(nullptr), - mPid(0) + mPid(0), + mMyPid(-1), + mCheckIsB2gOrDescendant(aCheckIsB2gOrDescendant) { } @@ -86,7 +89,14 @@ OpenFileFinder::Next(OpenFileFinder::Info* aInfo) // We found an open file contained within the directory tree passed // into the constructor. FillInfo(aInfo, resolvedPath); - return true; + // If sCheckIsB2gOrDescendant is set false, the caller cares about + // all processes which have open files. If sCheckIsB2gOrDescendant + // is set false, we only care about the b2g proccess or its descendants. + if (!mCheckIsB2gOrDescendant || aInfo->mIsB2gOrDescendant) { + return true; + } + LOG("Ignore process(%d), not a b2g process or its descendant.", + aInfo->mPid); } } // We've checked all of the files for this pid, move onto the next one. @@ -153,9 +163,41 @@ OpenFileFinder::FillInfo(OpenFileFinder::Info* aInfo, const nsACString& aPath) // 01234 int ppid = atoi(&closeParen[4]); // We assume that we're running in the parent process - if (ppid != getpid()) { + if (mMyPid == -1) { + mMyPid = getpid(); + } + + if (mPid == mMyPid) { + // This is chrome process + aInfo->mIsB2gOrDescendant = true; + DBG("Chrome process has open file(s)"); return; } + // For the rest (non-chrome process), we recursively check the ppid to know + // it is a descendant of b2g or not. See bug 931456. + while (ppid != mMyPid && ppid != 1) { + DBG("Process(%d) is not forked from b2g(%d) or Init(1), keep looking", + ppid, mMyPid); + nsPrintfCString ppStatPath("/proc/%d/stat", ppid); + ReadSysFile(ppStatPath.get(), stat, statString.Length()); + closeParen = strrchr(stat, ')'); + if (!closeParen) { + return; + } + ppid = atoi(&closeParen[4]); + } + if (ppid == 1) { + // This is a not a b2g process. + DBG("Non-b2g process has open file(s)"); + aInfo->mIsB2gOrDescendant = false; + return; + } + if (ppid == mMyPid) { + // This is a descendant of b2g. + DBG("Child process of chrome process has open file(s)"); + aInfo->mIsB2gOrDescendant = true; + } + // This looks like a content process. The comm field will be the // app name. aInfo->mAppName = aInfo->mComm; diff --git a/dom/system/gonk/OpenFileFinder.h b/dom/system/gonk/OpenFileFinder.h index e00404248de4..e4f5f12f1c1f 100644 --- a/dom/system/gonk/OpenFileFinder.h +++ b/dom/system/gonk/OpenFileFinder.h @@ -9,6 +9,19 @@ #include +#define USE_DEBUG 0 + +#undef LOG +#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "OpenFileFinder", ## args) +#define LOGW(args...) __android_log_print(ANDROID_LOG_WARN, "OpenFileFinder", ## args) +#define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "OpenFileFinder", ## args) + +#if USE_DEBUG +#define DBG(args...) __android_log_print(ANDROID_LOG_DEBUG, "OpenFileFinder" , ## args) +#else +#define DBG(args...) +#endif + namespace mozilla { namespace system { @@ -29,9 +42,10 @@ public: pid_t mPid; // pid of the process which has the file open nsCString mComm; // comm associated with pid nsCString mExe; // executable name associated with pid + bool mIsB2gOrDescendant; // this is b2g/its descendant or not }; - OpenFileFinder(const nsACString& aPath); + OpenFileFinder(const nsACString& aPath, bool aCheckIsB2gOrDescendant = true); ~OpenFileFinder(); bool First(Info* aInfo); // Return the first open file @@ -52,6 +66,8 @@ private: DIR* mProcDir; // Used for scanning /proc DIR* mFdDir; // Used for scanning /proc/PID/fd int mPid; // PID currently being processed + pid_t mMyPid; // PID of parent process, we assume we're running on it. + bool mCheckIsB2gOrDescendant; // Do we care about non-b2g process? }; } // system