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