1 /* $OpenBSD: server-fn.c,v 1.61 2012/09/03 08:48:57 nicm Exp $ */ 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 <stdlib.h> 22 #include <string.h> 23 #include <time.h> 24 #include <unistd.h> 25 26 #include "tmux.h" 27 28 struct session *server_next_session(struct session *); 29 void server_callback_identify(int, short, void *); 30 31 void 32 server_fill_environ(struct session *s, struct environ *env) 33 { 34 char var[MAXPATHLEN], *term; 35 u_int idx; 36 long pid; 37 38 if (s != NULL) { 39 term = options_get_string(&s->options, "default-terminal"); 40 environ_set(env, "TERM", term); 41 42 idx = s->idx; 43 } else 44 idx = -1; 45 pid = getpid(); 46 xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx); 47 environ_set(env, "TMUX", var); 48 } 49 50 void 51 server_write_ready(struct client *c) 52 { 53 if (c->flags & CLIENT_CONTROL) 54 return; 55 server_write_client(c, MSG_READY, NULL, 0); 56 } 57 58 int 59 server_write_client( 60 struct client *c, enum msgtype type, const void *buf, size_t len) 61 { 62 struct imsgbuf *ibuf = &c->ibuf; 63 int error; 64 65 if (c->flags & CLIENT_BAD) 66 return (-1); 67 log_debug("writing %d to client %d", type, c->ibuf.fd); 68 error = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1, 69 (void *) buf, len); 70 if (error == 1) 71 server_update_event(c); 72 return (error == 1 ? 0 : -1); 73 } 74 75 void 76 server_write_session( 77 struct session *s, enum msgtype type, const void *buf, size_t len) 78 { 79 struct client *c; 80 u_int i; 81 82 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 83 c = ARRAY_ITEM(&clients, i); 84 if (c == NULL || c->session == NULL) 85 continue; 86 if (c->session == s) 87 server_write_client(c, type, buf, len); 88 } 89 } 90 91 void 92 server_redraw_client(struct client *c) 93 { 94 c->flags |= CLIENT_REDRAW; 95 } 96 97 void 98 server_status_client(struct client *c) 99 { 100 c->flags |= CLIENT_STATUS; 101 } 102 103 void 104 server_redraw_session(struct session *s) 105 { 106 struct client *c; 107 u_int i; 108 109 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 110 c = ARRAY_ITEM(&clients, i); 111 if (c == NULL || c->session == NULL) 112 continue; 113 if (c->session == s) 114 server_redraw_client(c); 115 } 116 } 117 118 void 119 server_redraw_session_group(struct session *s) 120 { 121 struct session_group *sg; 122 123 if ((sg = session_group_find(s)) == NULL) 124 server_redraw_session(s); 125 else { 126 TAILQ_FOREACH(s, &sg->sessions, gentry) 127 server_redraw_session(s); 128 } 129 } 130 131 void 132 server_status_session(struct session *s) 133 { 134 struct client *c; 135 u_int i; 136 137 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 138 c = ARRAY_ITEM(&clients, i); 139 if (c == NULL || c->session == NULL) 140 continue; 141 if (c->session == s) 142 server_status_client(c); 143 } 144 } 145 146 void 147 server_status_session_group(struct session *s) 148 { 149 struct session_group *sg; 150 151 if ((sg = session_group_find(s)) == NULL) 152 server_status_session(s); 153 else { 154 TAILQ_FOREACH(s, &sg->sessions, gentry) 155 server_status_session(s); 156 } 157 } 158 159 void 160 server_redraw_window(struct window *w) 161 { 162 struct client *c; 163 u_int i; 164 165 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 166 c = ARRAY_ITEM(&clients, i); 167 if (c == NULL || c->session == NULL) 168 continue; 169 if (c->session->curw->window == w) 170 server_redraw_client(c); 171 } 172 w->flags |= WINDOW_REDRAW; 173 } 174 175 void 176 server_redraw_window_borders(struct window *w) 177 { 178 struct client *c; 179 u_int i; 180 181 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 182 c = ARRAY_ITEM(&clients, i); 183 if (c == NULL || c->session == NULL) 184 continue; 185 if (c->session->curw->window == w) 186 c->flags |= CLIENT_BORDERS; 187 } 188 } 189 190 void 191 server_status_window(struct window *w) 192 { 193 struct session *s; 194 195 /* 196 * This is slightly different. We want to redraw the status line of any 197 * clients containing this window rather than any where it is the 198 * current window. 199 */ 200 201 RB_FOREACH(s, sessions, &sessions) { 202 if (session_has(s, w) != NULL) 203 server_status_session(s); 204 } 205 } 206 207 void 208 server_lock(void) 209 { 210 struct client *c; 211 u_int i; 212 213 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 214 c = ARRAY_ITEM(&clients, i); 215 if (c == NULL || c->session == NULL) 216 continue; 217 server_lock_client(c); 218 } 219 } 220 221 void 222 server_lock_session(struct session *s) 223 { 224 struct client *c; 225 u_int i; 226 227 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 228 c = ARRAY_ITEM(&clients, i); 229 if (c == NULL || c->session == NULL || c->session != s) 230 continue; 231 server_lock_client(c); 232 } 233 } 234 235 void 236 server_lock_client(struct client *c) 237 { 238 const char *cmd; 239 size_t cmdlen; 240 struct msg_lock_data lockdata; 241 242 if (c->flags & CLIENT_SUSPENDED) 243 return; 244 245 cmd = options_get_string(&c->session->options, "lock-command"); 246 cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd); 247 if (cmdlen >= sizeof lockdata.cmd) 248 return; 249 250 tty_stop_tty(&c->tty); 251 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP)); 252 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR)); 253 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3)); 254 255 c->flags |= CLIENT_SUSPENDED; 256 server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata); 257 } 258 259 void 260 server_kill_window(struct window *w) 261 { 262 struct session *s, *next_s; 263 struct winlink *wl; 264 265 next_s = RB_MIN(sessions, &sessions); 266 while (next_s != NULL) { 267 s = next_s; 268 next_s = RB_NEXT(sessions, &sessions, s); 269 270 if (session_has(s, w) == NULL) 271 continue; 272 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) { 273 if (session_detach(s, wl)) { 274 server_destroy_session_group(s); 275 break; 276 } else 277 server_redraw_session_group(s); 278 } 279 280 if (options_get_number(&s->options, "renumber-windows")) 281 session_renumber_windows(s); 282 } 283 } 284 285 int 286 server_link_window(struct session *src, struct winlink *srcwl, 287 struct session *dst, int dstidx, int killflag, int selectflag, char **cause) 288 { 289 struct winlink *dstwl; 290 struct session_group *srcsg, *dstsg; 291 292 srcsg = session_group_find(src); 293 dstsg = session_group_find(dst); 294 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) { 295 xasprintf(cause, "sessions are grouped"); 296 return (-1); 297 } 298 299 dstwl = NULL; 300 if (dstidx != -1) 301 dstwl = winlink_find_by_index(&dst->windows, dstidx); 302 if (dstwl != NULL) { 303 if (dstwl->window == srcwl->window) { 304 xasprintf(cause, "same index: %d", dstidx); 305 return (-1); 306 } 307 if (killflag) { 308 /* 309 * Can't use session_detach as it will destroy session 310 * if this makes it empty. 311 */ 312 notify_window_unlinked(dst, dstwl->window); 313 dstwl->flags &= ~WINLINK_ALERTFLAGS; 314 winlink_stack_remove(&dst->lastw, dstwl); 315 winlink_remove(&dst->windows, dstwl); 316 317 /* Force select/redraw if current. */ 318 if (dstwl == dst->curw) { 319 selectflag = 1; 320 dst->curw = NULL; 321 } 322 } 323 } 324 325 if (dstidx == -1) 326 dstidx = -1 - options_get_number(&dst->options, "base-index"); 327 dstwl = session_attach(dst, srcwl->window, dstidx, cause); 328 if (dstwl == NULL) 329 return (-1); 330 331 if (selectflag) 332 session_select(dst, dstwl->idx); 333 server_redraw_session_group(dst); 334 335 return (0); 336 } 337 338 void 339 server_unlink_window(struct session *s, struct winlink *wl) 340 { 341 if (session_detach(s, wl)) 342 server_destroy_session_group(s); 343 else 344 server_redraw_session_group(s); 345 } 346 347 void 348 server_destroy_pane(struct window_pane *wp) 349 { 350 struct window *w = wp->window; 351 int old_fd; 352 struct screen_write_ctx ctx; 353 struct grid_cell gc; 354 355 old_fd = wp->fd; 356 if (wp->fd != -1) { 357 bufferevent_free(wp->event); 358 close(wp->fd); 359 wp->fd = -1; 360 } 361 362 if (options_get_number(&w->options, "remain-on-exit")) { 363 if (old_fd == -1) 364 return; 365 screen_write_start(&ctx, wp, &wp->base); 366 screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1); 367 screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1); 368 screen_write_linefeed(&ctx, 1); 369 memcpy(&gc, &grid_default_cell, sizeof gc); 370 gc.attr |= GRID_ATTR_BRIGHT; 371 screen_write_puts(&ctx, &gc, "Pane is dead"); 372 screen_write_stop(&ctx); 373 wp->flags |= PANE_REDRAW; 374 return; 375 } 376 377 layout_close_pane(wp); 378 window_remove_pane(w, wp); 379 380 if (TAILQ_EMPTY(&w->panes)) 381 server_kill_window(w); 382 else 383 server_redraw_window(w); 384 } 385 386 void 387 server_destroy_session_group(struct session *s) 388 { 389 struct session_group *sg; 390 391 if ((sg = session_group_find(s)) == NULL) 392 server_destroy_session(s); 393 else { 394 TAILQ_FOREACH(s, &sg->sessions, gentry) 395 server_destroy_session(s); 396 TAILQ_REMOVE(&session_groups, sg, entry); 397 free(sg); 398 } 399 } 400 401 struct session * 402 server_next_session(struct session *s) 403 { 404 struct session *s_loop, *s_out; 405 406 s_out = NULL; 407 RB_FOREACH(s_loop, sessions, &sessions) { 408 if (s_loop == s) 409 continue; 410 if (s_out == NULL || 411 timercmp(&s_loop->activity_time, &s_out->activity_time, <)) 412 s_out = s_loop; 413 } 414 return (s_out); 415 } 416 417 void 418 server_destroy_session(struct session *s) 419 { 420 struct client *c; 421 struct session *s_new; 422 u_int i; 423 424 if (!options_get_number(&s->options, "detach-on-destroy")) 425 s_new = server_next_session(s); 426 else 427 s_new = NULL; 428 429 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 430 c = ARRAY_ITEM(&clients, i); 431 if (c == NULL || c->session != s) 432 continue; 433 if (s_new == NULL) { 434 c->session = NULL; 435 c->flags |= CLIENT_EXIT; 436 } else { 437 c->last_session = NULL; 438 c->session = s_new; 439 notify_attached_session_changed(c); 440 session_update_activity(s_new); 441 server_redraw_client(c); 442 } 443 } 444 recalculate_sizes(); 445 } 446 447 void 448 server_check_unattached (void) 449 { 450 struct session *s; 451 452 /* 453 * If any sessions are no longer attached and have destroy-unattached 454 * set, collect them. 455 */ 456 RB_FOREACH(s, sessions, &sessions) { 457 if (!(s->flags & SESSION_UNATTACHED)) 458 continue; 459 if (options_get_number (&s->options, "destroy-unattached")) 460 session_destroy(s); 461 } 462 } 463 464 void 465 server_set_identify(struct client *c) 466 { 467 struct timeval tv; 468 int delay; 469 470 delay = options_get_number(&c->session->options, "display-panes-time"); 471 tv.tv_sec = delay / 1000; 472 tv.tv_usec = (delay % 1000) * 1000L; 473 474 if (event_initialized (&c->identify_timer)) 475 evtimer_del(&c->identify_timer); 476 evtimer_set(&c->identify_timer, server_callback_identify, c); 477 evtimer_add(&c->identify_timer, &tv); 478 479 c->flags |= CLIENT_IDENTIFY; 480 c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR); 481 server_redraw_client(c); 482 } 483 484 void 485 server_clear_identify(struct client *c) 486 { 487 if (c->flags & CLIENT_IDENTIFY) { 488 c->flags &= ~CLIENT_IDENTIFY; 489 c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR); 490 server_redraw_client(c); 491 } 492 } 493 494 /* ARGSUSED */ 495 void 496 server_callback_identify(unused int fd, unused short events, void *data) 497 { 498 struct client *c = data; 499 500 server_clear_identify(c); 501 } 502 503 void 504 server_update_event(struct client *c) 505 { 506 short events; 507 508 events = 0; 509 if (!(c->flags & CLIENT_BAD)) 510 events |= EV_READ; 511 if (c->ibuf.w.queued > 0) 512 events |= EV_WRITE; 513 if (event_initialized(&c->event)) 514 event_del(&c->event); 515 event_set(&c->event, c->ibuf.fd, events, server_client_callback, c); 516 event_add(&c->event, NULL); 517 } 518 519 /* Push stdout to client if possible. */ 520 void 521 server_push_stdout(struct client *c) 522 { 523 struct msg_stdout_data data; 524 size_t size; 525 526 size = EVBUFFER_LENGTH(c->stdout_data); 527 if (size == 0) 528 return; 529 if (size > sizeof data.data) 530 size = sizeof data.data; 531 532 memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size); 533 data.size = size; 534 535 if (server_write_client(c, MSG_STDOUT, &data, sizeof data) == 0) 536 evbuffer_drain(c->stdout_data, size); 537 } 538 539 /* Push stderr to client if possible. */ 540 void 541 server_push_stderr(struct client *c) 542 { 543 struct msg_stderr_data data; 544 size_t size; 545 546 size = EVBUFFER_LENGTH(c->stderr_data); 547 if (size == 0) 548 return; 549 if (size > sizeof data.data) 550 size = sizeof data.data; 551 552 memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size); 553 data.size = size; 554 555 if (server_write_client(c, MSG_STDERR, &data, sizeof data) == 0) 556 evbuffer_drain(c->stderr_data, size); 557 } 558 559 /* Set stdin callback. */ 560 int 561 server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int, 562 void *), void *cb_data, char **cause) 563 { 564 if (c == NULL) { 565 *cause = xstrdup("no client with stdin"); 566 return (-1); 567 } 568 if (c->flags & CLIENT_TERMINAL) { 569 *cause = xstrdup("stdin is a tty"); 570 return (-1); 571 } 572 if (c->stdin_callback != NULL) { 573 *cause = xstrdup("stdin in use"); 574 return (-1); 575 } 576 577 c->stdin_callback_data = cb_data; 578 c->stdin_callback = cb; 579 580 c->references++; 581 582 if (c->stdin_closed) 583 c->stdin_callback (c, 1, c->stdin_callback_data); 584 585 server_write_client(c, MSG_STDIN, NULL, 0); 586 587 return (0); 588 } 589