Make sure that the pager terminates before the process that feeds it.

Since the pager was spawned as child process, it does not notice when
the parent (which feeds its stdin) terminates. If the pager is 'less'
it so looses control over the terminal and cannot be controled by
the user anymore. For this reason, we register a function atexit()
that explicitly waits for the pager to terminate.
This commit is contained in:
Johannes Sixt
2007-01-18 14:18:11 +01:00
parent 85e94b981e
commit 16ea9c7aa3

13
pager.c
View File

@@ -12,6 +12,13 @@ static void run_pager(const char *pager)
execlp(pager, pager, NULL);
execl("/bin/sh", "sh", "-c", pager, NULL);
}
#else
static pid_t pager_pid;
static void collect_pager(void)
{
close(1); /* signals EOF to pager */
cwait(NULL, pager_pid, 0);
}
#endif
void setup_pager(void)
@@ -65,11 +72,15 @@ void setup_pager(void)
#else
/* spawn the pager */
pager_argv[2] = pager;
if (spawnvpe_pipe(pager_argv[0], pager_argv, environ, fd, NULL) < 0)
pager_pid = spawnvpe_pipe(pager_argv[0], pager_argv, environ, fd, NULL);
if (pager_pid < 0)
return;
/* original process continues, but writes to the pipe */
dup2(fd[1], 1);
close(fd[1]);
/* this makes sure that the parent terminates after the pager */
atexit(collect_pager);
#endif
}