Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 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: #include <sys/time.h>
! 21:
! 22: #include <string.h>
! 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: {
! 32: static const char *env[] = { NULL /* TMUX= */, "TERM=screen", NULL };
! 33: static char tmuxvar[MAXPATHLEN + 256];
! 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;
! 42:
! 43: return (env);
! 44: }
! 45:
! 46: void
! 47: server_write_client(
! 48: struct client *c, enum hdrtype type, const void *buf, size_t len)
! 49: {
! 50: struct hdr hdr;
! 51:
! 52: log_debug("writing %d to client %d", type, c->fd);
! 53:
! 54: hdr.type = type;
! 55: hdr.size = len;
! 56:
! 57: buffer_write(c->out, &hdr, sizeof hdr);
! 58: if (buf != NULL && len > 0)
! 59: buffer_write(c->out, buf, len);
! 60: }
! 61:
! 62: void
! 63: server_write_session(
! 64: struct session *s, enum hdrtype type, const void *buf, size_t len)
! 65: {
! 66: struct client *c;
! 67: u_int i;
! 68:
! 69: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 70: c = ARRAY_ITEM(&clients, i);
! 71: if (c == NULL || c->session == NULL)
! 72: continue;
! 73: if (c->session == s)
! 74: server_write_client(c, type, buf, len);
! 75: }
! 76: }
! 77:
! 78: void
! 79: server_write_window(
! 80: struct window *w, enum hdrtype type, const void *buf, size_t len)
! 81: {
! 82: struct client *c;
! 83: u_int i;
! 84:
! 85: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 86: c = ARRAY_ITEM(&clients, i);
! 87: if (c == NULL || c->session == NULL)
! 88: continue;
! 89: if (c->session->curw->window == w)
! 90: server_write_client(c, type, buf, len);
! 91: }
! 92: }
! 93:
! 94: void
! 95: server_redraw_client(struct client *c)
! 96: {
! 97: c->flags |= CLIENT_REDRAW;
! 98: }
! 99:
! 100: void
! 101: server_status_client(struct client *c)
! 102: {
! 103: c->flags |= CLIENT_STATUS;
! 104: }
! 105:
! 106: void
! 107: server_redraw_session(struct session *s)
! 108: {
! 109: struct client *c;
! 110: u_int i;
! 111:
! 112: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 113: c = ARRAY_ITEM(&clients, i);
! 114: if (c == NULL || c->session == NULL)
! 115: continue;
! 116: if (c->session == s)
! 117: server_redraw_client(c);
! 118: }
! 119: }
! 120:
! 121: void
! 122: server_status_session(struct session *s)
! 123: {
! 124: struct client *c;
! 125: u_int i;
! 126:
! 127: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 128: c = ARRAY_ITEM(&clients, i);
! 129: if (c == NULL || c->session == NULL)
! 130: continue;
! 131: if (c->session == s)
! 132: server_status_client(c);
! 133: }
! 134: }
! 135:
! 136: void
! 137: server_redraw_window(struct window *w)
! 138: {
! 139: struct client *c;
! 140: u_int i;
! 141:
! 142: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 143: c = ARRAY_ITEM(&clients, i);
! 144: if (c == NULL || c->session == NULL)
! 145: continue;
! 146: if (c->session->curw->window == w)
! 147: server_redraw_client(c);
! 148: }
! 149: w->flags |= WINDOW_REDRAW;
! 150: }
! 151:
! 152: void
! 153: server_status_window(struct window *w)
! 154: {
! 155: struct session *s;
! 156: u_int i;
! 157:
! 158: /*
! 159: * This is slightly different. We want to redraw the status line of any
! 160: * clients containing this window rather than any where it is the
! 161: * current window.
! 162: */
! 163:
! 164: for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
! 165: s = ARRAY_ITEM(&sessions, i);
! 166: if (s != NULL && session_has(s, w))
! 167: server_status_session(s);
! 168: }
! 169: }
! 170:
! 171: void
! 172: server_lock(void)
! 173: {
! 174: struct client *c;
! 175: u_int i;
! 176:
! 177: if (server_locked)
! 178: return;
! 179:
! 180: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 181: c = ARRAY_ITEM(&clients, i);
! 182: if (c == NULL || c->session == NULL)
! 183: continue;
! 184:
! 185: status_prompt_clear(c);
! 186: status_prompt_set(
! 187: c, "Password: ", server_lock_callback, c, PROMPT_HIDDEN);
! 188: server_redraw_client(c);
! 189: }
! 190: server_locked = 1;
! 191: }
! 192:
! 193: int
! 194: server_lock_callback(unused void *data, const char *s)
! 195: {
! 196: return (server_unlock(s));
! 197: }
! 198:
! 199: int
! 200: server_unlock(const char *s)
! 201: {
! 202: struct client *c;
! 203: u_int i;
! 204: char *out;
! 205:
! 206: if (!server_locked)
! 207: return (0);
! 208: server_activity = time(NULL);
! 209:
! 210: if (server_password != NULL) {
! 211: if (s == NULL)
! 212: return (-1);
! 213: out = crypt(s, server_password);
! 214: if (strcmp(out, server_password) != 0)
! 215: goto wrong;
! 216: }
! 217:
! 218: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 219: c = ARRAY_ITEM(&clients, i);
! 220: if (c == NULL)
! 221: continue;
! 222:
! 223: status_prompt_clear(c);
! 224: server_redraw_client(c);
! 225: }
! 226:
! 227: server_locked = 0;
! 228: return (0);
! 229:
! 230: wrong:
! 231: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
! 232: c = ARRAY_ITEM(&clients, i);
! 233: if (c == NULL)
! 234: continue;
! 235:
! 236: *c->prompt_buffer = '\0';
! 237: c->prompt_index = 0;
! 238: server_status_client(c);
! 239: }
! 240:
! 241: return (-1);
! 242: }