diff options
author | Benny Baumann <BenBE@geshi.org> | 2020-09-15 22:07:52 +0200 |
---|---|---|
committer | cgzones <cgzones@googlemail.com> | 2020-09-17 21:43:05 +0200 |
commit | 443a9437984c36b13986875c33818767cdadd17d (patch) | |
tree | f27bb58095ec1b45d9442edaf5fcec11b80721de /OpenFilesScreen.c | |
parent | 6921000481d3f8e3e0fecf89df436312efaa691a (diff) |
Properly close pipe handles when work is done
Diffstat (limited to 'OpenFilesScreen.c')
-rw-r--r-- | OpenFilesScreen.c | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index ae32590f..323913dd 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -53,18 +53,21 @@ void OpenFilesScreen_draw(InfoScreen* this) { static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData)); - OpenFiles_FileData* fdata = NULL; - OpenFiles_Data* item = &(pdata->data); - int fdpair[2]; + + int fdpair[2] = {0, 0}; if (pipe(fdpair) == -1) { pdata->error = 1; return pdata; } + pid_t child = fork(); if (child == -1) { + close(fdpair[1]); + close(fdpair[0]); pdata->error = 1; return pdata; } + if (child == 0) { close(fdpair[0]); dup2(fdpair[1], STDOUT_FILENO); @@ -80,12 +83,17 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { exit(127); } close(fdpair[1]); + + OpenFiles_Data* item = &(pdata->data); + OpenFiles_FileData* fdata = NULL; + FILE* fd = fdopen(fdpair[0], "r"); for (;;) { char* line = String_readLine(fd); if (!line) { break; } + unsigned char cmd = line[0]; if (cmd == 'f') { OpenFiles_FileData* nextFile = xCalloc(1, sizeof(OpenFiles_FileData)); @@ -101,15 +109,18 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { free(line); } fclose(fd); + int wstatus; if (waitpid(child, &wstatus, 0) == -1) { pdata->error = 1; return pdata; } + if (!WIFEXITED(wstatus)) pdata->error = 1; else pdata->error = WEXITSTATUS(wstatus); + return pdata; } |