}
/* Set terminal settings to reasonable defaults */
-static void set_term(void)
+static void set_sane_term(void)
{
struct termios tty;
{
/* empty tty_name means "use init's tty", else... */
if (tty_name[0]) {
- close(0);
- if ((device_open(tty_name, O_RDWR)) < 0) {
- dup2(1, 0); /* restore fd #0 - avoid nasty surprises */
+ int fd = device_open(tty_name, O_RDWR);
+ if (fd < 0) {
message(L_LOG | L_CONSOLE, "Can't open %s: %s",
tty_name, strerror(errno));
if (fail)
#else
_exit(2);
#endif
+ } else {
+ dup2(fd, 0);
+ dup2(fd, 1);
+ dup2(fd, 2);
+ if (fd > 2) close(fd);
}
}
- close(1);
- close(2);
- set_term();
- dup(0);
- dup(0);
+ set_sane_term();
}
static pid_t run(const struct init_action *a)
/* Figure out where the default console should be */
console_init();
- set_term();
+ set_sane_term();
chdir("/");
setsid();
{