/src/tmux/cmd-new-session.c
Line | Count | Source |
1 | | /* $OpenBSD: cmd-new-session.c,v 1.152 2026/07/10 13:38:45 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 <errno.h> |
22 | | #include <fcntl.h> |
23 | | #include <stdlib.h> |
24 | | #include <string.h> |
25 | | #include <termios.h> |
26 | | #include <unistd.h> |
27 | | |
28 | | #include "tmux.h" |
29 | | |
30 | | /* |
31 | | * Create a new session and attach to the current terminal unless -d is given. |
32 | | */ |
33 | | |
34 | 0 | #define NEW_SESSION_TEMPLATE "#{session_name}:" |
35 | | |
36 | | static enum cmd_retval cmd_new_session_exec(struct cmd *, struct cmdq_item *); |
37 | | |
38 | | const struct cmd_entry cmd_new_session_entry = { |
39 | | .name = "new-session", |
40 | | .alias = "new", |
41 | | |
42 | | .args = { "Ac:dDe:EF:f:n:Ps:t:x:Xy:", 0, -1, NULL }, |
43 | | .usage = "[-AdDEPX] [-c start-directory] [-e environment] [-F format] " |
44 | | "[-f flags] [-n window-name] [-s session-name] " |
45 | | CMD_TARGET_SESSION_USAGE " [-x width] [-y height] " |
46 | | "[shell-command [argument ...]]", |
47 | | |
48 | | .target = { 't', CMD_FIND_SESSION, CMD_FIND_CANFAIL }, |
49 | | |
50 | | .flags = CMD_STARTSERVER, |
51 | | .exec = cmd_new_session_exec |
52 | | }; |
53 | | |
54 | | const struct cmd_entry cmd_has_session_entry = { |
55 | | .name = "has-session", |
56 | | .alias = "has", |
57 | | |
58 | | .args = { "t:", 0, 0, NULL }, |
59 | | .usage = CMD_TARGET_SESSION_USAGE, |
60 | | |
61 | | .target = { 't', CMD_FIND_SESSION, 0 }, |
62 | | |
63 | | .flags = 0, |
64 | | .exec = cmd_new_session_exec |
65 | | }; |
66 | | |
67 | | static enum cmd_retval |
68 | | cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) |
69 | 0 | { |
70 | 0 | struct args *args = cmd_get_args(self); |
71 | 0 | struct cmd_find_state *current = cmdq_get_current(item); |
72 | 0 | struct cmd_find_state *target = cmdq_get_target(item); |
73 | 0 | struct client *c = cmdq_get_client(item); |
74 | 0 | struct session *s, *as, *groupwith = NULL; |
75 | 0 | struct environ *env; |
76 | 0 | struct options *oo; |
77 | 0 | struct termios tio, *tiop; |
78 | 0 | struct session_group *sg = NULL; |
79 | 0 | const char *errstr, *template, *group, *tmp; |
80 | 0 | char *cause, *cwd = NULL, *cp, *ename; |
81 | 0 | char *wname = NULL, *sname = NULL, *prefix = NULL; |
82 | 0 | int detached, already_attached, is_control = 0; |
83 | 0 | u_int sx, sy, dsx, dsy, count = args_count(args); |
84 | 0 | struct spawn_context sc = { 0 }; |
85 | 0 | enum cmd_retval retval; |
86 | 0 | struct cmd_find_state fs; |
87 | 0 | struct args_value *av; |
88 | |
|
89 | 0 | if (cmd_get_entry(self) == &cmd_has_session_entry) { |
90 | | /* |
91 | | * cmd_find_target() will fail if the session cannot be found, |
92 | | * so always return success here. |
93 | | */ |
94 | 0 | return (CMD_RETURN_NORMAL); |
95 | 0 | } |
96 | | |
97 | 0 | if (args_has(args, 't') && (count != 0 || args_has(args, 'n'))) { |
98 | 0 | cmdq_error(item, "command or window name given with target"); |
99 | 0 | return (CMD_RETURN_ERROR); |
100 | 0 | } |
101 | | |
102 | 0 | if ((tmp = args_get(args, 'n')) != NULL) { |
103 | 0 | ename = format_single(item, tmp, c, NULL, NULL, NULL); |
104 | 0 | if (!check_name(ename)) { |
105 | 0 | cmdq_error(item, "invalid window name: %s", ename); |
106 | 0 | free(ename); |
107 | 0 | return (CMD_RETURN_ERROR); |
108 | 0 | } |
109 | 0 | wname = clean_name(ename, 0); |
110 | 0 | free(ename); |
111 | 0 | } |
112 | 0 | if ((tmp = args_get(args, 's')) != NULL) { |
113 | 0 | ename = format_single(item, tmp, c, NULL, NULL, NULL); |
114 | 0 | if (!check_name(ename)) { |
115 | 0 | cmdq_error(item, "invalid session name: %s", ename); |
116 | 0 | free(ename); |
117 | 0 | goto fail; |
118 | 0 | } |
119 | 0 | sname = clean_name(ename, 0); |
120 | 0 | free(ename); |
121 | 0 | } |
122 | 0 | if (args_has(args, 'A')) { |
123 | 0 | if (sname != NULL) |
124 | 0 | as = session_find(sname); |
125 | 0 | else |
126 | 0 | as = target->s; |
127 | 0 | if (as != NULL) { |
128 | 0 | retval = cmd_attach_session(item, as->name, |
129 | 0 | args_has(args, 'D'), args_has(args, 'X'), 0, |
130 | 0 | args_get(args, 'c'), args_has(args, 'E'), |
131 | 0 | args_get(args, 'f')); |
132 | 0 | free(wname); |
133 | 0 | free(sname); |
134 | 0 | return (retval); |
135 | 0 | } |
136 | 0 | } |
137 | 0 | if (sname != NULL && session_find(sname) != NULL) { |
138 | 0 | cmdq_error(item, "duplicate session: %s", sname); |
139 | 0 | goto fail; |
140 | 0 | } |
141 | | |
142 | | /* Is this going to be part of a session group? */ |
143 | 0 | group = args_get(args, 't'); |
144 | 0 | if (group != NULL) { |
145 | 0 | groupwith = target->s; |
146 | 0 | if (groupwith == NULL) |
147 | 0 | sg = session_group_find(group); |
148 | 0 | else |
149 | 0 | sg = session_group_contains(groupwith); |
150 | 0 | if (sg != NULL) |
151 | 0 | prefix = xstrdup(sg->name); |
152 | 0 | else if (groupwith != NULL) |
153 | 0 | prefix = xstrdup(groupwith->name); |
154 | 0 | else { |
155 | 0 | if (!check_name(group)) { |
156 | 0 | cmdq_error(item, |
157 | 0 | "invalid session group name: %s", group); |
158 | 0 | goto fail; |
159 | 0 | } |
160 | 0 | prefix = clean_name(group, 0); |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | /* Set -d if no client. */ |
165 | 0 | detached = args_has(args, 'd'); |
166 | 0 | if (c == NULL) |
167 | 0 | detached = 1; |
168 | 0 | else if (c->flags & CLIENT_CONTROL) |
169 | 0 | is_control = 1; |
170 | | |
171 | | /* Is this client already attached? */ |
172 | 0 | already_attached = 0; |
173 | 0 | if (c != NULL && c->session != NULL) |
174 | 0 | already_attached = 1; |
175 | | |
176 | | /* Get the new session working directory. */ |
177 | 0 | if ((tmp = args_get(args, 'c')) != NULL) |
178 | 0 | cwd = format_single(item, tmp, c, NULL, NULL, NULL); |
179 | 0 | else |
180 | 0 | cwd = xstrdup(server_client_get_cwd(c, NULL)); |
181 | | |
182 | | /* |
183 | | * If this is a new client, check for nesting and save the termios |
184 | | * settings (part of which is used for new windows in this session). |
185 | | * |
186 | | * tcgetattr() is used rather than using tty.tio since if the client is |
187 | | * detached, tty_open won't be called. It must be done before opening |
188 | | * the terminal as that calls tcsetattr() to prepare for tmux taking |
189 | | * over. |
190 | | */ |
191 | 0 | if (!detached && |
192 | 0 | !already_attached && |
193 | 0 | c->fd != -1 && |
194 | 0 | (~c->flags & CLIENT_CONTROL)) { |
195 | 0 | if (server_client_check_nested(cmdq_get_client(item))) { |
196 | 0 | cmdq_error(item, "sessions should be nested with care, " |
197 | 0 | "unset $TMUX to force"); |
198 | 0 | goto fail; |
199 | 0 | } |
200 | 0 | if (tcgetattr(c->fd, &tio) != 0) |
201 | 0 | fatal("tcgetattr failed"); |
202 | 0 | tiop = &tio; |
203 | 0 | } else |
204 | 0 | tiop = NULL; |
205 | | |
206 | | /* Open the terminal if necessary. */ |
207 | 0 | if (!detached && !already_attached) { |
208 | 0 | if (server_client_open(c, &cause) != 0) { |
209 | 0 | cmdq_error(item, "open terminal failed: %s", cause); |
210 | 0 | free(cause); |
211 | 0 | goto fail; |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | /* Get default session size. */ |
216 | 0 | if (args_has(args, 'x')) { |
217 | 0 | tmp = args_get(args, 'x'); |
218 | 0 | if (strcmp(tmp, "-") == 0) { |
219 | 0 | if (c != NULL) |
220 | 0 | dsx = c->tty.sx; |
221 | 0 | else |
222 | 0 | dsx = 80; |
223 | 0 | } else { |
224 | 0 | dsx = strtonum(tmp, 1, USHRT_MAX, &errstr); |
225 | 0 | if (errstr != NULL) { |
226 | 0 | cmdq_error(item, "width %s", errstr); |
227 | 0 | goto fail; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | } else |
231 | 0 | dsx = 80; |
232 | 0 | if (args_has(args, 'y')) { |
233 | 0 | tmp = args_get(args, 'y'); |
234 | 0 | if (strcmp(tmp, "-") == 0) { |
235 | 0 | if (c != NULL) |
236 | 0 | dsy = c->tty.sy; |
237 | 0 | else |
238 | 0 | dsy = 24; |
239 | 0 | } else { |
240 | 0 | dsy = strtonum(tmp, 1, USHRT_MAX, &errstr); |
241 | 0 | if (errstr != NULL) { |
242 | 0 | cmdq_error(item, "height %s", errstr); |
243 | 0 | goto fail; |
244 | 0 | } |
245 | 0 | } |
246 | 0 | } else |
247 | 0 | dsy = 24; |
248 | | |
249 | | /* Find new session size. */ |
250 | 0 | if (!detached && !is_control) { |
251 | 0 | sx = c->tty.sx; |
252 | 0 | sy = c->tty.sy; |
253 | 0 | if (sy > 0 && options_get_number(global_s_options, "status")) |
254 | 0 | sy--; |
255 | 0 | } else { |
256 | 0 | tmp = options_get_string(global_s_options, "default-size"); |
257 | 0 | if (sscanf(tmp, "%ux%u", &sx, &sy) != 2) { |
258 | 0 | sx = dsx; |
259 | 0 | sy = dsy; |
260 | 0 | } else { |
261 | 0 | if (args_has(args, 'x')) |
262 | 0 | sx = dsx; |
263 | 0 | if (args_has(args, 'y')) |
264 | 0 | sy = dsy; |
265 | 0 | } |
266 | 0 | } |
267 | 0 | if (sx == 0) |
268 | 0 | sx = 1; |
269 | 0 | if (sy == 0) |
270 | 0 | sy = 1; |
271 | | |
272 | | /* Create the new session. */ |
273 | 0 | oo = options_create(global_s_options); |
274 | 0 | if (args_has(args, 'x') || args_has(args, 'y')) { |
275 | 0 | if (!args_has(args, 'x')) |
276 | 0 | dsx = sx; |
277 | 0 | if (!args_has(args, 'y')) |
278 | 0 | dsy = sy; |
279 | 0 | options_set_string(oo, "default-size", 0, "%ux%u", dsx, dsy); |
280 | 0 | } |
281 | 0 | env = environ_create(); |
282 | 0 | if (c != NULL && !args_has(args, 'E')) |
283 | 0 | environ_update(global_s_options, c->environ, env); |
284 | 0 | av = args_first_value(args, 'e'); |
285 | 0 | while (av != NULL) { |
286 | 0 | environ_put(env, av->string, 0); |
287 | 0 | av = args_next_value(av); |
288 | 0 | } |
289 | 0 | s = session_create(prefix, sname, cwd, env, oo, tiop); |
290 | | |
291 | | /* Spawn the initial window. */ |
292 | 0 | sc.item = item; |
293 | 0 | sc.s = s; |
294 | 0 | if (!detached) |
295 | 0 | sc.tc = c; |
296 | |
|
297 | 0 | sc.name = wname; |
298 | 0 | args_to_vector(args, &sc.argc, &sc.argv); |
299 | |
|
300 | 0 | sc.idx = -1; |
301 | 0 | sc.cwd = args_get(args, 'c'); |
302 | |
|
303 | 0 | sc.flags = 0; |
304 | |
|
305 | 0 | if (spawn_window(&sc, &cause) == NULL) { |
306 | 0 | session_destroy(s, 0, __func__); |
307 | 0 | cmdq_error(item, "create window failed: %s", cause); |
308 | 0 | free(cause); |
309 | 0 | goto fail; |
310 | 0 | } |
311 | | |
312 | | /* |
313 | | * If a target session is given, this is to be part of a session group, |
314 | | * so add it to the group and synchronize. |
315 | | */ |
316 | 0 | if (group != NULL) { |
317 | 0 | if (sg == NULL) { |
318 | 0 | if (groupwith != NULL) { |
319 | 0 | sg = session_group_new(groupwith->name); |
320 | 0 | session_group_add(sg, groupwith); |
321 | 0 | } else |
322 | 0 | sg = session_group_new(group); |
323 | 0 | } |
324 | 0 | session_group_add(sg, s); |
325 | 0 | session_group_synchronize_to(s); |
326 | 0 | session_select(s, RB_MIN(winlinks, &s->windows)->idx); |
327 | 0 | } |
328 | 0 | events_fire_session("session-created", s); |
329 | | |
330 | | /* |
331 | | * Set the client to the new session. If a command client exists, it is |
332 | | * taking this session and needs to get MSG_READY and stay around. |
333 | | */ |
334 | 0 | if (!detached) { |
335 | 0 | if (args_has(args, 'f')) |
336 | 0 | server_client_set_flags(c, args_get(args, 'f')); |
337 | 0 | if (!already_attached) { |
338 | 0 | if (~c->flags & CLIENT_CONTROL) |
339 | 0 | proc_send(c->peer, MSG_READY, -1, NULL, 0); |
340 | 0 | } else if (c->session != NULL) |
341 | 0 | c->last_session = c->session; |
342 | 0 | server_client_set_session(c, s); |
343 | 0 | if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT) |
344 | 0 | server_client_set_key_table(c, NULL); |
345 | 0 | } |
346 | | |
347 | | /* Print if requested. */ |
348 | 0 | if (args_has(args, 'P')) { |
349 | 0 | if ((template = args_get(args, 'F')) == NULL) |
350 | 0 | template = NEW_SESSION_TEMPLATE; |
351 | 0 | cp = format_single(item, template, c, s, s->curw, NULL); |
352 | 0 | cmdq_print(item, "%s", cp); |
353 | 0 | free(cp); |
354 | 0 | } |
355 | |
|
356 | 0 | if (!detached) |
357 | 0 | c->flags |= CLIENT_ATTACHED; |
358 | 0 | if (!args_has(args, 'd')) |
359 | 0 | cmd_find_from_session(current, s, 0); |
360 | |
|
361 | 0 | cmd_find_from_session(&fs, s, 0); |
362 | 0 | cmdq_insert_hook(s, item, &fs, "after-new-session"); |
363 | |
|
364 | 0 | if (cfg_finished) |
365 | 0 | cfg_show_causes(s); |
366 | |
|
367 | 0 | if (sc.argv != NULL) |
368 | 0 | cmd_free_argv(sc.argc, sc.argv); |
369 | 0 | free(cwd); |
370 | 0 | free(wname); |
371 | 0 | free(sname); |
372 | 0 | free(prefix); |
373 | 0 | return (CMD_RETURN_NORMAL); |
374 | | |
375 | 0 | fail: |
376 | 0 | if (sc.argv != NULL) |
377 | 0 | cmd_free_argv(sc.argc, sc.argv); |
378 | 0 | free(cwd); |
379 | 0 | free(wname); |
380 | 0 | free(sname); |
381 | 0 | free(prefix); |
382 | 0 | return (CMD_RETURN_ERROR); |
383 | 0 | } |