Open
Description
Connecting to UNIX sockets comes in be handy with QEMU, for example. Like when you have a bunch of VMs and want to connect to a serial console:
$ tio ~/VM/vm1.console
Quick POC shows that everything works, I'm able to read/write to a serial console with a simple diff:
diff --git a/src/tty.c b/src/tty.c
index 8c7bfa7..65c0289 100644
--- a/src/tty.c
+++ b/src/tty.c
@@ -1085,10 +1085,19 @@ int tty_connect(void)
struct timeval tv;
struct timeval *tv_p = &tv;
bool ignore_stdin = false;
+ struct sockaddr_un addr;
- /* Open tty device */
- fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK);
- if (fd < 0)
+ memset(&addr, 0, sizeof(addr));
+ addr.sun_family = AF_UNIX;
+ strncpy(addr.sun_path, option.tty_device, sizeof(addr.sun_path) - 1);
+
+ fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd == -1) {
+ tio_error_printf("Cannot allocate socket (%s)", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
tio_error_printf_silent("Could not open tty device (%s)", strerror(errno));
goto error_open;
@@ -1098,7 +1107,9 @@ int tty_connect(void)
if (!isatty(fd))
{
tio_error_printf("Not a tty device");
+#if 0
exit(EXIT_FAILURE);;
+#endif
}
/* Lock device file */
@@ -1139,7 +1150,9 @@ int tty_connect(void)
if (tcgetattr(fd, &tio_old) < 0)
{
tio_error_printf_silent("Could not get port settings (%s)", strerror(errno));
+#if 0
goto error_tcgetattr;
+#endif
}
#ifdef HAVE_IOSSIOSPEED
@@ -1169,7 +1182,9 @@ int tty_connect(void)
if (status == -1)
{
tio_error_printf_silent("Could not apply port settings (%s)", strerror(errno));
+#if 0
goto error_tcsetattr;
+#endif
}
/* Set arbitrary baudrate (only works on supported platforms) */
@@ -1178,7 +1193,9 @@ int tty_connect(void)
if (setspeed(fd, option.baudrate) != 0)
{
tio_error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
+#if 0
goto error_setspeed;
+#endif
}
}
@@ -1394,9 +1411,11 @@ int tty_connect(void)
return TIO_SUCCESS;
+#if 0
error_setspeed:
error_tcsetattr:
error_tcgetattr:
+#endif
error_read:
tty_disconnect();
error_open:
However it's necessary not call all TTY-related or else everything goes boom. I suspect that it's best to factor out two functions , one for each method. Anyway, if you are okay with the idea, I'll make a proper merge request.