Skip to content

Commit e9ab67b

Browse files
committed
Processing (Linux): interrupt poll & read syscalls when the child process is exited
Fix #1418
1 parent f073f48 commit e9ab67b

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/common/init.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,21 @@ static void resetConsole(void)
7676
}
7777

7878
#ifdef _WIN32
79-
BOOL WINAPI consoleHandler(DWORD signal)
79+
BOOL WINAPI consoleHandler(FF_MAYBE_UNUSED DWORD signal)
8080
{
81-
FF_UNUSED(signal);
8281
resetConsole();
8382
exit(0);
8483
}
8584
#else
86-
static void exitSignalHandler(int signal)
85+
static void exitSignalHandler(FF_MAYBE_UNUSED int signal)
8786
{
88-
FF_UNUSED(signal);
8987
resetConsole();
9088
exit(0);
9189
}
90+
static void chldSignalHandler(FF_MAYBE_UNUSED int signal)
91+
{
92+
// empty; used to interrupt the poll and read syscalls
93+
}
9294
#endif
9395

9496
void ffStart(void)
@@ -118,6 +120,7 @@ void ffStart(void)
118120
sigaction(SIGINT, &action, NULL);
119121
sigaction(SIGTERM, &action, NULL);
120122
sigaction(SIGQUIT, &action, NULL);
123+
sigaction(SIGCHLD, &(struct sigaction) { .sa_handler = chldSignalHandler }, NULL);
121124
#endif
122125

123126
//reset everything to default before we start printing

src/common/processing_linux.c

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "fastfetch.h"
22
#include "common/processing.h"
33
#include "common/io/io.h"
4-
#include "common/time.h"
54
#include "util/stringUtils.h"
65
#include "util/mallocHelper.h"
76

@@ -90,6 +89,16 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
9089
waitpid(childPid, NULL, 0);
9190
return "poll(&pollfd, 1, timeout) timeout (try increasing --processing-timeout)";
9291
}
92+
else if (errno == EINTR)
93+
{
94+
// The child process has been terminated. See `chldSignalHandler` in `common/init.c`
95+
if (waitpid(childPid, NULL, WNOHANG) == childPid)
96+
{
97+
// Read remaining data from the pipe
98+
fcntl(childPipeFd, F_SETFL, O_CLOEXEC | O_NONBLOCK);
99+
childPid = -1;
100+
}
101+
}
93102
else if (pollfd.revents & POLLERR)
94103
{
95104
kill(childPid, SIGTERM);
@@ -104,7 +113,7 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
104113
else if (nRead == 0)
105114
{
106115
int stat_loc = 0;
107-
if (waitpid(childPid, &stat_loc, 0) == childPid)
116+
if (childPid > 0 && waitpid(childPid, &stat_loc, 0) == childPid)
108117
{
109118
if (!WIFEXITED(stat_loc))
110119
return "child process exited abnormally";
@@ -113,10 +122,15 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
113122
// We only handle 127 as an error. See `getTerminalVersionUrxvt` in `terminalshell.c`
114123
return NULL;
115124
}
116-
return "waitpid() failed";
125+
return NULL;
117126
}
118127
else if (nRead < 0)
119-
break;
128+
{
129+
if (errno == EAGAIN)
130+
return NULL;
131+
else
132+
break;
133+
}
120134
};
121135

122136
return "read(childPipeFd, str, FF_PIPE_BUFSIZ) failed";

0 commit comments

Comments
 (0)