/src/tmux/cmd-kill-session.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-kill-session.c,v 1.31 2026/06/09 12:57:40 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 | | .alias = NULL, |
40 | | |
41 | | .args = { "aCgf:t:", 0, 0, NULL }, |
42 | | .usage = "[-aCg] [-f filter] " CMD_TARGET_SESSION_USAGE, |
43 | | |
44 | | .target = { 't', CMD_FIND_SESSION, 0 }, |
45 | | |
46 | | .flags = 0, |
47 | | .exec = cmd_kill_session_exec |
48 | | }; |
49 | | |
50 | | static enum cmd_retval |
51 | | cmd_kill_session_exec(struct cmd *self, struct cmdq_item *item) |
52 | 0 | { |
53 | 0 | struct args *args = cmd_get_args(self); |
54 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
55 | 0 | struct session *s = target->s, *sloop, *stmp; |
56 | 0 | struct session_group *sg; |
57 | 0 | struct winlink *wl; |
58 | 0 | const char *filter = args_get(args, 'f'); |
59 | |
|
60 | 0 | if (filter != NULL && (!args_has(args, 'a') || args_has(args, 'C'))) { |
61 | 0 | cmdq_error(item, "-f only valid with -a"); |
62 | 0 | return (CMD_RETURN_ERROR); |
63 | 0 | } |
64 | | |
65 | 0 | if (args_has(args, 'C')) { |
66 | 0 | RB_FOREACH(wl, winlinks, &s->windows) { |
67 | 0 | wl->window->flags &= ~WINDOW_ALERTFLAGS; |
68 | 0 | wl->flags &= ~WINLINK_ALERTFLAGS; |
69 | 0 | } |
70 | 0 | server_redraw_session(s); |
71 | 0 | } else if (args_has(args, 'a')) |
72 | 0 | return (cmd_kill_session_all(item, filter)); |
73 | 0 | else if (args_has(args, 'g') && |
74 | 0 | (sg = session_group_contains(s)) != NULL) { |
75 | 0 | TAILQ_FOREACH_SAFE(sloop, &sg->sessions, gentry, stmp) { |
76 | 0 | server_destroy_session(sloop); |
77 | 0 | session_destroy(sloop, 1, __func__); |
78 | 0 | } |
79 | 0 | } else { |
80 | 0 | server_destroy_session(s); |
81 | 0 | session_destroy(s, 1, __func__); |
82 | 0 | } |
83 | 0 | return (CMD_RETURN_NORMAL); |
84 | 0 | } |
85 | | |
86 | | static enum cmd_retval |
87 | | cmd_kill_session_all(struct cmdq_item *item, const char *filter) |
88 | 0 | { |
89 | 0 | struct session *s = cmdq_get_target(item)->s; |
90 | 0 | struct session *sloop, *stmp; |
91 | |
|
92 | 0 | RB_FOREACH_SAFE(sloop, sessions, &sessions, stmp) { |
93 | 0 | if (sloop == s) |
94 | 0 | continue; |
95 | 0 | if (!cmd_kill_session_filter(item, sloop, filter)) |
96 | 0 | continue; |
97 | 0 | server_destroy_session(sloop); |
98 | 0 | session_destroy(sloop, 1, __func__); |
99 | 0 | } |
100 | 0 | return (CMD_RETURN_NORMAL); |
101 | 0 | } |
102 | | |
103 | | static int |
104 | | cmd_kill_session_filter(struct cmdq_item *item, struct session *s, |
105 | | const char *filter) |
106 | 0 | { |
107 | 0 | struct format_tree *ft; |
108 | 0 | char *expanded; |
109 | 0 | int flag; |
110 | |
|
111 | 0 | if (filter == NULL) |
112 | 0 | return (1); |
113 | | |
114 | 0 | ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0); |
115 | 0 | format_defaults(ft, NULL, s, NULL, NULL); |
116 | |
|
117 | 0 | expanded = format_expand(ft, filter); |
118 | 0 | flag = format_true(expanded); |
119 | 0 | free(expanded); |
120 | |
|
121 | 0 | format_free(ft); |
122 | 0 | return (flag); |
123 | 0 | } |