Use openpty for cross-platform pty setup and improve startup errors
This commit is contained in:
parent
1cd0da319c
commit
2459c4559d
3 changed files with 59 additions and 37 deletions
7
Makefile
7
Makefile
|
|
@ -5,10 +5,15 @@ PREFIX ?= /usr/local
|
||||||
BINDIR = $(PREFIX)/bin
|
BINDIR = $(PREFIX)/bin
|
||||||
MANDIR ?= $(PREFIX)/share/man
|
MANDIR ?= $(PREFIX)/share/man
|
||||||
|
|
||||||
|
# openpty(3) lives in libutil on the BSDs, but in libc on Linux and macOS
|
||||||
|
# (and in musl). Resolve it in the shell so this Makefile works with both
|
||||||
|
# GNU make and BSD make.
|
||||||
|
PTY_LIB_SH = uname -s | grep -Eq 'BSD|DragonFly' && echo -lutil
|
||||||
|
|
||||||
all: murx
|
all: murx
|
||||||
|
|
||||||
murx: murx.c
|
murx: murx.c
|
||||||
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ murx.c $(LDLIBS)
|
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ murx.c $(LDLIBS) `$(PTY_LIB_SH)`
|
||||||
|
|
||||||
install: murx murx.1
|
install: murx murx.1
|
||||||
install -d "$(DESTDIR)$(BINDIR)" "$(DESTDIR)$(MANDIR)/man1"
|
install -d "$(DESTDIR)$(BINDIR)" "$(DESTDIR)$(MANDIR)/man1"
|
||||||
|
|
|
||||||
|
|
@ -370,7 +370,9 @@ This is "very basic" by design:
|
||||||
|
|
||||||
## Build notes
|
## Build notes
|
||||||
|
|
||||||
Requires a C99 compiler and POSIX. Build with `make`. Tested with Clang and
|
Requires a C99 compiler and POSIX. Build with `make`. Runs on Linux, macOS,
|
||||||
|
FreeBSD and OpenBSD (using `openpty(3)` for panes; the Makefile links `-lutil`
|
||||||
|
automatically on the BSDs where it is needed). Tested with Clang and
|
||||||
GCC 16; `grid_resize()` carries a guarded workaround for a GCC 16 optimizer bug
|
GCC 16; `grid_resize()` carries a guarded workaround for a GCC 16 optimizer bug
|
||||||
that otherwise collapses pane grids to 1x1.
|
that otherwise collapses pane grids to 1x1.
|
||||||
|
|
||||||
|
|
|
||||||
79
murx.c
79
murx.c
|
|
@ -35,7 +35,7 @@
|
||||||
* murx [session] attach to session (default "0"), or start it
|
* murx [session] attach to session (default "0"), or start it
|
||||||
* murx -h|--help show usage and all Ctrl-A shortcut chains
|
* murx -h|--help show usage and all Ctrl-A shortcut chains
|
||||||
*
|
*
|
||||||
* Pure C99 + POSIX (macOS / Linux / FreeBSD).
|
* Pure C99 + POSIX (macOS / Linux / FreeBSD / OpenBSD).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
@ -58,6 +58,14 @@
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
/* openpty(3) lives in <pty.h> on Linux and glibc, in <util.h> on the BSDs
|
||||||
|
and macOS. */
|
||||||
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__NetBSD__) || \
|
||||||
|
defined(__OpenBSD__) || defined(__APPLE__)
|
||||||
|
#include <util.h>
|
||||||
|
#else
|
||||||
|
#include <pty.h>
|
||||||
|
#endif
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
@ -1460,19 +1468,17 @@ static void pane_free(Pane *p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static Pane *pane_new(const char *cwd, int w, int h, const char *title) {
|
static Pane *pane_new(const char *cwd, int w, int h, const char *title) {
|
||||||
int master = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
struct winsize iws;
|
||||||
if (master < 0) return NULL;
|
memset(&iws, 0, sizeof iws);
|
||||||
if (grantpt(master) < 0 || unlockpt(master) < 0) {
|
iws.ws_col = tty_dimension(w > 0 ? w : 80);
|
||||||
close(master);
|
iws.ws_row = tty_dimension(h > 0 ? h : 24);
|
||||||
return NULL;
|
|
||||||
}
|
/* openpty() works on Linux, macOS and all BSDs. Linux exposes only
|
||||||
const char *slave = ptsname(master);
|
/dev/ptmx, while OpenBSD and friends use /dev/pty*; letting libc pick
|
||||||
if (!slave) {
|
the pty avoids hardcoding either scheme. */
|
||||||
close(master);
|
int master = -1, slave_fd = -1;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
char slave_path[PATH_MAX];
|
char slave_path[PATH_MAX];
|
||||||
snprintf(slave_path, sizeof slave_path, "%s", slave);
|
if (openpty(&master, &slave_fd, slave_path, NULL, &iws) < 0) return NULL;
|
||||||
|
|
||||||
const char *shell = getenv("SHELL");
|
const char *shell = getenv("SHELL");
|
||||||
if (!shell || !*shell) {
|
if (!shell || !*shell) {
|
||||||
|
|
@ -1480,19 +1486,11 @@ static Pane *pane_new(const char *cwd, int w, int h, const char *title) {
|
||||||
else shell = "/bin/sh";
|
else shell = "/bin/sh";
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Open the slave and give it its size before forking. The server keeps
|
/* openpty() gave the slave its size. The server keeps this fd for the
|
||||||
this fd for the pane's lifetime: a pty with no slave open would report
|
pane's lifetime: a pty with no slave open would report EOF on the
|
||||||
EOF on the master (and on macOS TIOCSWINSZ only works on the slave). */
|
master. Make it non-blocking so the server can drain it with poll(). */
|
||||||
struct winsize iws;
|
int flags = fcntl(slave_fd, F_GETFL, 0);
|
||||||
memset(&iws, 0, sizeof iws);
|
if (flags >= 0) fcntl(slave_fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
iws.ws_col = tty_dimension(w > 0 ? w : 80);
|
|
||||||
iws.ws_row = tty_dimension(h > 0 ? h : 24);
|
|
||||||
int slave_fd = open(slave_path, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
|
||||||
if (slave_fd < 0) {
|
|
||||||
close(master);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
ioctl(slave_fd, TIOCSWINSZ, &iws);
|
|
||||||
|
|
||||||
pid_t pid = fork();
|
pid_t pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
|
|
@ -3798,8 +3796,8 @@ static void server_start(const char *path, const char *name, int readyfd, const
|
||||||
|
|
||||||
Window *w = window_new(s, NULL);
|
Window *w = window_new(s, NULL);
|
||||||
if (!w) {
|
if (!w) {
|
||||||
|
server_fail(readyfd, "cannot create pane (openpty)", errno);
|
||||||
server_cleanup(s);
|
server_cleanup(s);
|
||||||
if (readyfd >= 0) close(readyfd);
|
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
s->cur = w;
|
s->cur = w;
|
||||||
|
|
@ -4006,18 +4004,33 @@ static int spawn_server(const char *path, const char *name, const DisplayOptions
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
close(p[1]);
|
close(p[1]);
|
||||||
char ok = 0;
|
char msg[256];
|
||||||
|
size_t used = 0;
|
||||||
ssize_t got;
|
ssize_t got;
|
||||||
do {
|
for (;;) {
|
||||||
got = read(p[0], &ok, 1);
|
got = read(p[0], msg + used, sizeof msg - 1 - used);
|
||||||
} while (got < 0 && errno == EINTR);
|
if (got > 0) {
|
||||||
|
used += (size_t)got;
|
||||||
|
if (used >= sizeof msg - 1) break;
|
||||||
|
} else if (got < 0 && errno == EINTR) {
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
close(p[0]);
|
close(p[0]);
|
||||||
|
msg[used] = '\0';
|
||||||
waitpid(pid, NULL, 0);
|
waitpid(pid, NULL, 0);
|
||||||
flock(lock, LOCK_UN);
|
flock(lock, LOCK_UN);
|
||||||
close(lock);
|
close(lock);
|
||||||
|
|
||||||
if (got != 1 || ok != '1') {
|
if (used < 1 || msg[0] != '1') {
|
||||||
|
if (used > 0) {
|
||||||
|
fprintf(stderr, "murx: server failed to start: %s\n", msg);
|
||||||
|
errno = 0; /* already reported */
|
||||||
|
} else {
|
||||||
errno = EIO;
|
errno = EIO;
|
||||||
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < 300; i++) {
|
for (int i = 0; i < 300; i++) {
|
||||||
|
|
@ -4414,8 +4427,10 @@ int main(int argc, char **argv) {
|
||||||
int fd = connect_session(path);
|
int fd = connect_session(path);
|
||||||
if (fd < 0) fd = spawn_server(path, name, &display);
|
if (fd < 0) fd = spawn_server(path, name, &display);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
|
if (errno != 0) {
|
||||||
fprintf(stderr, "murx: cannot start or attach session '%s': %s\n",
|
fprintf(stderr, "murx: cannot start or attach session '%s': %s\n",
|
||||||
name, strerror(errno));
|
name, strerror(errno));
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return run_client(fd, name, prefix, status);
|
return run_client(fd, name, prefix, status);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue