mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 00:56:10 +00:00 
			
		
		
		
	Instead of fixed size buffers for some messages, send only the string length.
This commit is contained in:
		
							
								
								
									
										66
									
								
								client.c
									
									
									
									
									
								
							
							
						
						
									
										66
									
								
								client.c
									
									
									
									
									
								
							@@ -488,33 +488,33 @@ client_write(int fd, const char *data, size_t size)
 | 
			
		||||
 | 
			
		||||
/* Dispatch imsgs when in wait state (before MSG_READY). */
 | 
			
		||||
int
 | 
			
		||||
client_dispatch_wait(void *data)
 | 
			
		||||
client_dispatch_wait(void *data0)
 | 
			
		||||
{
 | 
			
		||||
	struct imsg		imsg;
 | 
			
		||||
	ssize_t			n, datalen;
 | 
			
		||||
	struct msg_shell_data	shelldata;
 | 
			
		||||
	struct msg_exit_data	exitdata;
 | 
			
		||||
	struct msg_stdout_data	stdoutdata;
 | 
			
		||||
	struct msg_stderr_data	stderrdata;
 | 
			
		||||
	const char             *shellcmd = data;
 | 
			
		||||
	struct imsg		 imsg;
 | 
			
		||||
	char			*data;
 | 
			
		||||
	ssize_t			 n, datalen;
 | 
			
		||||
	struct msg_stdout_data	 stdoutdata;
 | 
			
		||||
	struct msg_stderr_data	 stderrdata;
 | 
			
		||||
	int			 retval;
 | 
			
		||||
 | 
			
		||||
	for (;;) {
 | 
			
		||||
		if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
 | 
			
		||||
			fatalx("imsg_get failed");
 | 
			
		||||
		if (n == 0)
 | 
			
		||||
			return (0);
 | 
			
		||||
 | 
			
		||||
		data = imsg.data;
 | 
			
		||||
		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 | 
			
		||||
 | 
			
		||||
		log_debug("got %d from server", imsg.hdr.type);
 | 
			
		||||
		switch (imsg.hdr.type) {
 | 
			
		||||
		case MSG_EXIT:
 | 
			
		||||
		case MSG_SHUTDOWN:
 | 
			
		||||
			if (datalen != sizeof exitdata) {
 | 
			
		||||
				if (datalen != 0)
 | 
			
		||||
					fatalx("bad MSG_EXIT size");
 | 
			
		||||
			} else {
 | 
			
		||||
				memcpy(&exitdata, imsg.data, sizeof exitdata);
 | 
			
		||||
				client_exitval = exitdata.retcode;
 | 
			
		||||
			if (datalen != sizeof retval && datalen != 0)
 | 
			
		||||
				fatalx("bad MSG_EXIT size");
 | 
			
		||||
			if (datalen == sizeof retval) {
 | 
			
		||||
				memcpy(&retval, data, sizeof retval);
 | 
			
		||||
				client_exitval = retval;
 | 
			
		||||
			}
 | 
			
		||||
			imsg_free(&imsg);
 | 
			
		||||
			return (-1);
 | 
			
		||||
@@ -534,15 +534,15 @@ client_dispatch_wait(void *data)
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_STDOUT:
 | 
			
		||||
			if (datalen != sizeof stdoutdata)
 | 
			
		||||
				fatalx("bad MSG_STDOUT");
 | 
			
		||||
			memcpy(&stdoutdata, imsg.data, sizeof stdoutdata);
 | 
			
		||||
				fatalx("bad MSG_STDOUT size");
 | 
			
		||||
			memcpy(&stdoutdata, data, sizeof stdoutdata);
 | 
			
		||||
 | 
			
		||||
			client_write(STDOUT_FILENO, stdoutdata.data, stdoutdata.size);
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_STDERR:
 | 
			
		||||
			if (datalen != sizeof stderrdata)
 | 
			
		||||
				fatalx("bad MSG_STDERR");
 | 
			
		||||
			memcpy(&stderrdata, imsg.data, sizeof stderrdata);
 | 
			
		||||
				fatalx("bad MSG_STDERR size");
 | 
			
		||||
			memcpy(&stderrdata, data, sizeof stderrdata);
 | 
			
		||||
 | 
			
		||||
			client_write(STDERR_FILENO, stderrdata.data, stderrdata.size);
 | 
			
		||||
			break;
 | 
			
		||||
@@ -558,14 +558,11 @@ client_dispatch_wait(void *data)
 | 
			
		||||
			imsg_free(&imsg);
 | 
			
		||||
			return (-1);
 | 
			
		||||
		case MSG_SHELL:
 | 
			
		||||
			if (datalen != sizeof shelldata)
 | 
			
		||||
				fatalx("bad MSG_SHELL size");
 | 
			
		||||
			memcpy(&shelldata, imsg.data, sizeof shelldata);
 | 
			
		||||
			shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
 | 
			
		||||
			if (data[datalen - 1] != '\0')
 | 
			
		||||
				fatalx("bad MSG_SHELL string");
 | 
			
		||||
 | 
			
		||||
			clear_signals(0);
 | 
			
		||||
 | 
			
		||||
			shell_exec(shelldata.shell, shellcmd);
 | 
			
		||||
			shell_exec(data, data0);
 | 
			
		||||
			/* NOTREACHED */
 | 
			
		||||
		case MSG_DETACH:
 | 
			
		||||
			client_write_server(MSG_EXITING, NULL, 0);
 | 
			
		||||
@@ -585,16 +582,18 @@ client_dispatch_wait(void *data)
 | 
			
		||||
int
 | 
			
		||||
client_dispatch_attached(void)
 | 
			
		||||
{
 | 
			
		||||
	struct imsg		imsg;
 | 
			
		||||
	struct msg_lock_data	lockdata;
 | 
			
		||||
	struct sigaction	sigact;
 | 
			
		||||
	ssize_t			n, datalen;
 | 
			
		||||
	struct imsg		 imsg;
 | 
			
		||||
	struct sigaction	 sigact;
 | 
			
		||||
	char			*data;
 | 
			
		||||
	ssize_t			 n, datalen;
 | 
			
		||||
 | 
			
		||||
	for (;;) {
 | 
			
		||||
		if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
 | 
			
		||||
			fatalx("imsg_get failed");
 | 
			
		||||
		if (n == 0)
 | 
			
		||||
			return (0);
 | 
			
		||||
 | 
			
		||||
		data = imsg.data;
 | 
			
		||||
		datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
 | 
			
		||||
 | 
			
		||||
		log_debug("got %d from server", imsg.hdr.type);
 | 
			
		||||
@@ -612,8 +611,7 @@ client_dispatch_attached(void)
 | 
			
		||||
			client_write_server(MSG_EXITING, NULL, 0);
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_EXIT:
 | 
			
		||||
			if (datalen != 0 &&
 | 
			
		||||
			    datalen != sizeof (struct msg_exit_data))
 | 
			
		||||
			if (datalen != 0 && datalen != sizeof (int))
 | 
			
		||||
				fatalx("bad MSG_EXIT size");
 | 
			
		||||
 | 
			
		||||
			client_write_server(MSG_EXITING, NULL, 0);
 | 
			
		||||
@@ -646,12 +644,10 @@ client_dispatch_attached(void)
 | 
			
		||||
			kill(getpid(), SIGTSTP);
 | 
			
		||||
			break;
 | 
			
		||||
		case MSG_LOCK:
 | 
			
		||||
			if (datalen != sizeof lockdata)
 | 
			
		||||
				fatalx("bad MSG_LOCK size");
 | 
			
		||||
			memcpy(&lockdata, imsg.data, sizeof lockdata);
 | 
			
		||||
			if (data[datalen - 1] != '\0')
 | 
			
		||||
				fatalx("bad MSG_LOCK string");
 | 
			
		||||
 | 
			
		||||
			lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
 | 
			
		||||
			system(lockdata.cmd);
 | 
			
		||||
			system(data);
 | 
			
		||||
			client_write_server(MSG_UNLOCK, NULL, 0);
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
 
 | 
			
		||||
@@ -695,8 +695,6 @@ server_client_repeat_timer(unused int fd, unused short events, void *data)
 | 
			
		||||
void
 | 
			
		||||
server_client_check_exit(struct client *c)
 | 
			
		||||
{
 | 
			
		||||
	struct msg_exit_data	exitdata;
 | 
			
		||||
 | 
			
		||||
	if (!(c->flags & CLIENT_EXIT))
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
@@ -707,9 +705,7 @@ server_client_check_exit(struct client *c)
 | 
			
		||||
	if (EVBUFFER_LENGTH(c->stderr_data) != 0)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	exitdata.retcode = c->retval;
 | 
			
		||||
	server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
 | 
			
		||||
 | 
			
		||||
	server_write_client(c, MSG_EXIT, &c->retval, sizeof c->retval);
 | 
			
		||||
	c->flags &= ~CLIENT_EXIT;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -995,16 +991,12 @@ server_client_msg_identify(
 | 
			
		||||
void
 | 
			
		||||
server_client_msg_shell(struct client *c)
 | 
			
		||||
{
 | 
			
		||||
	struct msg_shell_data	 data;
 | 
			
		||||
	const char		*shell;
 | 
			
		||||
	const char	*shell;
 | 
			
		||||
 | 
			
		||||
	shell = options_get_string(&global_s_options, "default-shell");
 | 
			
		||||
 | 
			
		||||
	if (*shell == '\0' || areshell(shell))
 | 
			
		||||
		shell = _PATH_BSHELL;
 | 
			
		||||
	if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
 | 
			
		||||
		strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
 | 
			
		||||
	server_write_client(c, MSG_SHELL, shell, strlen(shell) + 1);
 | 
			
		||||
 | 
			
		||||
	server_write_client(c, MSG_SHELL, &data, sizeof data);
 | 
			
		||||
	c->flags |= CLIENT_BAD;	/* it will die after exec */
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								server-fn.c
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								server-fn.c
									
									
									
									
									
								
							@@ -235,9 +235,8 @@ server_lock_session(struct session *s)
 | 
			
		||||
void
 | 
			
		||||
server_lock_client(struct client *c)
 | 
			
		||||
{
 | 
			
		||||
	const char		*cmd;
 | 
			
		||||
	size_t			 cmdlen;
 | 
			
		||||
	struct msg_lock_data	 lockdata;
 | 
			
		||||
	const char	*cmd;
 | 
			
		||||
	size_t		 cmdlen;
 | 
			
		||||
 | 
			
		||||
	if (c->flags & CLIENT_CONTROL)
 | 
			
		||||
		return;
 | 
			
		||||
@@ -246,8 +245,7 @@ server_lock_client(struct client *c)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	cmd = options_get_string(&c->session->options, "lock-command");
 | 
			
		||||
	cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
 | 
			
		||||
	if (cmdlen >= sizeof lockdata.cmd)
 | 
			
		||||
	if (strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
 | 
			
		||||
		return;
 | 
			
		||||
 | 
			
		||||
	tty_stop_tty(&c->tty);
 | 
			
		||||
@@ -256,7 +254,7 @@ server_lock_client(struct client *c)
 | 
			
		||||
	tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
 | 
			
		||||
 | 
			
		||||
	c->flags |= CLIENT_SUSPENDED;
 | 
			
		||||
	server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
 | 
			
		||||
	server_write_client(c, MSG_LOCK, cmd, strlen(cmd) + 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								tmux.h
									
									
									
									
									
								
							@@ -487,22 +487,10 @@ struct msg_identify_data {
 | 
			
		||||
	int		flags;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct msg_lock_data {
 | 
			
		||||
	char		cmd[COMMAND_LENGTH];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct msg_environ_data {
 | 
			
		||||
	char		var[ENVIRON_LENGTH];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct msg_shell_data {
 | 
			
		||||
	char		shell[MAXPATHLEN];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct msg_exit_data {
 | 
			
		||||
	int		retcode;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct msg_stdin_data {
 | 
			
		||||
	ssize_t	size;
 | 
			
		||||
	char	data[BUFSIZ];
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user