fd = open("/dev/console", O_RDWR|O_NOCTTY);
if (fd < 0) {
ERROR(pty->ctx, "Failed to open a new terminal: %s\n", strerror(errno));
- return -errno;
+ r = -errno;
+ goto ERROR;
}
DEBUG(pty->ctx, "Opened a new terminal %d\n", fd);
r = dup2(fd, STDIN_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard input: %s\n", strerror(errno));
- return -errno;
+ r = -errno;
+ goto ERROR;
}
// Connect the new terminal to standard output
r = dup2(fd, STDOUT_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard output: %s\n", strerror(errno));
- return -errno;
+ r = -errno;
+ goto ERROR;
}
// Connect the new terminal to standard error
r = dup2(fd, STDERR_FILENO);
if (r < 0) {
ERROR(pty->ctx, "Failed to open standard error: %s\n", strerror(errno));
- return -errno;
+ r = -errno;
+ goto ERROR;
}
- return 0;
+ERROR:
+ if (fd >= 0)
+ close(fd);
+
+ return r;
}
/*