summaryrefslogtreecommitdiff
path: root/usr.bin/tmux/job.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2013-03-25 11:43:02 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2013-03-25 11:43:02 +0000
commite6067a65342859bdeda474215d5c768d7f60747a (patch)
tree71a50f261a0a07547367ff1faaf703180201cacb /usr.bin/tmux/job.c
parent34f3b58de497f4f6dde4580bd55ee63344d8c5f8 (diff)
Extend jobs to support writing and use that for copy-pipe instead of
popen, from Chris Johnsen.
Diffstat (limited to 'usr.bin/tmux/job.c')
-rw-r--r--usr.bin/tmux/job.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/usr.bin/tmux/job.c b/usr.bin/tmux/job.c
index 0cc690613db..749e9a698e5 100644
--- a/usr.bin/tmux/job.c
+++ b/usr.bin/tmux/job.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: job.c,v 1.28 2013/03/22 10:31:22 nicm Exp $ */
+/* $OpenBSD: job.c,v 1.29 2013/03/25 11:43:01 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -33,13 +33,14 @@
*/
void job_callback(struct bufferevent *, short, void *);
+void job_write_callback(struct bufferevent *, void *);
/* All jobs list. */
struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs);
/* Start a job running, if it isn't already. */
struct job *
-job_run(const char *cmd,
+job_run(const char *cmd, struct session *s,
void (*callbackfn)(struct job *), void (*freefn)(void *), void *data)
{
struct job *job;
@@ -52,7 +53,9 @@ job_run(const char *cmd,
environ_init(&env);
environ_copy(&global_environ, &env);
- server_fill_environ(NULL, &env);
+ if (s != NULL)
+ environ_copy(&s->environ, &env);
+ server_fill_environ(s, &env);
switch (pid = fork()) {
case -1:
@@ -64,20 +67,20 @@ job_run(const char *cmd,
environ_push(&env);
environ_free(&env);
+ if (dup2(out[1], STDIN_FILENO) == -1)
+ fatal("dup2 failed");
if (dup2(out[1], STDOUT_FILENO) == -1)
fatal("dup2 failed");
- if (out[1] != STDOUT_FILENO)
+ if (out[1] != STDIN_FILENO && out[1] != STDOUT_FILENO)
close(out[1]);
close(out[0]);
nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
if (nullfd < 0)
fatal("open failed");
- if (dup2(nullfd, STDIN_FILENO) == -1)
- fatal("dup2 failed");
if (dup2(nullfd, STDERR_FILENO) == -1)
fatal("dup2 failed");
- if (nullfd != STDIN_FILENO && nullfd != STDERR_FILENO)
+ if (nullfd != STDERR_FILENO)
close(nullfd);
closefrom(STDERR_FILENO + 1);
@@ -104,7 +107,8 @@ job_run(const char *cmd,
job->fd = out[0];
setblocking(job->fd, 0);
- job->event = bufferevent_new(job->fd, NULL, NULL, job_callback, job);
+ job->event = bufferevent_new(job->fd, NULL, job_write_callback,
+ job_callback, job);
bufferevent_enable(job->event, EV_READ);
log_debug("run job %p: %s, pid %ld", job, job->cmd, (long) job->pid);
@@ -133,6 +137,22 @@ job_free(struct job *job)
free(job);
}
+/* Called when output buffer falls below low watermark (default is 0). */
+void
+job_write_callback(unused struct bufferevent *bufev, void *data)
+{
+ struct job *job = data;
+ size_t len = EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job->event));
+
+ log_debug("job write %p: %s, pid %ld, output left %lu", job, job->cmd,
+ (long) job->pid, (unsigned long) len);
+
+ if (len == 0) {
+ shutdown(job->fd, SHUT_WR);
+ bufferevent_disable(job->event, EV_WRITE);
+ }
+}
+
/* Job buffer error callback. */
void
job_callback(unused struct bufferevent *bufev, unused short events, void *data)