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.