I am trying to read /proc/net/xt_qtaguid/stats
in Android 6.
In Linux, files under /proc/
are not actually files: they are handled by procfs
, a special filesystem that executes code each time the file entry is read or written (code being a callback function defined in a kernel module). So those pseudo-files are not static like regular files, but completely dynamic. The size gives an interesting clue: (most of) those files have a length of 0 (as can be seen with ls -l
), but when read they show some content.
In short, it is to be expected that reading that same file from 2 different contexts yields 2 different results.
In this instance, the "file" callbacks are handled by xt_qtaguid
module for Android, which manages "per-application/delegated data usage monitoring".
This answer says:
this virtual file's read_proc function limit the uid, every application can only read the header and its own line.
The first part is a bit vague but seems to indicate the difference is based on user id, and the module will only "print" 2 lines of data when a regular application reads this file (please note Android assigns a unique user ID to each application and runs it as that user in a separate process).
You don't give enough details but I have to assume that when you print it from adb using cat
, you probably don't have the same user id and permissions as when you try to read it from your application. I did not track the exact implementation details (if you want to, the source for this module can be read here), but other variables might come into play.
The doc says:
In the case of applications that provide network data transfer as a service, such as the download manager, media streaming service, etc, it is possible to attribute the ownership of the network data transfer to the UID of the requesting application using the TrafficStats.setThreadStatsUid()
function call. The caller must hold the android.permission.MODIFY_NETWORK_ACCOUNTING
permission to re-assign the ownership of the network traffic.
So a process/application can use TrafficStats.setThreadStatsUid()
in order to get more lines from that file, but that requires MODIFY_NETWORK_ACCOUNTING
permission.
bufferedReader
but it's undefined. Are you sure you posted the correct code? Are you sure that only the first 2 lines are printed with your real code? If yes, then please upload your input file to somewhere we can download and inspect it. Do not copy paste, because maybe it's not accurate. We need the actual file to debug this, not something copy-pasted. – Foretopsail/proc/
? Those are special, with special permissions and size reported as 0, which might trip Java's readers (see this question, although it does not have very good answers)... I have not managed to reproduce it, even with an old Java 7. Maybe it would help if you could specify what file exactly is this, your kernel version, and java version. – Hepplewhite/proc/net/xt_qtaguid/stats
in Android 6. – FalconiformIOException
in thecatch
block toThrowable
. Perhaps some other exception type is been throw. Although typically you would see the corresponding stack trace in the common java command line program, that could be silenced some how so is always good generalize the code here to check for that explicitly. – Forethought