#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/vm.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/reboot.h>
#include <sys/conf.h>
#include <sys/vnode.h>
#include <sys/file.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_reboot.h>
int waittime = -1;
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;
static void proc_shutdown();
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();
sync(p, (void *)NULL, (int *)NULL);
ubc_unmountall();
IOSleep( 1 * 1000 );
if (panicstr == 0)
vfs_unmountall();
for (iter = 0; iter < 20; iter++) {
nbusy = count_busy_buffers();
if (nbusy == 0)
break;
printf("%d ", nbusy);
IOSleep( 4 * 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;
self = (struct proc *)current_proc();
p = pfind(1);
if (p && p != self)
task_suspend(p->task);
printf("Killing all processes ");
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)) {
if (p->p_sigcatch & sigmask(SIGTERM))
psignal(p, SIGTERM);
}
}
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_flag&P_SYSTEM) == 0) && (p->p_pptr->p_pid != 0) && (p != self)) {
if (p->p_sigcatch & sigmask(SIGTERM))
TERM_catch++;
}
}
if (TERM_catch == 0)
break;
}
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))
psignal(p, SIGKILL);
}
for (i = 0; i < 300; i++) {
IOSleep(200);
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))
break;
}
if (!p)
break;
}
p = allproc.lh_first;
while (p) {
if ((p->p_flag&P_SYSTEM) || (p->p_pptr->p_pid == 0) || (p == self)) {
p = p->p_list.le_next;
}
else {
if (p->exit_thread) {
thread_block(THREAD_CONTINUE_NULL);
}
else {
p->exit_thread = current_thread();
printf(".");
exit1(p, 1, (int *)NULL);
}
p = allproc.lh_first;
}
}
printf("\n");
p = allproc.lh_first;
while (p) {
#ifdef notyet
fdfree(p);
#endif
p = p->p_list.le_next;
}
printf("continuing\n");
}