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

1.46    ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.45 2017/07/12 10:04:51 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.43      nicm       39: static void cmd_pipe_pane_write_callback(struct bufferevent *, void *);
1.37      nicm       40: static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
1.4       nicm       41:
1.1       nicm       42: const struct cmd_entry cmd_pipe_pane_entry = {
1.34      nicm       43:        .name = "pipe-pane",
                     44:        .alias = "pipep",
                     45:
                     46:        .args = { "ot:", 0, 1 },
                     47:        .usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
                     48:
1.41      nicm       49:        .target = { 't', CMD_FIND_PANE, 0 },
1.35      nicm       50:
1.38      nicm       51:        .flags = CMD_AFTERHOOK,
1.34      nicm       52:        .exec = cmd_pipe_pane_exec
1.1       nicm       53: };
                     54:
1.37      nicm       55: static enum cmd_retval
1.39      nicm       56: cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       57: {
1.17      nicm       58:        struct args             *args = self->args;
1.41      nicm       59:        struct client           *c = cmd_find_client(item, NULL, 1);
                     60:        struct window_pane      *wp = item->target.wp;
                     61:        struct session          *s = item->target.s;
                     62:        struct winlink          *wl = item->target.wl;
1.29      nicm       63:        char                    *cmd;
1.18      nicm       64:        int                      old_fd, pipe_fd[2], null_fd;
1.29      nicm       65:        struct format_tree      *ft;
1.45      nicm       66:        sigset_t                 set, oldset;
1.1       nicm       67:
                     68:        /* Destroy the old pipe. */
                     69:        old_fd = wp->pipe_fd;
                     70:        if (wp->pipe_fd != -1) {
1.4       nicm       71:                bufferevent_free(wp->pipe_event);
1.1       nicm       72:                close(wp->pipe_fd);
                     73:                wp->pipe_fd = -1;
1.43      nicm       74:
                     75:                if (window_pane_destroy_ready(wp)) {
                     76:                        server_destroy_pane(wp, 1);
                     77:                        return (CMD_RETURN_NORMAL);
                     78:                }
1.1       nicm       79:        }
                     80:
                     81:        /* If no pipe command, that is enough. */
1.17      nicm       82:        if (args->argc == 0 || *args->argv[0] == '\0')
1.22      nicm       83:                return (CMD_RETURN_NORMAL);
1.1       nicm       84:
                     85:        /*
                     86:         * With -o, only open the new pipe if there was no previous one. This
                     87:         * allows a pipe to be toggled with a single key, for example:
                     88:         *
                     89:         *      bind ^p pipep -o 'cat >>~/output'
                     90:         */
1.17      nicm       91:        if (args_has(self->args, 'o') && old_fd != -1)
1.22      nicm       92:                return (CMD_RETURN_NORMAL);
1.1       nicm       93:
                     94:        /* Open the new pipe. */
1.4       nicm       95:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
1.39      nicm       96:                cmdq_error(item, "socketpair error: %s", strerror(errno));
1.22      nicm       97:                return (CMD_RETURN_ERROR);
1.1       nicm       98:        }
                     99:
1.29      nicm      100:        /* Expand the command. */
1.42      nicm      101:        ft = format_create(item->client, item, FORMAT_NONE, 0);
1.29      nicm      102:        format_defaults(ft, c, s, wl, wp);
                    103:        cmd = format_expand_time(ft, args->argv[0], time(NULL));
                    104:        format_free(ft);
                    105:
1.1       nicm      106:        /* Fork the child. */
1.45      nicm      107:        sigfillset(&set);
                    108:        sigprocmask(SIG_BLOCK, &set, &oldset);
1.1       nicm      109:        switch (fork()) {
                    110:        case -1:
1.45      nicm      111:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.39      nicm      112:                cmdq_error(item, "fork error: %s", strerror(errno));
1.29      nicm      113:
                    114:                free(cmd);
1.22      nicm      115:                return (CMD_RETURN_ERROR);
1.1       nicm      116:        case 0:
                    117:                /* Child process. */
1.46    ! nicm      118:                proc_clear_signals(server_proc, 1);
1.45      nicm      119:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1       nicm      120:                close(pipe_fd[0]);
                    121:
                    122:                if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                    123:                        _exit(1);
                    124:                if (pipe_fd[1] != STDIN_FILENO)
                    125:                        close(pipe_fd[1]);
                    126:
                    127:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
                    128:                if (dup2(null_fd, STDOUT_FILENO) == -1)
                    129:                        _exit(1);
                    130:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    131:                        _exit(1);
                    132:                if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
                    133:                        close(null_fd);
                    134:
1.16      nicm      135:                closefrom(STDERR_FILENO + 1);
                    136:
1.29      nicm      137:                execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
1.1       nicm      138:                _exit(1);
                    139:        default:
                    140:                /* Parent process. */
1.45      nicm      141:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1       nicm      142:                close(pipe_fd[1]);
                    143:
                    144:                wp->pipe_fd = pipe_fd[0];
1.5       nicm      145:                wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9       nicm      146:
1.43      nicm      147:                wp->pipe_event = bufferevent_new(wp->pipe_fd, NULL,
                    148:                    cmd_pipe_pane_write_callback, cmd_pipe_pane_error_callback,
                    149:                    wp);
1.4       nicm      150:                bufferevent_enable(wp->pipe_event, EV_WRITE);
1.9       nicm      151:
1.18      nicm      152:                setblocking(wp->pipe_fd, 0);
1.29      nicm      153:
                    154:                free(cmd);
1.22      nicm      155:                return (CMD_RETURN_NORMAL);
1.1       nicm      156:        }
1.4       nicm      157: }
                    158:
1.37      nicm      159: static void
1.43      nicm      160: cmd_pipe_pane_write_callback(__unused struct bufferevent *bufev, void *data)
                    161: {
                    162:        struct window_pane      *wp = data;
                    163:
                    164:        log_debug("%%%u pipe empty", wp->id);
                    165:        if (window_pane_destroy_ready(wp))
                    166:                server_destroy_pane(wp, 1);
                    167: }
                    168:
                    169: static void
1.30      nicm      170: cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
                    171:     __unused short what, void *data)
1.4       nicm      172: {
                    173:        struct window_pane      *wp = data;
                    174:
1.43      nicm      175:        log_debug("%%%u pipe error", wp->id);
                    176:
1.4       nicm      177:        bufferevent_free(wp->pipe_event);
                    178:        close(wp->pipe_fd);
                    179:        wp->pipe_fd = -1;
1.43      nicm      180:
                    181:        if (window_pane_destroy_ready(wp))
                    182:                server_destroy_pane(wp, 1);
1.1       nicm      183: }