Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.11
1.11 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.10 2009/07/26 12:58:44 nicm Exp $ */
1.1 nicm 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 <string.h>
1.5 nicm 22: #include <time.h>
1.1 nicm 23: #include <unistd.h>
24:
25: #include "tmux.h"
26:
27: int server_lock_callback(void *, const char *);
28:
29: const char **
30: server_fill_environ(struct session *s)
31: {
1.4 nicm 32: static const char *env[] = { NULL /* TMUX= */, NULL /* TERM */, NULL };
33: static char tmuxvar[MAXPATHLEN + 256], termvar[256];
1.1 nicm 34: u_int idx;
35:
36: if (session_index(s, &idx) != 0)
37: fatalx("session not found");
38:
39: xsnprintf(tmuxvar, sizeof tmuxvar,
40: "TMUX=%s,%ld,%u", socket_path, (long) getpid(), idx);
41: env[0] = tmuxvar;
1.4 nicm 42:
43: xsnprintf(termvar, sizeof termvar,
44: "TERM=%s", options_get_string(&s->options, "default-terminal"));
45: env[1] = termvar;
1.1 nicm 46:
47: return (env);
48: }
49:
50: void
1.10 nicm 51: server_write_error(struct client *c, const char *msg)
52: {
53: struct msg_print_data printdata;
54:
55: strlcpy(printdata.msg, msg, sizeof printdata.msg);
56: server_write_client(c, MSG_ERROR, &printdata, sizeof printdata);
57: }
58:
59: void
1.1 nicm 60: server_write_client(
1.11 ! nicm 61: struct client *c, enum msgtype type, const void *buf, size_t len)
1.1 nicm 62: {
63: struct hdr hdr;
64:
65: log_debug("writing %d to client %d", type, c->fd);
66:
67: hdr.type = type;
68: hdr.size = len;
69:
70: buffer_write(c->out, &hdr, sizeof hdr);
71: if (buf != NULL && len > 0)
72: buffer_write(c->out, buf, len);
73: }
74:
75: void
76: server_write_session(
1.11 ! nicm 77: struct session *s, enum msgtype type, const void *buf, size_t len)
1.1 nicm 78: {
79: struct client *c;
80: u_int i;
81:
82: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
83: c = ARRAY_ITEM(&clients, i);
84: if (c == NULL || c->session == NULL)
85: continue;
86: if (c->session == s)
87: server_write_client(c, type, buf, len);
88: }
89: }
90:
91: void
92: server_redraw_client(struct client *c)
93: {
94: c->flags |= CLIENT_REDRAW;
95: }
96:
97: void
98: server_status_client(struct client *c)
99: {
100: c->flags |= CLIENT_STATUS;
101: }
102:
103: void
104: server_redraw_session(struct session *s)
105: {
106: struct client *c;
107: u_int i;
108:
109: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
110: c = ARRAY_ITEM(&clients, i);
111: if (c == NULL || c->session == NULL)
112: continue;
113: if (c->session == s)
114: server_redraw_client(c);
115: }
116: }
117:
118: void
119: server_status_session(struct session *s)
120: {
121: struct client *c;
122: u_int i;
123:
124: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
125: c = ARRAY_ITEM(&clients, i);
126: if (c == NULL || c->session == NULL)
127: continue;
128: if (c->session == s)
129: server_status_client(c);
130: }
131: }
132:
133: void
134: server_redraw_window(struct window *w)
135: {
136: struct client *c;
137: u_int i;
138:
139: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
140: c = ARRAY_ITEM(&clients, i);
141: if (c == NULL || c->session == NULL)
142: continue;
143: if (c->session->curw->window == w)
144: server_redraw_client(c);
145: }
146: w->flags |= WINDOW_REDRAW;
147: }
148:
149: void
150: server_status_window(struct window *w)
151: {
152: struct session *s;
153: u_int i;
154:
155: /*
156: * This is slightly different. We want to redraw the status line of any
157: * clients containing this window rather than any where it is the
158: * current window.
159: */
160:
161: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
162: s = ARRAY_ITEM(&sessions, i);
163: if (s != NULL && session_has(s, w))
164: server_status_session(s);
165: }
166: }
167:
168: void
169: server_lock(void)
170: {
171: struct client *c;
172: u_int i;
173:
174: if (server_locked)
175: return;
176:
177: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
178: c = ARRAY_ITEM(&clients, i);
179: if (c == NULL || c->session == NULL)
180: continue;
181:
182: status_prompt_clear(c);
1.6 nicm 183: status_prompt_set(c,
1.7 nicm 184: "Password: ", server_lock_callback, NULL, c, PROMPT_HIDDEN);
1.1 nicm 185: server_redraw_client(c);
186: }
187: server_locked = 1;
188: }
189:
190: int
191: server_lock_callback(unused void *data, const char *s)
192: {
193: return (server_unlock(s));
194: }
195:
196: int
197: server_unlock(const char *s)
198: {
199: struct client *c;
200: u_int i;
201: char *out;
202:
203: if (!server_locked)
204: return (0);
205: server_activity = time(NULL);
206:
207: if (server_password != NULL) {
208: if (s == NULL)
209: return (-1);
210: out = crypt(s, server_password);
211: if (strcmp(out, server_password) != 0)
212: goto wrong;
213: }
214:
215: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
216: c = ARRAY_ITEM(&clients, i);
217: if (c == NULL)
218: continue;
219:
220: status_prompt_clear(c);
221: server_redraw_client(c);
222: }
223:
224: server_locked = 0;
1.9 nicm 225: password_failures = 0;
1.1 nicm 226: return (0);
227:
228: wrong:
1.9 nicm 229: password_failures++;
1.1 nicm 230: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
231: c = ARRAY_ITEM(&clients, i);
1.10 nicm 232: if (c == NULL || c->prompt_buffer == NULL)
1.1 nicm 233: continue;
234:
235: *c->prompt_buffer = '\0';
236: c->prompt_index = 0;
1.9 nicm 237: server_redraw_client(c);
1.1 nicm 238: }
239:
240: return (-1);
1.8 nicm 241: }
242:
243: void
244: server_kill_window(struct window *w)
245: {
246: struct session *s;
247: struct winlink *wl;
248: struct client *c;
249: u_int i, j;
250: int destroyed;
251:
252:
253: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
254: s = ARRAY_ITEM(&sessions, i);
255: if (s == NULL || !session_has(s, w))
256: continue;
257: if ((wl = winlink_find_by_window(&s->windows, w)) == NULL)
258: continue;
259:
260: destroyed = session_detach(s, wl);
261: for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
262: c = ARRAY_ITEM(&clients, j);
263: if (c == NULL || c->session != s)
264: continue;
265:
266: if (destroyed) {
267: c->session = NULL;
268: server_write_client(c, MSG_EXIT, NULL, 0);
269: } else
270: server_redraw_client(c);
271: }
272: }
273: recalculate_sizes();
1.1 nicm 274: }