If redrawing line 0 of the screen onto the tty, there can't be a wrap flag on
[tmux-openbsd.git] / job.c
blob28ba3e72a29c8e50f5278069b2b7511f07e15296
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <fcntl.h>
23 #include <paths.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "tmux.h"
30 * Job scheduling. Run queued commands in the background and record their
31 * output.
34 /* All jobs list. */
35 struct joblist all_jobs = SLIST_HEAD_INITIALIZER(&all_jobs);
37 RB_GENERATE(jobs, job, entry, job_cmp);
39 void job_callback(struct bufferevent *, short, void *);
41 int
42 job_cmp(struct job *job1, struct job *job2)
44 return (strcmp(job1->cmd, job2->cmd));
47 /* Initialise job tree. */
48 void
49 job_tree_init(struct jobs *jobs)
51 RB_INIT(jobs);
54 /* Destroy a job tree. */
55 void
56 job_tree_free(struct jobs *jobs)
58 struct job *job;
60 while (!RB_EMPTY(jobs)) {
61 job = RB_ROOT(jobs);
62 RB_REMOVE(jobs, jobs, job);
63 job_free(job);
67 /* Find a job and return it. */
68 struct job *
69 job_get(struct jobs *jobs, const char *cmd)
71 struct job job;
73 job.cmd = (char *) cmd;
74 return (RB_FIND(jobs, jobs, &job));
77 /* Add a job. */
78 struct job *
79 job_add(struct jobs *jobs, int flags, struct client *c, const char *cmd,
80 void (*callbackfn)(struct job *), void (*freefn)(void *), void *data)
82 struct job *job;
84 job = xmalloc(sizeof *job);
85 job->cmd = xstrdup(cmd);
86 job->pid = -1;
87 job->status = 0;
89 job->client = c;
91 job->fd = -1;
92 job->event = NULL;
94 job->callbackfn = callbackfn;
95 job->freefn = freefn;
96 job->data = data;
98 job->flags = flags;
100 if (jobs != NULL)
101 RB_INSERT(jobs, jobs, job);
102 SLIST_INSERT_HEAD(&all_jobs, job, lentry);
104 return (job);
107 /* Remove job from tree and free. */
108 void
109 job_remove(struct jobs *jobs, struct job *job)
111 if (jobs != NULL)
112 RB_REMOVE(jobs, jobs, job);
113 job_free(job);
116 /* Kill and free an individual job. */
117 void
118 job_free(struct job *job)
120 job_kill(job);
122 SLIST_REMOVE(&all_jobs, job, job, lentry);
123 xfree(job->cmd);
125 if (job->freefn != NULL && job->data != NULL)
126 job->freefn(job->data);
128 if (job->fd != -1)
129 close(job->fd);
130 if (job->event != NULL)
131 bufferevent_free(job->event);
133 xfree(job);
136 /* Start a job running, if it isn't already. */
138 job_run(struct job *job)
140 int nullfd, out[2], mode;
142 if (job->fd != -1 || job->pid != -1)
143 return (0);
145 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0)
146 return (-1);
148 switch (job->pid = fork()) {
149 case -1:
150 return (-1);
151 case 0: /* child */
152 server_signal_clear();
153 /* XXX environ? */
155 if (dup2(out[1], STDOUT_FILENO) == -1)
156 fatal("dup2 failed");
157 if (out[1] != STDOUT_FILENO)
158 close(out[1]);
159 close(out[0]);
161 nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
162 if (nullfd < 0)
163 fatal("open failed");
164 if (dup2(nullfd, STDIN_FILENO) == -1)
165 fatal("dup2 failed");
166 if (dup2(nullfd, STDERR_FILENO) == -1)
167 fatal("dup2 failed");
168 if (nullfd != STDIN_FILENO && nullfd != STDERR_FILENO)
169 close(nullfd);
171 execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL);
172 fatal("execl failed");
173 default: /* parent */
174 close(out[1]);
176 job->fd = out[0];
177 if ((mode = fcntl(job->fd, F_GETFL)) == -1)
178 fatal("fcntl failed");
179 if (fcntl(job->fd, F_SETFL, mode|O_NONBLOCK) == -1)
180 fatal("fcntl failed");
181 if (fcntl(job->fd, F_SETFD, FD_CLOEXEC) == -1)
182 fatal("fcntl failed");
184 if (job->event != NULL)
185 bufferevent_free(job->event);
186 job->event =
187 bufferevent_new(job->fd, NULL, NULL, job_callback, job);
188 bufferevent_enable(job->event, EV_READ);
190 return (0);
194 /* Job buffer error callback. */
195 /* ARGSUSED */
196 void
197 job_callback(unused struct bufferevent *bufev, unused short events, void *data)
199 struct job *job = data;
201 bufferevent_disable(job->event, EV_READ);
202 close(job->fd);
203 job->fd = -1;
205 if (job->pid == -1) {
206 if (job->callbackfn != NULL)
207 job->callbackfn(job);
208 if ((!job->flags & JOB_PERSIST))
209 job_free(job);
213 /* Job died (waitpid() returned its pid). */
214 void
215 job_died(struct job *job, int status)
217 job->status = status;
218 job->pid = -1;
220 if (job->fd == -1) {
221 if (job->callbackfn != NULL)
222 job->callbackfn(job);
223 if ((!job->flags & JOB_PERSIST))
224 job_free(job);
228 /* Kill a job. */
229 void
230 job_kill(struct job *job)
232 if (job->pid == -1)
233 return;
234 kill(job->pid, SIGTERM);
235 job->pid = -1;