Coverage Report

Created: 2026-06-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-attach-session.c
Line
Count
Source
1
/* $OpenBSD$ */
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 <unistd.h>
26
27
#include "tmux.h"
28
29
/*
30
 * Attach existing session to the current terminal.
31
 */
32
33
static enum cmd_retval  cmd_attach_session_exec(struct cmd *,
34
          struct cmdq_item *);
35
36
const struct cmd_entry cmd_attach_session_entry = {
37
  .name = "attach-session",
38
  .alias = "attach",
39
40
  .args = { "c:dEf:rt:x", 0, 0, NULL },
41
  .usage = "[-dErx] [-c working-directory] [-f flags] "
42
           CMD_TARGET_SESSION_USAGE,
43
44
  /* -t is special */
45
46
  .flags = CMD_STARTSERVER|CMD_READONLY,
47
  .exec = cmd_attach_session_exec
48
};
49
50
enum cmd_retval
51
cmd_attach_session(struct cmdq_item *item, const char *tflag, int dflag,
52
    int xflag, int rflag, const char *cflag, int Eflag, const char *fflag)
53
0
{
54
0
  struct cmd_find_state *current = cmdq_get_current(item);
55
0
  struct cmd_find_state  target;
56
0
  enum cmd_find_type   type;
57
0
  int      flags;
58
0
  struct client   *c = cmdq_get_client(item), *c_loop;
59
0
  struct session    *s;
60
0
  struct winlink    *wl;
61
0
  struct window_pane  *wp;
62
0
  char      *cwd, *cause;
63
0
  enum msgtype     msgtype;
64
0
  uid_t      uid;
65
66
0
  if (RB_EMPTY(&sessions)) {
67
0
    cmdq_error(item, "no sessions");
68
0
    return (CMD_RETURN_ERROR);
69
0
  }
70
71
0
  if (c == NULL)
72
0
    return (CMD_RETURN_NORMAL);
73
74
0
  if (server_client_check_nested(c)) {
75
0
    cmdq_error(item, "sessions should be nested with care, "
76
0
        "unset $TMUX to force");
77
0
    return (CMD_RETURN_ERROR);
78
0
  }
79
80
0
  if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
81
0
    type = CMD_FIND_PANE;
82
0
    flags = 0;
83
0
  } else {
84
0
    type = CMD_FIND_SESSION;
85
0
    flags = CMD_FIND_PREFER_UNATTACHED;
86
0
  }
87
0
  if (cmd_find_target(&target, item, tflag, type, flags) != 0)
88
0
    return (CMD_RETURN_ERROR);
89
0
  s = target.s;
90
0
  wl = target.wl;
91
0
  wp = target.wp;
92
93
0
  if (wl != NULL) {
94
0
    if (wp != NULL)
95
0
      window_set_active_pane(wp->window, wp, 1);
96
0
    session_set_current(s, wl);
97
0
    if (wp != NULL)
98
0
      cmd_find_from_winlink_pane(current, wl, wp, 0);
99
0
    else
100
0
      cmd_find_from_winlink(current, wl, 0);
101
0
  }
102
103
0
  if (cflag != NULL) {
104
0
    cwd = format_single(item, cflag, c, s, wl, wp);
105
0
    free((void *)s->cwd);
106
0
    s->cwd = cwd;
107
0
  }
108
0
  if (fflag)
109
0
    server_client_set_flags(c, fflag);
110
0
  if (rflag) {
111
0
    if (c->flags & CLIENT_READONLY) {
112
0
      uid = proc_get_peer_uid(c->peer);
113
0
      if (uid != getuid()) {
114
0
        cmdq_error(item, "client is read-only");
115
0
        return (CMD_RETURN_ERROR);
116
0
      }
117
0
    }
118
0
    c->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
119
0
  }
120
121
0
  c->last_session = c->session;
122
0
  if (c->session != NULL) {
123
0
    if (dflag || xflag) {
124
0
      if (xflag)
125
0
        msgtype = MSG_DETACHKILL;
126
0
      else
127
0
        msgtype = MSG_DETACH;
128
0
      TAILQ_FOREACH(c_loop, &clients, entry) {
129
0
        if (c_loop->session != s || c == c_loop)
130
0
          continue;
131
0
        server_client_detach(c_loop, msgtype);
132
0
      }
133
0
    }
134
0
    if (!Eflag)
135
0
      environ_update(s->options, c->environ, s->environ);
136
137
0
    server_client_set_session(c, s);
138
0
    if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
139
0
      server_client_set_key_table(c, NULL);
140
0
  } else {
141
0
    if (server_client_open(c, &cause) != 0) {
142
0
      cmdq_error(item, "open terminal failed: %s", cause);
143
0
      free(cause);
144
0
      return (CMD_RETURN_ERROR);
145
0
    }
146
147
0
    if (dflag || xflag) {
148
0
      if (xflag)
149
0
        msgtype = MSG_DETACHKILL;
150
0
      else
151
0
        msgtype = MSG_DETACH;
152
0
      TAILQ_FOREACH(c_loop, &clients, entry) {
153
0
        if (c_loop->session != s || c == c_loop)
154
0
          continue;
155
0
        server_client_detach(c_loop, msgtype);
156
0
      }
157
0
    }
158
0
    if (!Eflag)
159
0
      environ_update(s->options, c->environ, s->environ);
160
161
0
    server_client_set_session(c, s);
162
0
    server_client_set_key_table(c, NULL);
163
164
0
    if (~c->flags & CLIENT_CONTROL)
165
0
      proc_send(c->peer, MSG_READY, -1, NULL, 0);
166
0
    notify_client("client-attached", c);
167
0
    c->flags |= CLIENT_ATTACHED;
168
0
  }
169
170
0
  if (cfg_finished)
171
0
    cfg_show_causes(s);
172
173
0
  return (CMD_RETURN_NORMAL);
174
0
}
175
176
static enum cmd_retval
177
cmd_attach_session_exec(struct cmd *self, struct cmdq_item *item)
178
0
{
179
0
  struct args *args = cmd_get_args(self);
180
181
0
  return (cmd_attach_session(item, args_get(args, 't'),
182
0
      args_has(args, 'd'), args_has(args, 'x'), args_has(args, 'r'),
183
0
      args_get(args, 'c'), args_has(args, 'E'), args_get(args, 'f')));
184
0
}