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