In my VPN application up until Android-9, it was possible to read the /proc/net/tcp
files from apps targeting API level < 28. Access from apps no longer seems to work in Android Q. I'm getting an error /proc/net/tcp: open failed: EACCES (Permission denied)
while trying to read the file.
In Android-Q privacy change google has addressed Restriction on access to /proc/net filesystem
And I think ConnectivityManager.getConnectionOwnerUid()
can be used if the apps compileSDK version is 29. But unfortunately at the moment, I can't change my compileSDK version but I updated the targetSDK version to the latest IE, 29.
Any other possible way to read the file in Android-10? Posting my code for reference
public static final int INDEX_UID_COL = 7;
public static final int INDEX_LOCAL_ADDRESS_COL = 1;
public static final String PROC_FILE = "/proc/net/tcp";
public static String getPackageName(Context context, int srcPort) {
String packageName = "";
try {
BufferedReader br = new BufferedReader(new FileReader(PROC_FILE));
//Ignore first line
String line = br.readLine();
while ((line = br.readLine()) != null) {
/**
* Proc file table column sequence
* sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode
*/
String[] parts = line.trim().split("\s+");
if (parts.length >= 8) {
String localAddress = parts[INDEX_LOCAL_ADDRESS_COL];
if (!localAddress.isEmpty()) {
String[] localAddressParts = localAddress.split(":");
if (localAddressParts.length == 2) {
int port = Integer.parseInt(localAddressParts[1], 16);
if (port == srcPort) {
int uid = Integer.parseInt(parts[INDEX_UID_COL]);
packageName = context.getPackageManager().getNameForUid(uid);
break;
}
}
}
}
}
br.close();
} catch (Exception ex) {
Log.e("ProcFileParser", ex.getMessage());
}
return packageName;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…