If watching more than one distinct PID with --watchfd, display the PID as well as the FD number (#12).

This commit is contained in:
Andrew Wood
2025-09-28 16:36:06 +01:00
parent 77ed2e5989
commit 10ef5a8596
4 changed files with 33 additions and 8 deletions
+1
View File
@@ -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;
/*******************
-1
View File
@@ -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)
+6
View File
@@ -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");
}
+26 -7
View File
@@ -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);