GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-pipe-pane.c Lines: 0 53 0.0 %
Date: 2016-12-06 Branches: 0 25 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-pipe-pane.c,v 1.36 2016/01/19 15:59:12 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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/socket.h>
21
22
#include <errno.h>
23
#include <fcntl.h>
24
#include <paths.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <time.h>
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
36
enum cmd_retval	 cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
37
38
void	cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
39
40
const struct cmd_entry cmd_pipe_pane_entry = {
41
	.name = "pipe-pane",
42
	.alias = "pipep",
43
44
	.args = { "ot:", 0, 1 },
45
	.usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
46
47
	.tflag = CMD_PANE,
48
49
	.flags = 0,
50
	.exec = cmd_pipe_pane_exec
51
};
52
53
enum cmd_retval
54
cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
55
{
56
	struct args		*args = self->args;
57
	struct client		*c = cmdq->state.c;
58
	struct window_pane	*wp = cmdq->state.tflag.wp;
59
	struct session		*s = cmdq->state.tflag.s;
60
	struct winlink		*wl = cmdq->state.tflag.wl;
61
	char			*cmd;
62
	int			 old_fd, pipe_fd[2], null_fd;
63
	struct format_tree	*ft;
64
65
	/* Destroy the old pipe. */
66
	old_fd = wp->pipe_fd;
67
	if (wp->pipe_fd != -1) {
68
		bufferevent_free(wp->pipe_event);
69
		close(wp->pipe_fd);
70
		wp->pipe_fd = -1;
71
	}
72
73
	/* If no pipe command, that is enough. */
74
	if (args->argc == 0 || *args->argv[0] == '\0')
75
		return (CMD_RETURN_NORMAL);
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
	 */
83
	if (args_has(self->args, 'o') && old_fd != -1)
84
		return (CMD_RETURN_NORMAL);
85
86
	/* Open the new pipe. */
87
	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
88
		cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
89
		return (CMD_RETURN_ERROR);
90
	}
91
92
	/* Expand the command. */
93
	ft = format_create(cmdq, 0);
94
	format_defaults(ft, c, s, wl, wp);
95
	cmd = format_expand_time(ft, args->argv[0], time(NULL));
96
	format_free(ft);
97
98
	/* Fork the child. */
99
	switch (fork()) {
100
	case -1:
101
		cmdq_error(cmdq, "fork error: %s", strerror(errno));
102
103
		free(cmd);
104
		return (CMD_RETURN_ERROR);
105
	case 0:
106
		/* Child process. */
107
		close(pipe_fd[0]);
108
		clear_signals(1);
109
110
		if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
111
			_exit(1);
112
		if (pipe_fd[1] != STDIN_FILENO)
113
			close(pipe_fd[1]);
114
115
		null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
116
		if (dup2(null_fd, STDOUT_FILENO) == -1)
117
			_exit(1);
118
		if (dup2(null_fd, STDERR_FILENO) == -1)
119
			_exit(1);
120
		if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
121
			close(null_fd);
122
123
		closefrom(STDERR_FILENO + 1);
124
125
		execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
126
		_exit(1);
127
	default:
128
		/* Parent process. */
129
		close(pipe_fd[1]);
130
131
		wp->pipe_fd = pipe_fd[0];
132
		wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
133
134
		wp->pipe_event = bufferevent_new(wp->pipe_fd,
135
		    NULL, NULL, cmd_pipe_pane_error_callback, wp);
136
		bufferevent_enable(wp->pipe_event, EV_WRITE);
137
138
		setblocking(wp->pipe_fd, 0);
139
140
		free(cmd);
141
		return (CMD_RETURN_NORMAL);
142
	}
143
}
144
145
void
146
cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
147
    __unused short what, void *data)
148
{
149
	struct window_pane	*wp = data;
150
151
	bufferevent_free(wp->pipe_event);
152
	close(wp->pipe_fd);
153
	wp->pipe_fd = -1;
154
}