1 |
|
|
/* $OpenBSD: job.c,v 1.40 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 <fcntl.h> |
23 |
|
|
#include <paths.h> |
24 |
|
|
#include <signal.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <string.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
|
29 |
|
|
#include "tmux.h" |
30 |
|
|
|
31 |
|
|
/* |
32 |
|
|
* Job scheduling. Run queued commands in the background and record their |
33 |
|
|
* output. |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
void job_callback(struct bufferevent *, short, void *); |
37 |
|
|
void job_write_callback(struct bufferevent *, void *); |
38 |
|
|
|
39 |
|
|
/* All jobs list. */ |
40 |
|
|
struct joblist all_jobs = LIST_HEAD_INITIALIZER(all_jobs); |
41 |
|
|
|
42 |
|
|
/* Start a job running, if it isn't already. */ |
43 |
|
|
struct job * |
44 |
|
|
job_run(const char *cmd, struct session *s, const char *cwd, |
45 |
|
|
void (*callbackfn)(struct job *), void (*freefn)(void *), void *data) |
46 |
|
|
{ |
47 |
|
|
struct job *job; |
48 |
|
|
struct environ *env; |
49 |
|
|
pid_t pid; |
50 |
|
|
int nullfd, out[2]; |
51 |
|
|
const char *home; |
52 |
|
|
|
53 |
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0) |
54 |
|
|
return (NULL); |
55 |
|
|
|
56 |
|
|
env = environ_create(); |
57 |
|
|
environ_copy(global_environ, env); |
58 |
|
|
if (s != NULL) |
59 |
|
|
environ_copy(s->environ, env); |
60 |
|
|
server_fill_environ(s, env); |
61 |
|
|
|
62 |
|
|
switch (pid = fork()) { |
63 |
|
|
case -1: |
64 |
|
|
environ_free(env); |
65 |
|
|
close(out[0]); |
66 |
|
|
close(out[1]); |
67 |
|
|
return (NULL); |
68 |
|
|
case 0: /* child */ |
69 |
|
|
clear_signals(1); |
70 |
|
|
|
71 |
|
|
if (cwd == NULL || chdir(cwd) != 0) { |
72 |
|
|
if ((home = find_home()) == NULL || chdir(home) != 0) |
73 |
|
|
chdir("/"); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
environ_push(env); |
77 |
|
|
environ_free(env); |
78 |
|
|
|
79 |
|
|
if (dup2(out[1], STDIN_FILENO) == -1) |
80 |
|
|
fatal("dup2 failed"); |
81 |
|
|
if (dup2(out[1], STDOUT_FILENO) == -1) |
82 |
|
|
fatal("dup2 failed"); |
83 |
|
|
if (out[1] != STDIN_FILENO && out[1] != STDOUT_FILENO) |
84 |
|
|
close(out[1]); |
85 |
|
|
close(out[0]); |
86 |
|
|
|
87 |
|
|
nullfd = open(_PATH_DEVNULL, O_RDWR, 0); |
88 |
|
|
if (nullfd < 0) |
89 |
|
|
fatal("open failed"); |
90 |
|
|
if (dup2(nullfd, STDERR_FILENO) == -1) |
91 |
|
|
fatal("dup2 failed"); |
92 |
|
|
if (nullfd != STDERR_FILENO) |
93 |
|
|
close(nullfd); |
94 |
|
|
|
95 |
|
|
closefrom(STDERR_FILENO + 1); |
96 |
|
|
|
97 |
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL); |
98 |
|
|
fatal("execl failed"); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
/* parent */ |
102 |
|
|
environ_free(env); |
103 |
|
|
close(out[1]); |
104 |
|
|
|
105 |
|
|
job = xmalloc(sizeof *job); |
106 |
|
|
job->state = JOB_RUNNING; |
107 |
|
|
|
108 |
|
|
job->cmd = xstrdup(cmd); |
109 |
|
|
job->pid = pid; |
110 |
|
|
job->status = 0; |
111 |
|
|
|
112 |
|
|
LIST_INSERT_HEAD(&all_jobs, job, lentry); |
113 |
|
|
|
114 |
|
|
job->callbackfn = callbackfn; |
115 |
|
|
job->freefn = freefn; |
116 |
|
|
job->data = data; |
117 |
|
|
|
118 |
|
|
job->fd = out[0]; |
119 |
|
|
setblocking(job->fd, 0); |
120 |
|
|
|
121 |
|
|
job->event = bufferevent_new(job->fd, NULL, job_write_callback, |
122 |
|
|
job_callback, job); |
123 |
|
|
bufferevent_enable(job->event, EV_READ|EV_WRITE); |
124 |
|
|
|
125 |
|
|
log_debug("run job %p: %s, pid %ld", job, job->cmd, (long) job->pid); |
126 |
|
|
return (job); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/* Kill and free an individual job. */ |
130 |
|
|
void |
131 |
|
|
job_free(struct job *job) |
132 |
|
|
{ |
133 |
|
|
log_debug("free job %p: %s", job, job->cmd); |
134 |
|
|
|
135 |
|
|
LIST_REMOVE(job, lentry); |
136 |
|
|
free(job->cmd); |
137 |
|
|
|
138 |
|
|
if (job->freefn != NULL && job->data != NULL) |
139 |
|
|
job->freefn(job->data); |
140 |
|
|
|
141 |
|
|
if (job->pid != -1) |
142 |
|
|
kill(job->pid, SIGTERM); |
143 |
|
|
if (job->event != NULL) |
144 |
|
|
bufferevent_free(job->event); |
145 |
|
|
if (job->fd != -1) |
146 |
|
|
close(job->fd); |
147 |
|
|
|
148 |
|
|
free(job); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/* Called when output buffer falls below low watermark (default is 0). */ |
152 |
|
|
void |
153 |
|
|
job_write_callback(__unused struct bufferevent *bufev, void *data) |
154 |
|
|
{ |
155 |
|
|
struct job *job = data; |
156 |
|
|
size_t len = EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job->event)); |
157 |
|
|
|
158 |
|
|
log_debug("job write %p: %s, pid %ld, output left %zu", job, job->cmd, |
159 |
|
|
(long) job->pid, len); |
160 |
|
|
|
161 |
|
|
if (len == 0) { |
162 |
|
|
shutdown(job->fd, SHUT_WR); |
163 |
|
|
bufferevent_disable(job->event, EV_WRITE); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
/* Job buffer error callback. */ |
168 |
|
|
void |
169 |
|
|
job_callback(__unused struct bufferevent *bufev, __unused short events, |
170 |
|
|
void *data) |
171 |
|
|
{ |
172 |
|
|
struct job *job = data; |
173 |
|
|
|
174 |
|
|
log_debug("job error %p: %s, pid %ld", job, job->cmd, (long) job->pid); |
175 |
|
|
|
176 |
|
|
if (job->state == JOB_DEAD) { |
177 |
|
|
if (job->callbackfn != NULL) |
178 |
|
|
job->callbackfn(job); |
179 |
|
|
job_free(job); |
180 |
|
|
} else { |
181 |
|
|
bufferevent_disable(job->event, EV_READ); |
182 |
|
|
job->state = JOB_CLOSED; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
/* Job died (waitpid() returned its pid). */ |
187 |
|
|
void |
188 |
|
|
job_died(struct job *job, int status) |
189 |
|
|
{ |
190 |
|
|
log_debug("job died %p: %s, pid %ld", job, job->cmd, (long) job->pid); |
191 |
|
|
|
192 |
|
|
job->status = status; |
193 |
|
|
|
194 |
|
|
if (job->state == JOB_CLOSED) { |
195 |
|
|
if (job->callbackfn != NULL) |
196 |
|
|
job->callbackfn(job); |
197 |
|
|
job_free(job); |
198 |
|
|
} else { |
199 |
|
|
job->pid = -1; |
200 |
|
|
job->state = JOB_DEAD; |
201 |
|
|
} |
202 |
|
|
} |