| 175 | } |
| 176 | |
| 177 | int GetCurrentPID(const rdcstr &deviceID, const rdcstr &processName) |
| 178 | { |
| 179 | if(Android_Debug_ProcessLaunch()) |
| 180 | { |
| 181 | RDCLOG("Getting PID from device %s for process '%s'", deviceID.c_str(), processName.c_str()); |
| 182 | } |
| 183 | |
| 184 | // try 15 times, 200ms apart to find the pid, for a total of 3 seconds timeout waiting for the |
| 185 | // process to even be alive |
| 186 | for(int i = 0; i < 15; i++) |
| 187 | { |
| 188 | Process::ProcessResult pidOutput = |
| 189 | adbExecCommand(deviceID, StringFormat::Fmt("shell ps -A | grep %s", processName.c_str())); |
| 190 | |
| 191 | rdcstr &output = pidOutput.strStdout; |
| 192 | |
| 193 | output.trim(); |
| 194 | int space = output.find_first_of("\t "); |
| 195 | |
| 196 | if(Android_Debug_ProcessLaunch()) |
| 197 | { |
| 198 | RDCLOG("Output from ps -A: '%s'", output.c_str()); |
| 199 | } |
| 200 | |
| 201 | // if we didn't get a response, try without the -A as some android devices don't support that |
| 202 | // parameter |
| 203 | if(output.empty() || output.find(processName) == -1 || space == -1) |
| 204 | { |
| 205 | pidOutput = |
| 206 | adbExecCommand(deviceID, StringFormat::Fmt("shell ps | grep %s", processName.c_str())); |
| 207 | |
| 208 | output.trim(); |
| 209 | space = output.find_first_of("\t "); |
| 210 | |
| 211 | if(Android_Debug_ProcessLaunch()) |
| 212 | { |
| 213 | RDCLOG("Output from ps: '%s'", output.c_str()); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if(Android_Debug_ProcessLaunch()) |
| 218 | { |
| 219 | RDCLOG("Final output is '%s' and first space is at char %d", output.c_str(), space); |
| 220 | } |
| 221 | |
| 222 | // if we still didn't get a response, sleep and try again next time |
| 223 | if(output.empty() || output.find(processName) == -1 || space == -1) |
| 224 | { |
| 225 | if(Android_Debug_ProcessLaunch()) |
| 226 | { |
| 227 | RDCLOG("Didn't get valid PID line, waiting"); |
| 228 | } |
| 229 | |
| 230 | Threading::Sleep(200); |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | char *pid = &output[space]; |
no test coverage detected