netstat icon indicating copy to clipboard operation
netstat copied to clipboard

Proccess is not given in android N

Open rafiip opened this issue 8 years ago • 5 comments

Hello . execuring this netstat is fine with adb shell without root permission and it gives process name but when i try to get processname through shell in y application , the name pf the process is empty . sample result from netstat executed inside application :

D/BBB: Line Proto Recv-Q Send-Q Local Address               Foreign Address             State       Process
D/BBB: Line tcp6       1      0 ::ffff:10.0.2.15:38568      ::ffff:88.99.44.131:80      CLOSE_WAIT  
D/BBB: Line tcp6       1      0 ::ffff:10.0.2.15:38567      ::ffff:88.99.44.131:80      CLOSE_WAIT  
D/BBB: Line tcp6       0      0 ::ffff:10.0.2.15:48296      ::ffff:119.147.146.168:80   ESTABLISHED 

rafiip avatar Feb 23 '17 19:02 rafiip

I think probably it will be user permission issue because adb shell's user id(uid) is different from your app's user id(uid) in (Android) Linux. This netstat command can succeed every system call when you run it in your adb shell. But, your app's some system call can fail even though your app runs it through shell. Both of them haven't the same permission. And If you can see it on only Android N system, you should check a Android Developer Blog article. If you can show your code, I will follow it.

LipiLee avatar Feb 23 '17 21:02 LipiLee

I appreciate your response to this issue . The code is nothing particular , the code is as follows :

Process process = Runtime.getRuntime().exec(getFilesDirectory+"/netstat2");
 process.waitFor();
 Thread.sleep(3000);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
  while ((line = bufferedReader.readLine()) != null) {
   Log.d("BBB","Line "+line);
  }
				    	 

rafiip avatar Feb 23 '17 21:02 rafiip

I have checked this issue. Android application can't read "/proc" directory except for its own process directory in only Android N system. Because it fail to read other process's /proc directory(/proc/$PID), the process list is empty though netstat command is working in adb shell. If I find another solution, I will share it.

LipiLee avatar Feb 25 '17 07:02 LipiLee

Well , i have found a working solution . instead of packagename i return UID from netstat and from there , there is a command in packagemanager that returns packagename from UID String packagename = pm.getPackagesForUid(UID)[0];

rafiip avatar Feb 25 '17 08:02 rafiip

Good job! Thanks for sharing your solution.

LipiLee avatar Feb 25 '17 10:02 LipiLee