Respond to OSC 4 query.
[tmux-openbsd.git] / job.c
blobdad211f42963d05ce468ff054710364b478757bf
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <fcntl.h>
25 #include <paths.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <util.h>
32 #include "tmux.h"
35 * Job scheduling. Run queued commands in the background and record their
36 * output.
39 static void job_read_callback(struct bufferevent *, void *);
40 static void job_write_callback(struct bufferevent *, void *);
41 static void job_error_callback(struct bufferevent *, short, void *);
43 /* A single job. */
44 struct job {
45 enum {
46 JOB_RUNNING,
47 JOB_DEAD,
48 JOB_CLOSED
49 } state;
51 int flags;
53 char *cmd;
54 pid_t pid;
55 char tty[TTY_NAME_MAX];
56 int status;
58 int fd;
59 struct bufferevent *event;
61 job_update_cb updatecb;
62 job_complete_cb completecb;
63 job_free_cb freecb;
64 void *data;
66 LIST_ENTRY(job) entry;
69 /* All jobs list. */
70 static LIST_HEAD(joblist, job) all_jobs = LIST_HEAD_INITIALIZER(all_jobs);
72 /* Start a job running. */
73 struct job *
74 job_run(const char *cmd, int argc, char **argv, struct environ *e, struct session *s,
75 const char *cwd, job_update_cb updatecb, job_complete_cb completecb,
76 job_free_cb freecb, void *data, int flags, int sx, int sy)
78 struct job *job;
79 struct environ *env;
80 pid_t pid;
81 int nullfd, out[2], master;
82 const char *home;
83 sigset_t set, oldset;
84 struct winsize ws;
85 char **argvp, tty[TTY_NAME_MAX];
88 * Do not set TERM during .tmux.conf, it is nice to be able to use
89 * if-shell to decide on default-terminal based on outside TERM.
91 env = environ_for_session(s, !cfg_finished);
92 if (e != NULL) {
93 environ_copy(e, env);
96 sigfillset(&set);
97 sigprocmask(SIG_BLOCK, &set, &oldset);
99 if (flags & JOB_PTY) {
100 memset(&ws, 0, sizeof ws);
101 ws.ws_col = sx;
102 ws.ws_row = sy;
103 pid = fdforkpty(ptm_fd, &master, tty, NULL, &ws);
104 } else {
105 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0)
106 goto fail;
107 pid = fork();
109 if (cmd == NULL) {
110 cmd_log_argv(argc, argv, "%s:", __func__);
111 log_debug("%s: cwd=%s", __func__, cwd == NULL ? "" : cwd);
112 } else {
113 log_debug("%s: cmd=%s, cwd=%s", __func__, cmd,
114 cwd == NULL ? "" : cwd);
117 switch (pid) {
118 case -1:
119 if (~flags & JOB_PTY) {
120 close(out[0]);
121 close(out[1]);
123 goto fail;
124 case 0:
125 proc_clear_signals(server_proc, 1);
126 sigprocmask(SIG_SETMASK, &oldset, NULL);
128 if ((cwd == NULL || chdir(cwd) != 0) &&
129 ((home = find_home()) == NULL || chdir(home) != 0) &&
130 chdir("/") != 0)
131 fatal("chdir failed");
133 environ_push(env);
134 environ_free(env);
136 if (~flags & JOB_PTY) {
137 if (dup2(out[1], STDIN_FILENO) == -1)
138 fatal("dup2 failed");
139 if (dup2(out[1], STDOUT_FILENO) == -1)
140 fatal("dup2 failed");
141 if (out[1] != STDIN_FILENO && out[1] != STDOUT_FILENO)
142 close(out[1]);
143 close(out[0]);
145 nullfd = open(_PATH_DEVNULL, O_RDWR);
146 if (nullfd == -1)
147 fatal("open failed");
148 if (dup2(nullfd, STDERR_FILENO) == -1)
149 fatal("dup2 failed");
150 if (nullfd != STDERR_FILENO)
151 close(nullfd);
153 closefrom(STDERR_FILENO + 1);
155 if (cmd != NULL) {
156 execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
157 fatal("execl failed");
158 } else {
159 argvp = cmd_copy_argv(argc, argv);
160 execvp(argvp[0], argvp);
161 fatal("execvp failed");
165 sigprocmask(SIG_SETMASK, &oldset, NULL);
166 environ_free(env);
168 job = xmalloc(sizeof *job);
169 job->state = JOB_RUNNING;
170 job->flags = flags;
172 if (cmd != NULL)
173 job->cmd = xstrdup(cmd);
174 else
175 job->cmd = cmd_stringify_argv(argc, argv);
176 job->pid = pid;
177 strlcpy(job->tty, tty, sizeof job->tty);
178 job->status = 0;
180 LIST_INSERT_HEAD(&all_jobs, job, entry);
182 job->updatecb = updatecb;
183 job->completecb = completecb;
184 job->freecb = freecb;
185 job->data = data;
187 if (~flags & JOB_PTY) {
188 close(out[1]);
189 job->fd = out[0];
190 } else
191 job->fd = master;
192 setblocking(job->fd, 0);
194 job->event = bufferevent_new(job->fd, job_read_callback,
195 job_write_callback, job_error_callback, job);
196 if (job->event == NULL)
197 fatalx("out of memory");
198 bufferevent_enable(job->event, EV_READ|EV_WRITE);
200 log_debug("run job %p: %s, pid %ld", job, job->cmd, (long) job->pid);
201 return (job);
203 fail:
204 sigprocmask(SIG_SETMASK, &oldset, NULL);
205 environ_free(env);
206 return (NULL);
209 /* Take job's file descriptor and free the job. */
211 job_transfer(struct job *job, pid_t *pid, char *tty, size_t ttylen)
213 int fd = job->fd;
215 log_debug("transfer job %p: %s", job, job->cmd);
217 if (pid != NULL)
218 *pid = job->pid;
219 if (tty != NULL)
220 strlcpy(tty, job->tty, ttylen);
222 LIST_REMOVE(job, entry);
223 free(job->cmd);
225 if (job->freecb != NULL && job->data != NULL)
226 job->freecb(job->data);
228 if (job->event != NULL)
229 bufferevent_free(job->event);
231 free(job);
232 return (fd);
235 /* Kill and free an individual job. */
236 void
237 job_free(struct job *job)
239 log_debug("free job %p: %s", job, job->cmd);
241 LIST_REMOVE(job, entry);
242 free(job->cmd);
244 if (job->freecb != NULL && job->data != NULL)
245 job->freecb(job->data);
247 if (job->pid != -1)
248 kill(job->pid, SIGTERM);
249 if (job->event != NULL)
250 bufferevent_free(job->event);
251 if (job->fd != -1)
252 close(job->fd);
254 free(job);
257 /* Resize job. */
258 void
259 job_resize(struct job *job, u_int sx, u_int sy)
261 struct winsize ws;
263 if (job->fd == -1 || (~job->flags & JOB_PTY))
264 return;
266 log_debug("resize job %p: %ux%u", job, sx, sy);
268 memset(&ws, 0, sizeof ws);
269 ws.ws_col = sx;
270 ws.ws_row = sy;
271 if (ioctl(job->fd, TIOCSWINSZ, &ws) == -1)
272 fatal("ioctl failed");
275 /* Job buffer read callback. */
276 static void
277 job_read_callback(__unused struct bufferevent *bufev, void *data)
279 struct job *job = data;
281 if (job->updatecb != NULL)
282 job->updatecb(job);
286 * Job buffer write callback. Fired when the buffer falls below watermark
287 * (default is empty). If all the data has been written, disable the write
288 * event.
290 static void
291 job_write_callback(__unused struct bufferevent *bufev, void *data)
293 struct job *job = data;
294 size_t len = EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job->event));
296 log_debug("job write %p: %s, pid %ld, output left %zu", job, job->cmd,
297 (long) job->pid, len);
299 if (len == 0 && (~job->flags & JOB_KEEPWRITE)) {
300 shutdown(job->fd, SHUT_WR);
301 bufferevent_disable(job->event, EV_WRITE);
305 /* Job buffer error callback. */
306 static void
307 job_error_callback(__unused struct bufferevent *bufev, __unused short events,
308 void *data)
310 struct job *job = data;
312 log_debug("job error %p: %s, pid %ld", job, job->cmd, (long) job->pid);
314 if (job->state == JOB_DEAD) {
315 if (job->completecb != NULL)
316 job->completecb(job);
317 job_free(job);
318 } else {
319 bufferevent_disable(job->event, EV_READ);
320 job->state = JOB_CLOSED;
324 /* Job died (waitpid() returned its pid). */
325 void
326 job_check_died(pid_t pid, int status)
328 struct job *job;
330 LIST_FOREACH(job, &all_jobs, entry) {
331 if (pid == job->pid)
332 break;
334 if (job == NULL)
335 return;
336 if (WIFSTOPPED(status)) {
337 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
338 return;
339 killpg(job->pid, SIGCONT);
340 return;
342 log_debug("job died %p: %s, pid %ld", job, job->cmd, (long) job->pid);
344 job->status = status;
346 if (job->state == JOB_CLOSED) {
347 if (job->completecb != NULL)
348 job->completecb(job);
349 job_free(job);
350 } else {
351 job->pid = -1;
352 job->state = JOB_DEAD;
356 /* Get job status. */
358 job_get_status(struct job *job)
360 return (job->status);
363 /* Get job data. */
364 void *
365 job_get_data(struct job *job)
367 return (job->data);
370 /* Get job event. */
371 struct bufferevent *
372 job_get_event(struct job *job)
374 return (job->event);
377 /* Kill all jobs. */
378 void
379 job_kill_all(void)
381 struct job *job;
383 LIST_FOREACH(job, &all_jobs, entry) {
384 if (job->pid != -1)
385 kill(job->pid, SIGTERM);
389 /* Are any jobs still running? */
391 job_still_running(void)
393 struct job *job;
395 LIST_FOREACH(job, &all_jobs, entry) {
396 if ((~job->flags & JOB_NOWAIT) && job->state == JOB_RUNNING)
397 return (1);
399 return (0);
402 /* Print job summary. */
403 void
404 job_print_summary(struct cmdq_item *item, int blank)
406 struct job *job;
407 u_int n = 0;
409 LIST_FOREACH(job, &all_jobs, entry) {
410 if (blank) {
411 cmdq_print(item, "%s", "");
412 blank = 0;
414 cmdq_print(item, "Job %u: %s [fd=%d, pid=%ld, status=%d]",
415 n, job->cmd, job->fd, (long)job->pid, job->status);
416 n++;