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

1.5     ! nicm        1: /* $OpenBSD: client.c,v 1.4 2009/07/22 21:58:56 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:
                     36: void   client_handle_winch(struct client_ctx *);
                     37:
                     38: int
1.4       nicm       39: client_init(char *path, struct client_ctx *cctx, int cmdflags, int flags)
1.1       nicm       40: {
                     41:        struct sockaddr_un              sa;
                     42:        struct stat                     sb;
                     43:        struct msg_identify_data        data;
                     44:        struct winsize                  ws;
                     45:        size_t                          size;
                     46:        int                             mode;
                     47:        struct buffer                  *b;
                     48:        char                           *name;
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.1       nicm       57:                        if ((cctx->srv_fd = server_start(path)) == -1)
                     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:
                     76:        if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
                     77:                fatal("socket");
                     78:
                     79:        if (connect(
                     80:            cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
                     81:                if (errno == ECONNREFUSED) {
1.4       nicm       82:                        if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1       nicm       83:                                goto not_found;
                     84:                        if ((cctx->srv_fd = server_start(path)) == -1)
                     85:                                goto start_failed;
                     86:                        goto server_started;
                     87:                }
                     88:                goto not_found;
                     89:        }
                     90:
                     91: server_started:
                     92:        if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1)
                     93:                fatal("fcntl failed");
                     94:        if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
                     95:                fatal("fcntl failed");
                     96:        cctx->srv_in = buffer_create(BUFSIZ);
                     97:        cctx->srv_out = buffer_create(BUFSIZ);
                     98:
                     99:        if (isatty(STDIN_FILENO)) {
                    100:                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    101:                        fatal("ioctl(TIOCGWINSZ)");
                    102:                data.version = PROTOCOL_VERSION;
                    103:                data.flags = flags;
                    104:                data.sx = ws.ws_col;
                    105:                data.sy = ws.ws_row;
                    106:                *data.tty = '\0';
                    107:                if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    108:                        *data.cwd = '\0';
                    109:
                    110:                if ((name = ttyname(STDIN_FILENO)) == NULL)
                    111:                        fatal("ttyname failed");
                    112:                if (strlcpy(data.tty, name, sizeof data.tty) >= sizeof data.tty)
                    113:                        fatalx("ttyname failed");
                    114:
                    115:                b = buffer_create(BUFSIZ);
                    116:                cmd_send_string(b, getenv("TERM"));
                    117:                client_write_server2(cctx, MSG_IDENTIFY,
                    118:                    &data, sizeof data, BUFFER_OUT(b), BUFFER_USED(b));
                    119:                buffer_destroy(b);
                    120:        }
                    121:
                    122:        return (0);
                    123:
                    124: start_failed:
                    125:        log_warnx("server failed to start");
                    126:        return (1);
                    127:
                    128: not_found:
                    129:        log_warn("server not found");
                    130:        return (1);
                    131: }
                    132:
                    133: int
                    134: client_main(struct client_ctx *cctx)
                    135: {
                    136:        struct pollfd    pfd;
                    137:        char            *error;
                    138:        int              xtimeout; /* Yay for ncurses namespace! */
                    139:
                    140:        siginit();
                    141:
                    142:        logfile("client");
                    143:
                    144:        error = NULL;
                    145:        while (!sigterm) {
                    146:                if (sigchld) {
                    147:                        waitpid(WAIT_ANY, NULL, WNOHANG);
                    148:                        sigchld = 0;
                    149:                }
                    150:                if (sigwinch)
                    151:                        client_handle_winch(cctx);
                    152:                if (sigcont) {
                    153:                        siginit();
                    154:                        client_write_server(cctx, MSG_WAKEUP, NULL, 0);
                    155:                        sigcont = 0;
                    156:                }
                    157:
1.5     ! nicm      158:                switch (client_msg_dispatch(cctx)) {
1.1       nicm      159:                case -1:
                    160:                        goto out;
                    161:                case 0:
                    162:                        /* May be more in buffer, don't let poll block. */
                    163:                        xtimeout = 0;
                    164:                        break;
                    165:                default:
                    166:                        /* Out of data, poll may block. */
                    167:                        xtimeout = INFTIM;
                    168:                        break;
                    169:                }
                    170:
                    171:                pfd.fd = cctx->srv_fd;
                    172:                pfd.events = POLLIN;
                    173:                if (BUFFER_USED(cctx->srv_out) > 0)
                    174:                        pfd.events |= POLLOUT;
                    175:
                    176:                if (poll(&pfd, 1, xtimeout) == -1) {
                    177:                        if (errno == EAGAIN || errno == EINTR)
                    178:                                continue;
                    179:                        fatal("poll failed");
                    180:                }
                    181:
1.5     ! nicm      182:                if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
        !           183:                        cctx->exittype = CCTX_DIED;
        !           184:                        break;
        !           185:                }
1.1       nicm      186:        }
                    187:
                    188: out:
                    189:        if (sigterm) {
                    190:                printf("[terminated]\n");
                    191:                return (1);
                    192:        }
1.5     ! nicm      193:        switch (cctx->exittype) {
        !           194:        case CCTX_DIED:
        !           195:                printf("[lost server]\n");
        !           196:                return (0);
        !           197:        case CCTX_SHUTDOWN:
1.1       nicm      198:                printf("[server exited]\n");
                    199:                return (0);
1.5     ! nicm      200:        case CCTX_EXIT:
1.1       nicm      201:                printf("[exited]\n");
                    202:                return (0);
1.5     ! nicm      203:        case CCTX_DETACH:
1.1       nicm      204:                printf("[detached]\n");
                    205:                return (0);
1.5     ! nicm      206:        default:
        !           207:                printf("[error: %s]\n", cctx->errstr);
        !           208:                return (1);
1.1       nicm      209:        }
                    210: }
                    211:
                    212: void
                    213: client_handle_winch(struct client_ctx *cctx)
                    214: {
                    215:        struct msg_resize_data  data;
                    216:        struct winsize          ws;
                    217:
                    218:        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    219:                fatal("ioctl failed");
                    220:
                    221:        data.sx = ws.ws_col;
                    222:        data.sy = ws.ws_row;
                    223:        client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
                    224:
                    225:        sigwinch = 0;
                    226: }