From 7cf7591f0add44fe538ca33dd9fdba749d82c438 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 12 Apr 2014 15:15:40 +0200 Subject: [PATCH] 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 --- helpers.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/helpers.c b/helpers.c index cfa1c17e..904f2cc7 100644 --- a/helpers.c +++ b/helpers.c @@ -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); }