[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.11

1.11    ! nicm        1: /* $OpenBSD: client.c,v 1.10 2009/08/08 21:18:23 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/ioctl.h>
                     21: #include <sys/socket.h>
                     22: #include <sys/stat.h>
                     23: #include <sys/un.h>
                     24: #include <sys/wait.h>
                     25:
                     26: #include <errno.h>
                     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.11    ! nicm       36: void   client_send_environ(struct client_ctx *);
1.1       nicm       37: void   client_handle_winch(struct client_ctx *);
                     38:
                     39: int
1.4       nicm       40: client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
1.1       nicm       41: {
                     42:        struct sockaddr_un              sa;
                     43:        struct stat                     sb;
                     44:        struct msg_identify_data        data;
                     45:        struct winsize                  ws;
                     46:        size_t                          size;
1.10      nicm       47:        int                             fd, mode;
1.7       nicm       48:        char                           *name, *term;
1.2       nicm       49:        char                            rpathbuf[MAXPATHLEN];
                     50:
                     51:        if (realpath(path, rpathbuf) == NULL)
                     52:                strlcpy(rpathbuf, path, sizeof rpathbuf);
                     53:        setproctitle("client (%s)", rpathbuf);
1.1       nicm       54:
                     55:        if (lstat(path, &sb) != 0) {
1.4       nicm       56:                if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
1.10      nicm       57:                        if ((fd = server_start(path)) == -1)
1.1       nicm       58:                                goto start_failed;
                     59:                        goto server_started;
                     60:                }
                     61:                goto not_found;
                     62:        }
                     63:        if (!S_ISSOCK(sb.st_mode)) {
                     64:                errno = ENOTSOCK;
                     65:                goto not_found;
                     66:        }
                     67:
                     68:        memset(&sa, 0, sizeof sa);
                     69:        sa.sun_family = AF_UNIX;
                     70:        size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
                     71:        if (size >= sizeof sa.sun_path) {
                     72:                errno = ENAMETOOLONG;
                     73:                goto not_found;
                     74:        }
                     75:
1.10      nicm       76:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.1       nicm       77:                fatal("socket");
                     78:
1.10      nicm       79:        if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.1       nicm       80:                if (errno == ECONNREFUSED) {
1.4       nicm       81:                        if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1       nicm       82:                                goto not_found;
1.10      nicm       83:                        if ((fd = server_start(path)) == -1)
1.1       nicm       84:                                goto start_failed;
                     85:                        goto server_started;
                     86:                }
                     87:                goto not_found;
                     88:        }
                     89:
                     90: server_started:
1.10      nicm       91:        if ((mode = fcntl(fd, F_GETFL)) == -1)
1.1       nicm       92:                fatal("fcntl failed");
1.10      nicm       93:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
1.1       nicm       94:                fatal("fcntl failed");
1.10      nicm       95:        cctx->srv_fd = fd;
1.1       nicm       96:        cctx->srv_in = buffer_create(BUFSIZ);
                     97:        cctx->srv_out = buffer_create(BUFSIZ);
                     98:
1.11    ! nicm       99:        if (cmdflags & CMD_SENDENVIRON)
        !           100:                client_send_environ(cctx);
1.1       nicm      101:        if (isatty(STDIN_FILENO)) {
                    102:                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    103:                        fatal("ioctl(TIOCGWINSZ)");
                    104:                data.version = PROTOCOL_VERSION;
                    105:                data.flags = flags;
                    106:                data.sx = ws.ws_col;
                    107:                data.sy = ws.ws_row;
1.7       nicm      108:
1.1       nicm      109:                if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    110:                        *data.cwd = '\0';
                    111:
1.7       nicm      112:                *data.term = '\0';
                    113:                if ((term = getenv("TERM")) != NULL) {
                    114:                        if (strlcpy(data.term,
                    115:                            term, sizeof data.term) >= sizeof data.term)
                    116:                                *data.term = '\0';
                    117:                }
                    118:
                    119:                *data.tty = '\0';
1.1       nicm      120:                if ((name = ttyname(STDIN_FILENO)) == NULL)
                    121:                        fatal("ttyname failed");
                    122:                if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
                    123:                        fatalx("ttyname failed");
                    124:
1.7       nicm      125:                client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
1.1       nicm      126:        }
                    127:
                    128:        return (0);
                    129:
                    130: start_failed:
                    131:        log_warnx("server failed to start");
                    132:        return (1);
                    133:
                    134: not_found:
                    135:        log_warn("server not found");
                    136:        return (1);
                    137: }
                    138:
1.11    ! nicm      139: void
        !           140: client_send_environ(struct client_ctx *cctx)
        !           141: {
        !           142:        char                  **var;
        !           143:        struct msg_environ_data data;
        !           144:
        !           145:        for (var = environ; *var != NULL; var++) {
        !           146:                if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
        !           147:                        continue;
        !           148:                client_write_server(cctx, MSG_ENVIRON, &data, sizeof data);
        !           149:        }
        !           150: }
        !           151:
1.1       nicm      152: int
                    153: client_main(struct client_ctx *cctx)
                    154: {
                    155:        struct pollfd    pfd;
                    156:
                    157:        siginit();
                    158:
                    159:        logfile("client");
                    160:
1.8       nicm      161:        for (;;) {
                    162:                if (sigterm)
                    163:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
1.1       nicm      164:                if (sigchld) {
                    165:                        waitpid(WAIT_ANY, NULL, WNOHANG);
                    166:                        sigchld = 0;
                    167:                }
                    168:                if (sigwinch)
                    169:                        client_handle_winch(cctx);
                    170:                if (sigcont) {
                    171:                        siginit();
                    172:                        client_write_server(cctx, MSG_WAKEUP, NULL, 0);
                    173:                        sigcont = 0;
                    174:                }
                    175:
                    176:                pfd.fd = cctx->srv_fd;
                    177:                pfd.events = POLLIN;
                    178:                if (BUFFER_USED(cctx->srv_out) > 0)
                    179:                        pfd.events |= POLLOUT;
                    180:
1.9       nicm      181:                if (poll(&pfd, 1, INFTIM) == -1) {
1.1       nicm      182:                        if (errno == EAGAIN || errno == EINTR)
                    183:                                continue;
                    184:                        fatal("poll failed");
                    185:                }
                    186:
1.5       nicm      187:                if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
                    188:                        cctx->exittype = CCTX_DIED;
                    189:                        break;
                    190:                }
1.9       nicm      191:
                    192:                if (client_msg_dispatch(cctx) != 0)
                    193:                        break;
1.1       nicm      194:        }
                    195:
                    196:        if (sigterm) {
                    197:                printf("[terminated]\n");
                    198:                return (1);
                    199:        }
1.5       nicm      200:        switch (cctx->exittype) {
                    201:        case CCTX_DIED:
                    202:                printf("[lost server]\n");
                    203:                return (0);
                    204:        case CCTX_SHUTDOWN:
1.1       nicm      205:                printf("[server exited]\n");
                    206:                return (0);
1.5       nicm      207:        case CCTX_EXIT:
1.1       nicm      208:                printf("[exited]\n");
                    209:                return (0);
1.5       nicm      210:        case CCTX_DETACH:
1.1       nicm      211:                printf("[detached]\n");
                    212:                return (0);
1.5       nicm      213:        default:
                    214:                printf("[error: %s]\n", cctx->errstr);
                    215:                return (1);
1.1       nicm      216:        }
                    217: }
                    218:
                    219: void
                    220: client_handle_winch(struct client_ctx *cctx)
                    221: {
                    222:        struct msg_resize_data  data;
                    223:        struct winsize          ws;
                    224:
                    225:        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    226:                fatal("ioctl failed");
                    227:
                    228:        data.sx = ws.ws_col;
                    229:        data.sy = ws.ws_row;
                    230:        client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
                    231:
                    232:        sigwinch = 0;
1.9       nicm      233: }
                    234:
                    235: int
                    236: client_msg_dispatch(struct client_ctx *cctx)
                    237: {
                    238:        struct hdr               hdr;
                    239:        struct msg_print_data    printdata;
                    240:
                    241:        for (;;) {
                    242:                if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
                    243:                        return (0);
                    244:                memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
                    245:                if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
                    246:                        return (0);
                    247:                buffer_remove(cctx->srv_in, sizeof hdr);
                    248:
                    249:                switch (hdr.type) {
                    250:                case MSG_DETACH:
                    251:                        if (hdr.size != 0)
                    252:                                fatalx("bad MSG_DETACH size");
                    253:
                    254:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    255:                        cctx->exittype = CCTX_DETACH;
                    256:                        break;
                    257:                case MSG_ERROR:
                    258:                        if (hdr.size != sizeof printdata)
                    259:                                fatalx("bad MSG_PRINT size");
                    260:                        buffer_read(cctx->srv_in, &printdata, sizeof printdata);
1.11    ! nicm      261:
1.9       nicm      262:                        printdata.msg[(sizeof printdata.msg) - 1] = '\0';
                    263:                        cctx->errstr = xstrdup(printdata.msg);
                    264:                        return (-1);
                    265:                case MSG_EXIT:
                    266:                        if (hdr.size != 0)
                    267:                                fatalx("bad MSG_EXIT size");
                    268:
                    269:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    270:                        cctx->exittype = CCTX_EXIT;
                    271:                        break;
                    272:                case MSG_EXITED:
                    273:                        if (hdr.size != 0)
                    274:                                fatalx("bad MSG_EXITED size");
                    275:
                    276:                        return (-1);
                    277:                case MSG_SHUTDOWN:
                    278:                        if (hdr.size != 0)
                    279:                                fatalx("bad MSG_SHUTDOWN size");
                    280:
                    281:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    282:                        cctx->exittype = CCTX_SHUTDOWN;
                    283:                        break;
                    284:                case MSG_SUSPEND:
                    285:                        if (hdr.size != 0)
                    286:                                fatalx("bad MSG_SUSPEND size");
                    287:
                    288:                        client_suspend();
                    289:                        break;
                    290:                default:
                    291:                        fatalx("unexpected message");
                    292:                }
                    293:        }
1.1       nicm      294: }