Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "OpenFileFinder.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/FileUtils.h" |
michael@0 | 8 | #include "nsPrintfCString.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <sys/stat.h> |
michael@0 | 11 | #include <errno.h> |
michael@0 | 12 | |
michael@0 | 13 | #define USE_DEBUG 0 |
michael@0 | 14 | |
michael@0 | 15 | #undef LOG |
michael@0 | 16 | #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "OpenFileFinder", ## args) |
michael@0 | 17 | #define LOGW(args...) __android_log_print(ANDROID_LOG_WARN, "OpenFileFinder", ## args) |
michael@0 | 18 | #define ERR(args...) __android_log_print(ANDROID_LOG_ERROR, "OpenFileFinder", ## args) |
michael@0 | 19 | |
michael@0 | 20 | #if USE_DEBUG |
michael@0 | 21 | #define DBG(args...) __android_log_print(ANDROID_LOG_DEBUG, "OpenFileFinder" , ## args) |
michael@0 | 22 | #else |
michael@0 | 23 | #define DBG(args...) |
michael@0 | 24 | #endif |
michael@0 | 25 | |
michael@0 | 26 | namespace mozilla { |
michael@0 | 27 | namespace system { |
michael@0 | 28 | |
michael@0 | 29 | OpenFileFinder::OpenFileFinder(const nsACString& aPath, |
michael@0 | 30 | bool aCheckIsB2gOrDescendant /* = true */) |
michael@0 | 31 | : mPath(aPath), |
michael@0 | 32 | mProcDir(nullptr), |
michael@0 | 33 | mFdDir(nullptr), |
michael@0 | 34 | mPid(0), |
michael@0 | 35 | mCheckIsB2gOrDescendant(aCheckIsB2gOrDescendant) |
michael@0 | 36 | { |
michael@0 | 37 | // We assume that we're running in the parent process |
michael@0 | 38 | mMyPid = getpid(); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | OpenFileFinder::~OpenFileFinder() |
michael@0 | 42 | { |
michael@0 | 43 | Close(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | bool |
michael@0 | 47 | OpenFileFinder::First(OpenFileFinder::Info* aInfo) |
michael@0 | 48 | { |
michael@0 | 49 | Close(); |
michael@0 | 50 | |
michael@0 | 51 | mProcDir = opendir("/proc"); |
michael@0 | 52 | if (!mProcDir) { |
michael@0 | 53 | return false; |
michael@0 | 54 | } |
michael@0 | 55 | mState = NEXT_PID; |
michael@0 | 56 | return Next(aInfo); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | bool |
michael@0 | 60 | OpenFileFinder::Next(OpenFileFinder::Info* aInfo) |
michael@0 | 61 | { |
michael@0 | 62 | // NOTE: This function calls readdir and readlink, neither of which should |
michael@0 | 63 | // block since we're using the proc filesystem, which is a purely |
michael@0 | 64 | // kernel in-memory filesystem and doesn't depend on external driver |
michael@0 | 65 | // behaviour. |
michael@0 | 66 | while (mState != DONE) { |
michael@0 | 67 | switch (mState) { |
michael@0 | 68 | case NEXT_PID: { |
michael@0 | 69 | struct dirent *pidEntry; |
michael@0 | 70 | pidEntry = readdir(mProcDir); |
michael@0 | 71 | if (!pidEntry) { |
michael@0 | 72 | mState = DONE; |
michael@0 | 73 | break; |
michael@0 | 74 | } |
michael@0 | 75 | char *endPtr; |
michael@0 | 76 | mPid = strtol(pidEntry->d_name, &endPtr, 10); |
michael@0 | 77 | if (mPid == 0 || *endPtr != '\0') { |
michael@0 | 78 | // Not a +ve number - ignore |
michael@0 | 79 | continue; |
michael@0 | 80 | } |
michael@0 | 81 | // We've found a /proc/PID directory. Scan open file descriptors. |
michael@0 | 82 | if (mFdDir) { |
michael@0 | 83 | closedir(mFdDir); |
michael@0 | 84 | } |
michael@0 | 85 | nsPrintfCString fdDirPath("/proc/%d/fd", mPid); |
michael@0 | 86 | mFdDir = opendir(fdDirPath.get()); |
michael@0 | 87 | if (!mFdDir) { |
michael@0 | 88 | continue; |
michael@0 | 89 | } |
michael@0 | 90 | mState = CHECK_FDS; |
michael@0 | 91 | } |
michael@0 | 92 | // Fall through |
michael@0 | 93 | case CHECK_FDS: { |
michael@0 | 94 | struct dirent *fdEntry; |
michael@0 | 95 | while((fdEntry = readdir(mFdDir))) { |
michael@0 | 96 | if (!strcmp(fdEntry->d_name, ".") || |
michael@0 | 97 | !strcmp(fdEntry->d_name, "..")) { |
michael@0 | 98 | continue; |
michael@0 | 99 | } |
michael@0 | 100 | nsPrintfCString fdSymLink("/proc/%d/fd/%s", mPid, fdEntry->d_name); |
michael@0 | 101 | nsCString resolvedPath; |
michael@0 | 102 | if (ReadSymLink(fdSymLink, resolvedPath) && PathMatches(resolvedPath)) { |
michael@0 | 103 | // We found an open file contained within the directory tree passed |
michael@0 | 104 | // into the constructor. |
michael@0 | 105 | FillInfo(aInfo, resolvedPath); |
michael@0 | 106 | // If sCheckIsB2gOrDescendant is set false, the caller cares about |
michael@0 | 107 | // all processes which have open files. If sCheckIsB2gOrDescendant |
michael@0 | 108 | // is set false, we only care about the b2g proccess or its descendants. |
michael@0 | 109 | if (!mCheckIsB2gOrDescendant || aInfo->mIsB2gOrDescendant) { |
michael@0 | 110 | return true; |
michael@0 | 111 | } |
michael@0 | 112 | LOG("Ignore process(%d), not a b2g process or its descendant.", |
michael@0 | 113 | aInfo->mPid); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | // We've checked all of the files for this pid, move onto the next one. |
michael@0 | 117 | mState = NEXT_PID; |
michael@0 | 118 | continue; |
michael@0 | 119 | } |
michael@0 | 120 | case DONE: |
michael@0 | 121 | default: |
michael@0 | 122 | mState = DONE; // covers the default case |
michael@0 | 123 | break; |
michael@0 | 124 | } |
michael@0 | 125 | } |
michael@0 | 126 | return false; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | void |
michael@0 | 130 | OpenFileFinder::Close() |
michael@0 | 131 | { |
michael@0 | 132 | if (mFdDir) { |
michael@0 | 133 | closedir(mFdDir); |
michael@0 | 134 | } |
michael@0 | 135 | if (mProcDir) { |
michael@0 | 136 | closedir(mProcDir); |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | void |
michael@0 | 141 | OpenFileFinder::FillInfo(OpenFileFinder::Info* aInfo, const nsACString& aPath) |
michael@0 | 142 | { |
michael@0 | 143 | aInfo->mFileName = aPath; |
michael@0 | 144 | aInfo->mPid = mPid; |
michael@0 | 145 | nsPrintfCString exePath("/proc/%d/exe", mPid); |
michael@0 | 146 | ReadSymLink(exePath, aInfo->mExe); |
michael@0 | 147 | aInfo->mComm.Truncate(); |
michael@0 | 148 | aInfo->mAppName.Truncate(); |
michael@0 | 149 | nsPrintfCString statPath("/proc/%d/stat", mPid); |
michael@0 | 150 | nsCString statString; |
michael@0 | 151 | statString.SetLength(200); |
michael@0 | 152 | char *stat = statString.BeginWriting(); |
michael@0 | 153 | if (!stat) { |
michael@0 | 154 | return; |
michael@0 | 155 | } |
michael@0 | 156 | ReadSysFile(statPath.get(), stat, statString.Length()); |
michael@0 | 157 | // The stat line includes the comm field, surrounded by parenthesis. |
michael@0 | 158 | // However, the contents of the comm field itself is arbitrary and |
michael@0 | 159 | // and can include ')', so we search for the rightmost ) as being |
michael@0 | 160 | // the end of the comm field. |
michael@0 | 161 | char *closeParen = strrchr(stat, ')'); |
michael@0 | 162 | if (!closeParen) { |
michael@0 | 163 | return; |
michael@0 | 164 | } |
michael@0 | 165 | char *openParen = strchr(stat, '('); |
michael@0 | 166 | if (!openParen) { |
michael@0 | 167 | return; |
michael@0 | 168 | } |
michael@0 | 169 | if (openParen >= closeParen) { |
michael@0 | 170 | return; |
michael@0 | 171 | } |
michael@0 | 172 | nsDependentCSubstring comm(&openParen[1], closeParen - openParen - 1); |
michael@0 | 173 | aInfo->mComm = comm; |
michael@0 | 174 | // There is a single character field after the comm and then |
michael@0 | 175 | // the parent pid (the field we're interested in). |
michael@0 | 176 | // ) X ppid |
michael@0 | 177 | // 01234 |
michael@0 | 178 | int ppid = atoi(&closeParen[4]); |
michael@0 | 179 | |
michael@0 | 180 | if (mPid == mMyPid) { |
michael@0 | 181 | // This is chrome process |
michael@0 | 182 | aInfo->mIsB2gOrDescendant = true; |
michael@0 | 183 | DBG("Chrome process has open file(s)"); |
michael@0 | 184 | return; |
michael@0 | 185 | } |
michael@0 | 186 | // For the rest (non-chrome process), we recursively check the ppid to know |
michael@0 | 187 | // it is a descendant of b2g or not. See bug 931456. |
michael@0 | 188 | while (ppid != mMyPid && ppid != 1) { |
michael@0 | 189 | DBG("Process(%d) is not forked from b2g(%d) or Init(1), keep looking", |
michael@0 | 190 | ppid, mMyPid); |
michael@0 | 191 | nsPrintfCString ppStatPath("/proc/%d/stat", ppid); |
michael@0 | 192 | ReadSysFile(ppStatPath.get(), stat, statString.Length()); |
michael@0 | 193 | closeParen = strrchr(stat, ')'); |
michael@0 | 194 | if (!closeParen) { |
michael@0 | 195 | return; |
michael@0 | 196 | } |
michael@0 | 197 | ppid = atoi(&closeParen[4]); |
michael@0 | 198 | } |
michael@0 | 199 | if (ppid == 1) { |
michael@0 | 200 | // This is a not a b2g process. |
michael@0 | 201 | DBG("Non-b2g process has open file(s)"); |
michael@0 | 202 | aInfo->mIsB2gOrDescendant = false; |
michael@0 | 203 | return; |
michael@0 | 204 | } |
michael@0 | 205 | if (ppid == mMyPid) { |
michael@0 | 206 | // This is a descendant of b2g. |
michael@0 | 207 | DBG("Child process of chrome process has open file(s)"); |
michael@0 | 208 | aInfo->mIsB2gOrDescendant = true; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | // This looks like a content process. The comm field will be the |
michael@0 | 212 | // app name. |
michael@0 | 213 | aInfo->mAppName = aInfo->mComm; |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | bool |
michael@0 | 217 | OpenFileFinder::ReadSymLink(const nsACString& aSymLink, nsACString& aOutPath) |
michael@0 | 218 | { |
michael@0 | 219 | aOutPath.Truncate(); |
michael@0 | 220 | const char *symLink = aSymLink.BeginReading(); |
michael@0 | 221 | |
michael@0 | 222 | // Verify that we actually have a symlink. |
michael@0 | 223 | struct stat st; |
michael@0 | 224 | if (lstat(symLink, &st)) { |
michael@0 | 225 | return false; |
michael@0 | 226 | } |
michael@0 | 227 | if ((st.st_mode & S_IFMT) != S_IFLNK) { |
michael@0 | 228 | return false; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // Contrary to the documentation st.st_size doesn't seem to be a reliable |
michael@0 | 232 | // indication of the length when reading from /proc, so we use a fixed |
michael@0 | 233 | // size buffer instead. |
michael@0 | 234 | |
michael@0 | 235 | char resolvedSymLink[PATH_MAX]; |
michael@0 | 236 | ssize_t pathLength = readlink(symLink, resolvedSymLink, |
michael@0 | 237 | sizeof(resolvedSymLink) - 1); |
michael@0 | 238 | if (pathLength <= 0) { |
michael@0 | 239 | return false; |
michael@0 | 240 | } |
michael@0 | 241 | resolvedSymLink[pathLength] = '\0'; |
michael@0 | 242 | aOutPath.Assign(resolvedSymLink); |
michael@0 | 243 | return true; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | } // system |
michael@0 | 247 | } // mozilla |