Why can't I read from /proc in Java using Comm

2019-08-11 10:18发布

问题:

I had a problem with reading /proc/%d/stat files using my Java method copyFiles() (source code below). I have found workaround using similar readProc() method.

Now I am wondering what was the problem. Output files were created, but each file had 0 bytes (in /proc/ all files are 0 bytes because it is not standard filesystem). FileUtils is from the Apache Commons IO library.

I've tried to do the same using java.nio - again, IOException is being thrown that attributes are wrong for each file.

I removed some part of the code regarding parsing exceptions etc.

Why does this work with FileInputStream, but not with FileUtils.copyFile()?

public void copyFiles() {
    final File dir = new File("/proc");
    final String[] filedirArray = dir.list();
    long counter = 0;
    for(String filedir : filedirArray) {
            final File checkFile = new File(dir, filedir);
            if (checkFile.isDirectory()) {
                    try {
                            Integer.parseInt(filedir);
                            File srcFile = new File(checkFile, "stat");
                            File dstFile = new File("/home/waldekm/files/stat" + "." + Long.toString(counter++));
                            try {                                    
                                FileUtils.copyFile(srcFile, dstFile);
                            } catch (IOException e1) {}
                    } catch (NumberFormatException e) {
                            // not a number, do nothing
                    }                        
            }
    }
}

public static void readProc(final String src, final String dst) {
    FileInputStream in = null;
    FileOutputStream out = null;

    File srcFile = new File(src);
    File dstFile = new File(dst);

    try {
        in = new FileInputStream(srcFile);
        out = new FileOutputStream(dstFile);
        int c;
        while((c = in.read()) != -1) {
            out.write(c);
        }
    } catch (IOException e1) {
 }  finally {

        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e1) {}
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e1) {}
    }

回答1:

The reason is most likely that the operating system is reporting the file size as zero.

On my machine, man 2 stat says this:

"For most files under the /proc directory, stat() does not return the file size in the st_size field; instead the field is returned with the value 0."

(The stat system call will be what the JVM uses to find out what a file's size is.)



回答2:

Here is a code snipped that would read specific fields from a proc file, using methods that are available (but not documented directly) in the Process class of Android. Modify the FORMAT buffer and the output buffer size to read more/different values from the proc file,

int PROC_SPACE_TERM = (int)' ';
int PROC_OUT_LONG = 0x2000

public static final int[] PROCESS_STATS_FORMAT = new int[] {
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM,
    PROC_SPACE_TERM|PROC_OUT_LONG,                  // 13: utime
    PROC_SPACE_TERM|PROC_OUT_LONG                   // 14: stime
};

long buf[] = new long[2];

try {
    int pid = 1000; // Assume 1000 is a valid pid for a process. 
    Method mReadProcFile = 
              Process.class.getMethod("readProcFile", String.class,
                                      int[].class, String[].class,   
                                      long[].class, float[].class);
    mReadProcFile.invoke(null, "/proc/" + pid + "/stat", 
                         PROCESS_STATS_FORMAT, null, buf, null);

    return buf;

} catch(NoSuchMethodException e) {
    Log.e(TAG, "Error! Could not get access to JNI method - readProcFile");
} catch (InvocationTargetException e) {
Log.e(TAG, "Error! Could not invoke JNI method - readProcFile");
} catch (IllegalAccessException e) {
    Log.e(TAG, "Error! Illegal access while invoking JNI method - readProcFile");
}

return null;


回答3:

I see you are creating a FileInputStream to read a /proc file. Instead I suggest you create a FileReader object. FileInputStream gets tripped up by the lack of file length for /proc files but FileReader does not.