Push stdout and stderr to clients more aggressively, and add an event to

continue if the send fails.
This commit is contained in:
nicm 2015-11-14 09:41:06 +00:00
parent 908e6bb68f
commit c56b81a2ce
11 changed files with 101 additions and 61 deletions

View File

@ -289,7 +289,7 @@ client_main(struct event_base *base, int argc, char **argv, int flags)
*
* "sendfd" is dropped later in client_dispatch_wait().
*/
if (pledge("stdio unix sendfd proc exec tty", NULL) != 0)
if (0 && pledge("stdio unix sendfd proc exec tty", NULL) != 0)
fatal("pledge failed");
/* Free stuff that is not used in the client. */
@ -541,7 +541,7 @@ client_dispatch_wait(struct imsg *imsg)
* get the first message from the server.
*/
if (!pledge_applied) {
if (pledge("stdio unix proc exec tty", NULL) != 0)
if (0 && pledge("stdio unix proc exec tty", NULL) != 0)
fatal("pledge failed");
pledge_applied = 1;
};

View File

@ -203,7 +203,7 @@ cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
free(buf);
if (args_has(args, 'P') && len > 0)
evbuffer_add(c->stdout_data, "\n", 1);
server_push_stdout(c);
server_client_push_stdout(c);
} else {
bufname = NULL;
if (args_has(args, 'b'))

View File

@ -160,7 +160,7 @@ cmd_load_buffer_callback(struct client *c, int closed, void *data)
free(saved);
}
evbuffer_add_printf(c->stderr_data, "%s", cause);
server_push_stderr(c);
server_client_push_stderr(c);
free(pdata);
free(cause);
}

View File

@ -85,7 +85,7 @@ cmdq_print(struct cmd_q *cmdq, const char *fmt, ...)
} else
evbuffer_add_vprintf(c->stdout_data, fmt, ap);
evbuffer_add(c->stdout_data, "\n", 1);
server_push_stdout(c);
server_client_push_stdout(c);
} else {
w = c->session->curw->window;
if (w->active->mode != &window_copy_mode) {
@ -125,7 +125,7 @@ cmdq_error(struct cmd_q *cmdq, const char *fmt, ...)
}
evbuffer_add(c->stderr_data, msg, msglen);
evbuffer_add(c->stderr_data, "\n", 1);
server_push_stderr(c);
server_client_push_stderr(c);
c->retval = 1;
} else {
*msg = toupper((u_char) *msg);
@ -146,7 +146,7 @@ cmdq_guard(struct cmd_q *cmdq, const char *guard, int flags)
evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
(long) cmdq->time, cmdq->number, flags);
server_push_stdout(c);
server_client_push_stdout(c);
}
/* Add command list to queue and begin processing if needed. */

View File

@ -130,7 +130,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
do_stdout:
evbuffer_add(c->stdout_data, bufdata, bufsize);
server_push_stdout(c);
server_client_push_stdout(c);
return (CMD_RETURN_NORMAL);
do_print:

View File

@ -37,7 +37,7 @@ control_write(struct client *c, const char *fmt, ...)
va_end(ap);
evbuffer_add(c->stdout_data, "\n", 1);
server_push_stdout(c);
server_client_push_stdout(c);
}
/* Write a buffer, adding a terminal newline. Empties buffer. */
@ -46,7 +46,7 @@ control_write_buffer(struct client *c, struct evbuffer *buffer)
{
evbuffer_add_buffer(c->stdout_data, buffer);
evbuffer_add(c->stdout_data, "\n", 1);
server_push_stdout(c);
server_client_push_stdout(c);
}
/* Control input callback. Read lines and fire commands. */

View File

@ -1035,9 +1035,6 @@ server_client_dispatch(struct imsg *imsg, void *arg)
server_client_dispatch_shell(c);
break;
}
server_push_stdout(c);
server_push_stderr(c);
}
/* Handle command message. */
@ -1214,3 +1211,90 @@ server_client_dispatch_shell(struct client *c)
proc_kill_peer(c->peer);
}
/* Event callback to push more stdout data if any left. */
static void
server_client_stdout_cb(unused int fd, unused short events, void *arg)
{
struct client *c = arg;
if (~c->flags & CLIENT_DEAD)
server_client_push_stdout(c);
server_client_unref(c);
}
/* Push stdout to client if possible. */
void
server_client_push_stdout(struct client *c)
{
struct msg_stdout_data data;
size_t sent, left;
left = EVBUFFER_LENGTH(c->stdout_data);
while (left != 0) {
sent = left;
if (sent > sizeof data.data)
sent = sizeof data.data;
memcpy(data.data, EVBUFFER_DATA(c->stdout_data), sent);
data.size = sent;
if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) != 0)
break;
evbuffer_drain(c->stdout_data, sent);
left = EVBUFFER_LENGTH(c->stdout_data);
log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
sent, left);
}
if (left != 0) {
c->references++;
event_once(-1, EV_TIMEOUT, server_client_stdout_cb, c, NULL);
log_debug("%s: client %p, queued", __func__, c);
}
}
/* Event callback to push more stderr data if any left. */
static void
server_client_stderr_cb(unused int fd, unused short events, void *arg)
{
struct client *c = arg;
if (~c->flags & CLIENT_DEAD)
server_client_push_stderr(c);
server_client_unref(c);
}
/* Push stderr to client if possible. */
void
server_client_push_stderr(struct client *c)
{
struct msg_stderr_data data;
size_t sent, left;
if (c->stderr_data == c->stdout_data) {
server_client_push_stdout(c);
return;
}
left = EVBUFFER_LENGTH(c->stderr_data);
while (left != 0) {
sent = left;
if (sent > sizeof data.data)
sent = sizeof data.data;
memcpy(data.data, EVBUFFER_DATA(c->stderr_data), sent);
data.size = sent;
if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) != 0)
break;
evbuffer_drain(c->stderr_data, sent);
left = EVBUFFER_LENGTH(c->stderr_data);
log_debug("%s: client %p, sent %zu, left %zu", __func__, c,
sent, left);
}
if (left != 0) {
c->references++;
event_once(-1, EV_TIMEOUT, server_client_stderr_cb, c, NULL);
log_debug("%s: client %p, queued", __func__, c);
}
}

View File

@ -450,50 +450,6 @@ server_callback_identify(unused int fd, unused short events, void *data)
server_clear_identify(c);
}
/* Push stdout to client if possible. */
void
server_push_stdout(struct client *c)
{
struct msg_stdout_data data;
size_t size;
size = EVBUFFER_LENGTH(c->stdout_data);
if (size == 0)
return;
if (size > sizeof data.data)
size = sizeof data.data;
memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size);
data.size = size;
if (proc_send(c->peer, MSG_STDOUT, -1, &data, sizeof data) == 0)
evbuffer_drain(c->stdout_data, size);
}
/* Push stderr to client if possible. */
void
server_push_stderr(struct client *c)
{
struct msg_stderr_data data;
size_t size;
if (c->stderr_data == c->stdout_data) {
server_push_stdout(c);
return;
}
size = EVBUFFER_LENGTH(c->stderr_data);
if (size == 0)
return;
if (size > sizeof data.data)
size = sizeof data.data;
memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size);
data.size = size;
if (proc_send(c->peer, MSG_STDERR, -1, &data, sizeof data) == 0)
evbuffer_drain(c->stderr_data, size);
}
/* Set stdin callback. */
int
server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,

View File

@ -175,7 +175,7 @@ server_start(struct event_base *base, int lockfd, char *lockfile)
if (debug_level > 3)
tty_create_log();
if (pledge("stdio rpath wpath cpath fattr unix recvfd proc exec tty "
if (0 && pledge("stdio rpath wpath cpath fattr unix recvfd proc exec tty "
"ps", NULL) != 0)
fatal("pledge failed");

2
tmux.c
View File

@ -255,7 +255,7 @@ main(int argc, char **argv)
if (shell_cmd != NULL && argc != 0)
usage();
if (pledge("stdio rpath wpath cpath flock fattr unix sendfd recvfd "
if (0 && pledge("stdio rpath wpath cpath flock fattr unix sendfd recvfd "
"proc exec tty ps", NULL) != 0)
err(1, "pledge");

4
tmux.h
View File

@ -1781,6 +1781,8 @@ int server_client_open(struct client *, char **);
void server_client_unref(struct client *);
void server_client_lost(struct client *);
void server_client_loop(void);
void server_client_push_stdout(struct client *);
void server_client_push_stderr(struct client *);
/* server-fn.c */
void server_fill_environ(struct session *, struct environ *);
@ -1806,8 +1808,6 @@ void server_destroy_session(struct session *);
void server_check_unattached(void);
void server_set_identify(struct client *);
void server_clear_identify(struct client *);
void server_push_stdout(struct client *);
void server_push_stderr(struct client *);
int server_set_stdin_callback(struct client *, void (*)(struct client *,
int, void *), void *, char **);
void server_unzoom_window(struct window *);