run_getty(): Handle STDIO descriptors better.

The cppcheck tool raised an alert to the missing close() for
stdin when a console is opened for a getty.  This patch fixes
that warning and also makes the code slighlty more readable.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2014-04-12 15:15:43 +02:00
parent 143a257c25
commit 7cf7591f0a
+9 -5
View File
@@ -511,7 +511,7 @@ pid_t run_getty(char *cmd, char *args[], int console)
pid_t pid = fork();
if (!pid) {
int i;
int i, fd;
char c;
sigset_t nmask;
struct sigaction sa;
@@ -520,12 +520,13 @@ pid_t run_getty(char *cmd, char *args[], int console)
/* Detach from initial controlling TTY */
vhangup();
close(2);
close(1);
close(0);
close(STDERR_FILENO);
close(STDOUT_FILENO);
close(STDIN_FILENO);
/* Attach TTY to console */
if (open(CONSOLE, O_RDWR) != 0)
fd = open(CONSOLE, O_RDWR);
if (fd != STDIN_FILENO)
exit(1);
dup2(0, STDIN_FILENO);
@@ -561,6 +562,9 @@ pid_t run_getty(char *cmd, char *args[], int console)
execv(cmd, args);
}
if (console)
close(fd);
exit(0);
}