[BACK]Return to server-fn.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: server-fn.c,v 1.2 2009/06/25 06:15:04 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: #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_redraw_client(struct client *c)
                     80: {
                     81:        c->flags |= CLIENT_REDRAW;
                     82: }
                     83:
                     84: void
                     85: server_status_client(struct client *c)
                     86: {
                     87:        c->flags |= CLIENT_STATUS;
                     88: }
                     89:
                     90: void
                     91: server_redraw_session(struct session *s)
                     92: {
                     93:        struct client   *c;
                     94:        u_int            i;
                     95:
                     96:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     97:                c = ARRAY_ITEM(&clients, i);
                     98:                if (c == NULL || c->session == NULL)
                     99:                        continue;
                    100:                if (c->session == s)
                    101:                        server_redraw_client(c);
                    102:        }
                    103: }
                    104:
                    105: void
                    106: server_status_session(struct session *s)
                    107: {
                    108:        struct client   *c;
                    109:        u_int            i;
                    110:
                    111:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    112:                c = ARRAY_ITEM(&clients, i);
                    113:                if (c == NULL || c->session == NULL)
                    114:                        continue;
                    115:                if (c->session == s)
                    116:                        server_status_client(c);
                    117:        }
                    118: }
                    119:
                    120: void
                    121: server_redraw_window(struct window *w)
                    122: {
                    123:        struct client   *c;
                    124:        u_int            i;
                    125:
                    126:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    127:                c = ARRAY_ITEM(&clients, i);
                    128:                if (c == NULL || c->session == NULL)
                    129:                        continue;
                    130:                if (c->session->curw->window == w)
                    131:                        server_redraw_client(c);
                    132:        }
                    133:        w->flags |= WINDOW_REDRAW;
                    134: }
                    135:
                    136: void
                    137: server_status_window(struct window *w)
                    138: {
                    139:        struct session  *s;
                    140:        u_int            i;
                    141:
                    142:        /*
                    143:         * This is slightly different. We want to redraw the status line of any
                    144:         * clients containing this window rather than any where it is the
                    145:         * current window.
                    146:         */
                    147:
                    148:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    149:                s = ARRAY_ITEM(&sessions, i);
                    150:                if (s != NULL && session_has(s, w))
                    151:                        server_status_session(s);
                    152:        }
                    153: }
                    154:
                    155: void
                    156: server_lock(void)
                    157: {
                    158:        struct client   *c;
                    159:        u_int            i;
                    160:
                    161:        if (server_locked)
                    162:                return;
                    163:
                    164:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    165:                c = ARRAY_ITEM(&clients, i);
                    166:                if (c == NULL || c->session == NULL)
                    167:                        continue;
                    168:
                    169:                status_prompt_clear(c);
                    170:                status_prompt_set(
                    171:                    c, "Password: ", server_lock_callback, c, PROMPT_HIDDEN);
                    172:                server_redraw_client(c);
                    173:        }
                    174:        server_locked = 1;
                    175: }
                    176:
                    177: int
                    178: server_lock_callback(unused void *data, const char *s)
                    179: {
                    180:        return (server_unlock(s));
                    181: }
                    182:
                    183: int
                    184: server_unlock(const char *s)
                    185: {
                    186:        struct client   *c;
                    187:        u_int            i;
                    188:        char            *out;
                    189:
                    190:        if (!server_locked)
                    191:                return (0);
                    192:        server_activity = time(NULL);
                    193:
                    194:        if (server_password != NULL) {
                    195:                if (s == NULL)
                    196:                        return (-1);
                    197:                out = crypt(s, server_password);
                    198:                if (strcmp(out, server_password) != 0)
                    199:                        goto wrong;
                    200:        }
                    201:
                    202:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    203:                c = ARRAY_ITEM(&clients, i);
                    204:                if (c == NULL)
                    205:                        continue;
                    206:
                    207:                status_prompt_clear(c);
                    208:                server_redraw_client(c);
                    209:        }
                    210:
                    211:        server_locked = 0;
                    212:        return (0);
                    213:
                    214: wrong:
                    215:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    216:                c = ARRAY_ITEM(&clients, i);
1.3     ! nicm      217:                 if (c == NULL || c->prompt_buffer == NULL)
1.1       nicm      218:                        continue;
                    219:
                    220:                *c->prompt_buffer = '\0';
                    221:                c->prompt_index = 0;
                    222:                server_status_client(c);
                    223:        }
                    224:
                    225:        return (-1);
                    226: }