Sync OpenBSD patchset 202:

There aren't many client message types or code to handle them so get rid of
the lookup table and use a switch, merge the tiny handler functions into it,
and move the whole lot to client.c.

Also change client_msg_dispatch to consume as many messages as possible and
move the call to it to the right place so it checks for signals afterwards.

Prompted by suggestions from eric@.
pull/1/head
Tiago Cunha 2009-07-30 20:57:39 +00:00
parent 817e93ac94
commit 67266dc25c
4 changed files with 90 additions and 175 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client-fn.c,v 1.8 2009-07-30 20:21:55 tcunha Exp $ */
/* $Id: client-fn.c,v 1.9 2009-07-30 20:57:39 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -20,6 +20,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tmux.h"
@ -74,3 +75,23 @@ client_write_server(
if (buf != NULL && len > 0)
buffer_write(cctx->srv_out, buf, len);
}
void
client_suspend(void)
{
struct sigaction act;
memset(&act, 0, sizeof act);
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_DFL;
if (sigaction(SIGTSTP, &act, NULL) != 0)
fatal("sigaction failed");
act.sa_handler = sighandler;
if (sigaction(SIGCONT, &act, NULL) != 0)
fatal("sigaction failed");
kill(getpid(), SIGTSTP);
}

View File

@ -1,154 +0,0 @@
/* $Id: client-msg.c,v 1.23 2009-07-30 20:21:55 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tmux.h"
int client_msg_fn_detach(struct hdr *, struct client_ctx *);
int client_msg_fn_error(struct hdr *, struct client_ctx *);
int client_msg_fn_shutdown(struct hdr *, struct client_ctx *);
int client_msg_fn_exit(struct hdr *, struct client_ctx *);
int client_msg_fn_exited(struct hdr *, struct client_ctx *);
int client_msg_fn_suspend(struct hdr *, struct client_ctx *);
struct client_msg {
enum msgtype type;
int (*fn)(struct hdr *, struct client_ctx *);
};
struct client_msg client_msg_table[] = {
{ MSG_DETACH, client_msg_fn_detach },
{ MSG_ERROR, client_msg_fn_error },
{ MSG_EXIT, client_msg_fn_exit },
{ MSG_EXITED, client_msg_fn_exited },
{ MSG_SHUTDOWN, client_msg_fn_shutdown },
{ MSG_SUSPEND, client_msg_fn_suspend },
};
int
client_msg_dispatch(struct client_ctx *cctx)
{
struct hdr hdr;
struct client_msg *msg;
u_int i;
if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
return (1);
memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
return (1);
buffer_remove(cctx->srv_in, sizeof hdr);
for (i = 0; i < nitems(client_msg_table); i++) {
msg = client_msg_table + i;
if (msg->type == hdr.type)
return (msg->fn(&hdr, cctx));
}
fatalx("unexpected message");
}
int
client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx)
{
struct msg_print_data data;
if (hdr->size < sizeof data)
fatalx("bad MSG_PRINT size");
buffer_read(cctx->srv_in, &data, sizeof data);
data.msg[(sizeof data.msg) - 1] = '\0';
cctx->errstr = xstrdup(data.msg);
return (-1);
}
int
client_msg_fn_detach(struct hdr *hdr, struct client_ctx *cctx)
{
if (hdr->size != 0)
fatalx("bad MSG_DETACH size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_DETACH;
return (0);
}
int
client_msg_fn_shutdown(
struct hdr *hdr, struct client_ctx *cctx)
{
if (hdr->size != 0)
fatalx("bad MSG_SHUTDOWN size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_SHUTDOWN;
return (0);
}
int
client_msg_fn_exit(struct hdr *hdr, struct client_ctx *cctx)
{
if (hdr->size != 0)
fatalx("bad MSG_EXIT size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_EXIT;
return (0);
}
int
client_msg_fn_exited(struct hdr *hdr, unused struct client_ctx *cctx)
{
if (hdr->size != 0)
fatalx("bad MSG_EXITED size");
return (-1);
}
int
client_msg_fn_suspend(struct hdr *hdr, unused struct client_ctx *cctx)
{
struct sigaction act;
if (hdr->size != 0)
fatalx("bad MSG_SUSPEND size");
memset(&act, 0, sizeof act);
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_DFL;
if (sigaction(SIGTSTP, &act, NULL) != 0)
fatal("sigaction failed");
act.sa_handler = sighandler;
if (sigaction(SIGCONT, &act, NULL) != 0)
fatal("sigaction failed");
kill(getpid(), SIGTSTP);
return (0);
}

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.58 2009-07-30 20:50:10 tcunha Exp $ */
/* $Id: client.c,v 1.59 2009-07-30 20:57:39 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -141,7 +141,6 @@ int
client_main(struct client_ctx *cctx)
{
struct pollfd pfd;
int xtimeout; /* Yay for ncurses namespace! */
siginit();
@ -162,25 +161,12 @@ client_main(struct client_ctx *cctx)
sigcont = 0;
}
switch (client_msg_dispatch(cctx)) {
case -1:
goto out;
case 0:
/* May be more in buffer, don't let poll block. */
xtimeout = 0;
break;
default:
/* Out of data, poll may block. */
xtimeout = INFTIM;
break;
}
pfd.fd = cctx->srv_fd;
pfd.events = POLLIN;
if (BUFFER_USED(cctx->srv_out) > 0)
pfd.events |= POLLOUT;
if (poll(&pfd, 1, xtimeout) == -1) {
if (poll(&pfd, 1, INFTIM) == -1) {
if (errno == EAGAIN || errno == EINTR)
continue;
fatal("poll failed");
@ -190,9 +176,11 @@ client_main(struct client_ctx *cctx)
cctx->exittype = CCTX_DIED;
break;
}
if (client_msg_dispatch(cctx) != 0)
break;
}
out:
if (sigterm) {
printf("[terminated]\n");
return (1);
@ -231,3 +219,64 @@ client_handle_winch(struct client_ctx *cctx)
sigwinch = 0;
}
int
client_msg_dispatch(struct client_ctx *cctx)
{
struct hdr hdr;
struct msg_print_data printdata;
for (;;) {
if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
return (0);
memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
return (0);
buffer_remove(cctx->srv_in, sizeof hdr);
switch (hdr.type) {
case MSG_DETACH:
if (hdr.size != 0)
fatalx("bad MSG_DETACH size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_DETACH;
break;
case MSG_ERROR:
if (hdr.size != sizeof printdata)
fatalx("bad MSG_PRINT size");
buffer_read(cctx->srv_in, &printdata, sizeof printdata);
printdata.msg[(sizeof printdata.msg) - 1] = '\0';
cctx->errstr = xstrdup(printdata.msg);
return (-1);
case MSG_EXIT:
if (hdr.size != 0)
fatalx("bad MSG_EXIT size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_EXIT;
break;
case MSG_EXITED:
if (hdr.size != 0)
fatalx("bad MSG_EXITED size");
return (-1);
case MSG_SHUTDOWN:
if (hdr.size != 0)
fatalx("bad MSG_SHUTDOWN size");
client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->exittype = CCTX_SHUTDOWN;
break;
case MSG_SUSPEND:
if (hdr.size != 0)
fatalx("bad MSG_SUSPEND size");
client_suspend();
break;
default:
fatalx("unexpected message");
}
}
}

5
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.400 2009-07-30 20:45:20 tcunha Exp $ */
/* $Id: tmux.h,v 1.401 2009-07-30 20:57:39 tcunha Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -1337,13 +1337,12 @@ size_t cmd_option_print(struct cmd *, char *, size_t);
/* client.c */
int client_init(char *, struct client_ctx *, int, int);
int client_main(struct client_ctx *);
/* client-msg.c */
int client_msg_dispatch(struct client_ctx *);
/* client-fn.c */
void client_write_server(struct client_ctx *, enum msgtype, void *, size_t);
void client_fill_session(struct msg_command_data *);
void client_suspend(void);
/* key-bindings.c */
extern struct key_bindings key_bindings;