Bug 839080

Summary: ProcessInfoQuery does not properly handle pidfile with line ending or whitespace
Product: [Other] RHQ Project Reporter: Elias Ross <genman>
Component: AgentAssignee: Heiko W. Rupp <hrupp>
Status: CLOSED CURRENTRELEASE QA Contact: Mike Foley <mfoley>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 4.4CC: hrupp, mazz
Target Milestone: ---   
Target Release: RHQ 4.5.0   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2013-09-01 10:19:00 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
fix and test for this issue
none
fix rebased against master none

Description Elias Ross 2012-07-10 21:18:24 UTC
Description of problem:

If a pidfile is created with extra whitespace, like \r\n, then the PID is never matched.


Version-Release number of selected component (if applicable):

RHQ 4.4

How reproducible:

Create a pid with 'echo' or something. RHQ can't find it. Needs to strip whitespace when reading.

Test is roughly:

        File pidfile = File.createTempFile("test", ".pid");
        try {
            FileWriter fw = new FileWriter(pidfile);
            fw.write(" 3\r\n");
            fw.close();

            results = query.query("process|pidfile|match=" + pidfile.getCanonicalPath());
            assert results.size() == 1 : results;
            assertPidExists(3, results, "pidfile had pid #3 in it and should have matched pid 3");

Fixed code is roughly:

            FileInputStream fis = new FileInputStream(pidfileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
                contents = br.readLine();
                if (contents == null) {
                    throw new IOException("empty pid");
                }
            return contents.trim();

Patch is pending, with test cases.

Comment 1 Heiko W. Rupp 2012-07-18 19:04:12 UTC
Elias,
you are referring to 
org.rhq.core.system.pquery.ProcessInfoQuery#getPidfileContents ?

  Heiko

Comment 2 Elias Ross 2012-07-18 21:12:25 UTC
Yes, ProcessInfoQuery.java

This is what I suggest:

    private String getPidfileContents(String pidfileName) {
        String contents;

        try {
            FileInputStream fis = new FileInputStream(pidfileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            try {
                contents = br.readLine();
                if (contents == null) {
                    throw new IOException("empty pid");
                }
            } finally {
                fis.close();
            }
        } catch (FileNotFoundException e) {
            log.trace("pid not found");
            return "";
        } catch (IOException e) {
            log.warn("unable to read pid file " + pidfileName, e);
            return "";
        }

        return contents.trim();
    }

Comment 3 Heiko W. Rupp 2012-07-20 15:37:43 UTC
Master 4f7c64deab77

Comment 4 Elias Ross 2012-09-13 21:57:05 UTC
Created attachment 612646 [details]
fix and test for this issue

I know this has been mostly addressed already, although I think my fix also contains more logging and more testing. There's some other cleanup as well.

Comment 5 Elias Ross 2012-09-13 22:17:10 UTC
Created attachment 612649 [details]
fix rebased against master

Comment 6 John Mazzitelli 2012-09-19 10:48:43 UTC
i pushed elias' patch: git commit to master: 6887631b55d273e017257eea610dc5b0df8af1fa

Comment 7 Heiko W. Rupp 2013-09-01 10:19:00 UTC
Bulk closing of items that are on_qa and in old RHQ releases, which are out for a long time and where the issue has not been re-opened since.