Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/arg.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@
#include "cli.h"
#include "lsof.h"
#include <limits.h>
#include <stdint.h>

/*
* Local definitions
*/

#define CMDRXINCR 32 /* CmdRx[] allocation increment */

struct dir_seen_entry {
dev_t dev;
INODETYPE ino;
int used;
};

struct dir_seen {
struct dir_seen_entry *entries;
size_t count, capacity;
};

/*
* Local static variables
*/
Expand Down Expand Up @@ -855,6 +867,55 @@ static int enter_fd_lst(struct lsof_context *ctx, /* context */
return (0);
}

/* Hash both parts of a directory identity, including high inode bits. */
static size_t dir_hash(dev_t dev, INODETYPE ino) {
uint64_t h = (uint64_t)ino ^ ((uint64_t)dev * UINT64_C(0x9e3779b97f4a7c15));
h = (h ^ (h >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
h = (h ^ (h >> 27)) * UINT64_C(0x94d049bb133111eb);
return (size_t)(h ^ (h >> 31));
}

/* Record a directory; return 1 if it was already recorded in this search. */
static int seen_dir(struct lsof_context *ctx, struct dir_seen *seen,
const struct stat *sb) {
size_t i, h, capacity;
struct dir_seen_entry *entries;

/* Keep the open-addressed table at most half full. */
if (seen->count >= seen->capacity / 2) {
capacity = seen->capacity ? seen->capacity * 2 : 128;
if (capacity < seen->capacity ||
!(entries = calloc(capacity, sizeof(*entries)))) {
(void)fprintf(stderr, "%s: no space for directory search hash\n", Pn);
Error(ctx);
}
for (i = 0; i < seen->capacity; i++) {
if (!seen->entries[i].used)
continue;
h = dir_hash(seen->entries[i].dev, seen->entries[i].ino) &
(capacity - 1);
while (entries[h].used)
h = (h + 1) & (capacity - 1);
entries[h] = seen->entries[i];
}
free(seen->entries);
seen->entries = entries;
seen->capacity = capacity;
}
h = dir_hash(sb->st_dev, (INODETYPE)sb->st_ino) & (seen->capacity - 1);
while (seen->entries[h].used) {
if (seen->entries[h].dev == sb->st_dev &&
seen->entries[h].ino == (INODETYPE)sb->st_ino)
return (1);
h = (h + 1) & (seen->capacity - 1);
}
seen->entries[h].dev = sb->st_dev;
seen->entries[h].ino = (INODETYPE)sb->st_ino;
seen->entries[h].used = 1;
seen->count++;
return (0);
}

/*
* enter_dir() - enter the files of a directory for searching
*/
Expand All @@ -879,6 +940,7 @@ int enter_dir(struct lsof_context *ctx, /* context */
MALLOC_S fpl = (MALLOC_S)0;
MALLOC_S fpli = (MALLOC_S)0;
struct stat sb;
struct dir_seen seen = {0};
/*
* Check the directory path; reduce symbolic links; stat(2) it; make sure
* it's really a directory.
Expand Down Expand Up @@ -921,6 +983,8 @@ int enter_dir(struct lsof_context *ctx, /* context */
#endif /* defined(HASSPECDEVD) */

ddev = sb.st_dev;
if (descend && (Fxover & XO_SYMLINK))
(void)seen_dir(ctx, &seen, &sb);
/*
* Stack the directory and record it in Sfile for searching.
*/
Expand Down Expand Up @@ -1080,6 +1144,9 @@ int enter_dir(struct lsof_context *ctx, /* context */
} else
continue;
}
if (seen.capacity && (sb.st_mode & S_IFMT) == S_IFDIR &&
seen_dir(ctx, &seen, &sb))
continue;
if (av[0]) {
(void)free((FREE_P *)av[0]);
av[0] = (char *)NULL;
Expand Down Expand Up @@ -1128,6 +1195,7 @@ int enter_dir(struct lsof_context *ctx, /* context */
(void)free((FREE_P *)Dstk);
Dstk = (char **)NULL;
}
free(seen.entries);
if (!fct) {

/*
Expand Down
29 changes: 29 additions & 0 deletions tests/case-20-soft-errors.bash
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,34 @@ check_output() {
# Hard errors must win in either order within one FD list.
check_output usage -w -d '0,^1,3-1' || exit 1
check_output usage -w -d '0,3-1,^1' || exit 1
# Following directory symlinks must terminate even when they form cycles.
if command -v python3 >/dev/null 2>&1; then
mkdir "$work/tree" || exit 1
ln -s . "$work/tree/back1" || exit 1
ln -s . "$work/tree/back2" || exit 1
ln -s .. "$work/tree/parent" || exit 1
python3 - "$lsof" "$work" <<'PY' || exit 1
import os
import subprocess
import sys

tree = os.path.join(sys.argv[2], "tree")
# Exercise hash growth as well as repeated links to already visited directories.
for i in range(160):
os.mkdir(os.path.join(tree, f"dir{i}"))
os.symlink(f"dir{i}", os.path.join(tree, f"alias{i}"))
with open(os.path.join(sys.argv[2], "file")) as f, \
open(os.path.join(tree, "dir159", "file"), "w") as nested:
for prefix in ([], ["+d", sys.argv[2]]):
result = subprocess.run(
[sys.argv[1], "-w", "-a", "-p", str(os.getpid()), "-Ff"] +
prefix + ["-x", "l", "+D", tree],
capture_output=True, text=True, timeout=5)
assert result.returncode in (0, 1), (result.returncode, result.stderr)
for fd in (f.fileno(), nested.fileno()):
assert f"f{fd}\n" in result.stdout, result.stdout
print("PASS: recursive symlink search terminates and finds linked files")
PY
fi
echo "soft-error argument tests passed"
} > "$report" 2>&1
Loading