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