JDK-7132461 : os::available throws IOException for Linux /proc files
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2012-01-23
  • Updated: 2019-03-11
  • Resolved: 2014-04-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbd_minorResolved
Related Reports
Relates :  
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
This function is not used in jdk8 or 9, we're not going to backport a fix. Closing as not an issue
14-04-2014

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.
22-05-2013

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.
14-05-2013

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?
13-05-2013

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.
11-05-2013

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.
15-03-2013