Remove old buffer when renaming rather than complaining, GitHub issue
[tmux-openbsd.git] / job.c
blobfb33c0cf5762d25945c8b20e3bddb42a943296ea
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);
95 sigfillset(&set);
96 sigprocmask(SIG_BLOCK, &set, &oldset);
98 if (flags & JOB_PTY) {
99 memset(&ws, 0, sizeof ws);
100 ws.ws_col = sx;
101 ws.ws_row = sy;
102 pid = fdforkpty(ptm_fd, &master, tty, NULL, &ws);
103 } else {
104 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, out) != 0)
105 goto fail;
106 pid = fork();
108 if (cmd == NULL) {
109 cmd_log_argv(argc, argv, "%s:", __func__);
110 log_debug("%s: cwd=%s", __func__, cwd == NULL ? "" : cwd);
111 } else {
112 log_debug("%s: cmd=%s, cwd=%s", __func__, cmd,
113 cwd == NULL ? "" : cwd);
116 switch (pid) {
117 case -1:
118 if (~flags & JOB_PTY) {
119 close(out[0]);
120 close(out[1]);
122 goto fail;
123 case 0:
124 proc_clear_signals(server_proc, 1);
125 sigprocmask(SIG_SETMASK, &oldset, NULL);
127 if ((cwd == NULL || chdir(cwd) != 0) &&
128 ((home = find_home()) == NULL || chdir(home) != 0) &&
129 chdir("/") != 0)
130 fatal("chdir failed");
132 environ_push(env);
133 environ_free(env);
135 if (~flags & JOB_PTY) {
136 if (dup2(out[1], STDIN_FILENO) == -1)
137 fatal("dup2 failed");
138 if (dup2(out[1], STDOUT_FILENO) == -1)
139 fatal("dup2 failed");
140 if (out[1] != STDIN_FILENO && out[1] != STDOUT_FILENO)
141 close(out[1]);
142 close(out[0]);
144 nullfd = open(_PATH_DEVNULL, O_RDWR);
145 if (nullfd == -1)
146 fatal("open failed");
147 if (dup2(nullfd, STDERR_FILENO) == -1)
148 fatal("dup2 failed");
149 if (nullfd != STDERR_FILENO)
150 close(nullfd);
152 closefrom(STDERR_FILENO + 1);
154 if (cmd != NULL) {
155 execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
156 fatal("execl failed");
157 } else {
158 argvp = cmd_copy_argv(argc, argv);
159 execvp(argvp[0], argvp);
160 fatal("execvp failed");
164 sigprocmask(SIG_SETMASK, &oldset, NULL);
165 environ_free(env);
167 job = xmalloc(sizeof *job);
168 job->state = JOB_RUNNING;
169 job->flags = flags;
171 if (cmd != NULL)
172 job->cmd = xstrdup(cmd);
173 else
174 job->cmd = cmd_stringify_argv(argc, argv);
175 job->pid = pid;
176 strlcpy(job->tty, tty, sizeof job->tty);
177 job->status = 0;
179 LIST_INSERT_HEAD(&all_jobs, job, entry);
181 job->updatecb = updatecb;
182 job->completecb = completecb;
183 job->freecb = freecb;
184 job->data = data;
186 if (~flags & JOB_PTY) {
187 close(out[1]);
188 job->fd = out[0];
189 } else
190 job->fd = master;
191 setblocking(job->fd, 0);
193 job->event = bufferevent_new(job->fd, job_read_callback,
194 job_write_callback, job_error_callback, job);
195 if (job->event == NULL)
196 fatalx("out of memory");
197 bufferevent_enable(job->event, EV_READ|EV_WRITE);
199 log_debug("run job %p: %s, pid %ld", job, job->cmd, (long) job->pid);
200 return (job);
202 fail:
203 sigprocmask(SIG_SETMASK, &oldset, NULL);
204 environ_free(env);
205 return (NULL);
208 /* Take job's file descriptor and free the job. */
210 job_transfer(struct job *job, pid_t *pid, char *tty, size_t ttylen)
212 int fd = job->fd;
214 log_debug("transfer job %p: %s", job, job->cmd);
216 if (pid != NULL)
217 *pid = job->pid;
218 if (tty != NULL)
219 strlcpy(tty, job->tty, ttylen);
221 LIST_REMOVE(job, entry);
222 free(job->cmd);
224 if (job->freecb != NULL && job->data != NULL)
225 job->freecb(job->data);
227 if (job->event != NULL)
228 bufferevent_free(job->event);
230 free(job);
231 return (fd);
234 /* Kill and free an individual job. */
235 void
236 job_free(struct job *job)
238 log_debug("free job %p: %s", job, job->cmd);
240 LIST_REMOVE(job, entry);
241 free(job->cmd);
243 if (job->freecb != NULL && job->data != NULL)
244 job->freecb(job->data);
246 if (job->pid != -1)
247 kill(job->pid, SIGTERM);
248 if (job->event != NULL)
249 bufferevent_free(job->event);
250 if (job->fd != -1)
251 close(job->fd);
253 free(job);
256 /* Resize job. */
257 void
258 job_resize(struct job *job, u_int sx, u_int sy)
260 struct winsize ws;
262 if (job->fd == -1 || (~job->flags & JOB_PTY))
263 return;
265 log_debug("resize job %p: %ux%u", job, sx, sy);
267 memset(&ws, 0, sizeof ws);
268 ws.ws_col = sx;
269 ws.ws_row = sy;
270 if (ioctl(job->fd, TIOCSWINSZ, &ws) == -1)
271 fatal("ioctl failed");
274 /* Job buffer read callback. */
275 static void
276 job_read_callback(__unused struct bufferevent *bufev, void *data)
278 struct job *job = data;
280 if (job->updatecb != NULL)
281 job->updatecb(job);
285 * Job buffer write callback. Fired when the buffer falls below watermark
286 * (default is empty). If all the data has been written, disable the write
287 * event.
289 static void
290 job_write_callback(__unused struct bufferevent *bufev, void *data)
292 struct job *job = data;
293 size_t len = EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job->event));
295 log_debug("job write %p: %s, pid %ld, output left %zu", job, job->cmd,
296 (long) job->pid, len);
298 if (len == 0 && (~job->flags & JOB_KEEPWRITE)) {
299 shutdown(job->fd, SHUT_WR);
300 bufferevent_disable(job->event, EV_WRITE);
304 /* Job buffer error callback. */
305 static void
306 job_error_callback(__unused struct bufferevent *bufev, __unused short events,
307 void *data)
309 struct job *job = data;
311 log_debug("job error %p: %s, pid %ld", job, job->cmd, (long) job->pid);
313 if (job->state == JOB_DEAD) {
314 if (job->completecb != NULL)
315 job->completecb(job);
316 job_free(job);
317 } else {
318 bufferevent_disable(job->event, EV_READ);
319 job->state = JOB_CLOSED;
323 /* Job died (waitpid() returned its pid). */
324 void
325 job_check_died(pid_t pid, int status)
327 struct job *job;
329 LIST_FOREACH(job, &all_jobs, entry) {
330 if (pid == job->pid)
331 break;
333 if (job == NULL)
334 return;
335 if (WIFSTOPPED(status)) {
336 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
337 return;
338 killpg(job->pid, SIGCONT);
339 return;
341 log_debug("job died %p: %s, pid %ld", job, job->cmd, (long) job->pid);
343 job->status = status;
345 if (job->state == JOB_CLOSED) {
346 if (job->completecb != NULL)
347 job->completecb(job);
348 job_free(job);
349 } else {
350 job->pid = -1;
351 job->state = JOB_DEAD;
355 /* Get job status. */
357 job_get_status(struct job *job)
359 return (job->status);
362 /* Get job data. */
363 void *
364 job_get_data(struct job *job)
366 return (job->data);
369 /* Get job event. */
370 struct bufferevent *
371 job_get_event(struct job *job)
373 return (job->event);
376 /* Kill all jobs. */
377 void
378 job_kill_all(void)
380 struct job *job;
382 LIST_FOREACH(job, &all_jobs, entry) {
383 if (job->pid != -1)
384 kill(job->pid, SIGTERM);
388 /* Are any jobs still running? */
390 job_still_running(void)
392 struct job *job;
394 LIST_FOREACH(job, &all_jobs, entry) {
395 if ((~job->flags & JOB_NOWAIT) && job->state == JOB_RUNNING)
396 return (1);
398 return (0);
401 /* Print job summary. */
402 void
403 job_print_summary(struct cmdq_item *item, int blank)
405 struct job *job;
406 u_int n = 0;
408 LIST_FOREACH(job, &all_jobs, entry) {
409 if (blank) {
410 cmdq_print(item, "%s", "");
411 blank = 0;
413 cmdq_print(item, "Job %u: %s [fd=%d, pid=%ld, status=%d]",
414 n, job->cmd, job->fd, (long)job->pid, job->status);
415 n++;