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