]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * File: bsd/kern/kern_shutdown.c
31 * Copyright (C) 1989, NeXT, Inc.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/proc_internal.h>
41 #include <sys/reboot.h>
43 #include <sys/vnode_internal.h>
44 #include <sys/file_internal.h>
45 #include <sys/clist.h>
46 #include <sys/callout.h>
48 #include <sys/msgbuf.h>
49 #include <sys/ioctl.h>
50 #include <sys/signal.h>
52 #include <kern/task.h>
53 #include <sys/quota.h>
54 #include <vm/vm_kern.h>
55 #include <mach/vm_param.h>
56 #include <sys/filedesc.h>
57 #include <mach/host_priv.h>
58 #include <mach/host_reboot.h>
60 #include <security/audit/audit.h>
62 #include <kern/sched_prim.h> /* for thread_block() */
63 #include <kern/host.h> /* for host_priv_self() */
64 #include <net/if_var.h> /* for if_down_all() */
65 #include <sys/buf_internal.h> /* for count_busy_buffers() */
66 #include <sys/mount_internal.h> /* for vfs_unmountall() */
67 #include <mach/task.h> /* for task_suspend() */
68 #include <sys/sysproto.h> /* abused for sync() */
69 #include <kern/clock.h> /* for delay_for_interval() */
71 #include <sys/kdebug.h>
73 int system_inshutdown
= 0;
75 /* XXX should be in a header file somewhere, but isn't */
76 extern void md_prepare_for_shutdown(int, int, char *);
77 extern void (*unmountroot_pre_hook
)(void);
80 unsigned int proc_shutdown_exitcount
= 0;
82 static int sd_openlog(vfs_context_t
);
83 static int sd_closelog(vfs_context_t
);
84 static void sd_log(vfs_context_t
, const char *, ...);
85 static void proc_shutdown(void);
87 extern void IOSystemShutdownNotification(void);
96 int signo
; /* the signal to be posted */
97 int setsdstate
; /* shutdown state to be set */
98 int countproc
; /* count processes on action */
99 int activecount
; /* number of processes on which action was done */
102 static vnode_t sd_logvp
= NULLVP
;
103 static off_t sd_log_offset
= 0;
106 static int sd_filt1(proc_t
, void *);
107 static int sd_filt2(proc_t
, void *);
108 static int sd_callback1(proc_t p
, void * arg
);
109 static int sd_callback2(proc_t p
, void * arg
);
110 static int sd_callback3(proc_t p
, void * arg
);
113 boot(int paniced
, int howto
, char *command
)
115 struct proc
*p
= current_proc(); /* XXX */
116 int hostboot_option
=0;
119 system_inshutdown
= 1;
121 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
124 * Temporary hack to notify the power management root domain
125 * that the system will shut down.
127 IOSystemShutdownNotification();
129 md_prepare_for_shutdown(paniced
, howto
, command
);
131 if ((howto
&RB_QUICK
)==RB_QUICK
&& waittime
< 0) {
133 printf("Quick reboot...\n");
134 if ((howto
&RB_NOSYNC
)==0) {
135 sync(p
, (void *)NULL
, (int *)NULL
);
138 else if ((howto
&RB_NOSYNC
)==0 && waittime
< 0) {
143 printf("syncing disks... ");
146 * Release vnodes held by texts before sync.
149 /* handle live procs (deallocate their root and current directories). */
156 if (unmountroot_pre_hook
!= NULL
)
157 unmountroot_pre_hook();
159 sync(p
, (void *)NULL
, (int *)NULL
);
162 * Now that all processes have been terminated and system is
163 * sync'ed up, suspend init
166 if (initproc
&& p
!= initproc
)
167 task_suspend(initproc
->task
);
170 kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace");
173 * Unmount filesystems
177 /* Wait for the buffer cache to clean remaining dirty buffers */
178 for (iter
= 0; iter
< 100; iter
++) {
179 nbusy
= count_busy_buffers();
182 printf("%d ", nbusy
);
183 delay_for_interval( 1 * nbusy
, 1000 * 1000);
186 printf("giving up\n");
193 * Can't just use an splnet() here to disable the network
194 * because that will lock out softints which the disk
195 * drivers depend on to finish DMAs.
198 #endif /* NETWORKING */
200 if (howto
& RB_POWERDOWN
)
201 hostboot_option
= HOST_REBOOT_HALT
;
203 hostboot_option
= HOST_REBOOT_HALT
;
204 if (paniced
== RB_PANIC
)
205 hostboot_option
= HOST_REBOOT_HALT
;
207 if (howto
& RB_UPSDELAY
) {
208 hostboot_option
= HOST_REBOOT_UPSDELAY
;
211 host_reboot(host_priv_self(), hostboot_option
);
213 thread_funnel_set(kernel_flock
, FALSE
);
217 sd_openlog(vfs_context_t ctx
)
222 /* Open shutdown log */
223 if ((error
= vnode_open(PROC_SHUTDOWN_LOG
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), 0644, 0, &sd_logvp
, ctx
))) {
224 printf("Failed to open %s: error %d\n", PROC_SHUTDOWN_LOG
, error
);
229 vnode_setsize(sd_logvp
, (off_t
)0, 0, ctx
);
231 /* Write a little header */
233 sd_log(ctx
, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv
.tv_sec
);
239 sd_closelog(vfs_context_t ctx
)
242 if (sd_logvp
!= NULLVP
) {
243 VNOP_FSYNC(sd_logvp
, MNT_WAIT
, ctx
);
244 error
= vnode_close(sd_logvp
, FWRITE
, ctx
);
251 sd_log(vfs_context_t ctx
, const char *fmt
, ...)
253 int resid
, log_error
, len
;
257 /* If the log isn't open yet, open it */
258 if (sd_logvp
== NULLVP
) {
259 if (sd_openlog(ctx
) != 0) {
260 /* Couldn't open, we fail out */
265 va_start(arglist
, fmt
);
266 len
= vsnprintf(logbuf
, sizeof(logbuf
), fmt
, arglist
);
267 log_error
= vn_rdwr(UIO_WRITE
, sd_logvp
, (caddr_t
)logbuf
, len
, sd_log_offset
,
268 UIO_SYSSPACE
, IO_UNIT
| IO_NOAUTH
, vfs_context_ucred(ctx
), &resid
, vfs_context_proc(ctx
));
269 if (log_error
== EIO
|| log_error
== 0) {
270 sd_log_offset
+= (len
- resid
);
278 sd_filt1(proc_t p
, void * args
)
280 proc_t self
= current_proc();
281 struct sd_filterargs
* sf
= (struct sd_filterargs
*)args
;
282 int delayterm
= sf
-> delayterm
;
283 int shutdownstate
= sf
->shutdownstate
;
285 if (((p
->p_flag
&P_SYSTEM
) != 0) || (p
->p_ppid
== 0)
286 ||(p
== self
) || (p
->p_stat
== SZOMB
)
287 || (p
->p_shutdownstate
!= shutdownstate
)
288 ||((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
))
289 || ((p
->p_sigcatch
& sigmask(SIGTERM
))== 0)) {
298 sd_callback1(proc_t p
, void * args
)
300 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
301 int signo
= sd
->signo
;
302 int setsdstate
= sd
->setsdstate
;
303 int countproc
= sd
->countproc
;
306 p
->p_shutdownstate
= setsdstate
;
307 if (p
->p_stat
!= SZOMB
) {
309 if (countproc
!= 0) {
311 p
->p_listflag
|= P_LIST_EXITCOUNT
;
312 proc_shutdown_exitcount
++;
321 return(PROC_RETURNED
);
325 sd_filt2(proc_t p
, void * args
)
327 proc_t self
= current_proc();
328 struct sd_filterargs
* sf
= (struct sd_filterargs
*)args
;
329 int delayterm
= sf
-> delayterm
;
330 int shutdownstate
= sf
->shutdownstate
;
332 if (((p
->p_flag
&P_SYSTEM
) != 0) || (p
->p_ppid
== 0)
333 ||(p
== self
) || (p
->p_stat
== SZOMB
)
334 || (p
->p_shutdownstate
== shutdownstate
)
335 ||((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
))) {
343 sd_callback2(proc_t p
, void * args
)
345 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
346 int signo
= sd
->signo
;
347 int setsdstate
= sd
->setsdstate
;
348 int countproc
= sd
->countproc
;
351 p
->p_shutdownstate
= setsdstate
;
352 if (p
->p_stat
!= SZOMB
) {
354 if (countproc
!= 0) {
356 p
->p_listflag
|= P_LIST_EXITCOUNT
;
357 proc_shutdown_exitcount
++;
366 return(PROC_RETURNED
);
371 sd_callback3(proc_t p
, void * args
)
373 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
374 vfs_context_t ctx
= vfs_context_current();
376 int setsdstate
= sd
->setsdstate
;
379 p
->p_shutdownstate
= setsdstate
;
380 if (p
->p_stat
!= SZOMB
) {
382 * NOTE: following code ignores sig_lock and plays
383 * with exit_thread correctly. This is OK unless we
384 * are a multiprocessor, in which case I do not
385 * understand the sig_lock. This needs to be fixed.
388 if (p
->exit_thread
) { /* someone already doing it */
390 /* give him a chance */
391 thread_block(THREAD_CONTINUE_NULL
);
393 p
->exit_thread
= current_thread();
396 sd_log(ctx
, "%s[%d] had to be forced closed with exit1().\n", p
->p_comm
, p
->p_pid
);
399 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
400 p
->p_pid
, 0, 1, 0, 0);
402 exit1(p
, 1, (int *)NULL
);
407 return(PROC_RETURNED
);
414 * Shutdown down proc system (release references to current and root
415 * dirs for each process).
417 * POSIX modifications:
419 * For POSIX fcntl() file locking call vno_lockrelease() on
420 * the file to release all of its record locks, if any.
426 vfs_context_t ctx
= vfs_context_current();
427 struct proc
*p
, *self
;
429 struct sd_filterargs sfargs
;
430 struct sd_iterargs sdargs
;
435 * Kill as many procs as we can. (Except ourself...)
437 self
= (struct proc
*)current_proc();
440 * Signal the init with SIGTERM so that he does not launch
444 if (p
&& p
!= self
) {
449 printf("Killing all processes ");
453 * send SIGTERM to those procs interested in catching one
455 sfargs
.delayterm
= delayterm
;
456 sfargs
.shutdownstate
= 0;
457 sdargs
.signo
= SIGTERM
;
458 sdargs
.setsdstate
= 1;
459 sdargs
.countproc
= 1;
460 sdargs
.activecount
= 0;
463 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
464 proc_rebootscan(sd_callback1
, (void *)&sdargs
, sd_filt1
, (void *)&sfargs
);
466 if (sdargs
.activecount
!= 0 && proc_shutdown_exitcount
!= 0) {
468 if (proc_shutdown_exitcount
!= 0) {
470 * now wait for up to 30 seconds to allow those procs catching SIGTERM
472 * as soon as these procs have exited, we'll continue on to the next step
476 error
= msleep(&proc_shutdown_exitcount
, proc_list_mlock
, PWAIT
, "shutdownwait", &ts
);
478 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
479 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
480 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
482 for (p
= zombproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
483 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
484 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
491 if (error
== ETIMEDOUT
) {
493 * log the names of the unresponsive tasks
499 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
500 if (p
->p_shutdownstate
== 1) {
501 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
502 sd_log(ctx
, "%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
508 delay_for_interval(1000 * 5, 1000 * 1000);
512 * send a SIGKILL to all the procs still hanging around
514 sfargs
.delayterm
= delayterm
;
515 sfargs
.shutdownstate
= 2;
516 sdargs
.signo
= SIGKILL
;
517 sdargs
.setsdstate
= 2;
518 sdargs
.countproc
= 1;
519 sdargs
.activecount
= 0;
521 /* post a SIGKILL to all that catch SIGTERM and not marked for delay */
522 proc_rebootscan(sd_callback2
, (void *)&sdargs
, sd_filt2
, (void *)&sfargs
);
524 if (sdargs
.activecount
!= 0 && proc_shutdown_exitcount
!= 0) {
526 if (proc_shutdown_exitcount
!= 0) {
528 * wait for up to 60 seconds to allow these procs to exit normally
530 * History: The delay interval was changed from 100 to 200
531 * for NFS requests in particular.
535 error
= msleep(&proc_shutdown_exitcount
, proc_list_mlock
, PWAIT
, "shutdownwait", &ts
);
537 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
538 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
539 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
541 for (p
= zombproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
542 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
543 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
551 * if we still have procs that haven't exited, then brute force 'em
553 sfargs
.delayterm
= delayterm
;
554 sfargs
.shutdownstate
= 3;
556 sdargs
.setsdstate
= 3;
557 sdargs
.countproc
= 0;
558 sdargs
.activecount
= 0;
560 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
561 proc_rebootscan(sd_callback3
, (void *)&sdargs
, sd_filt2
, (void *)&sfargs
);
564 /* Now start the termination of processes that are marked for delayed termn */
565 if (delayterm
== 0) {
572 /* drop the ref on initproc */
574 printf("continuing\n");