[BACK]Return to cmd-pipe-pane.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-pipe-pane.c, Revision 1.56

1.56    ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.55 2020/05/21 07:24:13 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.36      nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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>
1.4       nicm       20: #include <sys/socket.h>
1.1       nicm       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
                     24: #include <paths.h>
1.45      nicm       25: #include <signal.h>
1.29      nicm       26: #include <stdlib.h>
1.1       nicm       27: #include <string.h>
1.13      nicm       28: #include <time.h>
1.1       nicm       29: #include <unistd.h>
                     30:
                     31: #include "tmux.h"
                     32:
                     33: /*
                     34:  * Open pipe to redirect pane output. If already open, close first.
                     35:  */
                     36:
1.39      nicm       37: static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       38:
1.47      nicm       39: static void cmd_pipe_pane_read_callback(struct bufferevent *, void *);
1.43      nicm       40: static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
1.37      nicm       41: static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
1.4       nicm       42:
1.1       nicm       43: const struct cmd_entry cmd_pipe_pane_entry = {
1.34      nicm       44:        .name = "pipe-pane",
                     45:        .alias = "pipep",
                     46:
1.47      nicm       47:        .args = { "IOot:", 0, 1 },
1.56    ! nicm       48:        .usage = "[-IOo] " CMD_TARGET_PANE_USAGE " [shell-command]",
1.34      nicm       49:
1.41      nicm       50:        .target = { 't', CMD_FIND_PANE, 0 },
1.35      nicm       51:
1.38      nicm       52:        .flags = CMD_AFTERHOOK,
1.34      nicm       53:        .exec = cmd_pipe_pane_exec
1.1       nicm       54: };
                     55:
1.37      nicm       56: static enum cmd_retval
1.39      nicm       57: cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       58: {
1.55      nicm       59:        struct args                     *args = cmd_get_args(self);
                     60:        struct cmd_find_state           *target = cmdq_get_target(item);
                     61:        struct client                   *tc = cmdq_get_target_client(item);
                     62:        struct window_pane              *wp = target->wp;
                     63:        struct session                  *s = target->s;
                     64:        struct winlink                  *wl = target->wl;
                     65:        struct window_pane_offset       *wpo = &wp->pipe_offset;
                     66:        char                            *cmd;
                     67:        int                              old_fd, pipe_fd[2], null_fd, in, out;
                     68:        struct format_tree              *ft;
                     69:        sigset_t                         set, oldset;
1.1       nicm       70:
                     71:        /* Destroy the old pipe. */
                     72:        old_fd = wp->pipe_fd;
                     73:        if (wp->pipe_fd != -1) {
1.4       nicm       74:                bufferevent_free(wp->pipe_event);
1.1       nicm       75:                close(wp->pipe_fd);
                     76:                wp->pipe_fd = -1;
1.43      nicm       77:
                     78:                if (window_pane_destroy_ready(wp)) {
                     79:                        server_destroy_pane(wp, 1);
                     80:                        return (CMD_RETURN_NORMAL);
                     81:                }
1.1       nicm       82:        }
                     83:
                     84:        /* If no pipe command, that is enough. */
1.17      nicm       85:        if (args->argc == 0 || *args->argv[0] == '\0')
1.22      nicm       86:                return (CMD_RETURN_NORMAL);
1.1       nicm       87:
                     88:        /*
                     89:         * With -o, only open the new pipe if there was no previous one. This
                     90:         * allows a pipe to be toggled with a single key, for example:
                     91:         *
                     92:         *      bind ^p pipep -o 'cat >>~/output'
                     93:         */
1.52      nicm       94:        if (args_has(args, 'o') && old_fd != -1)
1.22      nicm       95:                return (CMD_RETURN_NORMAL);
1.1       nicm       96:
1.47      nicm       97:        /* What do we want to do? Neither -I or -O is -O. */
1.52      nicm       98:        if (args_has(args, 'I')) {
1.47      nicm       99:                in = 1;
1.52      nicm      100:                out = args_has(args, 'O');
1.47      nicm      101:        } else {
                    102:                in = 0;
                    103:                out = 1;
                    104:        }
                    105:
1.1       nicm      106:        /* Open the new pipe. */
1.4       nicm      107:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
1.39      nicm      108:                cmdq_error(item, "socketpair error: %s", strerror(errno));
1.22      nicm      109:                return (CMD_RETURN_ERROR);
1.1       nicm      110:        }
                    111:
1.29      nicm      112:        /* Expand the command. */
1.53      nicm      113:        ft = format_create(cmdq_get_client(item), item, FORMAT_NONE, 0);
1.54      nicm      114:        format_defaults(ft, tc, s, wl, wp);
1.51      nicm      115:        cmd = format_expand_time(ft, args->argv[0]);
1.29      nicm      116:        format_free(ft);
                    117:
1.1       nicm      118:        /* Fork the child. */
1.45      nicm      119:        sigfillset(&set);
                    120:        sigprocmask(SIG_BLOCK, &set, &oldset);
1.1       nicm      121:        switch (fork()) {
                    122:        case -1:
1.45      nicm      123:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.39      nicm      124:                cmdq_error(item, "fork error: %s", strerror(errno));
1.29      nicm      125:
                    126:                free(cmd);
1.22      nicm      127:                return (CMD_RETURN_ERROR);
1.1       nicm      128:        case 0:
                    129:                /* Child process. */
1.46      nicm      130:                proc_clear_signals(server_proc, 1);
1.45      nicm      131:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1       nicm      132:                close(pipe_fd[0]);
                    133:
                    134:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
1.47      nicm      135:                if (out) {
                    136:                        if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                    137:                                _exit(1);
                    138:                } else {
                    139:                        if (dup2(null_fd, STDIN_FILENO) == -1)
                    140:                                _exit(1);
                    141:                }
                    142:                if (in) {
                    143:                        if (dup2(pipe_fd[1], STDOUT_FILENO) == -1)
                    144:                                _exit(1);
                    145:                        if (pipe_fd[1] != STDOUT_FILENO)
                    146:                                close(pipe_fd[1]);
                    147:                } else {
                    148:                        if (dup2(null_fd, STDOUT_FILENO) == -1)
                    149:                                _exit(1);
                    150:                }
1.1       nicm      151:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    152:                        _exit(1);
1.16      nicm      153:                closefrom(STDERR_FILENO + 1);
                    154:
1.29      nicm      155:                execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
1.1       nicm      156:                _exit(1);
                    157:        default:
                    158:                /* Parent process. */
1.45      nicm      159:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1       nicm      160:                close(pipe_fd[1]);
                    161:
                    162:                wp->pipe_fd = pipe_fd[0];
1.55      nicm      163:                memcpy(wpo, &wp->offset, sizeof *wpo);
1.9       nicm      164:
1.47      nicm      165:                setblocking(wp->pipe_fd, 0);
                    166:                wp->pipe_event = bufferevent_new(wp->pipe_fd,
                    167:                    cmd_pipe_pane_read_callback,
                    168:                    cmd_pipe_pane_write_callback,
                    169:                    cmd_pipe_pane_error_callback,
1.43      nicm      170:                    wp);
1.48      nicm      171:                if (wp->pipe_event == NULL)
                    172:                        fatalx("out of memory");
1.47      nicm      173:                if (out)
                    174:                        bufferevent_enable(wp->pipe_event, EV_WRITE);
                    175:                if (in)
                    176:                        bufferevent_enable(wp->pipe_event, EV_READ);
1.29      nicm      177:
                    178:                free(cmd);
1.22      nicm      179:                return (CMD_RETURN_NORMAL);
1.1       nicm      180:        }
1.4       nicm      181: }
                    182:
1.37      nicm      183: static void
1.47      nicm      184: cmd_pipe_pane_read_callback(__unused struct bufferevent *bufev, void *data)
                    185: {
                    186:        struct window_pane      *wp = data;
                    187:        struct evbuffer         *evb = wp->pipe_event->input;
                    188:        size_t                   available;
                    189:
                    190:        available = EVBUFFER_LENGTH(evb);
                    191:        log_debug("%%%u pipe read %zu", wp->id, available);
                    192:
                    193:        bufferevent_write(wp->event, EVBUFFER_DATA(evb), available);
                    194:        evbuffer_drain(evb, available);
                    195:
                    196:        if (window_pane_destroy_ready(wp))
                    197:                server_destroy_pane(wp, 1);
                    198: }
                    199:
                    200: static void
1.43      nicm      201: cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
                    202: {
                    203:        struct window_pane      *wp = data;
                    204:
                    205:        log_debug("%%%u pipe empty", wp->id);
1.47      nicm      206:
1.43      nicm      207:        if (window_pane_destroy_ready(wp))
                    208:                server_destroy_pane(wp, 1);
                    209: }
                    210:
                    211: static void
1.30      nicm      212: cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
                    213:     __unused short what, void *data)
1.4       nicm      214: {
                    215:        struct window_pane      *wp = data;
                    216:
1.43      nicm      217:        log_debug("%%%u pipe error", wp->id);
                    218:
1.4       nicm      219:        bufferevent_free(wp->pipe_event);
                    220:        close(wp->pipe_fd);
                    221:        wp->pipe_fd = -1;
1.43      nicm      222:
                    223:        if (window_pane_destroy_ready(wp))
                    224:                server_destroy_pane(wp, 1);
1.1       nicm      225: }