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