Coverage Report

Created: 2026-10-17 06:19

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