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()
This commit is contained in:
XANTRONIX Development 2020-09-03 00:42:02 -04:00 committed by XANTRONIX Industrial
parent 8f3bcc3d41
commit adaeb0d6d2
4 changed files with 40 additions and 32 deletions

View file

@ -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

View file

@ -3,7 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <termios.h>
#include <util.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -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:

View file

@ -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 \

View file

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
@ -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;
}