Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions src/ks_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,21 @@ static void __set_blocking(int fd, int blocking)
static ks_bool_t wakeup_stdout()
{
char procpath[256];
FILE *fp = NULL;
fd_set set;
struct timeval timeout;
int fd;
ks_bool_t done_reading = KS_FALSE;
char buf[1024];
size_t skipped = 0;

snprintf(procpath, sizeof(procpath), "/proc/%d/fd/%d", getpid(), fileno(stdout));
if ((fp = fopen(procpath, "r")) == NULL) {
if ((fd = open(procpath, O_RDONLY)) < 0) {
g_wakeup_stdout_fails++;
return KS_FALSE;
}

timeout.tv_sec = 0;
timeout.tv_usec = 0;

while (!done_reading) {
FD_ZERO(&set);
FD_SET(fileno(fp), &set);

if (select(fileno(fp) + 1, &set, NULL, NULL, &timeout) != 1) {
done_reading = KS_TRUE;
} else {
size_t consumed = fread(buf, 1, sizeof(buf), fp);
skipped += consumed;
done_reading = consumed < sizeof(buf);
}
}
fclose(fp);
__set_blocking(fd, 0);
// read until EAGAIN
while (read(fd, buf, sizeof(buf)) > 0);
close(fd);

g_wakeup_stdout_successes++;
return KS_TRUE;
Expand Down