#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/vm.h>
#include <sys/proc_internal.h>
#include <sys/user.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/vnode_internal.h>
#include <sys/file_internal.h>
#include <sys/clist.h>
#include <sys/callout.h>
#include <sys/mbuf.h>
#include <sys/msgbuf.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/tty.h>
#include <kern/task.h>
#include <sys/quota.h>
#include <ufs/ufs/inode.h>
#if NCPUS > 1
#include <kern/processor.h>
#include <kern/thread.h>
#include <sys/lock.h>
#endif
#include <vm/vm_kern.h>
#include <mach/vm_param.h>
#include <sys/filedesc.h>
#include <mach/host_priv.h>
#include <mach/host_reboot.h>
#include <bsm/audit_kernel.h>
int waittime = -1;
static void proc_shutdown();
void
boot(paniced, howto, command)
int paniced, howto;
char *command;
{
register int i;
int s;
struct proc *p = current_proc();
int hostboot_option=0;
int funnel_state;
struct proc *launchd_proc;
extern void md_prepare_for_shutdown(int paniced, int howto, char * command);
funnel_state = thread_funnel_set(kernel_flock, TRUE);
md_prepare_for_shutdown(paniced, howto, command);
if ((howto&RB_NOSYNC)==0 && waittime < 0) {
int iter, nbusy;
waittime = 0;
printf("syncing disks... ");
proc_shutdown();
audit_shutdown();
sync(p, (void *)NULL, (int *)NULL);
launchd_proc = pfind(1);
if (launchd_proc && p != launchd_proc) {
task_suspend(launchd_proc->task);
}
vfs_unmountall();
for (iter = 0; iter < 100; iter++) {
nbusy = count_busy_buffers();
if (nbusy == 0)
break;
printf("%d ", nbusy);
IOSleep( 1 * nbusy );
}
if (nbusy)
printf("giving up\n");
else
printf("done\n");
}
if_down_all();
if (howto & RB_POWERDOWN)
hostboot_option = HOST_REBOOT_HALT;
if (howto & RB_HALT)
hostboot_option = HOST_REBOOT_HALT;
if (paniced == RB_PANIC)
hostboot_option = HOST_REBOOT_HALT;
if (hostboot_option == HOST_REBOOT_HALT)
IOSleep( 1 * 1000 );
host_reboot(host_priv_self(), hostboot_option);
thread_funnel_set(kernel_flock, FALSE);
}
static void
proc_shutdown()
{
struct proc *p, *self;
struct vnode **cdirp, **rdirp, *vp;
int restart, i, TERM_catch;
int delayterm = 0;
self = (struct proc *)current_proc();
p = pfind(1);
if (p && p != self) {
psignal(p, SIGTERM);
}
printf("Killing all processes ");
sigterm_loop:
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_stat != SZOMB) && (p->p_shutdownstate == 0)) {
if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
continue;
}
if (p->p_sigcatch & sigmask(SIGTERM)) {
p->p_shutdownstate = 1;
if (proc_refinternal(p, 1) == p) {
psignal(p, SIGTERM);
proc_dropinternal(p, 1);
}
goto sigterm_loop;
}
}
}
for (i = 0; i < 300; i++) {
IOSleep(100);
TERM_catch = 0;
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (p->p_shutdownstate == 1) {
TERM_catch++;
}
}
if (TERM_catch == 0)
break;
}
if (TERM_catch) {
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (p->p_shutdownstate == 1) {
printf("%s[%d]: didn't act on SIGTERM\n", p->p_comm, p->p_pid);
}
}
IOSleep(1000 * 5);
}
sigkill_loop:
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (((p->p_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self) && (p->p_stat != SZOMB) && (p->p_shutdownstate != 2)) {
if ((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM)) {
continue;
}
if (proc_refinternal(p, 1) == p) {
psignal(p, SIGKILL);
proc_dropinternal(p, 1);
}
p->p_shutdownstate = 2;
goto sigkill_loop;
}
}
for (i = 0; i < 300; i++) {
IOSleep(200);
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (p->p_shutdownstate == 2)
break;
}
if (!p)
break;
}
p = allproc.lh_first;
while (p) {
if ((p->p_shutdownstate == 3) || (p->p_flag&P_SYSTEM) || (!delayterm && ((p->p_lflag& P_LDELAYTERM)))
|| (p->p_pptr->p_pid == 0) || (p == self)) {
p = p->p_list.le_next;
}
else {
p->p_shutdownstate = 3;
if (p->exit_thread) {
thread_block(THREAD_CONTINUE_NULL);
} else {
p->exit_thread = current_thread();
printf(".");
if (proc_refinternal(p, 1) == p) {
exit1(p, 1, (int *)NULL);
proc_dropinternal(p, 1);
}
}
p = allproc.lh_first;
}
}
printf("\n");
if (delayterm == 0) {
delayterm = 1;
goto sigterm_loop;
}
printf("continuing\n");
}