Subject: timeout: Program called by timeout cannot interact with tty stdin when timeout is called by exec()

From: Zhaofeng Yang <[email-masked]>
Date: Sat, 02 Apr 2022 18:22:06 +0800

Hi GNU Team,

I found that program called by timeout cannot interact with tty stdin
when timeout is called by exec().

A simplest example is `timeout 10 timeout 5 cat`. cat cannot read input
from tty stdin.

I also tried to run `timeout 5 cat` in other programs by exec(), and
all of them cannot read tty stdin. For example,

import subprocess
subprocess.run(f"timeout 5 cat", shell=True)

with Python 3.

I guessed it is a problem with exec(), but trying with the simple C
program

/* system.c */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(const int argc, const char* argv[]){
	size_t cmd_length = argc;
	for(int i=1; i<argc; i++){
		cmd_length += strlen(argv[i]);
	}
	char* cmd_alloc = calloc(cmd_length, sizeof(char));
	char* cmd = cmd_alloc;
	for(int i=1; i<argc; i++){
		strncpy(cmd_alloc, argv[i], strlen(argv[i]));
		cmd_alloc += strlen(argv[i]);
		cmd_alloc[0] = ' ';
		cmd_alloc++;
	}
	cmd_alloc[ cmd_alloc != cmd ? -1 : 0 ] = '\0';
	//printf("%s\n", cmd);
	system(cmd);
	free(cmd);
	return 0;
}

with `./system ./system cat` it works as expected, while running
`./system timeout 5 cat` the bug occurs.

This bug (or is it a feature?) appears in coreutils 8.32 and 9.0, as I
have tested.
-- 
Zhaofeng Yang <[email-masked]>