From b911acf559b72b746127a8820dac1b904dbf2972 Mon Sep 17 00:00:00 2001 From: James Blackburn Date: Thu, 27 May 2010 10:01:52 +0000 Subject: [PATCH] Bug 314504 ProcessList leaks file descriptors during #getProcessList() --- .../cdt/internal/core/aix/ProcessList.java | 11 ++++++++-- .../cdt/internal/core/linux/ProcessList.java | 11 ++++++++-- .../cdt/internal/core/macosx/ProcessList.java | 4 ++-- .../cdt/internal/core/qnx/ProcessList.java | 4 ++-- .../internal/core/solaris/ProcessList.java | 4 ++-- .../cdt/internal/core/win32/ProcessList.java | 20 +++++++++++++------ 6 files changed, 38 insertions(+), 16 deletions(-) diff --git a/core/org.eclipse.cdt.core.aix/src/org/eclipse/cdt/internal/core/aix/ProcessList.java b/core/org.eclipse.cdt.core.aix/src/org/eclipse/cdt/internal/core/aix/ProcessList.java index 6d9e6f030ed..de76002331d 100644 --- a/core/org.eclipse.cdt.core.aix/src/org/eclipse/cdt/internal/core/aix/ProcessList.java +++ b/core/org.eclipse.cdt.core.aix/src/org/eclipse/cdt/internal/core/aix/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -60,13 +60,20 @@ public class ProcessList implements IProcessList { for (int i = 0; i < pidFiles.length; i++) { File cmdLine = new File(pidFiles[i], "cmdline"); //$NON-NLS-1$ StringBuffer line = new StringBuffer(); + FileReader reader = null; try { - FileReader reader = new FileReader(cmdLine); + reader = new FileReader(cmdLine); int c; while ((c = reader.read()) > 0) { line.append((char)c); } } catch (IOException e) { + } finally { + try { + if (reader != null) + reader.close(); + } catch (IOException e) {/* Don't care */} + reader = null; } String name = line.toString(); if (name.length() == 0) { diff --git a/core/org.eclipse.cdt.core.linux/src/org/eclipse/cdt/internal/core/linux/ProcessList.java b/core/org.eclipse.cdt.core.linux/src/org/eclipse/cdt/internal/core/linux/ProcessList.java index 3277a672c8d..35e864d1222 100644 --- a/core/org.eclipse.cdt.core.linux/src/org/eclipse/cdt/internal/core/linux/ProcessList.java +++ b/core/org.eclipse.cdt.core.linux/src/org/eclipse/cdt/internal/core/linux/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -60,13 +60,20 @@ public class ProcessList implements IProcessList { for (int i = 0; i < pidFiles.length; i++) { File cmdLine = new File(pidFiles[i], "cmdline"); //$NON-NLS-1$ StringBuffer line = new StringBuffer(); + FileReader reader = null; try { - FileReader reader = new FileReader(cmdLine); + reader = new FileReader(cmdLine); int c; while ((c = reader.read()) > 0) { line.append((char)c); } } catch (IOException e) { + } finally { + try { + if (reader != null) + reader.close(); + } catch (IOException e) {/* Don't care */} + reader = null; } String name = line.toString(); if (name.length() == 0) { diff --git a/core/org.eclipse.cdt.core.macosx/src/org/eclipse/cdt/internal/core/macosx/ProcessList.java b/core/org.eclipse.cdt.core.macosx/src/org/eclipse/cdt/internal/core/macosx/ProcessList.java index e0b955398eb..d025eaa853d 100644 --- a/core/org.eclipse.cdt.core.macosx/src/org/eclipse/cdt/internal/core/macosx/ProcessList.java +++ b/core/org.eclipse.cdt.core.macosx/src/org/eclipse/cdt/internal/core/macosx/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2009 IBM Corporation and others. + * Copyright (c) 2005, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -66,7 +66,7 @@ public class ProcessList implements IProcessList { } } } - + psOutput.close(); } catch(Exception e) { /* Ignore */ } diff --git a/core/org.eclipse.cdt.core.qnx/src/org/eclipse/cdt/internal/core/qnx/ProcessList.java b/core/org.eclipse.cdt.core.qnx/src/org/eclipse/cdt/internal/core/qnx/ProcessList.java index 9f2e030dd5e..77ec0272d97 100644 --- a/core/org.eclipse.cdt.core.qnx/src/org/eclipse/cdt/internal/core/qnx/ProcessList.java +++ b/core/org.eclipse.cdt.core.qnx/src/org/eclipse/cdt/internal/core/qnx/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -74,7 +74,7 @@ public class ProcessList implements IProcessList { procInfo.add(new ProcessInfo(pidStr, nameStr)); } - + pidinOutput.close(); pidin.destroy(); } catch(Exception e) { /* Ignore */ diff --git a/core/org.eclipse.cdt.core.solaris/src/org/eclipse/cdt/internal/core/solaris/ProcessList.java b/core/org.eclipse.cdt.core.solaris/src/org/eclipse/cdt/internal/core/solaris/ProcessList.java index 84ce74e770b..d6623cae7da 100644 --- a/core/org.eclipse.cdt.core.solaris/src/org/eclipse/cdt/internal/core/solaris/ProcessList.java +++ b/core/org.eclipse.cdt.core.solaris/src/org/eclipse/cdt/internal/core/solaris/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -66,7 +66,7 @@ public class ProcessList implements IProcessList { } } } - + psOutput.close(); } catch(Exception e) { /* Ignore */ } diff --git a/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/ProcessList.java b/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/ProcessList.java index c8182920983..e9ba0a98c0f 100644 --- a/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/ProcessList.java +++ b/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/ProcessList.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 QNX Software Systems and others. + * Copyright (c) 2000, 2010 QNX Software Systems and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -43,6 +43,7 @@ public class ProcessList implements IProcessList { String command = null; InputStream in = null; Bundle bundle = Platform.getBundle(CCorePlugin.PLUGIN_ID); + IProcessInfo[] procInfos = NOPROCESS; try { URL url = FileLocator.find(bundle, new Path("$os$/listtasks.exe"), null); //$NON-NLS-1$ @@ -53,16 +54,23 @@ public class ProcessList implements IProcessList { if (file.exists()) { command = file.getCanonicalPath(); if (command != null) { - p = ProcessFactory.getFactory().exec(command); - in = p.getInputStream(); - InputStreamReader reader = new InputStreamReader(in); - return parseListTasks(reader); + try { + p = ProcessFactory.getFactory().exec(command); + in = p.getInputStream(); + InputStreamReader reader = new InputStreamReader(in); + procInfos = parseListTasks(reader); + } finally { + if (in != null) + in.close(); + if (p != null) + p.destroy(); + } } } } } catch (IOException e) { } - return NOPROCESS; + return procInfos; } public IProcessInfo[] parseListTasks(InputStreamReader reader) {