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() */
70 #include <libkern/OSAtomic.h>
72 #include <sys/kdebug.h>
74 uint32_t system_inshutdown
= 0;
76 /* XXX should be in a header file somewhere, but isn't */
77 extern void (*unmountroot_pre_hook
)(void);
79 unsigned int proc_shutdown_exitcount
= 0;
81 static int sd_openlog(vfs_context_t
);
82 static int sd_closelog(vfs_context_t
);
83 static void sd_log(vfs_context_t
, const char *, ...);
84 static void proc_shutdown(void);
85 static void kernel_hwm_panic_info(void);
86 extern void IOSystemShutdownNotification(void);
95 int signo
; /* the signal to be posted */
96 int setsdstate
; /* shutdown state to be set */
97 int countproc
; /* count processes on action */
98 int activecount
; /* number of processes on which action was done */
101 static vnode_t sd_logvp
= NULLVP
;
102 static off_t sd_log_offset
= 0;
105 static int sd_filt1(proc_t
, void *);
106 static int sd_filt2(proc_t
, void *);
107 static int sd_callback1(proc_t p
, void * arg
);
108 static int sd_callback2(proc_t p
, void * arg
);
109 static int sd_callback3(proc_t p
, void * arg
);
111 extern boolean_t panic_include_zprint
;
112 extern vm_offset_t panic_kext_memory_info
;
113 extern vm_size_t panic_kext_memory_size
;
116 kernel_hwm_panic_info(void)
118 mach_memory_info_t
*memory_info
;
119 unsigned int num_sites
;
122 panic_include_zprint
= TRUE
;
123 panic_kext_memory_info
= 0;
124 panic_kext_memory_size
= 0;
126 num_sites
= VM_KERN_MEMORY_COUNT
+ VM_KERN_COUNTER_COUNT
;
127 panic_kext_memory_size
= round_page(num_sites
* sizeof(mach_zone_info_t
));
129 kr
= kmem_alloc(kernel_map
, (vm_offset_t
*) &panic_kext_memory_info
, panic_kext_memory_size
, VM_KERN_MEMORY_OSFMK
);
130 if (kr
!= KERN_SUCCESS
) {
131 panic_kext_memory_info
= 0;
134 memory_info
= (mach_memory_info_t
*)panic_kext_memory_info
;
135 vm_page_diagnose(memory_info
, num_sites
);
140 reboot_kernel(int howto
, char *message
)
142 int hostboot_option
=0;
144 if (!OSCompareAndSwap(0, 1, &system_inshutdown
)) {
145 if ( (howto
&RB_QUICK
) == RB_QUICK
)
150 * Temporary hack to notify the power management root domain
151 * that the system will shut down.
153 IOSystemShutdownNotification();
155 if ((howto
&RB_QUICK
)==RB_QUICK
) {
156 printf("Quick reboot...\n");
157 if ((howto
&RB_NOSYNC
)==0) {
158 sync((proc_t
)NULL
, (void *)NULL
, (int *)NULL
);
161 else if ((howto
&RB_NOSYNC
)==0) {
164 printf("syncing disks... ");
167 * Release vnodes held by texts before sync.
170 /* handle live procs (deallocate their root and current directories), suspend initproc */
177 if (unmountroot_pre_hook
!= NULL
)
178 unmountroot_pre_hook();
180 sync((proc_t
)NULL
, (void *)NULL
, (int *)NULL
);
183 kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace");
186 * Unmount filesystems
190 /* Wait for the buffer cache to clean remaining dirty buffers */
191 for (iter
= 0; iter
< 100; iter
++) {
192 nbusy
= count_busy_buffers();
195 printf("%d ", nbusy
);
196 delay_for_interval( 1 * nbusy
, 1000 * 1000);
199 printf("giving up\n");
205 * Can't just use an splnet() here to disable the network
206 * because that will lock out softints which the disk
207 * drivers depend on to finish DMAs.
210 #endif /* NETWORKING */
214 if (howto
& RB_PANIC
) {
215 if (strncmp(message
, "Kernel memory has exceeded limits", 33) == 0) {
216 kernel_hwm_panic_info();
218 panic ("userspace panic: %s", message
);
221 if (howto
& RB_POWERDOWN
)
222 hostboot_option
= HOST_REBOOT_HALT
;
224 hostboot_option
= HOST_REBOOT_HALT
;
226 if (howto
& RB_UPSDELAY
) {
227 hostboot_option
= HOST_REBOOT_UPSDELAY
;
230 host_reboot(host_priv_self(), hostboot_option
);
232 * should not be reached
238 sd_openlog(vfs_context_t ctx
)
243 /* Open shutdown log */
244 if ((error
= vnode_open(PROC_SHUTDOWN_LOG
, (O_CREAT
| FWRITE
| O_NOFOLLOW
), 0644, 0, &sd_logvp
, ctx
))) {
245 printf("Failed to open %s: error %d\n", PROC_SHUTDOWN_LOG
, error
);
250 vnode_setsize(sd_logvp
, (off_t
)0, 0, ctx
);
252 /* Write a little header */
254 sd_log(ctx
, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv
.tv_sec
);
260 sd_closelog(vfs_context_t ctx
)
263 if (sd_logvp
!= NULLVP
) {
264 VNOP_FSYNC(sd_logvp
, MNT_WAIT
, ctx
);
265 error
= vnode_close(sd_logvp
, FWRITE
, ctx
);
272 sd_log(vfs_context_t ctx
, const char *fmt
, ...)
274 int resid
, log_error
, len
;
278 /* If the log isn't open yet, open it */
279 if (sd_logvp
== NULLVP
) {
280 if (sd_openlog(ctx
) != 0) {
281 /* Couldn't open, we fail out */
286 va_start(arglist
, fmt
);
287 len
= vsnprintf(logbuf
, sizeof(logbuf
), fmt
, arglist
);
288 log_error
= vn_rdwr(UIO_WRITE
, sd_logvp
, (caddr_t
)logbuf
, len
, sd_log_offset
,
289 UIO_SYSSPACE
, IO_UNIT
| IO_NOAUTH
, vfs_context_ucred(ctx
), &resid
, vfs_context_proc(ctx
));
290 if (log_error
== EIO
|| log_error
== 0) {
291 sd_log_offset
+= (len
- resid
);
299 sd_filt1(proc_t p
, void * args
)
301 proc_t self
= current_proc();
302 struct sd_filterargs
* sf
= (struct sd_filterargs
*)args
;
303 int delayterm
= sf
-> delayterm
;
304 int shutdownstate
= sf
->shutdownstate
;
306 if (((p
->p_flag
&P_SYSTEM
) != 0) || (p
->p_ppid
== 0)
307 ||(p
== self
) || (p
->p_stat
== SZOMB
)
308 || (p
->p_shutdownstate
!= shutdownstate
)
309 ||((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
))
310 || ((p
->p_sigcatch
& sigmask(SIGTERM
))== 0)) {
319 sd_callback1(proc_t p
, void * args
)
321 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
322 int signo
= sd
->signo
;
323 int setsdstate
= sd
->setsdstate
;
324 int countproc
= sd
->countproc
;
327 p
->p_shutdownstate
= setsdstate
;
328 if (p
->p_stat
!= SZOMB
) {
330 if (countproc
!= 0) {
332 p
->p_listflag
|= P_LIST_EXITCOUNT
;
333 proc_shutdown_exitcount
++;
342 return(PROC_RETURNED
);
346 sd_filt2(proc_t p
, void * args
)
348 proc_t self
= current_proc();
349 struct sd_filterargs
* sf
= (struct sd_filterargs
*)args
;
350 int delayterm
= sf
-> delayterm
;
351 int shutdownstate
= sf
->shutdownstate
;
353 if (((p
->p_flag
&P_SYSTEM
) != 0) || (p
->p_ppid
== 0)
354 ||(p
== self
) || (p
->p_stat
== SZOMB
)
355 || (p
->p_shutdownstate
== shutdownstate
)
356 ||((delayterm
== 0) && ((p
->p_lflag
& P_LDELAYTERM
) == P_LDELAYTERM
))) {
364 sd_callback2(proc_t p
, void * args
)
366 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
367 int signo
= sd
->signo
;
368 int setsdstate
= sd
->setsdstate
;
369 int countproc
= sd
->countproc
;
372 p
->p_shutdownstate
= setsdstate
;
373 if (p
->p_stat
!= SZOMB
) {
375 if (countproc
!= 0) {
377 p
->p_listflag
|= P_LIST_EXITCOUNT
;
378 proc_shutdown_exitcount
++;
387 return(PROC_RETURNED
);
392 sd_callback3(proc_t p
, void * args
)
394 struct sd_iterargs
* sd
= (struct sd_iterargs
*)args
;
395 vfs_context_t ctx
= vfs_context_current();
397 int setsdstate
= sd
->setsdstate
;
400 p
->p_shutdownstate
= setsdstate
;
401 if (p
->p_stat
!= SZOMB
) {
403 * NOTE: following code ignores sig_lock and plays
404 * with exit_thread correctly. This is OK unless we
405 * are a multiprocessor, in which case I do not
406 * understand the sig_lock. This needs to be fixed.
409 if (p
->exit_thread
) { /* someone already doing it */
411 /* give him a chance */
412 thread_block(THREAD_CONTINUE_NULL
);
414 p
->exit_thread
= current_thread();
417 sd_log(ctx
, "%s[%d] had to be forced closed with exit1().\n", p
->p_comm
, p
->p_pid
);
420 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC
, BSD_PROC_FRCEXIT
) | DBG_FUNC_NONE
,
421 p
->p_pid
, 0, 1, 0, 0);
423 exit1(p
, 1, (int *)NULL
);
428 return(PROC_RETURNED
);
435 * Shutdown down proc system (release references to current and root
436 * dirs for each process).
438 * POSIX modifications:
440 * For POSIX fcntl() file locking call vno_lockrelease() on
441 * the file to release all of its record locks, if any.
447 vfs_context_t ctx
= vfs_context_current();
448 struct proc
*p
, *self
;
450 struct sd_filterargs sfargs
;
451 struct sd_iterargs sdargs
;
456 * Kill as many procs as we can. (Except ourself...)
458 self
= (struct proc
*)current_proc();
461 * Signal the init with SIGTERM so that he does not launch
465 if (p
&& p
!= self
) {
470 printf("Killing all processes ");
474 * send SIGTERM to those procs interested in catching one
476 sfargs
.delayterm
= delayterm
;
477 sfargs
.shutdownstate
= 0;
478 sdargs
.signo
= SIGTERM
;
479 sdargs
.setsdstate
= 1;
480 sdargs
.countproc
= 1;
481 sdargs
.activecount
= 0;
484 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
485 proc_rebootscan(sd_callback1
, (void *)&sdargs
, sd_filt1
, (void *)&sfargs
);
487 if (sdargs
.activecount
!= 0 && proc_shutdown_exitcount
!= 0) {
489 if (proc_shutdown_exitcount
!= 0) {
491 * now wait for up to 30 seconds to allow those procs catching SIGTERM
493 * as soon as these procs have exited, we'll continue on to the next step
497 error
= msleep(&proc_shutdown_exitcount
, proc_list_mlock
, PWAIT
, "shutdownwait", &ts
);
499 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
500 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
501 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
503 for (p
= zombproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
504 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
505 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
512 if (error
== ETIMEDOUT
) {
514 * log the names of the unresponsive tasks
520 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
521 if (p
->p_shutdownstate
== 1) {
522 printf("%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
523 sd_log(ctx
, "%s[%d]: didn't act on SIGTERM\n", p
->p_comm
, p
->p_pid
);
529 delay_for_interval(1000 * 5, 1000 * 1000);
533 * send a SIGKILL to all the procs still hanging around
535 sfargs
.delayterm
= delayterm
;
536 sfargs
.shutdownstate
= 2;
537 sdargs
.signo
= SIGKILL
;
538 sdargs
.setsdstate
= 2;
539 sdargs
.countproc
= 1;
540 sdargs
.activecount
= 0;
542 /* post a SIGKILL to all that catch SIGTERM and not marked for delay */
543 proc_rebootscan(sd_callback2
, (void *)&sdargs
, sd_filt2
, (void *)&sfargs
);
545 if (sdargs
.activecount
!= 0 && proc_shutdown_exitcount
!= 0) {
547 if (proc_shutdown_exitcount
!= 0) {
549 * wait for up to 60 seconds to allow these procs to exit normally
551 * History: The delay interval was changed from 100 to 200
552 * for NFS requests in particular.
556 error
= msleep(&proc_shutdown_exitcount
, proc_list_mlock
, PWAIT
, "shutdownwait", &ts
);
558 for (p
= allproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
559 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
560 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
562 for (p
= zombproc
.lh_first
; p
; p
= p
->p_list
.le_next
) {
563 if ((p
->p_listflag
& P_LIST_EXITCOUNT
) == P_LIST_EXITCOUNT
)
564 p
->p_listflag
&= ~P_LIST_EXITCOUNT
;
572 * if we still have procs that haven't exited, then brute force 'em
574 sfargs
.delayterm
= delayterm
;
575 sfargs
.shutdownstate
= 3;
577 sdargs
.setsdstate
= 3;
578 sdargs
.countproc
= 0;
579 sdargs
.activecount
= 0;
581 /* post a SIGTERM to all that catch SIGTERM and not marked for delay */
582 proc_rebootscan(sd_callback3
, (void *)&sdargs
, sd_filt2
, (void *)&sfargs
);
585 /* Now start the termination of processes that are marked for delayed termn */
586 if (delayterm
== 0) {
594 * Now that all other processes have been terminated, suspend init
596 task_suspend_internal(initproc
->task
);
598 /* drop the ref on initproc */
600 printf("continuing\n");