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