Annotation of src/usr.bin/tmux/client-msg.c, Revision 1.6
1.6 ! nicm 1: /* $OpenBSD: client-msg.c,v 1.5 2009/07/26 12:58:44 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdint.h>
22: #include <stdlib.h>
23: #include <string.h>
24: #include <unistd.h>
25:
26: #include "tmux.h"
27:
1.4 nicm 28: int client_msg_fn_detach(struct hdr *, struct client_ctx *);
29: int client_msg_fn_error(struct hdr *, struct client_ctx *);
30: int client_msg_fn_shutdown(struct hdr *, struct client_ctx *);
31: int client_msg_fn_exit(struct hdr *, struct client_ctx *);
32: int client_msg_fn_exited(struct hdr *, struct client_ctx *);
33: int client_msg_fn_suspend(struct hdr *, struct client_ctx *);
1.1 nicm 34:
35: struct client_msg {
1.6 ! nicm 36: enum msgtype type;
1.4 nicm 37: int (*fn)(struct hdr *, struct client_ctx *);
1.1 nicm 38: };
39: struct client_msg client_msg_table[] = {
40: { MSG_DETACH, client_msg_fn_detach },
41: { MSG_ERROR, client_msg_fn_error },
42: { MSG_EXIT, client_msg_fn_exit },
43: { MSG_EXITED, client_msg_fn_exited },
44: { MSG_SHUTDOWN, client_msg_fn_shutdown },
45: { MSG_SUSPEND, client_msg_fn_suspend },
46: };
47:
48: int
1.4 nicm 49: client_msg_dispatch(struct client_ctx *cctx)
1.1 nicm 50: {
51: struct hdr hdr;
52: struct client_msg *msg;
53: u_int i;
54:
55: if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
56: return (1);
57: memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
58: if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
59: return (1);
60: buffer_remove(cctx->srv_in, sizeof hdr);
61:
62: for (i = 0; i < nitems(client_msg_table); i++) {
63: msg = client_msg_table + i;
1.4 nicm 64: if (msg->type == hdr.type)
65: return (msg->fn(&hdr, cctx));
1.1 nicm 66: }
67: fatalx("unexpected message");
68: }
69:
70: int
1.4 nicm 71: client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx)
1.1 nicm 72: {
1.5 nicm 73: struct msg_print_data data;
1.4 nicm 74:
1.5 nicm 75: if (hdr->size < sizeof data)
76: fatalx("bad MSG_PRINT size");
77: buffer_read(cctx->srv_in, &data, sizeof data);
1.1 nicm 78:
1.5 nicm 79: data.msg[(sizeof data.msg) - 1] = '\0';
80: cctx->errstr = xstrdup(data.msg);
1.1 nicm 81:
82: return (-1);
83: }
84:
85: int
1.4 nicm 86: client_msg_fn_detach(struct hdr *hdr, struct client_ctx *cctx)
1.1 nicm 87: {
88: if (hdr->size != 0)
89: fatalx("bad MSG_DETACH size");
90:
91: client_write_server(cctx, MSG_EXITING, NULL, 0);
1.4 nicm 92: cctx->exittype = CCTX_DETACH;
1.1 nicm 93:
94: return (0);
95: }
96:
97: int
98: client_msg_fn_shutdown(
1.4 nicm 99: struct hdr *hdr, struct client_ctx *cctx)
1.1 nicm 100: {
101: if (hdr->size != 0)
102: fatalx("bad MSG_SHUTDOWN size");
103:
104: client_write_server(cctx, MSG_EXITING, NULL, 0);
1.4 nicm 105: cctx->exittype = CCTX_SHUTDOWN;
1.1 nicm 106:
107: return (0);
108: }
109:
110: int
1.4 nicm 111: client_msg_fn_exit(struct hdr *hdr, struct client_ctx *cctx)
1.1 nicm 112: {
113: if (hdr->size != 0)
114: fatalx("bad MSG_EXIT size");
115:
116: client_write_server(cctx, MSG_EXITING, NULL, 0);
1.4 nicm 117: cctx->exittype = CCTX_EXIT;
1.1 nicm 118:
119: return (0);
120: }
121:
122: int
1.4 nicm 123: client_msg_fn_exited(struct hdr *hdr, unused struct client_ctx *cctx)
1.1 nicm 124: {
125: if (hdr->size != 0)
126: fatalx("bad MSG_EXITED size");
127:
128: return (-1);
129: }
130:
131: int
1.4 nicm 132: client_msg_fn_suspend(struct hdr *hdr, unused struct client_ctx *cctx)
1.1 nicm 133: {
134: struct sigaction act;
135:
136: if (hdr->size != 0)
137: fatalx("bad MSG_SUSPEND size");
138:
139: memset(&act, 0, sizeof act);
140: sigemptyset(&act.sa_mask);
141: act.sa_flags = SA_RESTART;
142:
143: act.sa_handler = SIG_DFL;
144: if (sigaction(SIGTSTP, &act, NULL) != 0)
145: fatal("sigaction failed");
146:
147: act.sa_handler = sighandler;
148: if (sigaction(SIGCONT, &act, NULL) != 0)
149: fatal("sigaction failed");
150:
151: kill(getpid(), SIGTSTP);
152:
153: return (0);
154: }