diff options
author | Karel Zak | 2012-09-07 13:16:10 +0200 |
---|---|---|
committer | Karel Zak | 2012-09-07 13:18:22 +0200 |
commit | 7a84c0393a7bad637657579ae99b75bd5cb2982a (patch) | |
tree | 38c48b48f01f925d858a8522fe9d477838225fd7 /login-utils | |
parent | switch_root: check open() return value [coverity scan] (diff) | |
download | kernel-qcow2-util-linux-7a84c0393a7bad637657579ae99b75bd5cb2982a.tar.gz kernel-qcow2-util-linux-7a84c0393a7bad637657579ae99b75bd5cb2982a.tar.xz kernel-qcow2-util-linux-7a84c0393a7bad637657579ae99b75bd5cb2982a.zip |
sulogin: cleanup file descriptors usage
- use _FILENO macros rather than magic numbers
- check for dup() result
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'login-utils')
-rw-r--r-- | login-utils/sulogin.c | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/login-utils/sulogin.c b/login-utils/sulogin.c index d7d44b607..db783a2a0 100644 --- a/login-utils/sulogin.c +++ b/login-utils/sulogin.c @@ -433,7 +433,7 @@ static void fixtty(void) reset_virtual_console(&tp, fl); - if (tcsetattr(0, TCSADRAIN, &tp)) + if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tp)) warn(_("tcsetattr failed")); } @@ -522,10 +522,12 @@ int main(int argc, char **argv) if ((fd = open(tty, O_RDWR)) < 0) { warn(_("cannot open %s"), tty); - fd = dup(0); + fd = dup(STDIN_FILENO); } - if (!isatty(fd)) { + if (fd < 0) { + warn(_("cannot duplicate stdin file descriptor")); + } else if (!isatty(fd)) { warn(_("%s: not a tty"), tty); close(fd); } else { @@ -550,30 +552,31 @@ int main(int argc, char **argv) if (ttypgrp > 0) ioctl(0, TIOCNOTTY, (char *)1); sigaction(SIGHUP, &saved_sighup, NULL); - close(0); - close(1); - close(2); + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); if (fd > 2) close(fd); if ((fd = open(tty, O_RDWR|O_NOCTTY)) < 0) warn(_("cannot open %s"), tty); else { - ioctl(0, TIOCSCTTY, (char *)1); + ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1); tcsetpgrp(fd, ppgrp); - dup2(fd, 0); - dup2(fd, 1); - dup2(fd, 2); - if (fd > 2) + dup2(fd, STDIN_FILENO); + dup2(fd, STDOUT_FILENO); + dup2(fd, STDERR_FILENO); + + if (fd > STDERR_FILENO) close(fd); } } else - if (fd > 2) + if (fd > STDERR_FILENO) close(fd); } } else if (getpid() == 1) { /* We are init. We hence need to set a session anyway */ setsid(); - if (ioctl(0, TIOCSCTTY, (char *)1)) + if (ioctl(STDIN_FILENO, TIOCSCTTY, (char *)1)) warn(_("TIOCSCTTY: ioctl failed")); } |