mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	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").
This commit is contained in:
		@@ -53,13 +53,9 @@ void
 | 
			
		||||
server_client_create(int fd)
 | 
			
		||||
{
 | 
			
		||||
	struct client	*c;
 | 
			
		||||
	int		 mode;
 | 
			
		||||
	u_int		 i;
 | 
			
		||||
 | 
			
		||||
	if ((mode = fcntl(fd, F_GETFL)) == -1)
 | 
			
		||||
		fatal("fcntl failed");
 | 
			
		||||
	if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
 | 
			
		||||
		fatal("fcntl failed");
 | 
			
		||||
	setblocking(fd, 0);
 | 
			
		||||
 | 
			
		||||
	c = xcalloc(1, sizeof *c);
 | 
			
		||||
	c->references = 0;
 | 
			
		||||
@@ -124,16 +120,22 @@ server_client_lost(struct client *c)
 | 
			
		||||
	if (c->flags & CLIENT_TERMINAL)
 | 
			
		||||
		tty_free(&c->tty);
 | 
			
		||||
 | 
			
		||||
	if (c->stdin_fd != -1)
 | 
			
		||||
	if (c->stdin_fd != -1) {
 | 
			
		||||
		setblocking(c->stdin_fd, 1);
 | 
			
		||||
		close(c->stdin_fd);
 | 
			
		||||
	}
 | 
			
		||||
	if (c->stdin_event != NULL)
 | 
			
		||||
		bufferevent_free(c->stdin_event);
 | 
			
		||||
	if (c->stdout_fd != -1)
 | 
			
		||||
	if (c->stdout_fd != -1) {
 | 
			
		||||
		setblocking(c->stdout_fd, 1);
 | 
			
		||||
		close(c->stdout_fd);
 | 
			
		||||
	}
 | 
			
		||||
	if (c->stdout_event != NULL)
 | 
			
		||||
		bufferevent_free(c->stdout_event);
 | 
			
		||||
	if (c->stderr_fd != -1)
 | 
			
		||||
	if (c->stderr_fd != -1) {
 | 
			
		||||
		setblocking(c->stderr_fd, 1);
 | 
			
		||||
		close(c->stderr_fd);
 | 
			
		||||
	}
 | 
			
		||||
	if (c->stderr_event != NULL)
 | 
			
		||||
		bufferevent_free(c->stderr_event);
 | 
			
		||||
 | 
			
		||||
@@ -632,6 +634,7 @@ server_client_in_callback(
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
 | 
			
		||||
	setblocking(c->stdin_fd, 1);
 | 
			
		||||
	close(c->stdin_fd);
 | 
			
		||||
	c->stdin_fd = -1;
 | 
			
		||||
 | 
			
		||||
@@ -647,6 +650,7 @@ server_client_out_callback(
 | 
			
		||||
	struct client	*c = data;
 | 
			
		||||
 | 
			
		||||
	bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
 | 
			
		||||
	setblocking(c->stdout_fd, 1);
 | 
			
		||||
	close(c->stdout_fd);
 | 
			
		||||
	c->stdout_fd = -1;
 | 
			
		||||
}
 | 
			
		||||
@@ -659,6 +663,7 @@ server_client_err_callback(
 | 
			
		||||
	struct client	*c = data;
 | 
			
		||||
 | 
			
		||||
	bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
 | 
			
		||||
	setblocking(c->stderr_fd, 1);
 | 
			
		||||
	close(c->stderr_fd);
 | 
			
		||||
	c->stderr_fd = -1;
 | 
			
		||||
}
 | 
			
		||||
@@ -672,7 +677,6 @@ server_client_msg_dispatch(struct client *c)
 | 
			
		||||
	struct msg_identify_data identifydata;
 | 
			
		||||
	struct msg_environ_data	 environdata;
 | 
			
		||||
	ssize_t			 n, datalen;
 | 
			
		||||
	int			 mode;
 | 
			
		||||
 | 
			
		||||
	if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
 | 
			
		||||
		return (-1);
 | 
			
		||||
@@ -712,9 +716,7 @@ server_client_msg_dispatch(struct client *c)
 | 
			
		||||
			    NULL, NULL, server_client_in_callback, c);
 | 
			
		||||
			if (c->stdin_event == NULL)
 | 
			
		||||
				fatalx("failed to create stdin event");
 | 
			
		||||
 | 
			
		||||
			if ((mode = fcntl(c->stdin_fd, F_GETFL)) != -1)
 | 
			
		||||
				fcntl(c->stdin_fd, F_SETFL, mode|O_NONBLOCK);
 | 
			
		||||
			setblocking(c->stdin_fd, 0);
 | 
			
		||||
 | 
			
		||||
			server_client_msg_identify(c, &identifydata, imsg.fd);
 | 
			
		||||
			break;
 | 
			
		||||
@@ -729,9 +731,8 @@ server_client_msg_dispatch(struct client *c)
 | 
			
		||||
			    NULL, NULL, server_client_out_callback, c);
 | 
			
		||||
			if (c->stdout_event == NULL)
 | 
			
		||||
				fatalx("failed to create stdout event");
 | 
			
		||||
			setblocking(c->stdout_fd, 0);
 | 
			
		||||
 | 
			
		||||
			if ((mode = fcntl(c->stdout_fd, F_GETFL)) != -1)
 | 
			
		||||
				fcntl(c->stdout_fd, F_SETFL, mode|O_NONBLOCK);
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_STDERR:
 | 
			
		||||
			if (datalen != 0)
 | 
			
		||||
@@ -744,9 +745,8 @@ server_client_msg_dispatch(struct client *c)
 | 
			
		||||
			    NULL, NULL, server_client_err_callback, c);
 | 
			
		||||
			if (c->stderr_event == NULL)
 | 
			
		||||
				fatalx("failed to create stderr event");
 | 
			
		||||
			setblocking(c->stderr_fd, 0);
 | 
			
		||||
 | 
			
		||||
			if ((mode = fcntl(c->stderr_fd, F_GETFL)) != -1)
 | 
			
		||||
				fcntl(c->stderr_fd, F_SETFL, mode|O_NONBLOCK);
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_RESIZE:
 | 
			
		||||
			if (datalen != 0)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user