Skip to content

Commit f844b13

Browse files
author
Jeff Xie
committed
Treat NFS ESTALE fds as unlinked for +L selection
On NFS clients, a file can be deleted on the server while still referenced by an open fd on the client. In such cases, stat(2)/lstat(2) on /proc/<pid>/fd/<n> may fail with ESTALE ("Stale file handle"). lsof's +L selection is commonly used to find open-but-unlinked files, but ESTALE fds were not selectable. Record per-fd errno from statsafely() / lstatsafely() and, when +L selection is enabled (SELNLINK), mark the file as matching and annotate NAME with " (STALE)". This helps identify NFS open-but-deleted/stale file handles using existing +L workflows without introducing new command-line options. Signed-off-by: Jeff Xie <xiehuan09@gmail.com>
1 parent d1702af commit f844b13

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

lib/dialects/linux/dproc.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ static int process_id(struct lsof_context *ctx, /* context */
917917
static int pathil = 0;
918918
char *rest;
919919
int txts = 0;
920+
int enss_fd = 0;
921+
int enls_fd = 0;
920922

921923
#if defined(HASSELINUX)
922924
cntxlist_t *cntxp;
@@ -1206,13 +1208,15 @@ static int process_id(struct lsof_context *ctx, /* context */
12061208
} else {
12071209
if (HasNFS) {
12081210
if (lstatsafely(ctx, path, &lsb)) {
1211+
enls_fd = errno;
12091212
(void)statEx(ctx, pbuf, &lsb, &ls);
12101213
enls = errno;
12111214
} else {
12121215
enls = 0;
12131216
ls = SB_ALL;
12141217
}
12151218
if (statsafely(ctx, path, &sb)) {
1219+
enss_fd = errno;
12161220
(void)statEx(ctx, pbuf, &sb, &ss);
12171221
enss = errno;
12181222
} else {
@@ -1353,6 +1357,12 @@ static int process_id(struct lsof_context *ctx, /* context */
13531357
enter_nm(ctx, rest);
13541358
}
13551359

1360+
if ((Selflags & SELNLINK) &&
1361+
(enss_fd == ESTALE || enls_fd == ESTALE)) {
1362+
Lf->sf |= SELNLINK;
1363+
(void)add_nma(ctx, " (STALE)", 8);
1364+
}
1365+
13561366
if (Lf->sf)
13571367
link_lfile(ctx);
13581368
}

0 commit comments

Comments
 (0)