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>
33 * Job scheduling. Run queued commands in the background and record their
37 static void job_read_callback(struct bufferevent
*, void *);
38 static void job_write_callback(struct bufferevent
*, void *);
39 static void job_error_callback(struct bufferevent
*, short, void *);
53 char tty
[TTY_NAME_MAX
];
57 struct bufferevent
*event
;
59 job_update_cb updatecb
;
60 job_complete_cb completecb
;
64 LIST_ENTRY(job
) entry
;
68 static LIST_HEAD(joblist
, job
) all_jobs
= LIST_HEAD_INITIALIZER(all_jobs
);
70 /* Start a job running. */
72 job_run(const char *cmd
, int argc
, char **argv
, struct environ
*e
, struct session
*s
,
73 const char *cwd
, job_update_cb updatecb
, job_complete_cb completecb
,
74 job_free_cb freecb
, void *data
, int flags
, int sx
, int sy
)
79 int nullfd
, out
[2], master
;
83 char **argvp
, tty
[TTY_NAME_MAX
];
86 * Do not set TERM during .tmux.conf, it is nice to be able to use
87 * if-shell to decide on default-terminal based on outside TERM.
89 env
= environ_for_session(s
, !cfg_finished
);
94 sigprocmask(SIG_BLOCK
, &set
, &oldset
);
96 if (flags
& JOB_PTY
) {
97 memset(&ws
, 0, sizeof ws
);
100 pid
= fdforkpty(ptm_fd
, &master
, tty
, NULL
, &ws
);
102 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, out
) != 0)
107 cmd_log_argv(argc
, argv
, "%s:", __func__
);
108 log_debug("%s: cwd=%s", __func__
, cwd
== NULL
? "" : cwd
);
110 log_debug("%s: cmd=%s, cwd=%s", __func__
, cmd
,
111 cwd
== NULL
? "" : cwd
);
116 if (~flags
& JOB_PTY
) {
122 proc_clear_signals(server_proc
, 1);
123 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
125 if ((cwd
== NULL
|| chdir(cwd
) != 0) &&
126 ((home
= find_home()) == NULL
|| chdir(home
) != 0) &&
128 fatal("chdir failed");
133 if (~flags
& JOB_PTY
) {
134 if (dup2(out
[1], STDIN_FILENO
) == -1)
135 fatal("dup2 failed");
136 if (dup2(out
[1], STDOUT_FILENO
) == -1)
137 fatal("dup2 failed");
138 if (out
[1] != STDIN_FILENO
&& out
[1] != STDOUT_FILENO
)
142 nullfd
= open(_PATH_DEVNULL
, O_RDWR
);
144 fatal("open failed");
145 if (dup2(nullfd
, STDERR_FILENO
) == -1)
146 fatal("dup2 failed");
147 if (nullfd
!= STDERR_FILENO
)
150 closefrom(STDERR_FILENO
+ 1);
153 execl(_PATH_BSHELL
, "sh", "-c", cmd
, (char *) NULL
);
154 fatal("execl failed");
156 argvp
= cmd_copy_argv(argc
, argv
);
157 execvp(argvp
[0], argvp
);
158 fatal("execvp failed");
162 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
165 job
= xmalloc(sizeof *job
);
166 job
->state
= JOB_RUNNING
;
170 job
->cmd
= xstrdup(cmd
);
172 job
->cmd
= cmd_stringify_argv(argc
, argv
);
174 strlcpy(job
->tty
, tty
, sizeof job
->tty
);
177 LIST_INSERT_HEAD(&all_jobs
, job
, entry
);
179 job
->updatecb
= updatecb
;
180 job
->completecb
= completecb
;
181 job
->freecb
= freecb
;
184 if (~flags
& JOB_PTY
) {
189 setblocking(job
->fd
, 0);
191 job
->event
= bufferevent_new(job
->fd
, job_read_callback
,
192 job_write_callback
, job_error_callback
, job
);
193 if (job
->event
== NULL
)
194 fatalx("out of memory");
195 bufferevent_enable(job
->event
, EV_READ
|EV_WRITE
);
197 log_debug("run job %p: %s, pid %ld", job
, job
->cmd
, (long) job
->pid
);
201 sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
206 /* Take job's file descriptor and free the job. */
208 job_transfer(struct job
*job
, pid_t
*pid
, char *tty
, size_t ttylen
)
212 log_debug("transfer job %p: %s", job
, job
->cmd
);
217 strlcpy(tty
, job
->tty
, ttylen
);
219 LIST_REMOVE(job
, entry
);
222 if (job
->freecb
!= NULL
&& job
->data
!= NULL
)
223 job
->freecb(job
->data
);
225 if (job
->event
!= NULL
)
226 bufferevent_free(job
->event
);
232 /* Kill and free an individual job. */
234 job_free(struct job
*job
)
236 log_debug("free job %p: %s", job
, job
->cmd
);
238 LIST_REMOVE(job
, entry
);
241 if (job
->freecb
!= NULL
&& job
->data
!= NULL
)
242 job
->freecb(job
->data
);
245 kill(job
->pid
, SIGTERM
);
246 if (job
->event
!= NULL
)
247 bufferevent_free(job
->event
);
256 job_resize(struct job
*job
, u_int sx
, u_int sy
)
260 if (job
->fd
== -1 || (~job
->flags
& JOB_PTY
))
263 log_debug("resize job %p: %ux%u", job
, sx
, sy
);
265 memset(&ws
, 0, sizeof ws
);
268 if (ioctl(job
->fd
, TIOCSWINSZ
, &ws
) == -1)
269 fatal("ioctl failed");
272 /* Job buffer read callback. */
274 job_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
276 struct job
*job
= data
;
278 if (job
->updatecb
!= NULL
)
283 * Job buffer write callback. Fired when the buffer falls below watermark
284 * (default is empty). If all the data has been written, disable the write
288 job_write_callback(__unused
struct bufferevent
*bufev
, void *data
)
290 struct job
*job
= data
;
291 size_t len
= EVBUFFER_LENGTH(EVBUFFER_OUTPUT(job
->event
));
293 log_debug("job write %p: %s, pid %ld, output left %zu", job
, job
->cmd
,
294 (long) job
->pid
, len
);
296 if (len
== 0 && (~job
->flags
& JOB_KEEPWRITE
)) {
297 shutdown(job
->fd
, SHUT_WR
);
298 bufferevent_disable(job
->event
, EV_WRITE
);
302 /* Job buffer error callback. */
304 job_error_callback(__unused
struct bufferevent
*bufev
, __unused
short events
,
307 struct job
*job
= data
;
309 log_debug("job error %p: %s, pid %ld", job
, job
->cmd
, (long) job
->pid
);
311 if (job
->state
== JOB_DEAD
) {
312 if (job
->completecb
!= NULL
)
313 job
->completecb(job
);
316 bufferevent_disable(job
->event
, EV_READ
);
317 job
->state
= JOB_CLOSED
;
321 /* Job died (waitpid() returned its pid). */
323 job_check_died(pid_t pid
, int status
)
327 LIST_FOREACH(job
, &all_jobs
, entry
) {
333 if (WIFSTOPPED(status
)) {
334 if (WSTOPSIG(status
) == SIGTTIN
|| WSTOPSIG(status
) == SIGTTOU
)
336 killpg(job
->pid
, SIGCONT
);
339 log_debug("job died %p: %s, pid %ld", job
, job
->cmd
, (long) job
->pid
);
341 job
->status
= status
;
343 if (job
->state
== JOB_CLOSED
) {
344 if (job
->completecb
!= NULL
)
345 job
->completecb(job
);
349 job
->state
= JOB_DEAD
;
353 /* Get job status. */
355 job_get_status(struct job
*job
)
357 return (job
->status
);
362 job_get_data(struct job
*job
)
369 job_get_event(struct job
*job
)
380 LIST_FOREACH(job
, &all_jobs
, entry
) {
382 kill(job
->pid
, SIGTERM
);
386 /* Are any jobs still running? */
388 job_still_running(void)
392 LIST_FOREACH(job
, &all_jobs
, entry
) {
393 if ((~job
->flags
& JOB_NOWAIT
) && job
->state
== JOB_RUNNING
)
399 /* Print job summary. */
401 job_print_summary(struct cmdq_item
*item
, int blank
)
406 LIST_FOREACH(job
, &all_jobs
, entry
) {
408 cmdq_print(item
, "%s", "");
411 cmdq_print(item
, "Job %u: %s [fd=%d, pid=%ld, status=%d]",
412 n
, job
->cmd
, job
->fd
, (long)job
->pid
, job
->status
);