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

Annotation of src/usr.bin/tmux/client.c, Revision 1.43

1.43    ! nicm        1: /* $OpenBSD: client.c,v 1.42 2010/06/28 22:10:42 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/socket.h>
                     21: #include <sys/stat.h>
                     22: #include <sys/un.h>
                     23: #include <sys/wait.h>
                     24:
                     25: #include <errno.h>
1.30      nicm       26: #include <event.h>
1.1       nicm       27: #include <fcntl.h>
                     28: #include <pwd.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <syslog.h>
                     32: #include <unistd.h>
                     33:
                     34: #include "tmux.h"
                     35:
1.25      nicm       36: struct imsgbuf client_ibuf;
1.30      nicm       37: struct event   client_event;
1.25      nicm       38: const char     *client_exitmsg;
1.32      nicm       39: int            client_exitval;
1.1       nicm       40:
1.30      nicm       41: void           client_send_identify(int);
                     42: void           client_send_environ(void);
                     43: void           client_write_server(enum msgtype, void *, size_t);
1.31      nicm       44: void           client_update_event(void);
1.30      nicm       45: void           client_signal(int, short, void *);
                     46: void           client_callback(int, short, void *);
                     47: int            client_dispatch(void);
1.25      nicm       48:
                     49: struct imsgbuf *
                     50: client_init(char *path, int cmdflags, int flags)
1.1       nicm       51: {
1.26      nicm       52:        struct sockaddr_un      sa;
                     53:        size_t                  size;
                     54:        int                     fd, mode;
                     55:        char                    rpathbuf[MAXPATHLEN];
1.2       nicm       56:
                     57:        if (realpath(path, rpathbuf) == NULL)
                     58:                strlcpy(rpathbuf, path, sizeof rpathbuf);
                     59:        setproctitle("client (%s)", rpathbuf);
1.1       nicm       60:
                     61:        memset(&sa, 0, sizeof sa);
                     62:        sa.sun_family = AF_UNIX;
                     63:        size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
                     64:        if (size >= sizeof sa.sun_path) {
                     65:                errno = ENAMETOOLONG;
                     66:                goto not_found;
                     67:        }
                     68:
1.10      nicm       69:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18      nicm       70:                fatal("socket failed");
1.1       nicm       71:
1.10      nicm       72:        if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.28      nicm       73:                if (!(cmdflags & CMD_STARTSERVER))
                     74:                        goto not_found;
                     75:                switch (errno) {
                     76:                case ECONNREFUSED:
                     77:                        if (unlink(path) != 0)
1.1       nicm       78:                                goto not_found;
1.28      nicm       79:                        /* FALLTHROUGH */
                     80:                case ENOENT:
1.10      nicm       81:                        if ((fd = server_start(path)) == -1)
1.1       nicm       82:                                goto start_failed;
                     83:                        goto server_started;
                     84:                }
                     85:                goto not_found;
                     86:        }
                     87:
                     88: server_started:
1.10      nicm       89:        if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1       nicm       90:                fatal("fcntl failed");
1.10      nicm       91:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.22      nicm       92:                fatal("fcntl failed");
                     93:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
1.1       nicm       94:                fatal("fcntl failed");
1.25      nicm       95:        imsg_init(&client_ibuf, fd);
1.1       nicm       96:
1.11      nicm       97:        if (cmdflags & CMD_SENDENVIRON)
1.25      nicm       98:                client_send_environ();
1.42      nicm       99:        client_send_identify(flags);
1.1       nicm      100:
1.25      nicm      101:        return (&client_ibuf);
1.1       nicm      102:
                    103: start_failed:
                    104:        log_warnx("server failed to start");
1.25      nicm      105:        return (NULL);
1.1       nicm      106:
                    107: not_found:
                    108:        log_warn("server not found");
1.25      nicm      109:        return (NULL);
1.1       nicm      110: }
                    111:
1.11      nicm      112: void
1.26      nicm      113: client_send_identify(int flags)
                    114: {
                    115:        struct msg_identify_data        data;
                    116:        char                           *term;
                    117:        int                             fd;
                    118:
                    119:        data.flags = flags;
                    120:
                    121:        if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    122:                *data.cwd = '\0';
1.35      nicm      123:
1.26      nicm      124:        term = getenv("TERM");
                    125:        if (term == NULL ||
                    126:            strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
                    127:                *data.term = '\0';
                    128:
                    129:        if ((fd = dup(STDIN_FILENO)) == -1)
                    130:                fatal("dup failed");
                    131:        imsg_compose(&client_ibuf,
                    132:            MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.42      nicm      133:
                    134:        if ((fd = dup(STDOUT_FILENO)) == -1)
                    135:                fatal("dup failed");
                    136:        imsg_compose(&client_ibuf, MSG_STDOUT, PROTOCOL_VERSION, -1, fd, NULL, 0);
                    137:
                    138:        if ((fd = dup(STDERR_FILENO)) == -1)
                    139:                fatal("dup failed");
                    140:        imsg_compose(&client_ibuf, MSG_STDERR, PROTOCOL_VERSION, -1, fd, NULL, 0);
1.26      nicm      141: }
                    142:
                    143: void
1.25      nicm      144: client_send_environ(void)
1.11      nicm      145: {
1.26      nicm      146:        struct msg_environ_data data;
1.11      nicm      147:        char                  **var;
                    148:
1.35      nicm      149:        for (var = environ; *var != NULL; var++) {
1.11      nicm      150:                if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
                    151:                        continue;
1.25      nicm      152:                client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11      nicm      153:        }
                    154: }
                    155:
1.25      nicm      156: void
                    157: client_write_server(enum msgtype type, void *buf, size_t len)
                    158: {
1.35      nicm      159:        imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.25      nicm      160: }
                    161:
1.31      nicm      162: void
                    163: client_update_event(void)
                    164: {
                    165:        short   events;
                    166:
                    167:        event_del(&client_event);
                    168:        events = EV_READ;
                    169:        if (client_ibuf.w.queued > 0)
                    170:                events |= EV_WRITE;
                    171:        event_set(&client_event, client_ibuf.fd, events, client_callback, NULL);
                    172:        event_add(&client_event, NULL);
                    173: }
                    174:
1.25      nicm      175: __dead void
                    176: client_main(void)
1.1       nicm      177: {
1.30      nicm      178:        logfile("client");
                    179:
                    180:        /* Note: event_init() has already been called. */
                    181:
1.35      nicm      182:        /* Set up signals. */
1.38      nicm      183:        set_signals(client_signal);
1.41      nicm      184:
                    185:        /*
                    186:         * Send a resize message immediately in case the terminal size has
                    187:         * changed between the identify message to the server and the MSG_READY
                    188:         * telling us to move into the client code.
                    189:         */
                    190:         client_write_server(MSG_RESIZE, NULL, 0);
1.1       nicm      191:
1.17      nicm      192:        /*
                    193:         * imsg_read in the first client poll loop (before the terminal has
1.30      nicm      194:         * been initialised) may have read messages into the buffer after the
                    195:         * MSG_READY switched to here. Process anything outstanding now to
                    196:         * avoid hanging waiting for messages that have already arrived.
1.17      nicm      197:         */
1.25      nicm      198:        if (client_dispatch() != 0)
1.17      nicm      199:                goto out;
                    200:
1.31      nicm      201:        /* Set the event and dispatch. */
1.32      nicm      202:        client_update_event();
1.30      nicm      203:        event_dispatch();
1.17      nicm      204:
                    205: out:
1.25      nicm      206:        /* Print the exit message, if any, and exit. */
1.32      nicm      207:        if (client_exitmsg != NULL && !login_shell)
                    208:                printf("[%s]\n", client_exitmsg);
                    209:        exit(client_exitval);
1.9       nicm      210: }
                    211:
1.34      nicm      212: /* ARGSUSED */
1.30      nicm      213: void
1.31      nicm      214: client_signal(int sig, unused short events, unused void *data)
1.30      nicm      215: {
                    216:        struct sigaction        sigact;
                    217:
                    218:        switch (sig) {
1.39      jsing     219:        case SIGHUP:
                    220:                client_exitmsg = "lost tty";
                    221:                client_exitval = 1;
                    222:                client_write_server(MSG_EXITING, NULL, 0);
                    223:                break;
1.30      nicm      224:        case SIGTERM:
                    225:                client_exitmsg = "terminated";
1.32      nicm      226:                client_exitval = 1;
1.30      nicm      227:                client_write_server(MSG_EXITING, NULL, 0);
                    228:                break;
                    229:        case SIGWINCH:
                    230:                client_write_server(MSG_RESIZE, NULL, 0);
                    231:                break;
                    232:        case SIGCONT:
                    233:                memset(&sigact, 0, sizeof sigact);
                    234:                sigemptyset(&sigact.sa_mask);
                    235:                sigact.sa_flags = SA_RESTART;
                    236:                sigact.sa_handler = SIG_IGN;
                    237:                if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    238:                        fatal("sigaction failed");
                    239:                client_write_server(MSG_WAKEUP, NULL, 0);
                    240:                break;
                    241:        }
                    242:
1.31      nicm      243:        client_update_event();
1.30      nicm      244: }
                    245:
1.34      nicm      246: /* ARGSUSED */
1.30      nicm      247: void
                    248: client_callback(unused int fd, short events, unused void *data)
                    249: {
1.33      nicm      250:        ssize_t n;
1.30      nicm      251:
                    252:        if (events & EV_READ) {
                    253:                if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
                    254:                        goto lost_server;
                    255:                if (client_dispatch() != 0) {
                    256:                        event_loopexit(NULL);
                    257:                        return;
1.35      nicm      258:                }
1.30      nicm      259:        }
1.35      nicm      260:
1.30      nicm      261:        if (events & EV_WRITE) {
                    262:                if (msgbuf_write(&client_ibuf.w) < 0)
                    263:                        goto lost_server;
                    264:        }
                    265:
1.31      nicm      266:        client_update_event();
1.30      nicm      267:        return;
                    268:
                    269: lost_server:
                    270:        client_exitmsg = "lost server";
1.32      nicm      271:        client_exitval = 1;
1.30      nicm      272:        event_loopexit(NULL);
                    273: }
                    274:
1.9       nicm      275: int
1.25      nicm      276: client_dispatch(void)
1.9       nicm      277: {
1.30      nicm      278:        struct imsg             imsg;
                    279:        struct msg_lock_data    lockdata;
                    280:        struct sigaction        sigact;
                    281:        ssize_t                 n, datalen;
1.9       nicm      282:
                    283:        for (;;) {
1.25      nicm      284:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12      nicm      285:                        fatalx("imsg_get failed");
                    286:                if (n == 0)
1.9       nicm      287:                        return (0);
1.12      nicm      288:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9       nicm      289:
1.30      nicm      290:                log_debug("client got %d", imsg.hdr.type);
1.12      nicm      291:                switch (imsg.hdr.type) {
1.9       nicm      292:                case MSG_DETACH:
1.12      nicm      293:                        if (datalen != 0)
1.9       nicm      294:                                fatalx("bad MSG_DETACH size");
                    295:
1.25      nicm      296:                        client_write_server(MSG_EXITING, NULL, 0);
                    297:                        client_exitmsg = "detached";
1.9       nicm      298:                        break;
                    299:                case MSG_EXIT:
1.43    ! nicm      300:                        if (datalen != 0 &&
        !           301:                            datalen != sizeof (struct msg_exit_data))
1.9       nicm      302:                                fatalx("bad MSG_EXIT size");
1.12      nicm      303:
1.25      nicm      304:                        client_write_server(MSG_EXITING, NULL, 0);
                    305:                        client_exitmsg = "exited";
1.9       nicm      306:                        break;
                    307:                case MSG_EXITED:
1.12      nicm      308:                        if (datalen != 0)
1.9       nicm      309:                                fatalx("bad MSG_EXITED size");
                    310:
1.12      nicm      311:                        imsg_free(&imsg);
1.9       nicm      312:                        return (-1);
                    313:                case MSG_SHUTDOWN:
1.12      nicm      314:                        if (datalen != 0)
1.9       nicm      315:                                fatalx("bad MSG_SHUTDOWN size");
                    316:
1.25      nicm      317:                        client_write_server(MSG_EXITING, NULL, 0);
                    318:                        client_exitmsg = "server exited";
1.32      nicm      319:                        client_exitval = 1;
1.9       nicm      320:                        break;
                    321:                case MSG_SUSPEND:
1.12      nicm      322:                        if (datalen != 0)
1.9       nicm      323:                                fatalx("bad MSG_SUSPEND size");
                    324:
1.30      nicm      325:                        memset(&sigact, 0, sizeof sigact);
                    326:                        sigemptyset(&sigact.sa_mask);
                    327:                        sigact.sa_flags = SA_RESTART;
                    328:                        sigact.sa_handler = SIG_DFL;
                    329:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    330:                                fatal("sigaction failed");
                    331:                        kill(getpid(), SIGTSTP);
1.21      nicm      332:                        break;
                    333:                case MSG_LOCK:
                    334:                        if (datalen != sizeof lockdata)
                    335:                                fatalx("bad MSG_LOCK size");
                    336:                        memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35      nicm      337:
1.21      nicm      338:                        lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
                    339:                        system(lockdata.cmd);
1.25      nicm      340:                        client_write_server(MSG_UNLOCK, NULL, 0);
1.9       nicm      341:                        break;
                    342:                default:
                    343:                        fatalx("unexpected message");
                    344:                }
1.12      nicm      345:
                    346:                imsg_free(&imsg);
1.9       nicm      347:        }
1.1       nicm      348: }