United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7132461 os::available throws IOException for Linux /proc files
JDK-7132461 : os::available throws IOException for Linux /proc files

Details
Type:
Bug
Submit Date:
2012-01-23
Status:
Closed
Updated Date:
2014-04-15
Project Name:
JDK
Resolved Date:
2014-04-14
Component:
hotspot
OS:
linux
Sub-Component:
runtime
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
7
Fixed Versions:
tbd_minor

Related Reports

Sub Tasks

Description
FULL PRODUCT VERSION :
java version "1.7.0_147-icedtea"
OpenJDK Runtime Environment (IcedTea7 2.0) (Gentoo build 1.7.0_147-icedtea-b147)
OpenJDK 64-Bit Server VM (build 21.0-b17, mixed mode)

Same for:

java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) 64-Bit Server VM (build 20.4-b02, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Linux tsdh 3.2.1-gentoo #1 SMP PREEMPT Sun Jan 22 21:06:25 CET 2012 x86_64 Intel(R) Core(TM)2 Duo CPU T8100 @ 2.10GHz GenuineIntel GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Calling FileInputStream.available() always throws an IOException "Invalid argument" for FileInputStream of files in the Linux /proc file system.

For example, this works just fine:

  FileInputStream fis = new FileInputStream("/etc/hosts");
  System.out.println(fis.available());

But this throws an IOException:

  FileInputStream fis = new FileInputStream("/proc/cpuinfo");
  System.out.println(fis.available());

Exception in thread "main" java.io.IOException: Invalid argument
	at java.io.FileInputStream.available(Native Method)
	at Foo.main(Foo.java:12)



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
On a Linux system, execute

  FileInputStream fis = new FileInputStream("/proc/cpuinfo");
  System.out.println(fis.available());

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Get some non-negative integer back from FileInputStream.available().
ACTUAL -
A java.io.IOException is thrown.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.io.IOException: Invalid argument
	at java.io.FileInputStream.available(Native Method)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.FileInputStream;
import java.io.IOException;

public class Foo {
	public static void main(String[] args) throws IOException  {
		FileInputStream fis = new FileInputStream("/etc/hosts");
		System.out.println(fis.available()); // Works fine

		fis = new FileInputStream("/proc/cpuinfo");
		System.out.println(fis.available()); // Throws IOException

	}
}

---------- END SOURCE ----------

                                    

Comments
The JDK no longer uses os::available in jdk8 (see JDK-8001334). We should at least investigate why we have this issue in jdk7u. It the issue is in os::available then we need to move this bug back to the hotspot component.
                                     
2013-03-15
The issue is because the size of /proc files cannot be obtained by calling lseek with SEEK_END directive. That system call just fails and return -1.

The problem is solved in jdk core library side after the fix of JDK-8001334 was pushed. Because in that fix I make use of st_size field of stat64 struct to get the file size. And this works well with these special /proc files. Since the fix of JDK-8001334 only touched the codes inside jdk library, the issue still exists in hotspot area.
                                     
2013-05-11
So I am unclear what is expected of hotspot here. os::available doesn't know what it is being asked to examine, so it just does what it can and reports any errors. Is this not better solved on the JDK side using the same technique employed for 8?
                                     
2013-05-13
The IO exception happens when os::available tries to get the file size by invoking lseek system call, lseek64(fd, 0, SEEK_END).
Hotspot can do the same thing as what IO library code does now to work around the above lseek64 call and try to get the file size from stat64.st_size first. 
                                     
2013-05-14
The proposed fix is uploaded at http://cr.openjdk.java.net/~dxu/7132461/webrev.00/. Since its wrapper JVM_Available is not used in jdk library after the fix of JDK-8001334 in JDK8, it may be feasible to remove it in JDK8, and apply the fix in jdk7.
                                     
2013-05-22
This function is not used in jdk8 or 9, we're not going to backport a fix. Closing as not an issue
                                     
2014-04-14



Hardware and Software, Engineered to Work Together