From 65357c8c46154de4e4eca14075bfe5523bb5fc14 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Mon, 7 Dec 2020 10:26:01 +0100 Subject: New upstream version 3.0.3 --- TraceScreen.c | 145 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 53 deletions(-) (limited to 'TraceScreen.c') diff --git a/TraceScreen.c b/TraceScreen.c index 0a3485e..47cf0ab 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -1,30 +1,32 @@ /* htop - TraceScreen.c (C) 2005-2006 Hisham H. Muhammad -Released under the GNU GPL, see the COPYING file +Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ -#include "TraceScreen.h" +#include "config.h" // IWYU pragma: keep -#include "CRT.h" -#include "ProcessList.h" -#include "ListItem.h" -#include "IncSet.h" -#include "StringUtils.h" -#include "FunctionBar.h" +#include "TraceScreen.h" +#include +#include +#include +#include #include -#include -#include #include -#include +#include #include -#include +#include #include -#include #include -#include + +#include "CRT.h" +#include "FunctionBar.h" +#include "IncSet.h" +#include "Panel.h" +#include "ProvideCurses.h" +#include "XUtils.h" static const char* const TraceScreenFunctions[] = {"Search ", "Filter ", "AutoScroll ", "Stop Tracing ", "Done ", NULL}; @@ -33,7 +35,7 @@ static const char* const TraceScreenKeys[] = {"F3", "F4", "F8", "F9", "Esc"}; static int TraceScreenEvents[] = {KEY_F(3), KEY_F(4), KEY_F(8), KEY_F(9), 27}; -InfoScreenClass TraceScreen_class = { +const InfoScreenClass TraceScreen_class = { .super = { .extends = Class(Object), .delete = TraceScreen_delete @@ -44,14 +46,13 @@ InfoScreenClass TraceScreen_class = { }; TraceScreen* TraceScreen_new(Process* process) { - TraceScreen* this = xMalloc(sizeof(TraceScreen)); + // This initializes all TraceScreen variables to "false" so only default = true ones need to be set below + TraceScreen* this = xCalloc(1, sizeof(TraceScreen)); Object_setClass(this, Class(TraceScreen)); this->tracing = true; - this->contLine = false; - this->follow = false; FunctionBar* fuBar = FunctionBar_new(TraceScreenFunctions, TraceScreenKeys, TraceScreenEvents); CRT_disableDelay(); - return (TraceScreen*) InfoScreen_init(&this->super, process, fuBar, LINES-2, ""); + return (TraceScreen*) InfoScreen_init(&this->super, process, fuBar, LINES - 2, ""); } void TraceScreen_delete(Object* cast) { @@ -59,63 +60,100 @@ void TraceScreen_delete(Object* cast) { if (this->child > 0) { kill(this->child, SIGTERM); waitpid(this->child, NULL, 0); + } + + if (this->strace) { fclose(this->strace); } + CRT_enableDelay(); - free(InfoScreen_done((InfoScreen*)cast)); + free(InfoScreen_done((InfoScreen*)this)); } void TraceScreen_draw(InfoScreen* this) { attrset(CRT_colors[PANEL_HEADER_FOCUS]); mvhline(0, 0, ' ', COLS); - mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, this->process->comm); + mvprintw(0, 0, "Trace of process %d - %s", this->process->pid, Process_getCommand(this->process)); attrset(CRT_colors[DEFAULT_COLOR]); IncSet_drawBar(this->inc); } bool TraceScreen_forkTracer(TraceScreen* this) { - char buffer[1001]; - int error = pipe(this->fdpair); - if (error == -1) return false; - this->child = fork(); - if (this->child == -1) return false; - if (this->child == 0) { + int fdpair[2] = {0, 0}; + + if (pipe(fdpair) == -1) + return false; + + if (fcntl(fdpair[0], F_SETFL, O_NONBLOCK) < 0) + goto err; + + if (fcntl(fdpair[1], F_SETFL, O_NONBLOCK) < 0) + goto err; + + pid_t child = fork(); + if (child == -1) + goto err; + + if (child == 0) { + close(fdpair[0]); + + dup2(fdpair[1], STDOUT_FILENO); + dup2(fdpair[1], STDERR_FILENO); + close(fdpair[1]); + CRT_dropPrivileges(); - dup2(this->fdpair[1], STDERR_FILENO); - int ok = fcntl(this->fdpair[1], F_SETFL, O_NONBLOCK); - if (ok != -1) { - xSnprintf(buffer, sizeof(buffer), "%d", this->super.process->pid); - execlp("strace", "strace", "-T", "-tt", "-s", "512", "-p", buffer, NULL); - } + + char buffer[32] = {0}; + xSnprintf(buffer, sizeof(buffer), "%d", this->super.process->pid); + execlp("strace", "strace", "-T", "-tt", "-s", "512", "-p", buffer, NULL); + + // Should never reach here, unless execlp fails ... const char* message = "Could not execute 'strace'. Please make sure it is available in your $PATH."; - ssize_t written = write(this->fdpair[1], message, strlen(message)); + ssize_t written = write(STDERR_FILENO, message, strlen(message)); (void) written; - exit(1); + + exit(127); } - int ok = fcntl(this->fdpair[0], F_SETFL, O_NONBLOCK); - if (ok == -1) return false; - this->strace = fdopen(this->fdpair[0], "r"); - this->fd_strace = fileno(this->strace); + + FILE* fd = fdopen(fdpair[0], "r"); + if (!fd) + goto err; + + close(fdpair[1]); + + this->child = child; + this->strace = fd; return true; + +err: + close(fdpair[1]); + close(fdpair[0]); + return false; } void TraceScreen_updateTrace(InfoScreen* super) { TraceScreen* this = (TraceScreen*) super; - char buffer[1001]; + char buffer[1025]; + + int fd_strace = fileno(this->strace); + assert(fd_strace != -1); + fd_set fds; FD_ZERO(&fds); // FD_SET(STDIN_FILENO, &fds); - FD_SET(this->fd_strace, &fds); - struct timeval tv; - tv.tv_sec = 0; tv.tv_usec = 500; - int ready = select(this->fd_strace+1, &fds, NULL, NULL, &tv); - int nread = 0; - if (ready > 0 && FD_ISSET(this->fd_strace, &fds)) - nread = fread(buffer, 1, 1000, this->strace); + FD_SET(fd_strace, &fds); + + struct timeval tv = { .tv_sec = 0, .tv_usec = 500 }; + int ready = select(fd_strace + 1, &fds, NULL, NULL, &tv); + + size_t nread = 0; + if (ready > 0 && FD_ISSET(fd_strace, &fds)) + nread = fread(buffer, 1, sizeof(buffer) - 1, this->strace); + if (nread && this->tracing) { - char* line = buffer; + const char* line = buffer; buffer[nread] = '\0'; - for (int i = 0; i < nread; i++) { + for (size_t i = 0; i < nread; i++) { if (buffer[i] == '\n') { buffer[i] = '\0'; if (this->contLine) { @@ -124,16 +162,17 @@ void TraceScreen_updateTrace(InfoScreen* super) { } else { InfoScreen_addLine(&this->super, line); } - line = buffer+i+1; + line = buffer + i + 1; } } - if (line < buffer+nread) { + if (line < buffer + nread) { InfoScreen_addLine(&this->super, line); buffer[nread] = '\0'; this->contLine = true; } - if (this->follow) - Panel_setSelected(this->super.display, Panel_size(this->super.display)-1); + if (this->follow) { + Panel_setSelected(this->super.display, Panel_size(this->super.display) - 1); + } } } -- cgit v1.2.3