From 6d384c3ffea22fcdb92655cd7e7a59129c7143d7 Mon Sep 17 00:00:00 2001 From: lns Date: Tue, 26 Nov 2019 23:54:43 +0100 Subject: limit the size of /proc/*/cmdline and max printable terminal len/width, improved FD/FILE printing including CMDLINE/PID --- progressbar.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'progressbar.c') diff --git a/progressbar.c b/progressbar.c index 904ec27..c67c0f3 100644 --- a/progressbar.c +++ b/progressbar.c @@ -12,6 +12,9 @@ #include #include +#define MAX_CMDLINE_LEN 512 +#define MAX_TERMINAL_LEN 2048 + struct filtered_dir_entries { struct dirent ** entries; @@ -114,9 +117,10 @@ struct file_info { int proc_fdinfo_fd; long int current_position; long int max_size; + char cmdline[MAX_CMDLINE_LEN]; struct { struct winsize dimensions; - char output[BUFSIZ]; + char output[MAX_TERMINAL_LEN]; size_t printable_chars; size_t unprintable_chars; } terminal; @@ -165,6 +169,28 @@ static int read_and_parse_fd_pos(struct file_info * const finfo) return 0; } +static int read_proc_cmdline(struct file_info * const finfo, + const char * const proc_pid) +{ + int cmdline_fd; + char buf[MAX_CMDLINE_LEN]; + + if (snprintf(buf, sizeof buf, "%s/%s/cmdline", + "/proc", proc_pid) <= 0) + { + finfo->cmdline[0] = '\0'; + return -1; + } + cmdline_fd = open(buf, 0); + if (read(cmdline_fd, buf, sizeof buf) <= 0) + { + finfo->cmdline[0] = '\0'; + return -1; + } + + return snprintf(finfo->cmdline, sizeof finfo->cmdline, "%.*s (%s)", MAX_CMDLINE_LEN - 35, buf, proc_pid); +} + static int reset_terminal_output(struct file_info * const finfo) { finfo->terminal.output[0] = '\r'; @@ -182,7 +208,7 @@ static size_t remaining_printable_chars(struct file_info * const finfo) static int vadd_printable_buf(struct file_info * const finfo, const char * format, va_list ap) { - char tmp_buf[BUFSIZ]; + char tmp_buf[MAX_TERMINAL_LEN]; int snprintf_retval; size_t remaining_len; @@ -373,8 +399,6 @@ int main(int argc, char **argv) exit(EXIT_FAILURE); } - printf("PID: %s, FD: %s, FILE: '%.*s'\n", pid, fd, (int)realpath_used, file_realpath); - int proc_fd_fd = open_in_procfs(pid, fd, PROC_SUBDIR_FD); if (proc_fd_fd < 0) { perror("open proc_fd"); @@ -393,6 +417,10 @@ int main(int argc, char **argv) } close(proc_fd_fd); + read_proc_cmdline(&finfo, pid); + printf("FD..: '/proc/%s/fd/%s'\nFILE: '%.*s'\nCMD.: %s\n", + pid, fd, (int)realpath_used, file_realpath, finfo.cmdline); + while (!read_and_parse_fd_pos(&finfo)) { if (reset_terminal_output(&finfo) < 0) { break; -- cgit v1.2.3