Coverage Report

Created: 2026-04-12 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/server-fn.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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
#include <sys/wait.h>
21
#include <sys/uio.h>
22
23
#include <signal.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <time.h>
27
#include <unistd.h>
28
29
#include "tmux.h"
30
31
static void server_destroy_session_group(struct session *);
32
33
void
34
server_redraw_client(struct client *c)
35
0
{
36
0
  c->flags |= CLIENT_ALLREDRAWFLAGS;
37
0
}
38
39
void
40
server_status_client(struct client *c)
41
0
{
42
0
  c->flags |= CLIENT_REDRAWSTATUS;
43
0
}
44
45
void
46
server_redraw_session(struct session *s)
47
0
{
48
0
  struct client *c;
49
50
0
  TAILQ_FOREACH(c, &clients, entry) {
51
0
    if (c->session == s)
52
0
      server_redraw_client(c);
53
0
  }
54
0
}
55
56
void
57
server_redraw_session_group(struct session *s)
58
0
{
59
0
  struct session_group  *sg;
60
61
0
  if ((sg = session_group_contains(s)) == NULL)
62
0
    server_redraw_session(s);
63
0
  else {
64
0
    TAILQ_FOREACH(s, &sg->sessions, gentry)
65
0
      server_redraw_session(s);
66
0
  }
67
0
}
68
69
void
70
server_status_session(struct session *s)
71
0
{
72
0
  struct client *c;
73
74
0
  TAILQ_FOREACH(c, &clients, entry) {
75
0
    if (c->session == s)
76
0
      server_status_client(c);
77
0
  }
78
0
}
79
80
void
81
server_status_session_group(struct session *s)
82
0
{
83
0
  struct session_group  *sg;
84
85
0
  if ((sg = session_group_contains(s)) == NULL)
86
0
    server_status_session(s);
87
0
  else {
88
0
    TAILQ_FOREACH(s, &sg->sessions, gentry)
89
0
      server_status_session(s);
90
0
  }
91
0
}
92
93
void
94
server_redraw_window(struct window *w)
95
0
{
96
0
  struct client *c;
97
98
0
  TAILQ_FOREACH(c, &clients, entry) {
99
0
    if (c->session != NULL &&
100
0
        c->session->curw != NULL &&
101
0
        c->session->curw->window == w)
102
0
      server_redraw_client(c);
103
0
  }
104
0
}
105
106
void
107
server_redraw_window_borders(struct window *w)
108
0
{
109
0
  struct client *c;
110
111
0
  TAILQ_FOREACH(c, &clients, entry) {
112
0
    if (c->session != NULL &&
113
0
        c->session->curw != NULL &&
114
0
        c->session->curw->window == w)
115
0
      c->flags |= CLIENT_REDRAWBORDERS;
116
0
  }
117
0
}
118
119
void
120
server_status_window(struct window *w)
121
0
{
122
0
  struct session  *s;
123
124
  /*
125
   * This is slightly different. We want to redraw the status line of any
126
   * clients containing this window rather than anywhere it is the
127
   * current window.
128
   */
129
130
0
  RB_FOREACH(s, sessions, &sessions) {
131
0
    if (session_has(s, w))
132
0
      server_status_session(s);
133
0
  }
134
0
}
135
136
void
137
server_lock(void)
138
0
{
139
0
  struct client *c;
140
141
0
  TAILQ_FOREACH(c, &clients, entry) {
142
0
    if (c->session != NULL)
143
0
      server_lock_client(c);
144
0
  }
145
0
}
146
147
void
148
server_lock_session(struct session *s)
149
0
{
150
0
  struct client *c;
151
152
0
  TAILQ_FOREACH(c, &clients, entry) {
153
0
    if (c->session == s)
154
0
      server_lock_client(c);
155
0
  }
156
0
}
157
158
void
159
server_lock_client(struct client *c)
160
0
{
161
0
  const char  *cmd;
162
163
0
  if (c->flags & CLIENT_CONTROL)
164
0
    return;
165
166
0
  if (c->flags & CLIENT_SUSPENDED)
167
0
    return;
168
169
0
  cmd = options_get_string(c->session->options, "lock-command");
170
0
  if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
171
0
    return;
172
173
0
  tty_stop_tty(&c->tty);
174
0
  tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
175
0
  tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
176
0
  tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
177
178
0
  c->flags |= CLIENT_SUSPENDED;
179
0
  proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
180
0
}
181
182
void
183
server_kill_pane(struct window_pane *wp)
184
0
{
185
0
  struct window *w = wp->window;
186
187
0
  if (window_count_panes(w) == 1) {
188
0
    server_kill_window(w, 1);
189
0
    recalculate_sizes();
190
0
  } else {
191
0
    server_unzoom_window(w);
192
0
    server_client_remove_pane(wp);
193
0
    layout_close_pane(wp);
194
0
    window_remove_pane(w, wp);
195
0
    server_redraw_window(w);
196
0
  }
197
0
}
198
199
void
200
server_kill_window(struct window *w, int renumber)
201
0
{
202
0
  struct session  *s, *s1;
203
0
  struct winlink  *wl;
204
205
0
  RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
206
0
    if (!session_has(s, w))
207
0
      continue;
208
209
0
    server_unzoom_window(w);
210
0
    while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
211
0
      if (session_detach(s, wl)) {
212
0
        server_destroy_session_group(s);
213
0
        break;
214
0
      }
215
0
      server_redraw_session_group(s);
216
0
    }
217
218
0
    if (renumber)
219
0
      server_renumber_session(s);
220
0
  }
221
0
  recalculate_sizes();
222
0
}
223
224
void
225
server_renumber_session(struct session *s)
226
0
{
227
0
  struct session_group  *sg;
228
229
0
  if (options_get_number(s->options, "renumber-windows")) {
230
0
    if ((sg = session_group_contains(s)) != NULL) {
231
0
      TAILQ_FOREACH(s, &sg->sessions, gentry)
232
0
          session_renumber_windows(s);
233
0
    } else
234
0
      session_renumber_windows(s);
235
0
  }
236
0
}
237
238
void
239
server_renumber_all(void)
240
0
{
241
0
  struct session  *s;
242
243
0
  RB_FOREACH(s, sessions, &sessions)
244
0
    server_renumber_session(s);
245
0
}
246
247
int
248
server_link_window(struct session *src, struct winlink *srcwl,
249
    struct session *dst, int dstidx, int killflag, int selectflag,
250
    char **cause)
251
0
{
252
0
  struct winlink    *dstwl;
253
0
  struct session_group  *srcsg, *dstsg;
254
255
0
  srcsg = session_group_contains(src);
256
0
  dstsg = session_group_contains(dst);
257
0
  if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
258
0
    xasprintf(cause, "sessions are grouped");
259
0
    return (-1);
260
0
  }
261
262
0
  dstwl = NULL;
263
0
  if (dstidx != -1)
264
0
    dstwl = winlink_find_by_index(&dst->windows, dstidx);
265
0
  if (dstwl != NULL) {
266
0
    if (dstwl->window == srcwl->window) {
267
0
      xasprintf(cause, "same index: %d", dstidx);
268
0
      return (-1);
269
0
    }
270
0
    if (killflag) {
271
      /*
272
       * Can't use session_detach as it will destroy session
273
       * if this makes it empty.
274
       */
275
0
      notify_session_window("window-unlinked", dst,
276
0
          dstwl->window);
277
0
      dstwl->flags &= ~WINLINK_ALERTFLAGS;
278
0
      winlink_stack_remove(&dst->lastw, dstwl);
279
0
      winlink_remove(&dst->windows, dstwl);
280
281
      /* Force select/redraw if current. */
282
0
      if (dstwl == dst->curw) {
283
0
        selectflag = 1;
284
0
        dst->curw = NULL;
285
0
      }
286
0
    }
287
0
  }
288
289
0
  if (dstidx == -1)
290
0
    dstidx = -1 - options_get_number(dst->options, "base-index");
291
0
  dstwl = session_attach(dst, srcwl->window, dstidx, cause);
292
0
  if (dstwl == NULL)
293
0
    return (-1);
294
295
0
  if (marked_pane.wl == srcwl)
296
0
    marked_pane.wl = dstwl;
297
0
  if (selectflag)
298
0
    session_select(dst, dstwl->idx);
299
0
  server_redraw_session_group(dst);
300
301
0
  return (0);
302
0
}
303
304
void
305
server_unlink_window(struct session *s, struct winlink *wl)
306
0
{
307
0
  if (session_detach(s, wl))
308
0
    server_destroy_session_group(s);
309
0
  else
310
0
    server_redraw_session_group(s);
311
0
}
312
313
void
314
server_destroy_pane(struct window_pane *wp, int notify)
315
0
{
316
0
  struct window   *w = wp->window;
317
0
  struct screen_write_ctx  ctx;
318
0
  struct grid_cell   gc;
319
0
  int      remain_on_exit;
320
0
  const char    *s;
321
0
  char      *expanded;
322
0
  u_int      sx = screen_size_x(&wp->base);
323
0
  u_int      sy = screen_size_y(&wp->base);
324
325
0
  if (wp->fd != -1) {
326
#ifdef HAVE_UTEMPTER
327
    utempter_remove_record(wp->fd);
328
    kill(getpid(), SIGCHLD);
329
#endif
330
0
    bufferevent_free(wp->event);
331
0
    wp->event = NULL;
332
0
    close(wp->fd);
333
0
    wp->fd = -1;
334
0
  }
335
336
0
  remain_on_exit = options_get_number(wp->options, "remain-on-exit");
337
0
  if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
338
0
    return;
339
0
  switch (remain_on_exit) {
340
0
  case 0:
341
0
    break;
342
0
  case 2:
343
0
    if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
344
0
      break;
345
    /* FALLTHROUGH */
346
0
  case 1:
347
0
  case 3:
348
0
    if (wp->flags & PANE_STATUSDRAWN)
349
0
      return;
350
0
    wp->flags |= PANE_STATUSDRAWN;
351
352
0
    gettimeofday(&wp->dead_time, NULL);
353
0
    if (notify)
354
0
      notify_pane("pane-died", wp);
355
356
0
    s = options_get_string(wp->options, "remain-on-exit-format");
357
0
    if (*s != '\0') {
358
0
      screen_write_start_pane(&ctx, wp, &wp->base);
359
0
      screen_write_scrollregion(&ctx, 0, sy - 1);
360
0
      screen_write_cursormove(&ctx, 0, sy - 1, 0);
361
0
      screen_write_linefeed(&ctx, 1, 8);
362
0
      memcpy(&gc, &grid_default_cell, sizeof gc);
363
364
0
      expanded = format_single(NULL, s, NULL, NULL, NULL, wp);
365
0
      format_draw(&ctx, &gc, sx, expanded, NULL, 0);
366
0
      free(expanded);
367
368
0
      screen_write_stop(&ctx);
369
0
    }
370
0
    wp->base.mode &= ~MODE_CURSOR;
371
372
0
    wp->flags |= PANE_REDRAW;
373
0
    return;
374
0
  }
375
376
0
  if (notify)
377
0
    notify_pane("pane-exited", wp);
378
379
0
  server_unzoom_window(w);
380
0
  server_client_remove_pane(wp);
381
0
  layout_close_pane(wp);
382
0
  window_remove_pane(w, wp);
383
384
0
  if (TAILQ_EMPTY(&w->panes))
385
0
    server_kill_window(w, 1);
386
0
  else
387
0
    server_redraw_window(w);
388
0
}
389
390
static void
391
server_destroy_session_group(struct session *s)
392
0
{
393
0
  struct session_group  *sg;
394
0
  struct session    *s1;
395
396
0
  if ((sg = session_group_contains(s)) == NULL) {
397
0
    server_destroy_session(s);
398
0
    session_destroy(s, 1, __func__);
399
0
  } else {
400
0
    TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
401
0
      server_destroy_session(s);
402
0
      session_destroy(s, 1, __func__);
403
0
    }
404
0
  }
405
0
}
406
407
static struct session *
408
server_find_session(struct session *s,
409
    int (*f)(struct session *, struct session *))
410
0
{
411
0
  struct session *s_loop, *s_out = NULL;
412
413
0
  RB_FOREACH(s_loop, sessions, &sessions) {
414
0
    if (s_loop != s && f(s_loop, s_out))
415
0
      s_out = s_loop;
416
0
  }
417
0
  return (s_out);
418
0
}
419
420
static int
421
server_newer_session(struct session *s_loop, struct session *s_out)
422
0
{
423
0
  if (s_out == NULL)
424
0
    return (1);
425
0
  return (timercmp(&s_loop->activity_time, &s_out->activity_time, >));
426
0
}
427
428
static int
429
server_newer_detached_session(struct session *s_loop, struct session *s_out)
430
0
{
431
0
  if (s_loop->attached)
432
0
    return (0);
433
0
  return (server_newer_session(s_loop, s_out));
434
0
}
435
436
void
437
server_destroy_session(struct session *s)
438
0
{
439
0
  struct client *c;
440
0
  struct session  *s_new = NULL, *cs_new = NULL, *use_s;
441
0
  int    detach_on_destroy;
442
443
0
  detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
444
0
  if (detach_on_destroy == 0)
445
0
    s_new = server_find_session(s, server_newer_session);
446
0
  else if (detach_on_destroy == 2)
447
0
    s_new = server_find_session(s, server_newer_detached_session);
448
0
  else if (detach_on_destroy == 3)
449
0
    s_new = session_previous_session(s, NULL);
450
0
  else if (detach_on_destroy == 4)
451
0
    s_new = session_next_session(s, NULL);
452
453
  /*
454
   * If no suitable new session was found above, then look for any
455
   * session as an alternative in case a client needs it.
456
   */
457
0
  if (s_new == NULL &&
458
0
      (detach_on_destroy == 1 || detach_on_destroy == 2))
459
0
    cs_new = server_find_session(s, server_newer_session);
460
461
0
  TAILQ_FOREACH(c, &clients, entry) {
462
0
    if (c->session != s)
463
0
      continue;
464
0
    use_s = s_new;
465
0
    if (use_s == NULL && (c->flags & CLIENT_NO_DETACH_ON_DESTROY))
466
0
      use_s = cs_new;
467
468
0
    c->session = NULL;
469
0
    c->last_session = NULL;
470
0
    server_client_set_session(c, use_s);
471
0
    if (use_s == NULL)
472
0
      c->flags |= CLIENT_EXIT;
473
0
  }
474
0
  recalculate_sizes();
475
0
}
476
477
void
478
server_check_unattached(void)
479
0
{
480
0
  struct session    *s;
481
0
  struct session_group  *sg;
482
483
  /*
484
   * If any sessions are no longer attached and have destroy-unattached
485
   * set, collect them.
486
   */
487
0
  RB_FOREACH(s, sessions, &sessions) {
488
0
    if (s->attached != 0)
489
0
      continue;
490
0
    switch (options_get_number(s->options, "destroy-unattached")) {
491
0
    case 0: /* off */
492
0
      continue;
493
0
    case 1: /* on */
494
0
      break;
495
0
    case 2: /* keep-last */
496
0
      sg = session_group_contains(s);
497
0
      if (sg == NULL || session_group_count(sg) <= 1)
498
0
        continue;
499
0
      break;
500
0
    case 3: /* keep-group */
501
0
      sg = session_group_contains(s);
502
0
      if (sg != NULL && session_group_count(sg) == 1)
503
0
        continue;
504
0
      break;
505
0
    }
506
0
    session_destroy(s, 1, __func__);
507
0
  }
508
0
}
509
510
void
511
server_unzoom_window(struct window *w)
512
0
{
513
0
  if (window_unzoom(w, 1) == 0)
514
0
    server_redraw_window(w);
515
0
}