From 10ef5a8596d692aef016ada0718e8783bb05aeeb Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 28 Sep 2025 16:36:06 +0100 Subject: [PATCH] If watching more than one distinct PID with --watchfd, display the PID as well as the FD number (#12). --- src/include/pv-internal.h | 1 + src/pv/loop.c | 1 - src/pv/state.c | 6 ++++++ src/pv/watchpid.c | 33 ++++++++++++++++++++++++++------- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/include/pv-internal.h b/src/include/pv-internal.h index 9b95780..9157e70 100644 --- a/src/include/pv-internal.h +++ b/src/include/pv-internal.h @@ -167,6 +167,7 @@ struct pvstate_s { /*@only@*/ /*@null@*/ pid_t *pid; /* array of processes to watch fds of */ /*@only@*/ /*@null@*/ int *fd; /* array of fds to watch in each one (0=all) */ unsigned int count; /* number of items in these arrays */ + bool multiple_pids; /* true if more than one distinct PID */ } watchfd; /******************* diff --git a/src/pv/loop.c b/src/pv/loop.c index 91aefb9..fadd256 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -593,7 +593,6 @@ int pv_main_loop(pvstate_t state) * * Returns nonzero on error. * - * TODO: if more than one PID is being watched, display the PID as well. * TODO: re-add "%N" after receiving a remote update, e.g. "-R pid -u block". */ int pv_watchfd_loop(pvstate_t state) diff --git a/src/pv/state.c b/src/pv/state.c index 175b10b..2eda4d1 100644 --- a/src/pv/state.c +++ b/src/pv/state.c @@ -721,6 +721,7 @@ void pv_state_watchfds(pvstate_t state, unsigned int watchfd_count, const pid_t state->watchfd.fd = NULL; } state->watchfd.count = 0; + state->watchfd.multiple_pids = false; /* Allocate empty new arrays of the right size. */ new_pid_array = calloc((size_t) (watchfd_count + 1), sizeof(pid_t)); @@ -744,6 +745,11 @@ void pv_state_watchfds(pvstate_t state, unsigned int watchfd_count, const pid_t for (item_idx = 0; item_idx < watchfd_count; item_idx++) { state->watchfd.pid[item_idx] = pids[item_idx]; state->watchfd.fd[item_idx] = fds[item_idx]; + if ((item_idx > 0) && (pids[item_idx] != pids[item_idx - 1])) + state->watchfd.multiple_pids = true; } state->watchfd.count = watchfd_count; + + debug("%s=%d, %s=%s", "watchfd.count", state->watchfd.count, "multiple_pids", + state->watchfd.multiple_pids ? "true" : "false"); } diff --git a/src/pv/watchpid.c b/src/pv/watchpid.c index a7994d4..77fed08 100644 --- a/src/pv/watchpid.c +++ b/src/pv/watchpid.c @@ -617,6 +617,9 @@ int pv_watchpid_scanfds(pvstate_t state, pid_t watch_pid, int watch_fd, int *arr * Set the display name for the given watched file descriptor, truncating at * the relevant places according to the current screen width. * + * If more than one distinct PID is being watched, include the PID in the + * name as well as the file descriptor number. + * * If the file descriptor is pointing to a file under the current working * directory, show its relative path, not the full path. */ @@ -639,20 +642,36 @@ void pv_watchpid_setname(pvstate_t state, pvwatchfd_t info) } max_display_length = (int) (state->control.width / 2) - 6; + if (state->watchfd.multiple_pids) + max_display_length -= 9; if (max_display_length >= (int) path_length) { - (void) pv_snprintf(info->display_name, - PV_SIZEOF_DISPLAY_NAME, "%4d:%.498s", info->watch_fd, file_fdpath); + if (state->watchfd.multiple_pids) { + (void) pv_snprintf(info->display_name, + PV_SIZEOF_DISPLAY_NAME, "%8d:%4d:%.498s", (int) (info->watch_pid), + info->watch_fd, file_fdpath); + } else { + (void) pv_snprintf(info->display_name, + PV_SIZEOF_DISPLAY_NAME, "%4d:%.498s", info->watch_fd, file_fdpath); + } } else { int prefix_length, suffix_length; prefix_length = max_display_length / 4; suffix_length = max_display_length - prefix_length - 3; - (void) pv_snprintf(info->display_name, - PV_SIZEOF_DISPLAY_NAME, - "%4d:%.*s...%.*s", - info->watch_fd, prefix_length, - file_fdpath, suffix_length, file_fdpath + path_length - suffix_length); + if (state->watchfd.multiple_pids) { + (void) pv_snprintf(info->display_name, + PV_SIZEOF_DISPLAY_NAME, + "%8d:%4d:%.*s...%.*s", + (int) (info->watch_pid), info->watch_fd, prefix_length, + file_fdpath, suffix_length, file_fdpath + path_length - suffix_length); + } else { + (void) pv_snprintf(info->display_name, + PV_SIZEOF_DISPLAY_NAME, + "%4d:%.*s...%.*s", + info->watch_fd, prefix_length, + file_fdpath, suffix_length, file_fdpath + path_length - suffix_length); + } } debug("%s: %d: [%s]", "set name for fd", info->watch_fd, info->display_name);