From adaeb0d6d246a06cfcf4727225452cc45df72017 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Thu, 3 Sep 2020 00:42:02 -0400 Subject: [PATCH] Use openpty(3) to open PTYs Use openpty(3) to open PTYs, to achieve compatibility with OpenBSD as posix_openpty() returns a file descriptor which does not block on read() nor select() --- examples/Makefile | 2 +- examples/daemon.c | 49 ++++++++++++++++++++++++++++++----------------- src/Makefile | 2 +- src/sock.c | 19 +++++++----------- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index ae8e0a4..e0682ad 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -5,7 +5,7 @@ CC = $(CROSS)cc INCLUDE_PATH = ../include CFLAGS += -I$(INCLUDE_PATH) -LDFLAGS = -L../src -lpatty +LDFLAGS = -L../src -lpatty -lutil EXAMPLES = daemon connect listen ax25dump decode diff --git a/examples/daemon.c b/examples/daemon.c index 8a51fb1..72197ba 100644 --- a/examples/daemon.c +++ b/examples/daemon.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include @@ -51,28 +51,39 @@ int main(int argc, char **argv) { usage(argc, argv, "Too many arguments provided"); } - info.fd = strcmp(argv[1], "/dev/ptmx") == 0? - posix_openpt(O_RDWR | O_NOCTTY): - open(argv[1], O_RDWR | O_NOCTTY); + if (strcmp(argv[1], "/dev/ptmx") == 0) { + int ptysub; + char ptyname[256]; - if (info.fd < 0) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], "open()", argv[1], strerror(errno)); - - goto error_open; - } - - if (isatty(info.fd) && grantpt(info.fd) == 0 && unlockpt(info.fd) == 0) { - char *pts; - - if ((pts = ptsname(info.fd)) == NULL) { - fprintf(stderr, "%s: %s: %s: %s\n", - argv[0], argv[1], "ptsname()", strerror(errno)); + if (openpty(&info.fd, &ptysub, ptyname, NULL, NULL) < 0) { + fprintf(stderr, "%s: %s: %s\n", + argv[0], "openpty()", strerror(errno)); goto error_open; } - fprintf(stderr, "pts %s\n", pts); + if (grantpt(info.fd) < 0) { + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], ptyname, "grantpt()", strerror(errno)); + + goto error_grantpt; + } + + if (unlockpt(info.fd) < 0) { + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], ptyname, "unlockpt()", strerror(errno)); + + goto error_unlockpt; + } + + fprintf(stderr, "pts %s\n", ptyname); + } else { + if ((info.fd = open(argv[1], O_RDWR | O_NOCTTY)) < 0) { + fprintf(stderr, "%s: %s: %s: %s\n", + argv[0], "open()", argv[1], strerror(errno)); + + goto error_open; + } } errno = 0; @@ -136,6 +147,8 @@ error_daemon_init: patty_daemon_destroy(daemon); error_daemon_new: +error_grantpt: +error_unlockpt: close(info.fd); error_open: diff --git a/src/Makefile b/src/Makefile index 91e3030..8a7853d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,7 +5,7 @@ HEADER_SUBDIR = patty CC = $(CROSS)cc CFLAGS = $(CGFLAGS) -fPIC -Wall -O2 -I$(INCLUDE_PATH) -LDFLAGS = +LDFLAGS = -lutil HEADERS = kiss.h ax25.h client.h ax25/if.h ax25/frame.h \ ax25/sock.h ax25/route.h ax25/server.h daemon.h list.h \ diff --git a/src/sock.c b/src/sock.c index 7226716..bb6b974 100644 --- a/src/sock.c +++ b/src/sock.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -17,10 +18,10 @@ struct slot { }; static int bind_pty(patty_ax25_sock *sock) { - char *pty; + int ptysub; - if ((sock->fd = posix_openpt(O_RDWR | O_NOCTTY)) < 0) { - goto error_open; + if (openpty(&sock->fd, &ptysub, sock->pty, NULL, NULL) < 0) { + goto error_openpty; } if (grantpt(sock->fd) < 0) { @@ -31,20 +32,14 @@ static int bind_pty(patty_ax25_sock *sock) { goto error_unlockpt; } - if ((pty = ptsname(sock->fd)) == NULL) { - goto error_ptsname; - } - - (void)strncpy(sock->pty, pty, sizeof(sock->pty)-1); - return 0; -error_ptsname: error_unlockpt: error_grantpt: - close(sock->fd); + (void)close(ptysub); + (void)close(sock->fd); -error_open: +error_openpty: return -1; }