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

1.15    ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.14 2010/06/14 23:06:13 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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>
1.4       nicm       20: #include <sys/socket.h>
1.1       nicm       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
                     24: #include <paths.h>
                     25: #include <string.h>
1.13      nicm       26: #include <time.h>
1.1       nicm       27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
                     31: /*
                     32:  * Open pipe to redirect pane output. If already open, close first.
                     33:  */
                     34:
                     35: int    cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
                     36:
1.4       nicm       37: void   cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
                     38:
1.1       nicm       39: const struct cmd_entry cmd_pipe_pane_entry = {
                     40:        "pipe-pane", "pipep",
                     41:        CMD_TARGET_PANE_USAGE "[-o] [command]",
1.7       nicm       42:        CMD_ARG01, "o",
1.1       nicm       43:        cmd_target_init,
                     44:        cmd_target_parse,
                     45:        cmd_pipe_pane_exec,
                     46:        cmd_target_free,
                     47:        cmd_target_print
                     48: };
                     49:
                     50: int
                     51: cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
                     52: {
                     53:        struct cmd_target_data  *data = self->data;
1.13      nicm       54:        struct client           *c;
1.1       nicm       55:        struct window_pane      *wp;
1.13      nicm       56:        char                    *command;
1.1       nicm       57:        int                      old_fd, pipe_fd[2], null_fd, mode;
                     58:
1.14      nicm       59:        if ((c = cmd_find_client(ctx, NULL)) == NULL)
1.13      nicm       60:                return (-1);
                     61:
1.2       nicm       62:        if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
1.1       nicm       63:                return (-1);
                     64:
                     65:        /* Destroy the old pipe. */
                     66:        old_fd = wp->pipe_fd;
                     67:        if (wp->pipe_fd != -1) {
1.4       nicm       68:                bufferevent_free(wp->pipe_event);
1.1       nicm       69:                close(wp->pipe_fd);
                     70:                wp->pipe_fd = -1;
                     71:        }
                     72:
                     73:        /* If no pipe command, that is enough. */
                     74:        if (data->arg == NULL || *data->arg == '\0')
                     75:                return (0);
                     76:
                     77:        /*
                     78:         * With -o, only open the new pipe if there was no previous one. This
                     79:         * allows a pipe to be toggled with a single key, for example:
                     80:         *
                     81:         *      bind ^p pipep -o 'cat >>~/output'
                     82:         */
1.7       nicm       83:        if (cmd_check_flag(data->chflags, 'o') && old_fd != -1)
1.1       nicm       84:                return (0);
                     85:
                     86:        /* Open the new pipe. */
1.4       nicm       87:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
                     88:                ctx->error(ctx, "socketpair error: %s", strerror(errno));
1.1       nicm       89:                return (-1);
                     90:        }
                     91:
                     92:        /* Fork the child. */
                     93:        switch (fork()) {
                     94:        case -1:
                     95:                ctx->error(ctx, "fork error: %s", strerror(errno));
1.9       nicm       96:                return (-1);
1.1       nicm       97:        case 0:
                     98:                /* Child process. */
                     99:                close(pipe_fd[0]);
1.15    ! nicm      100:                clear_signals(1);
1.1       nicm      101:
                    102:                if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                    103:                        _exit(1);
                    104:                if (pipe_fd[1] != STDIN_FILENO)
                    105:                        close(pipe_fd[1]);
                    106:
                    107:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
                    108:                if (dup2(null_fd, STDOUT_FILENO) == -1)
                    109:                        _exit(1);
                    110:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    111:                        _exit(1);
                    112:                if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
                    113:                        close(null_fd);
                    114:
1.13      nicm      115:                command = status_replace(c, NULL, data->arg, time(NULL), 0);
                    116:                execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
1.1       nicm      117:                _exit(1);
                    118:        default:
                    119:                /* Parent process. */
                    120:                close(pipe_fd[1]);
                    121:
                    122:                wp->pipe_fd = pipe_fd[0];
1.5       nicm      123:                wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9       nicm      124:
1.4       nicm      125:                wp->pipe_event = bufferevent_new(wp->pipe_fd,
                    126:                    NULL, NULL, cmd_pipe_pane_error_callback, wp);
                    127:                bufferevent_enable(wp->pipe_event, EV_WRITE);
1.9       nicm      128:
1.1       nicm      129:                if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1)
                    130:                        fatal("fcntl failed");
                    131:                if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    132:                        fatal("fcntl failed");
                    133:                if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
1.9       nicm      134:                        fatal("fcntl failed");
1.1       nicm      135:                return (0);
                    136:        }
1.4       nicm      137: }
                    138:
1.8       nicm      139: /* ARGSUSED */
1.4       nicm      140: void
                    141: cmd_pipe_pane_error_callback(
                    142:     unused struct bufferevent *bufev, unused short what, void *data)
                    143: {
                    144:        struct window_pane      *wp = data;
                    145:
                    146:        bufferevent_free(wp->pipe_event);
                    147:        close(wp->pipe_fd);
                    148:        wp->pipe_fd = -1;
1.1       nicm      149: }