diff --git a/tools/hciattach.c b/tools/hciattach.c
index da154df..4dc5be5 100644
--- a/tools/hciattach.c
+++ b/tools/hciattach.c
if (tcgetattr(fd, &ti) < 0) {
perror("Can't get port settings");
- return -1;
+ goto fail;
}
cfmakeraw(&ti);
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
perror("Can't set port settings");
- return -1;
+ goto fail;
}
/* Set initial baudrate */
if (set_speed(fd, &ti, u->init_speed) < 0) {
perror("Can't set initial baud rate");
- return -1;
+ goto fail;
}
tcflush(fd, TCIOFLUSH);
}
if (u->init && u->init(fd, u, &ti) < 0)
- return -1;
+ goto fail;
tcflush(fd, TCIOFLUSH);
/* Set actual baudrate */
if (set_speed(fd, &ti, u->speed) < 0) {
perror("Can't set baud rate");
- return -1;
+ goto fail;
}
/* Set TTY to N_HCI line discipline */
i = N_HCI;
if (ioctl(fd, TIOCSETD, &i) < 0) {
perror("Can't set line discipline");
- return -1;
+ goto fail;
}
if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
perror("Can't set UART flags");
- return -1;
+ goto fail;
}
if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
perror("Can't set device");
- return -1;
+ goto fail;
}
if (u->post && u->post(fd, u, &ti) < 0)
- return -1;
+ goto fail;
return fd;
+
+fail:
+ close(fd);
+ return -1;
}
static void usage(void)