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

1.8     ! nicm        1: /* $OpenBSD: client.c,v 1.7 2009/07/26 12:58:44 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;
1.7       nicm       47:        char                           *name, *term;
1.2       nicm       48:        char                            rpathbuf[MAXPATHLEN];
                     49:
                     50:        if (realpath(path, rpathbuf) == NULL)
                     51:                strlcpy(rpathbuf, path, sizeof rpathbuf);
                     52:        setproctitle("client (%s)", rpathbuf);
1.1       nicm       53:
                     54:        if (lstat(path, &sb) != 0) {
1.4       nicm       55:                if (cmdflags & CMD_STARTSERVER && errno == ENOENT) {
1.1       nicm       56:                        if ((cctx->srv_fd = server_start(path)) == -1)
                     57:                                goto start_failed;
                     58:                        goto server_started;
                     59:                }
                     60:                goto not_found;
                     61:        }
                     62:        if (!S_ISSOCK(sb.st_mode)) {
                     63:                errno = ENOTSOCK;
                     64:                goto not_found;
                     65:        }
                     66:
                     67:        memset(&sa, 0, sizeof sa);
                     68:        sa.sun_family = AF_UNIX;
                     69:        size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
                     70:        if (size >= sizeof sa.sun_path) {
                     71:                errno = ENAMETOOLONG;
                     72:                goto not_found;
                     73:        }
                     74:
                     75:        if ((cctx->srv_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
                     76:                fatal("socket");
                     77:
                     78:        if (connect(
                     79:            cctx->srv_fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
                     80:                if (errno == ECONNREFUSED) {
1.4       nicm       81:                        if (unlink(path) != 0 || !(cmdflags & CMD_STARTSERVER))
1.1       nicm       82:                                goto not_found;
                     83:                        if ((cctx->srv_fd = server_start(path)) == -1)
                     84:                                goto start_failed;
                     85:                        goto server_started;
                     86:                }
                     87:                goto not_found;
                     88:        }
                     89:
                     90: server_started:
                     91:        if ((mode = fcntl(cctx->srv_fd, F_GETFL)) == -1)
                     92:                fatal("fcntl failed");
                     93:        if (fcntl(cctx->srv_fd, F_SETFL, mode|O_NONBLOCK) == -1)
                     94:                fatal("fcntl failed");
                     95:        cctx->srv_in = buffer_create(BUFSIZ);
                     96:        cctx->srv_out = buffer_create(BUFSIZ);
                     97:
                     98:        if (isatty(STDIN_FILENO)) {
                     99:                if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    100:                        fatal("ioctl(TIOCGWINSZ)");
                    101:                data.version = PROTOCOL_VERSION;
                    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.7       nicm      122:                client_write_server(cctx, MSG_IDENTIFY, &data, sizeof data);
1.1       nicm      123:        }
                    124:
                    125:        return (0);
                    126:
                    127: start_failed:
                    128:        log_warnx("server failed to start");
                    129:        return (1);
                    130:
                    131: not_found:
                    132:        log_warn("server not found");
                    133:        return (1);
                    134: }
                    135:
                    136: int
                    137: client_main(struct client_ctx *cctx)
                    138: {
                    139:        struct pollfd    pfd;
                    140:        int              xtimeout; /* Yay for ncurses namespace! */
                    141:
                    142:        siginit();
                    143:
                    144:        logfile("client");
                    145:
1.8     ! nicm      146:        for (;;) {
        !           147:                if (sigterm)
        !           148:                        client_write_server(cctx, MSG_EXITING, NULL, 0);
1.1       nicm      149:                if (sigchld) {
                    150:                        waitpid(WAIT_ANY, NULL, WNOHANG);
                    151:                        sigchld = 0;
                    152:                }
                    153:                if (sigwinch)
                    154:                        client_handle_winch(cctx);
                    155:                if (sigcont) {
                    156:                        siginit();
                    157:                        client_write_server(cctx, MSG_WAKEUP, NULL, 0);
                    158:                        sigcont = 0;
                    159:                }
                    160:
1.5       nicm      161:                switch (client_msg_dispatch(cctx)) {
1.1       nicm      162:                case -1:
                    163:                        goto out;
                    164:                case 0:
                    165:                        /* May be more in buffer, don't let poll block. */
                    166:                        xtimeout = 0;
                    167:                        break;
                    168:                default:
                    169:                        /* Out of data, poll may block. */
                    170:                        xtimeout = INFTIM;
                    171:                        break;
                    172:                }
                    173:
                    174:                pfd.fd = cctx->srv_fd;
                    175:                pfd.events = POLLIN;
                    176:                if (BUFFER_USED(cctx->srv_out) > 0)
                    177:                        pfd.events |= POLLOUT;
                    178:
                    179:                if (poll(&pfd, 1, xtimeout) == -1) {
                    180:                        if (errno == EAGAIN || errno == EINTR)
                    181:                                continue;
                    182:                        fatal("poll failed");
                    183:                }
                    184:
1.5       nicm      185:                if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
                    186:                        cctx->exittype = CCTX_DIED;
                    187:                        break;
                    188:                }
1.1       nicm      189:        }
                    190:
                    191: out:
                    192:        if (sigterm) {
                    193:                printf("[terminated]\n");
                    194:                return (1);
                    195:        }
1.5       nicm      196:        switch (cctx->exittype) {
                    197:        case CCTX_DIED:
                    198:                printf("[lost server]\n");
                    199:                return (0);
                    200:        case CCTX_SHUTDOWN:
1.1       nicm      201:                printf("[server exited]\n");
                    202:                return (0);
1.5       nicm      203:        case CCTX_EXIT:
1.1       nicm      204:                printf("[exited]\n");
                    205:                return (0);
1.5       nicm      206:        case CCTX_DETACH:
1.1       nicm      207:                printf("[detached]\n");
                    208:                return (0);
1.5       nicm      209:        default:
                    210:                printf("[error: %s]\n", cctx->errstr);
                    211:                return (1);
1.1       nicm      212:        }
                    213: }
                    214:
                    215: void
                    216: client_handle_winch(struct client_ctx *cctx)
                    217: {
                    218:        struct msg_resize_data  data;
                    219:        struct winsize          ws;
                    220:
                    221:        if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1)
                    222:                fatal("ioctl failed");
                    223:
                    224:        data.sx = ws.ws_col;
                    225:        data.sy = ws.ws_row;
                    226:        client_write_server(cctx, MSG_RESIZE, &data, sizeof data);
                    227:
                    228:        sigwinch = 0;
                    229: }