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

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