Skip to content

lsof -F p prints more than just pid #103

Closed

Description

@po5857

In earlier versions of lsof, -F p printed only the pid:

$ sudo lsof -i tcp:22 -s tcp:LISTEN -F p
p677
$ rpm -q lsof
lsof-4.87-6.el7.x86_64

Newer versions print filehandles also, which breaks scripts parsing this output:

$ sudo lsof -i tcp:22 -s tcp:LISTEN -F p
p609
f5
f6
$ rpm -q lsof
lsof-4.93.2-3.fc32.x86_64

Is this behavior change intentional?

Activity

  1. masatake commented on Aug 24, 2020

    @masatake
    Contributor

    Could you tell me the version of the "earlier version"?

  2. po5857 commented on Aug 25, 2020

    @po5857
    Author

    4.87

  3. masatake commented on Aug 25, 2020

    @masatake
    Contributor

    Thank you.

  4. masatake commented on Aug 25, 2020

    @masatake
    Contributor

    I found this is an expected behavior.
    See 00DIST file. I found following entry in 4.88:

    		Fixed field output to insure that the field descriptor field is
    		always selected, since it identifies the file set.  The bug was
    		reported by Gary Plewa <[email protected]>.
    
  5. po5857 commented on Aug 25, 2020

    @po5857
    Author

    I'm not sure how that makes sense. I specifically asked lsof to only print the pid, but lsof decided I wanted to also see the file descriptors? Why are we overriding the admin's choice here? If I wanted both, I should use "lsof -Ffp".

  6. masatake commented on Aug 25, 2020

    @masatake
    Contributor

    I get the same question. However, as far as reading the man page of lsof, the behavior is intentional.
    The behavior is documented. There may be users who wrote scripts depending on this behavior.
    Therefore I'm in negative for changing the behavior again.

    diff --git a/proc.c b/proc.c
    index 682183b..8152d82 100644
    --- a/proc.c
    +++ b/proc.c
    @@ -1441,7 +1441,9 @@ print_proc()
                if (!Lf)
                    return(rv);
                rv = 1;
    -           (void) printf("%c%d%c", LSOF_FID_PID, Lp->pid, Terminator);
    +
    +           if (FieldSel[LSOF_FIX_PID].st)
    +               (void) printf("%c%d%c", LSOF_FID_PID, Lp->pid, Terminator);
     
     #if    defined(HASTASKS)
                if (FieldSel[LSOF_FIX_TID].st && Lp->tid)
    diff --git a/store.c b/store.c
    index 4e7b8f7..6d41297 100644
    --- a/store.c
    +++ b/store.c
    @@ -210,7 +210,7 @@ struct fieldsel FieldSel[] = {
         { LSOF_FID_CT,     0,  LSOF_FNM_CT,     &Fsv,     FSV_CT    }, /*  2 */
         { LSOF_FID_DEVCH,  0,  LSOF_FNM_DEVCH,  NULL,     0                 }, /*  3 */
         { LSOF_FID_DEVN,   0,  LSOF_FNM_DEVN,   NULL,     0                 }, /*  4 */
    -    { LSOF_FID_FD,     1,  LSOF_FNM_FD,     NULL,     0                 }, /*  5 */
    +    { LSOF_FID_FD,     0,  LSOF_FNM_FD,     NULL,     0                 }, /*  5 */
         { LSOF_FID_FA,     0,  LSOF_FNM_FA,     &Fsv,     FSV_FA    }, /*  6 */
         { LSOF_FID_FG,     0,  LSOF_FNM_FG,     &Fsv,     FSV_FG    }, /*  7 */
         { LSOF_FID_INODE,  0,  LSOF_FNM_INODE,  NULL,     0                 }, /*  8 */
    @@ -218,12 +218,12 @@ struct fieldsel FieldSel[] = {
         { LSOF_FID_TID,    0,  LSOF_FNM_TID,    NULL,     0                 }, /* 11 */
         { LSOF_FID_LOCK,   0,  LSOF_FNM_LOCK,   NULL,     0                 }, /* 11 */
         { LSOF_FID_LOGIN,  0,  LSOF_FNM_LOGIN,  NULL,     0                 }, /* 12 */
    -    { LSOF_FID_MARK,   1,  LSOF_FNM_MARK,   NULL,     0                 }, /* 13 */
    +    { LSOF_FID_MARK,   0,  LSOF_FNM_MARK,   NULL,     0                 }, /* 13 */
         { LSOF_FID_TCMD,   0,  LSOF_FNM_TCMD,   NULL,     0                 }, /* 14 */
         { LSOF_FID_NAME,   0,  LSOF_FNM_NAME,   NULL,     0                 }, /* 15 */
         { LSOF_FID_NI,     0,  LSOF_FNM_NI,     &Fsv,     FSV_NI    }, /* 16 */
         { LSOF_FID_OFFSET, 0,  LSOF_FNM_OFFSET, NULL,     0                 }, /* 17 */
    -    { LSOF_FID_PID,    1,  LSOF_FNM_PID,    NULL,     0                 }, /* 18 */
    +    { LSOF_FID_PID,    0,  LSOF_FNM_PID,    NULL,     0                 }, /* 18 */
         { LSOF_FID_PGID,   0,  LSOF_FNM_PGID,   &Fpgid,   1                 }, /* 19 */
         { LSOF_FID_PROTO,  0,  LSOF_FNM_PROTO,  NULL,     0                 }, /* 20 */
         { LSOF_FID_RDEV,   0,  LSOF_FNM_RDEV,   NULL,     0                 }, /* 21 */
    
  7. po5857 commented on Aug 26, 2020

    @po5857
    Author

    Everyone currently running RHEL/CentOS 6 or 7 rely on the old behavior. Once those distros reach EOL and users migrate to RHEL/CentOS 8, their scripts will break (as did ours). The current behavior is nonsensical. I ask for a pid, and I get more than I asked for. What's the point of the -F flag if it is ignored? Do we need to add a new -F option such as "O" which stands for "the pid, and Only the pid"?

  8. masatake commented on Aug 27, 2020

    @masatake
    Contributor

    I sent a mail to Gary Plewa to know the original bug report.
    The bug was reported in 6 or 7 year ago. So I will wait for a response for (at least) two weeks.
    Please, wait for awhile.

  9. masatake commented on Sep 15, 2020

    @masatake
    Contributor

    I have not received a reply yet. So I will apply my change: #103 (comment) .

  10. masatake commented on Sep 22, 2020

    @masatake
    Contributor

    Everyone currently running RHEL/CentOS 6 or 7 rely on the old behavior. Once those distros reach EOL and users migrate to RHEL/CentOS 8, their scripts will break (as did ours). The current behavior is nonsensical. I ask for a pid, and I get more than I asked for. What's the point of the -F flag if it is ignored? Do we need to add a new -F option such as "O" which stands for "the pid, and Only the pid"?

    I agree with enabling 'fd (f)' by default is non-sense.
    How about 'pid (p)' and 'mark (m)'? Even on RHEL7, they are enabled by default.

    % /usr/bin/lsof -p $$ -F f
    p14028
    fcwd
    frtd
    ftxt
    fmem
    fmem
    fmem
    fmem
    fmem
    fmem
    fmem
    f0
    f1
    f2
    f255
    

    Though I specified only 'f', lsof also prints 'p'.

    Disabling 'p' by default is easy. However, it breaks the scripts will break.

    About 'm', I think enabling by default is acceptable.

    @po5857, do you have any comments?

  11. po5857 commented on Sep 23, 2020

    @po5857
    Author

    The fact that "lsof -F f" also prints the pid makes some sense when it is not pid restricted (with -p X). Otherwise you'd just get a list of a bunch of file descriptors and have no idea what pid they were associated with. One could argue that "lsof -p X -F f" should not print the pid since we already provided it, but given that the output of that command already prints multiple lines, current consumers will already know they need to filter out the pid #.

    The way lsof used to work (in my original example) is that it would always only print one line of output, and not require further filtering. Now it requires | grep ^p.

  12. added a commit that references this issue on Oct 20, 2020
    4a4af32
  13. masatake commented on Oct 23, 2020

    @masatake
    Contributor

    @po5857, can I ask you to review the change for the man page in #125?

  14. po5857 commented on Oct 24, 2020

    @po5857
    Author

    This change:

    • always produces two fields, the PID (`p') field and the
    • the marker (`m') field.

    Is a bit deceptive, since the marker field is only output when in repeat mode. Perhaps this would be more accurate:

    always prints one field, the PID (`p') field. In repeat mode, the marker is also printed.

    Similar here:

    • m marker between repeated output (always selected)

    should perhaps be:

    • m marker between repeated output (always selected in repeat mode)
  15. masatake commented on Oct 24, 2020

    @masatake
    Contributor

    Thank you very much. I will reflect your comment in my change soon and merge it.

  16. added a commit that references this issue on Oct 24, 2020
    65ddee2
  17. added a commit that references this issue on Oct 24, 2020
    811dc78
  18. masatake commented on Oct 24, 2020

    @masatake
    Contributor

    Merge the change. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions