Annotation of src/usr.bin/tmux/server-msg.c, Revision 1.21
1.21 ! nicm 1: /* $OpenBSD: server-msg.c,v 1.20 2009/09/23 06:18:47 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>
1.19 nicm 20: #include <sys/ioctl.h>
1.1 nicm 21:
22: #include <errno.h>
1.21 ! nicm 23: #include <paths.h>
1.1 nicm 24: #include <stdlib.h>
25: #include <string.h>
26: #include <time.h>
27: #include <unistd.h>
28:
29: #include "tmux.h"
30:
1.9 nicm 31: void server_msg_command(struct client *, struct msg_command_data *);
1.13 nicm 32: void server_msg_identify(struct client *, struct msg_identify_data *, int);
1.21 ! nicm 33: void server_msg_shell(struct client *);
1.9 nicm 34:
35: void printflike2 server_msg_command_error(struct cmd_ctx *, const char *, ...);
36: void printflike2 server_msg_command_print(struct cmd_ctx *, const char *, ...);
37: void printflike2 server_msg_command_info(struct cmd_ctx *, const char *, ...);
1.1 nicm 38:
39: int
40: server_msg_dispatch(struct client *c)
41: {
1.11 nicm 42: struct imsg imsg;
1.9 nicm 43: struct msg_command_data commanddata;
44: struct msg_identify_data identifydata;
1.10 nicm 45: struct msg_environ_data environdata;
1.11 nicm 46: ssize_t n, datalen;
47:
48: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
49: return (-1);
1.1 nicm 50:
51: for (;;) {
1.11 nicm 52: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
53: return (-1);
54: if (n == 0)
1.1 nicm 55: return (0);
1.11 nicm 56: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
57:
58: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
59: server_write_client(c, MSG_VERSION, NULL, 0);
60: c->flags |= CLIENT_BAD;
61: imsg_free(&imsg);
62: continue;
63: }
1.1 nicm 64:
1.11 nicm 65: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
66: switch (imsg.hdr.type) {
1.9 nicm 67: case MSG_COMMAND:
1.11 nicm 68: if (datalen != sizeof commanddata)
1.9 nicm 69: fatalx("bad MSG_COMMAND size");
1.11 nicm 70: memcpy(&commanddata, imsg.data, sizeof commanddata);
1.9 nicm 71:
72: server_msg_command(c, &commanddata);
73: break;
74: case MSG_IDENTIFY:
1.11 nicm 75: if (datalen != sizeof identifydata)
1.9 nicm 76: fatalx("bad MSG_IDENTIFY size");
1.18 nicm 77: if (imsg.fd == -1)
78: fatalx("MSG_IDENTIFY missing fd");
1.11 nicm 79: memcpy(&identifydata, imsg.data, sizeof identifydata);
1.9 nicm 80:
1.13 nicm 81: server_msg_identify(c, &identifydata, imsg.fd);
1.9 nicm 82: break;
83: case MSG_RESIZE:
1.19 nicm 84: if (datalen != 0)
1.9 nicm 85: fatalx("bad MSG_RESIZE size");
86:
1.19 nicm 87: tty_resize(&c->tty);
88: recalculate_sizes();
89: server_redraw_client(c);
1.9 nicm 90: break;
91: case MSG_EXITING:
1.11 nicm 92: if (datalen != 0)
1.9 nicm 93: fatalx("bad MSG_EXITING size");
94:
95: c->session = NULL;
1.12 nicm 96: tty_close(&c->tty);
1.9 nicm 97: server_write_client(c, MSG_EXITED, NULL, 0);
98: break;
1.20 nicm 99: case MSG_WAKEUP:
1.9 nicm 100: case MSG_UNLOCK:
1.11 nicm 101: if (datalen != 0)
1.9 nicm 102: fatalx("bad MSG_WAKEUP size");
103:
104: c->flags &= ~CLIENT_SUSPENDED;
105: tty_start_tty(&c->tty);
106: server_redraw_client(c);
1.20 nicm 107: server_activity = time(NULL);
1.10 nicm 108: break;
109: case MSG_ENVIRON:
1.11 nicm 110: if (datalen != sizeof environdata)
1.10 nicm 111: fatalx("bad MSG_ENVIRON size");
1.11 nicm 112: memcpy(&environdata, imsg.data, sizeof environdata);
1.10 nicm 113:
114: environdata.var[(sizeof environdata.var) - 1] = '\0';
115: if (strchr(environdata.var, '=') != NULL)
116: environ_put(&c->environ, environdata.var);
1.9 nicm 117: break;
1.21 ! nicm 118: case MSG_SHELL:
! 119: if (datalen != 0)
! 120: fatalx("bad MSG_SHELL size");
! 121:
! 122: server_msg_shell(c);
! 123: break;
1.9 nicm 124: default:
125: fatalx("unexpected message");
1.1 nicm 126: }
1.11 nicm 127:
128: imsg_free(&imsg);
1.1 nicm 129: }
130: }
131:
132: void printflike2
1.9 nicm 133: server_msg_command_error(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 134: {
1.7 nicm 135: struct msg_print_data data;
136: va_list ap;
1.1 nicm 137:
138: va_start(ap, fmt);
1.7 nicm 139: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 140: va_end(ap);
141:
1.7 nicm 142: server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
1.1 nicm 143: }
144:
145: void printflike2
1.9 nicm 146: server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 147: {
1.7 nicm 148: struct msg_print_data data;
149: va_list ap;
1.1 nicm 150:
151: va_start(ap, fmt);
1.7 nicm 152: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 153: va_end(ap);
154:
1.7 nicm 155: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 156: }
157:
158: void printflike2
1.9 nicm 159: server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 160: {
1.7 nicm 161: struct msg_print_data data;
162: va_list ap;
1.1 nicm 163:
164: if (be_quiet)
165: return;
166:
167: va_start(ap, fmt);
1.7 nicm 168: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 169: va_end(ap);
170:
1.7 nicm 171: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 172: }
173:
1.6 nicm 174: void
1.9 nicm 175: server_msg_command(struct client *c, struct msg_command_data *data)
1.1 nicm 176: {
1.9 nicm 177: struct cmd_ctx ctx;
178: struct cmd_list *cmdlist = NULL;
179: struct cmd *cmd;
180: int argc;
181: char **argv, *cause;
1.1 nicm 182:
183: server_activity = time(NULL);
184:
1.9 nicm 185: ctx.error = server_msg_command_error;
186: ctx.print = server_msg_command_print;
187: ctx.info = server_msg_command_info;
1.1 nicm 188:
1.9 nicm 189: ctx.msgdata = data;
1.1 nicm 190: ctx.curclient = NULL;
191:
192: ctx.cmdclient = c;
193:
1.9 nicm 194: argc = data->argc;
195: data->argv[(sizeof data->argv) - 1] = '\0';
196: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
197: server_msg_command_error(&ctx, "command too long");
1.7 nicm 198: goto error;
199: }
200:
201: if (argc == 0) {
202: argc = 1;
203: argv = xcalloc(1, sizeof *argv);
204: *argv = xstrdup("new-session");
205: }
206:
207: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
1.9 nicm 208: server_msg_command_error(&ctx, "%s", cause);
1.7 nicm 209: cmd_free_argv(argc, argv);
210: goto error;
211: }
212: cmd_free_argv(argc, argv);
213:
1.9 nicm 214: if (data->pid != -1) {
1.1 nicm 215: TAILQ_FOREACH(cmd, cmdlist, qentry) {
216: if (cmd->entry->flags & CMD_CANTNEST) {
1.9 nicm 217: server_msg_command_error(&ctx,
1.1 nicm 218: "sessions should be nested with care. "
219: "unset $TMUX to force");
1.7 nicm 220: goto error;
1.1 nicm 221: }
222: }
223: }
224:
225: if (cmd_list_exec(cmdlist, &ctx) != 1)
226: server_write_client(c, MSG_EXIT, NULL, 0);
227: cmd_list_free(cmdlist);
1.7 nicm 228: return;
229:
230: error:
231: if (cmdlist != NULL)
232: cmd_list_free(cmdlist);
233: server_write_client(c, MSG_EXIT, NULL, 0);
1.1 nicm 234: }
235:
1.6 nicm 236: void
1.13 nicm 237: server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
1.1 nicm 238: {
239: c->cwd = NULL;
1.9 nicm 240: data->cwd[(sizeof data->cwd) - 1] = '\0';
241: if (*data->cwd != '\0')
242: c->cwd = xstrdup(data->cwd);
243:
244: data->term[(sizeof data->term) - 1] = '\0';
1.18 nicm 245: tty_init(&c->tty, fd, data->term);
1.9 nicm 246: if (data->flags & IDENTIFY_UTF8)
1.1 nicm 247: c->tty.flags |= TTY_UTF8;
1.9 nicm 248: if (data->flags & IDENTIFY_256COLOURS)
1.1 nicm 249: c->tty.term_flags |= TERM_256COLOURS;
1.9 nicm 250: else if (data->flags & IDENTIFY_88COLOURS)
1.1 nicm 251: c->tty.term_flags |= TERM_88COLOURS;
1.9 nicm 252: if (data->flags & IDENTIFY_HASDEFAULTS)
1.1 nicm 253: c->tty.term_flags |= TERM_HASDEFAULTS;
1.18 nicm 254:
1.19 nicm 255: tty_resize(&c->tty);
1.5 nicm 256:
1.1 nicm 257: c->flags |= CLIENT_TERMINAL;
1.21 ! nicm 258: }
! 259:
! 260: void
! 261: server_msg_shell(struct client *c)
! 262: {
! 263: struct msg_shell_data data;
! 264: const char *shell;
! 265:
! 266: shell = options_get_string(&global_s_options, "default-shell");
! 267:
! 268: if (*shell == '\0' || areshell(shell))
! 269: shell = _PATH_BSHELL;
! 270: if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
! 271: strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
! 272:
! 273: server_write_client(c, MSG_SHELL, &data, sizeof data);
! 274: c->flags |= CLIENT_BAD; /* it will die after exec */
1.1 nicm 275: }