Coverage Report

Created: 2026-10-17 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-kill-session.c
Line
Count
Source
1
/* $OpenBSD: cmd-kill-session.c,v 1.32 2026/10/09 13:12:14 nicm Exp $ */
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
21
#include <stdlib.h>
22
23
#include "tmux.h"
24
25
/*
26
 * Destroy session, detaching all clients attached to it and destroying any
27
 * windows linked only to this session.
28
 *
29
 * Note this deliberately has no alias to make it hard to hit by accident.
30
 */
31
32
static enum cmd_retval  cmd_kill_session_exec(struct cmd *, struct cmdq_item *);
33
static enum cmd_retval  cmd_kill_session_all(struct cmdq_item *, const char *);
34
static int    cmd_kill_session_filter(struct cmdq_item *,
35
          struct session *, const char *);
36
37
const struct cmd_entry cmd_kill_session_entry = {
38
  .name = "kill-session",
39
  .description = "Destroy one or more sessions.",
40
  .alias = NULL,
41
42
  .args = { "aCgf:t:", 0, 0, NULL },
43
  .usage = "[-aCg] [-f filter] " CMD_TARGET_SESSION_USAGE,
44
45
  .target = { 't', CMD_FIND_SESSION, 0 },
46
47
  .flags = 0,
48
  .exec = cmd_kill_session_exec
49
};
50
51
static enum cmd_retval
52
cmd_kill_session_exec(struct cmd *self, struct cmdq_item *item)
53
0
{
54
0
  struct args   *args = cmd_get_args(self);
55
0
  struct cmd_find_state *target = cmdq_get_target(item);
56
0
  struct session    *s = target->s, *sloop, *stmp;
57
0
  struct session_group  *sg;
58
0
  struct winlink    *wl;
59
0
  const char    *filter = args_get(args, 'f');
60
61
0
  if (filter != NULL && (!args_has(args, 'a') || args_has(args, 'C'))) {
62
0
    cmdq_error(item, "-f only valid with -a");
63
0
    return (CMD_RETURN_ERROR);
64
0
  }
65
66
0
  if (args_has(args, 'C')) {
67
0
    RB_FOREACH(wl, winlinks, &s->windows) {
68
0
      wl->window->flags &= ~WINDOW_ALERTFLAGS;
69
0
      wl->flags &= ~WINLINK_ALERTFLAGS;
70
0
    }
71
0
    server_redraw_session(s);
72
0
  } else if (args_has(args, 'a'))
73
0
    return (cmd_kill_session_all(item, filter));
74
0
  else if (args_has(args, 'g') &&
75
0
      (sg = session_group_contains(s)) != NULL) {
76
0
    TAILQ_FOREACH_SAFE(sloop, &sg->sessions, gentry, stmp) {
77
0
      server_destroy_session(sloop);
78
0
      session_destroy(sloop, 1, __func__);
79
0
    }
80
0
  } else {
81
0
    server_destroy_session(s);
82
0
    session_destroy(s, 1, __func__);
83
0
  }
84
0
  return (CMD_RETURN_NORMAL);
85
0
}
86
87
static enum cmd_retval
88
cmd_kill_session_all(struct cmdq_item *item, const char *filter)
89
0
{
90
0
  struct session  *s = cmdq_get_target(item)->s;
91
0
  struct session  *sloop, *stmp;
92
93
0
  RB_FOREACH_SAFE(sloop, sessions, &sessions, stmp) {
94
0
    if (sloop == s)
95
0
      continue;
96
0
    if (!cmd_kill_session_filter(item, sloop, filter))
97
0
      continue;
98
0
    server_destroy_session(sloop);
99
0
    session_destroy(sloop, 1, __func__);
100
0
  }
101
0
  return (CMD_RETURN_NORMAL);
102
0
}
103
104
static int
105
cmd_kill_session_filter(struct cmdq_item *item, struct session *s,
106
    const char *filter)
107
0
{
108
0
  struct format_tree  *ft;
109
0
  char      *expanded;
110
0
  int      flag;
111
112
0
  if (filter == NULL)
113
0
    return (1);
114
115
0
  ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
116
0
  format_defaults(ft, NULL, s, NULL, NULL);
117
118
0
  expanded = format_expand(ft, filter);
119
0
  flag = format_true(expanded);
120
0
  free(expanded);
121
122
0
  format_free(ft);
123
0
  return (flag);
124
0
}