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 06:19:00 EDT Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---
Attachments:
Description Flags
fix and test for this issue
none
fix rebased against master none

Description Elias Ross 2012-07-10 17:18:24 EDT
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 15:04:12 EDT
Elias,
you are referring to 
org.rhq.core.system.pquery.ProcessInfoQuery#getPidfileContents ?

  Heiko
Comment 2 Elias Ross 2012-07-18 17:12:25 EDT
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 11:37:43 EDT
Master 4f7c64deab77
Comment 4 Elias Ross 2012-09-13 17:57:05 EDT
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 18:17:10 EDT
Created attachment 612649 [details]
fix rebased against master
Comment 6 John Mazzitelli 2012-09-19 06:48:43 EDT
i pushed elias' patch: git commit to master: 6887631b55d273e017257eea610dc5b0df8af1fa
Comment 7 Heiko W. Rupp 2013-09-01 06:19:00 EDT
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.