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

1.13    ! nicm        1: /* $OpenBSD: client.c,v 1.12 2009/08/11 17:18:35 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.12      nicm       95:        imsg_init(&cctx->ibuf, fd);
1.1       nicm       96:
1.11      nicm       97:        if (cmdflags & CMD_SENDENVIRON)
                     98:                client_send_environ(cctx);
1.1       nicm       99:        if (isatty(STDIN_FILENO)) {
                    100:                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    101:                        fatal("ioctl(TIOCGWINSZ)");
                    102:                data.flags = flags;
                    103:                data.sx = ws.ws_col;
                    104:                data.sy = ws.ws_row;
1.7       nicm      105:
1.1       nicm      106:                if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    107:                        *data.cwd = '\0';
                    108:
1.7       nicm      109:                *data.term = '\0';
                    110:                if ((term = getenv("TERM")) != NULL) {
                    111:                        if (strlcpy(data.term,
                    112:                            term, sizeof data.term) >= sizeof data.term)
                    113:                                *data.term = '\0';
                    114:                }
                    115:
                    116:                *data.tty = '\0';
1.1       nicm      117:                if ((name = ttyname(STDIN_FILENO)) == NULL)
                    118:                        fatal("ttyname failed");
                    119:                if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
                    120:                        fatalx("ttyname failed");
                    121:
1.13    ! nicm      122:                imsg_compose(&cctx->ibuf, MSG_IDENTIFY,
        !           123:                    PROTOCOL_VERSION, -1, STDIN_FILENO, &data, sizeof data);
1.1       nicm      124:        }
                    125:
                    126:        return (0);
                    127:
                    128: start_failed:
                    129:        log_warnx("server failed to start");
                    130:        return (1);
                    131:
                    132: not_found:
                    133:        log_warn("server not found");
                    134:        return (1);
                    135: }
                    136:
1.11      nicm      137: void
                    138: client_send_environ(struct client_ctx *cctx)
                    139: {
                    140:        char                  **var;
                    141:        struct msg_environ_data data;
                    142:
                    143:        for (var = environ; *var != NULL; var++) {
                    144:                if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
                    145:                        continue;
                    146:                client_write_server(cctx, MSG_ENVIRON, &data, sizeof data);
                    147:        }
                    148: }
                    149:
1.1       nicm      150: int
                    151: client_main(struct client_ctx *cctx)
                    152: {
                    153:        struct pollfd    pfd;
1.12      nicm      154:        int              nfds;
1.1       nicm      155:
                    156:        siginit();
                    157:
                    158:        logfile("client");
                    159:
1.8       nicm      160:        for (;;) {
                    161:                if (sigterm)
                    162:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
1.1       nicm      163:                if (sigchld) {
                    164:                        waitpid(WAIT_ANY, NULL, WNOHANG);
                    165:                        sigchld = 0;
                    166:                }
                    167:                if (sigwinch)
                    168:                        client_handle_winch(cctx);
                    169:                if (sigcont) {
                    170:                        siginit();
                    171:                        client_write_server(cctx, MSG_WAKEUP, NULL, 0);
                    172:                        sigcont = 0;
                    173:                }
                    174:
1.12      nicm      175:                pfd.fd = cctx->ibuf.fd;
1.1       nicm      176:                pfd.events = POLLIN;
1.12      nicm      177:                if (cctx->ibuf.w.queued > 0)
1.1       nicm      178:                        pfd.events |= POLLOUT;
                    179:
1.12      nicm      180:                if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
1.1       nicm      181:                        if (errno == EAGAIN || errno == EINTR)
                    182:                                continue;
                    183:                        fatal("poll failed");
                    184:                }
1.12      nicm      185:                if (nfds == 0)
                    186:                        continue;
                    187:
                    188:                if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))
                    189:                        fatalx("socket error");
1.1       nicm      190:
1.12      nicm      191:                if (pfd.revents & POLLIN) {
                    192:                        if (client_msg_dispatch(cctx) != 0)
                    193:                                break;
1.5       nicm      194:                }
1.9       nicm      195:
1.12      nicm      196:                if (pfd.revents & POLLOUT) {
                    197:                        if (msgbuf_write(&cctx->ibuf.w) < 0) {
                    198:                                cctx->exittype = CCTX_DIED;
                    199:                                break;
                    200:                        }
                    201:                }
1.1       nicm      202:        }
                    203:
                    204:        if (sigterm) {
                    205:                printf("[terminated]\n");
                    206:                return (1);
                    207:        }
1.5       nicm      208:        switch (cctx->exittype) {
                    209:        case CCTX_DIED:
                    210:                printf("[lost server]\n");
                    211:                return (0);
                    212:        case CCTX_SHUTDOWN:
1.1       nicm      213:                printf("[server exited]\n");
                    214:                return (0);
1.5       nicm      215:        case CCTX_EXIT:
1.1       nicm      216:                printf("[exited]\n");
                    217:                return (0);
1.5       nicm      218:        case CCTX_DETACH:
1.1       nicm      219:                printf("[detached]\n");
                    220:                return (0);
1.5       nicm      221:        default:
                    222:                printf("[error: %s]\n", cctx->errstr);
                    223:                return (1);
1.1       nicm      224:        }
                    225: }
                    226:
                    227: void
                    228: client_handle_winch(struct client_ctx *cctx)
                    229: {
                    230:        struct msg_resize_data  data;
                    231:        struct winsize          ws;
                    232:
                    233:        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    234:                fatal("ioctl failed");
                    235:
                    236:        data.sx = ws.ws_col;
                    237:        data.sy = ws.ws_row;
                    238:        client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
                    239:
                    240:        sigwinch = 0;
1.9       nicm      241: }
                    242:
                    243: int
                    244: client_msg_dispatch(struct client_ctx *cctx)
                    245: {
1.12      nicm      246:        struct imsg              imsg;
1.9       nicm      247:        struct msg_print_data    printdata;
1.12      nicm      248:        ssize_t                  n, datalen;
                    249:
                    250:        if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0) {
                    251:                cctx->exittype = CCTX_DIED;
                    252:                return (-1);
                    253:        }
1.9       nicm      254:
                    255:        for (;;) {
1.12      nicm      256:                if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1)
                    257:                        fatalx("imsg_get failed");
                    258:                if (n == 0)
1.9       nicm      259:                        return (0);
1.12      nicm      260:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9       nicm      261:
1.12      nicm      262:                switch (imsg.hdr.type) {
1.9       nicm      263:                case MSG_DETACH:
1.12      nicm      264:                        if (datalen != 0)
1.9       nicm      265:                                fatalx("bad MSG_DETACH size");
                    266:
                    267:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    268:                        cctx->exittype = CCTX_DETACH;
                    269:                        break;
                    270:                case MSG_ERROR:
1.12      nicm      271:                        if (datalen != sizeof printdata)
                    272:                                fatalx("bad MSG_ERROR size");
                    273:                        memcpy(&printdata, imsg.data, sizeof printdata);
1.11      nicm      274:
1.9       nicm      275:                        printdata.msg[(sizeof printdata.msg) - 1] = '\0';
                    276:                        cctx->errstr = xstrdup(printdata.msg);
1.12      nicm      277:                        imsg_free(&imsg);
1.9       nicm      278:                        return (-1);
                    279:                case MSG_EXIT:
1.12      nicm      280:                        if (datalen != 0)
1.9       nicm      281:                                fatalx("bad MSG_EXIT size");
1.12      nicm      282:
1.9       nicm      283:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    284:                        cctx->exittype = CCTX_EXIT;
                    285:                        break;
                    286:                case MSG_EXITED:
1.12      nicm      287:                        if (datalen != 0)
1.9       nicm      288:                                fatalx("bad MSG_EXITED size");
                    289:
1.12      nicm      290:                        imsg_free(&imsg);
1.9       nicm      291:                        return (-1);
                    292:                case MSG_SHUTDOWN:
1.12      nicm      293:                        if (datalen != 0)
1.9       nicm      294:                                fatalx("bad MSG_SHUTDOWN size");
                    295:
                    296:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
                    297:                        cctx->exittype = CCTX_SHUTDOWN;
                    298:                        break;
                    299:                case MSG_SUSPEND:
1.12      nicm      300:                        if (datalen != 0)
1.9       nicm      301:                                fatalx("bad MSG_SUSPEND size");
                    302:
                    303:                        client_suspend();
                    304:                        break;
                    305:                default:
                    306:                        fatalx("unexpected message");
                    307:                }
1.12      nicm      308:
                    309:                imsg_free(&imsg);
1.9       nicm      310:        }
1.1       nicm      311: }