From 4266a340480e27258cc6575570db59331a2beaa4 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 8 Jan 2011 01:52:38 +0000 Subject: Move all calls to fcntl(...O_NONBLOCK) into a function and clear the flag on the stdio file descriptors before closing them (fixes things like "tmux ls && cat"). --- usr.bin/tmux/tmux.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'usr.bin/tmux/tmux.c') diff --git a/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c index 58ea6662bc3..5c994dfdd47 100644 --- a/usr.bin/tmux/tmux.c +++ b/usr.bin/tmux/tmux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.c,v 1.100 2011/01/03 23:35:21 nicm Exp $ */ +/* $OpenBSD: tmux.c,v 1.101 2011/01/08 01:52:37 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -194,12 +194,25 @@ makesocketpath(const char *label) return (path); } +void +setblocking(int fd, int state) +{ + int mode; + + if ((mode = fcntl(fd, F_GETFL)) != -1) { + if (!state) + mode |= O_NONBLOCK; + else + mode &= ~O_NONBLOCK; + fcntl(fd, F_SETFL, mode); + } +} + __dead void shell_exec(const char *shell, const char *shellcmd) { const char *shellname, *ptr; char *argv0; - int mode; ptr = strrchr(shell, '/'); if (ptr != NULL && *(ptr + 1) != '\0') @@ -212,12 +225,9 @@ shell_exec(const char *shell, const char *shellcmd) xasprintf(&argv0, "%s", shellname); setenv("SHELL", shell, 1); - if ((mode = fcntl(STDIN_FILENO, F_GETFL)) != -1) - fcntl(STDIN_FILENO, F_SETFL, mode & ~O_NONBLOCK); - if ((mode = fcntl(STDOUT_FILENO, F_GETFL)) != -1) - fcntl(STDOUT_FILENO, F_SETFL, mode & ~O_NONBLOCK); - if ((mode = fcntl(STDERR_FILENO, F_GETFL)) != -1) - fcntl(STDERR_FILENO, F_SETFL, mode & ~O_NONBLOCK); + setblocking(STDIN_FILENO, 1); + setblocking(STDOUT_FILENO, 1); + setblocking(STDERR_FILENO, 1); closefrom(STDERR_FILENO + 1); execl(shell, argv0, "-c", shellcmd, (char *) NULL); -- cgit v1.2.3