#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>
#include <kern/sched_prim.h>
#include <kern/host.h>
#include <net/if_var.h>
#include <sys/buf_internal.h>
#include <sys/mount_internal.h>
#include <mach/task.h>
#include <sys/sysproto.h>
#include <kern/clock.h>
extern void md_prepare_for_shutdown(int, int, char *);
int waittime = -1;
static int shutting_down = 0;
static void proc_shutdown(void);
int in_shutdown(void);
extern void IOSystemShutdownNotification(void);
struct sd_filterargs{
int delayterm;
int shutdownstate;
};
struct sd_iterargs {
int signo;
int setsdstate;
};
static int sd_filt1(proc_t, void *);
static int sd_filt2(proc_t, void *);
static int sd_callback1(proc_t p, void * arg);
static int sd_callback2(proc_t p, void * arg);
static int sd_callback3(proc_t p, void * arg);
void
boot(int paniced, int howto, char *command)
{
struct proc *p = current_proc();
int hostboot_option=0;
int funnel_state;
funnel_state = thread_funnel_set(kernel_flock, TRUE);
IOSystemShutdownNotification();
shutting_down = 1;
md_prepare_for_shutdown(paniced, howto, command);
if ((howto&RB_NOSYNC)==0 && waittime < 0) {
int iter, nbusy;
waittime = 0;
printf("syncing disks... ");
proc_shutdown();
#if AUDIT
audit_shutdown();
#endif
sync(p, (void *)NULL, (int *)NULL);
if (initproc && p != initproc)
task_suspend(initproc->task);
vfs_unmountall();
for (iter = 0; iter < 100; iter++) {
nbusy = count_busy_buffers();
if (nbusy == 0)
break;
printf("%d ", nbusy);
delay_for_interval( 1 * nbusy, 1000 * 1000);
}
if (nbusy)
printf("giving up\n");
else
printf("done\n");
}
#if NETWORKING
if_down_all();
#endif
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 (howto & RB_UPSDELAY) {
hostboot_option = HOST_REBOOT_UPSDELAY;
}
host_reboot(host_priv_self(), hostboot_option);
thread_funnel_set(kernel_flock, FALSE);
}
static int
sd_filt1(proc_t p, void * args)
{
proc_t self = current_proc();
struct sd_filterargs * sf = (struct sd_filterargs *)args;
int delayterm = sf-> delayterm;
int shutdownstate = sf->shutdownstate;
if (((p->p_flag&P_SYSTEM) != 0) || (p->p_ppid == 0)
||(p == self) || (p->p_stat == SZOMB)
|| (p->p_shutdownstate != shutdownstate)
||((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM))
|| ((p->p_sigcatch & sigmask(SIGTERM))== 0)) {
return(0);
}
else
return(1);
}
static int
sd_callback1(proc_t p, void * args)
{
struct sd_iterargs * sd = (struct sd_iterargs *)args;
int signo = sd->signo;
int setsdstate = sd->setsdstate;
proc_lock(p);
p->p_shutdownstate = setsdstate;
if (p->p_stat != SZOMB) {
proc_unlock(p);
psignal(p, signo);
} else
proc_unlock(p);
return(PROC_RETURNED);
}
static int
sd_filt2(proc_t p, void * args)
{
proc_t self = current_proc();
struct sd_filterargs * sf = (struct sd_filterargs *)args;
int delayterm = sf-> delayterm;
int shutdownstate = sf->shutdownstate;
if (((p->p_flag&P_SYSTEM) != 0) || (p->p_ppid == 0)
||(p == self) || (p->p_stat == SZOMB)
|| (p->p_shutdownstate == shutdownstate)
||((delayterm == 0) && ((p->p_lflag& P_LDELAYTERM) == P_LDELAYTERM))) {
return(0);
}
else
return(1);
}
static int
sd_callback2(proc_t p, void * args)
{
struct sd_iterargs * sd = (struct sd_iterargs *)args;
int signo = sd->signo;
int setsdstate = sd->setsdstate;
proc_lock(p);
p->p_shutdownstate = setsdstate;
if (p->p_stat != SZOMB) {
proc_unlock(p);
psignal(p, signo);
} else
proc_unlock(p);
return(PROC_RETURNED);
}
static int
sd_callback3(proc_t p, void * args)
{
struct sd_iterargs * sd = (struct sd_iterargs *)args;
int setsdstate = sd->setsdstate;
proc_lock(p);
p->p_shutdownstate = setsdstate;
if (p->p_stat != SZOMB) {
if (p->exit_thread) {
proc_unlock(p);
thread_block(THREAD_CONTINUE_NULL);
} else {
p->exit_thread = current_thread();
printf(".");
proc_unlock(p);
exit1(p, 1, (int *)NULL);
}
} else
proc_unlock(p);
return(PROC_RETURNED);
}
static void
proc_shutdown(void)
{
struct proc *p, *self;
int i, TERM_catch;
int delayterm = 0;
struct sd_filterargs sfargs;
struct sd_iterargs sdargs;
self = (struct proc *)current_proc();
p = proc_find(1);
if (p && p != self) {
psignal(p, SIGTERM);
}
proc_rele(p);
printf("Killing all processes ");
sigterm_loop:
sfargs.delayterm = delayterm;
sfargs.shutdownstate = 0;
sdargs.signo = SIGTERM;
sdargs.setsdstate = 1;
proc_rebootscan(sd_callback1, (void *)&sdargs, sd_filt1, (void *)&sfargs);
for (i = 0; i < 300; i++) {
delay_for_interval(100, 1000 * 1000);
TERM_catch = 0;
proc_list_lock();
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (p->p_shutdownstate == 1) {
TERM_catch++;
}
}
proc_list_unlock();
if (TERM_catch == 0)
break;
}
if (TERM_catch) {
proc_list_lock();
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);
}
}
proc_list_unlock();
delay_for_interval(1000 * 5, 1000 * 1000);
}
sfargs.delayterm = delayterm;
sfargs.shutdownstate = 2;
sdargs.signo = SIGKILL;
sdargs.setsdstate = 2;
proc_rebootscan(sd_callback2, (void *)&sdargs, sd_filt2, (void *)&sfargs);
for (i = 0; i < 300; i++) {
delay_for_interval(200, 1000 * 1000);
proc_list_lock();
for (p = allproc.lh_first; p; p = p->p_list.le_next) {
if (p->p_shutdownstate == 2)
break;
}
proc_list_unlock();
if (!p)
break;
}
sfargs.delayterm = delayterm;
sfargs.shutdownstate = 3;
sdargs.signo = 0;
sdargs.setsdstate = 3;
proc_rebootscan(sd_callback3, (void *)&sdargs, sd_filt2, (void *)&sfargs);
printf("\n");
if (delayterm == 0) {
delayterm = 1;
goto sigterm_loop;
}
proc_rele(initproc);
printf("continuing\n");
}
int
in_shutdown(void)
{
return shutting_down;
}