1 /* $OpenBSD: cmd-attach-session.c,v 1.34 2015/04/25 18:09:28 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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 <unistd.h> 26 27 #include "tmux.h" 28 29 /* 30 * Attach existing session to the current terminal. 31 */ 32 33 enum cmd_retval cmd_attach_session_exec(struct cmd *, struct cmd_q *); 34 35 const struct cmd_entry cmd_attach_session_entry = { 36 "attach-session", "attach", 37 "c:drt:", 0, 0, 38 "[-dr] [-c working-directory] " CMD_TARGET_SESSION_USAGE, 39 CMD_CANTNEST|CMD_STARTSERVER, 40 cmd_attach_session_exec 41 }; 42 43 enum cmd_retval 44 cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag, 45 const char *cflag) 46 { 47 struct session *s; 48 struct client *c; 49 struct winlink *wl = NULL; 50 struct window *w = NULL; 51 struct window_pane *wp = NULL; 52 const char *update; 53 char *cause; 54 int fd; 55 struct format_tree *ft; 56 char *cp; 57 58 if (RB_EMPTY(&sessions)) { 59 cmdq_error(cmdq, "no sessions"); 60 return (CMD_RETURN_ERROR); 61 } 62 63 if (tflag == NULL) { 64 if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) 65 return (CMD_RETURN_ERROR); 66 } else if (tflag[strcspn(tflag, ":.")] != '\0') { 67 if ((wl = cmd_find_pane(cmdq, tflag, &s, &wp)) == NULL) 68 return (CMD_RETURN_ERROR); 69 } else { 70 if ((s = cmd_find_session(cmdq, tflag, 1)) == NULL) 71 return (CMD_RETURN_ERROR); 72 w = window_find_by_id_str(tflag); 73 if (w == NULL) { 74 wp = window_pane_find_by_id_str(tflag); 75 if (wp != NULL) 76 w = wp->window; 77 } 78 if (w != NULL) 79 wl = winlink_find_by_window(&s->windows, w); 80 } 81 82 if (cmdq->client == NULL) 83 return (CMD_RETURN_NORMAL); 84 85 if (wl != NULL) { 86 if (wp != NULL) 87 window_set_active_pane(wp->window, wp); 88 session_set_current(s, wl); 89 } 90 91 if (cmdq->client->session != NULL) { 92 if (dflag) { 93 /* 94 * Can't use server_write_session in case attaching to 95 * the same session as currently attached to. 96 */ 97 TAILQ_FOREACH(c, &clients, entry) { 98 if (c->session != s || c == cmdq->client) 99 continue; 100 server_write_client(c, MSG_DETACH, 101 c->session->name, 102 strlen(c->session->name) + 1); 103 } 104 } 105 106 if (cflag != NULL) { 107 ft = format_create(); 108 format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, 109 NULL, NULL); 110 cp = format_expand(ft, cflag); 111 format_free(ft); 112 113 fd = open(cp, O_RDONLY|O_DIRECTORY); 114 free(cp); 115 if (fd == -1) { 116 cmdq_error(cmdq, "bad working directory: %s", 117 strerror(errno)); 118 return (CMD_RETURN_ERROR); 119 } 120 close(s->cwd); 121 s->cwd = fd; 122 } 123 124 cmdq->client->session = s; 125 notify_attached_session_changed(cmdq->client); 126 session_update_activity(s); 127 server_redraw_client(cmdq->client); 128 s->curw->flags &= ~WINLINK_ALERTFLAGS; 129 } else { 130 if (server_client_open(cmdq->client, &cause) != 0) { 131 cmdq_error(cmdq, "open terminal failed: %s", cause); 132 free(cause); 133 return (CMD_RETURN_ERROR); 134 } 135 136 if (cflag != NULL) { 137 ft = format_create(); 138 format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, 139 NULL, NULL); 140 cp = format_expand(ft, cflag); 141 format_free(ft); 142 143 fd = open(cp, O_RDONLY|O_DIRECTORY); 144 free(cp); 145 if (fd == -1) { 146 cmdq_error(cmdq, "bad working directory: %s", 147 strerror(errno)); 148 return (CMD_RETURN_ERROR); 149 } 150 close(s->cwd); 151 s->cwd = fd; 152 } 153 154 if (rflag) 155 cmdq->client->flags |= CLIENT_READONLY; 156 157 if (dflag) { 158 server_write_session(s, MSG_DETACH, s->name, 159 strlen(s->name) + 1); 160 } 161 162 update = options_get_string(&s->options, "update-environment"); 163 environ_update(update, &cmdq->client->environ, &s->environ); 164 165 cmdq->client->session = s; 166 notify_attached_session_changed(cmdq->client); 167 session_update_activity(s); 168 server_redraw_client(cmdq->client); 169 s->curw->flags &= ~WINLINK_ALERTFLAGS; 170 171 server_write_ready(cmdq->client); 172 cmdq->client_exit = 0; 173 } 174 recalculate_sizes(); 175 server_update_socket(); 176 177 return (CMD_RETURN_NORMAL); 178 } 179 180 enum cmd_retval 181 cmd_attach_session_exec(struct cmd *self, struct cmd_q *cmdq) 182 { 183 struct args *args = self->args; 184 185 return (cmd_attach_session(cmdq, args_get(args, 't'), 186 args_has(args, 'd'), args_has(args, 'r'), args_get(args, 'c'))); 187 } 188