tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Patch: rfcomm_sppd + openpty



I've been running the below patch for about a year, which makes
rfcomm_sppd (bluetooth-related) optionally use openpty(3) when the
argument to -t is "auto".

I'm neither sure this is the right way to do it, nor that support for
manually specifying a tty is still needed at all.

Anyway I figured I toss it here for people to look at.  If it is
considered okay, I'd also update the man page to reflect the change.

It might be somewhat hackish.

Any comments?

CC plunky since they seem to have done most of the work on rfcomm_sppd.



diff --git a/usr.bin/rfcomm_sppd/Makefile b/usr.bin/rfcomm_sppd/Makefile
index 92aeded..8ca43ea 100644
--- a/usr.bin/rfcomm_sppd/Makefile
+++ b/usr.bin/rfcomm_sppd/Makefile
@@ -6,6 +6,6 @@ PROG=		rfcomm_sppd
 SRCS=		rfcomm_sppd.c
 
 DPADD+=		${LIBBLUETOOTH}
-LDADD+=		-lbluetooth
+LDADD+=		-lbluetooth -lutil
 
 .include <bsd.prog.mk>
diff --git a/usr.bin/rfcomm_sppd/rfcomm_sppd.c b/usr.bin/rfcomm_sppd/rfcomm_sppd.c
index 561b806..49b207c 100644
--- a/usr.bin/rfcomm_sppd/rfcomm_sppd.c
+++ b/usr.bin/rfcomm_sppd/rfcomm_sppd.c
@@ -84,10 +84,11 @@ __RCSID("$NetBSD: rfcomm_sppd.c,v 1.16 2013/12/09 09:35:17 wiz Exp $");
 #include <syslog.h>
 #include <termios.h>
 #include <unistd.h>
+#include <util.h>
 
 #include <netbt/rfcomm.h>
 
-static int open_tty(const char *);
+static int open_tty(const char *, char *, size_t);
 static int open_client(bdaddr_t *, bdaddr_t *, int, uintmax_t, const char *);
 static int open_server(bdaddr_t *, uint16_t, uint8_t, int, const char *);
 static void copy_data(int, int);
@@ -121,7 +122,7 @@ main(int argc, char *argv[])
 	bdaddr_t		laddr, raddr;
 	struct pollfd		pfd[2];
 	const char		*service;
-	char			*ep, *tty;
+	char			*ep, *tty, pts[PATH_MAX];
 	int			n, lm, rfcomm, tty_in, tty_out;
 	uint16_t		psm;
 	uint8_t			channel;
@@ -190,7 +191,7 @@ main(int argc, char *argv[])
 			break;
 
 		case 't': /* Slave TTY name */
-			if (optarg[0] != '/')
+			if (optarg[0] != '/' && strcmp(optarg, "auto") != 0)
 				asprintf(&tty, "%s%s", _PATH_DEV, optarg);
 			else
 				tty = optarg;
@@ -218,8 +219,13 @@ main(int argc, char *argv[])
 		tty_in = STDIN_FILENO;
 		tty_out = STDOUT_FILENO;
 	} else {
-		tty_in = open_tty(tty);
+		tty_in = open_tty(tty, pts, sizeof(pts));
 		tty_out = tty_in;
+		if (strcmp(tty, "auto") == 0) {
+			tty = pts;
+			puts(pts);
+			fflush(stdout);
+		}
 	}
 
 	/* open RFCOMM */
@@ -286,12 +292,25 @@ main(int argc, char *argv[])
 }
 
 static int
-open_tty(const char *tty)
+open_tty(const char *tty, char *pts, size_t pts_sz)
 {
 	char		 pty[PATH_MAX], *slash;
 	struct group	*gr = NULL;
 	gid_t		 ttygid;
-	int		 master;
+	int		 master, slave;
+
+	/*
+	 * Automatic pty via openpty(3), if requested
+	 */
+	if (strcmp(tty, "auto") == 0) {
+		if (openpty(&master, &slave, pty, NULL, NULL) == -1)
+			err(EXIT_FAILURE, "openpty");
+
+		if (pts && pts_sz && strlcpy(pts, pty, pts_sz) >= pts_sz)
+			errx(EXIT_FAILURE, "Pts name too long `%s'", pty);
+
+		return master;
+	}
 
 	/*
 	 * Construct master PTY name. The slave tty name must be less than



Home | Main Index | Thread Index | Old Index