xref: /openbsd-src/usr.bin/tmux/job.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /* $OpenBSD: job.c,v 1.12 2009/11/04 21:10:49 nicm Exp $ */
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>
20 #include <sys/socket.h>
21 
22 #include <fcntl.h>
23 #include <paths.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Job scheduling. Run queued commands in the background and record their
31  * output.
32  */
33 
34 /* All jobs list. */
35 struct joblist	all_jobs = SLIST_HEAD_INITIALIZER(&all_jobs);
36 
37 RB_GENERATE(jobs, job, entry, job_cmp);
38 
39 void	job_callback(struct bufferevent *, short, void *);
40 
41 int
42 job_cmp(struct job *job1, struct job *job2)
43 {
44 	return (strcmp(job1->cmd, job2->cmd));
45 }
46 
47 /* Initialise job tree. */
48 void
49 job_tree_init(struct jobs *jobs)
50 {
51 	RB_INIT(jobs);
52 }
53 
54 /* Destroy a job tree. */
55 void
56 job_tree_free(struct jobs *jobs)
57 {
58 	struct job	*job;
59 
60 	while (!RB_EMPTY(jobs)) {
61 		job = RB_ROOT(jobs);
62 		RB_REMOVE(jobs, jobs, job);
63 		job_free(job);
64 	}
65 }
66 
67 /* Find a job and return it. */
68 struct job *
69 job_get(struct jobs *jobs, const char *cmd)
70 {
71 	struct job	job;
72 
73 	job.cmd = (char *) cmd;
74 	return (RB_FIND(jobs, jobs, &job));
75 }
76 
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)
81 {
82 	struct job	*job;
83 
84 	job = xmalloc(sizeof *job);
85 	job->cmd = xstrdup(cmd);
86 	job->pid = -1;
87 	job->status = 0;
88 
89 	job->client = c;
90 
91 	job->fd = -1;
92 	job->event = NULL;
93 
94 	job->callbackfn = callbackfn;
95 	job->freefn = freefn;
96 	job->data = data;
97 
98 	job->flags = flags;
99 
100 	if (jobs != NULL)
101 		RB_INSERT(jobs, jobs, job);
102 	SLIST_INSERT_HEAD(&all_jobs, job, lentry);
103 
104 	return (job);
105 }
106 
107 /* Remove job from tree and free. */
108 void
109 job_remove(struct jobs *jobs, struct job *job)
110 {
111 	if (jobs != NULL)
112 		RB_REMOVE(jobs, jobs, job);
113 	job_free(job);
114 }
115 
116 /* Kill and free an individual job. */
117 void
118 job_free(struct job *job)
119 {
120 	job_kill(job);
121 
122 	SLIST_REMOVE(&all_jobs, job, job, lentry);
123 	xfree(job->cmd);
124 
125 	if (job->freefn != NULL && job->data != NULL)
126 		job->freefn(job->data);
127 
128 	if (job->fd != -1)
129 		close(job->fd);
130 	if (job->event != NULL)
131 		bufferevent_free(job->event);
132 
133 	xfree(job);
134 }
135 
136 /* Start a job running, if it isn't already. */
137 int
138 job_run(struct job *job)
139 {
140 	int	nullfd, out[2], mode;
141 
142 	if (job->fd != -1 || job->pid != -1)
143 		return (0);
144 
145 	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0)
146 		return (-1);
147 
148 	switch (job->pid = fork()) {
149 	case -1:
150 		return (-1);
151 	case 0:		/* child */
152 		server_signal_clear();
153 		/* XXX environ? */
154 
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]);
160 
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);
170 
171 		execl(_PATH_BSHELL, "sh", "-c", job->cmd, (char *) NULL);
172 		fatal("execl failed");
173 	default:	/* parent */
174 		close(out[1]);
175 
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");
183 
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);
189 
190 		return (0);
191 	}
192 }
193 
194 /* Job buffer error callback. */
195 void
196 job_callback(unused struct bufferevent *bufev, unused short events, void *data)
197 {
198 	struct job	*job = data;
199 
200 	bufferevent_disable(job->event, EV_READ);
201 	close(job->fd);
202 	job->fd = -1;
203 
204 	if (job->pid == -1) {
205 		if (job->callbackfn != NULL)
206 			job->callbackfn(job);
207 		if ((!job->flags & JOB_PERSIST))
208 			job_free(job);
209 	}
210 }
211 
212 /* Job died (waitpid() returned its pid). */
213 void
214 job_died(struct job *job, int status)
215 {
216 	job->status = status;
217 	job->pid = -1;
218 
219 	if (job->fd == -1) {
220 		if (job->callbackfn != NULL)
221 			job->callbackfn(job);
222 		if ((!job->flags & JOB_PERSIST))
223 			job_free(job);
224 	}
225 }
226 
227 /* Kill a job. */
228 void
229 job_kill(struct job *job)
230 {
231 	if (job->pid == -1)
232 		return;
233 	kill(job->pid, SIGTERM);
234 	job->pid = -1;
235 }
236