2 * Copyright (c) 1997-2014 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * File: am-utils/amd/sched.c
46 #endif /* HAVE_CONFIG_H */
51 typedef struct pjob pjob;
54 qelem hdr; /* Linked list */
55 int pid; /* Process ID of job */
56 cb_fun *cb_fun; /* Callback function */
57 opaque_t cb_arg; /* Argument for callback */
58 int w; /* everyone these days uses int, not a "union wait" */
59 wchan_t wchan; /* Wait channel */
63 qelem proc_list_head = {&proc_list_head, &proc_list_head};
64 qelem proc_wait_list = {&proc_wait_list, &proc_wait_list};
69 ins_que(qelem *elem, qelem *pred)
71 qelem *p = pred->q_forw;
83 qelem *p = elem->q_forw;
84 qelem *p2 = elem->q_back;
92 sched_job(cb_fun *cf, opaque_t ca)
94 pjob *p = ALLOC(struct pjob);
100 * Now place on wait queue
102 ins_que(&p->hdr, &proc_wait_list);
109 * tf: The task to execute (ta is its arguments)
110 * cf: Continuation function (ca is its arguments)
113 run_task(task_fun *tf, opaque_t ta, cb_fun *cf, opaque_t ca)
115 pjob *p = sched_job(cf, ca);
116 #ifdef HAVE_SIGACTION
118 #else /* not HAVE_SIGACTION */
120 #endif /* not HAVE_SIGACTION */
122 p->wchan = (wchan_t) p;
124 #ifdef HAVE_SIGACTION
125 sigemptyset(&new); /* initialize signal set we wish to block */
126 sigaddset(&new, SIGCHLD); /* only block on SIGCHLD */
127 sigprocmask(SIG_BLOCK, &new, &mask);
128 #else /* not HAVE_SIGACTION */
129 mask = sigblock(sigmask(SIGCHLD));
130 #endif /* not HAVE_SIGACTION */
132 if ((p->pid = background())) {
133 #ifdef HAVE_SIGACTION
134 sigprocmask(SIG_SETMASK, &mask, NULL);
135 #else /* not HAVE_SIGACTION */
137 #endif /* not HAVE_SIGACTION */
141 /* child code runs here, parent has returned to caller */
150 * Schedule a task to be run when woken up
153 sched_task(cb_fun *cf, opaque_t ca, wchan_t wchan)
156 * Allocate a new task
158 pjob *p = sched_job(cf, ca);
160 dlog("SLEEP on %p", wchan);
163 p->w = 0; /* was memset (when ->w was union) */
171 ins_que(&p->hdr, &proc_list_head);
177 wakeup(wchan_t wchan)
185 * Can't use ITER() here because
186 * wakeupjob() juggles the list.
188 for (p = AM_FIRST(pjob, &proc_wait_list);
189 p2 = NEXT(pjob, p), p != HEAD(pjob, &proc_wait_list);
191 if (p->wchan == wchan) {
199 wakeup_task(int rc, int term, wchan_t wchan)
206 get_mntfs_wchan(mntfs *mf)
210 mf->mf_ops->get_wchan)
211 return mf->mf_ops->get_wchan(mf);
217 * Run any pending tasks.
218 * This must be called with SIGCHLD disabled
224 * Keep taking the first item off the list and processing it.
226 * Done this way because the callback can, quite reasonably,
227 * queue a new task, so no local reference into the list can be
230 while (AM_FIRST(pjob, &proc_list_head) != HEAD(pjob, &proc_list_head)) {
231 pjob *p = AM_FIRST(pjob, &proc_list_head);
234 * This job has completed
239 * Do callback if it exists
242 /* these two trigraphs will ensure compatibility with strict POSIX.1 */
243 p->cb_fun(WIFEXITED(p->w) ? WEXITSTATUS(p->w) : 0,
244 WIFSIGNALED(p->w) ? WTERMSIG(p->w) : 0,
255 int w; /* everyone these days uses int, not a "union wait" */
259 while ((pid = waitpid((pid_t) -1, &w, WNOHANG)) > 0) {
260 #else /* not HAVE_WAITPID */
261 while ((pid = wait3( &w, WNOHANG, (struct rusage *) NULL)) > 0) {
262 #endif /* not HAVE_WAITPID */
266 plog(XLOG_ERROR, "Process %d exited with signal %d",
269 dlog("Process %d exited with status %d",
270 pid, WEXITSTATUS(w));
272 for (p = AM_FIRST(pjob, &proc_wait_list);
273 p2 = NEXT(pjob, p), p != HEAD(pjob, &proc_wait_list);
280 } /* end of for loop */
282 if (p == HEAD(pjob, &proc_wait_list))
283 dlog("can't locate task block for pid %d", pid);
286 * Must count down children inside the while loop, otherwise we won't
287 * count them all, and NumChildren (and later backoff) will be set
288 * incorrectly. SH/RUNIT 940519.
290 if (--NumChildren < 0)
292 } /* end of "while wait..." loop */
294 #ifdef REINSTALL_SIGNAL_HANDLER
295 signal(sig, sigchld);
296 #endif /* REINSTALL_SIGNAL_HANDLER */
298 if (select_intr_valid)
299 longjmp(select_intr, sig);