]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/kern_shutdown.c
xnu-1504.9.17.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * File: bsd/kern/kern_shutdown.c
30 *
31 * Copyright (C) 1989, NeXT, Inc.
32 *
33 */
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/vm.h>
91447636 39#include <sys/proc_internal.h>
1c79356b 40#include <sys/user.h>
1c79356b
A
41#include <sys/reboot.h>
42#include <sys/conf.h>
91447636
A
43#include <sys/vnode_internal.h>
44#include <sys/file_internal.h>
1c79356b
A
45#include <sys/clist.h>
46#include <sys/callout.h>
47#include <sys/mbuf.h>
48#include <sys/msgbuf.h>
49#include <sys/ioctl.h>
50#include <sys/signal.h>
51#include <sys/tty.h>
52#include <kern/task.h>
9bccf70c 53#include <sys/quota.h>
1c79356b
A
54#include <vm/vm_kern.h>
55#include <mach/vm_param.h>
56#include <sys/filedesc.h>
91447636 57#include <mach/host_priv.h>
1c79356b 58#include <mach/host_reboot.h>
91447636 59
b0d623f7 60#include <security/audit/audit.h>
1c79356b 61
2d21ac55
A
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
b0d623f7
A
71#include <sys/kdebug.h>
72
73int system_inshutdown = 0;
74
2d21ac55
A
75/* XXX should be in a header file somewhere, but isn't */
76extern void md_prepare_for_shutdown(int, int, char *);
b0d623f7 77extern void (*unmountroot_pre_hook)(void);
2d21ac55 78
1c79356b 79int waittime = -1;
b0d623f7 80unsigned int proc_shutdown_exitcount = 0;
2d21ac55 81
b0d623f7
A
82static int sd_openlog(vfs_context_t);
83static int sd_closelog(vfs_context_t);
84static void sd_log(vfs_context_t, const char *, ...);
2d21ac55 85static void proc_shutdown(void);
2d21ac55
A
86
87extern void IOSystemShutdownNotification(void);
88
89struct sd_filterargs{
90 int delayterm;
91 int shutdownstate;
92};
93
94
95struct sd_iterargs {
b0d623f7
A
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 */
2d21ac55
A
100};
101
b0d623f7
A
102static vnode_t sd_logvp = NULLVP;
103static off_t sd_log_offset = 0;
104
105
2d21ac55
A
106static int sd_filt1(proc_t, void *);
107static int sd_filt2(proc_t, void *);
108static int sd_callback1(proc_t p, void * arg);
109static int sd_callback2(proc_t p, void * arg);
110static int sd_callback3(proc_t p, void * arg);
1c79356b
A
111
112void
2d21ac55 113boot(int paniced, int howto, char *command)
1c79356b 114{
1c79356b
A
115 struct proc *p = current_proc(); /* XXX */
116 int hostboot_option=0;
117 int funnel_state;
1c79356b 118
b0d623f7
A
119 system_inshutdown = 1;
120
1c79356b
A
121 funnel_state = thread_funnel_set(kernel_flock, TRUE);
122
2d21ac55
A
123 /*
124 * Temporary hack to notify the power management root domain
125 * that the system will shut down.
126 */
127 IOSystemShutdownNotification();
128
0b4e3aa0 129 md_prepare_for_shutdown(paniced, howto, command);
1c79356b 130
b0d623f7
A
131 if ((howto&RB_QUICK)==RB_QUICK && waittime < 0) {
132 waittime = 0;
133 printf("Quick reboot...\n");
134 if ((howto&RB_NOSYNC)==0) {
135 sync(p, (void *)NULL, (int *)NULL);
136 }
137 }
138 else if ((howto&RB_NOSYNC)==0 && waittime < 0) {
1c79356b
A
139 int iter, nbusy;
140
141 waittime = 0;
142
143 printf("syncing disks... ");
144
145 /*
146 * Release vnodes held by texts before sync.
147 */
148
149 /* handle live procs (deallocate their root and current directories). */
150 proc_shutdown();
151
b0d623f7 152#if CONFIG_AUDIT
2d21ac55
A
153 audit_shutdown();
154#endif
55e303ae 155
b0d623f7
A
156 if (unmountroot_pre_hook != NULL)
157 unmountroot_pre_hook();
158
1c79356b
A
159 sync(p, (void *)NULL, (int *)NULL);
160
91447636 161 /*
2d21ac55
A
162 * Now that all processes have been terminated and system is
163 * sync'ed up, suspend init
91447636 164 */
1c79356b 165
2d21ac55
A
166 if (initproc && p != initproc)
167 task_suspend(initproc->task);
1c79356b 168
b0d623f7
A
169 if (kdebug_enable)
170 kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace");
171
1c79356b
A
172 /*
173 * Unmount filesystems
174 */
91447636 175 vfs_unmountall();
1c79356b
A
176
177 /* Wait for the buffer cache to clean remaining dirty buffers */
91447636 178 for (iter = 0; iter < 100; iter++) {
1c79356b
A
179 nbusy = count_busy_buffers();
180 if (nbusy == 0)
181 break;
182 printf("%d ", nbusy);
2d21ac55 183 delay_for_interval( 1 * nbusy, 1000 * 1000);
1c79356b
A
184 }
185 if (nbusy)
186 printf("giving up\n");
187 else
188 printf("done\n");
189 }
190
2d21ac55 191#if NETWORKING
1c79356b
A
192 /*
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.
196 */
197 if_down_all();
2d21ac55 198#endif /* NETWORKING */
1c79356b
A
199
200 if (howto & RB_POWERDOWN)
201 hostboot_option = HOST_REBOOT_HALT;
202 if (howto & RB_HALT)
203 hostboot_option = HOST_REBOOT_HALT;
204 if (paniced == RB_PANIC)
205 hostboot_option = HOST_REBOOT_HALT;
206
0c530ab8
A
207 if (howto & RB_UPSDELAY) {
208 hostboot_option = HOST_REBOOT_UPSDELAY;
209 }
210
1c79356b
A
211 host_reboot(host_priv_self(), hostboot_option);
212
213 thread_funnel_set(kernel_flock, FALSE);
214}
215
b0d623f7
A
216static int
217sd_openlog(vfs_context_t ctx)
218{
219 int error = 0;
220 struct timeval tv;
221
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);
225 sd_logvp = NULLVP;
226 return error;
227 }
228
229 vnode_setsize(sd_logvp, (off_t)0, 0, ctx);
230
231 /* Write a little header */
232 microtime(&tv);
233 sd_log(ctx, "Process shutdown log. Current time is %lu (in seconds).\n\n", tv.tv_sec);
234
235 return 0;
236}
237
238static int
239sd_closelog(vfs_context_t ctx)
240{
241 int error = 0;
242 if (sd_logvp != NULLVP) {
243 VNOP_FSYNC(sd_logvp, MNT_WAIT, ctx);
244 error = vnode_close(sd_logvp, FWRITE, ctx);
245 }
246
247 return error;
248}
249
250static void
251sd_log(vfs_context_t ctx, const char *fmt, ...)
252{
253 int resid, log_error, len;
254 char logbuf[100];
255 va_list arglist;
256
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 */
261 return;
262 }
263 }
264
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);
271 }
272
273 va_end(arglist);
274
275}
276
2d21ac55
A
277static int
278sd_filt1(proc_t p, void * args)
279{
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;
284
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)) {
290 return(0);
291 }
292 else
293 return(1);
294}
295
296
297static int
298sd_callback1(proc_t p, void * args)
299{
300 struct sd_iterargs * sd = (struct sd_iterargs *)args;
301 int signo = sd->signo;
302 int setsdstate = sd->setsdstate;
b0d623f7 303 int countproc = sd->countproc;
2d21ac55
A
304
305 proc_lock(p);
306 p->p_shutdownstate = setsdstate;
307 if (p->p_stat != SZOMB) {
308 proc_unlock(p);
b0d623f7
A
309 if (countproc != 0) {
310 proc_list_lock();
311 p->p_listflag |= P_LIST_EXITCOUNT;
312 proc_shutdown_exitcount++;
313 proc_list_unlock();
314 }
315
2d21ac55 316 psignal(p, signo);
b0d623f7
A
317 if (countproc != 0)
318 sd->activecount++;
2d21ac55
A
319 } else
320 proc_unlock(p);
321 return(PROC_RETURNED);
2d21ac55
A
322}
323
324static int
325sd_filt2(proc_t p, void * args)
326{
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;
331
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))) {
336 return(0);
337 }
338 else
339 return(1);
340}
341
342static int
343sd_callback2(proc_t p, void * args)
344{
345 struct sd_iterargs * sd = (struct sd_iterargs *)args;
346 int signo = sd->signo;
347 int setsdstate = sd->setsdstate;
b0d623f7 348 int countproc = sd->countproc;
2d21ac55
A
349
350 proc_lock(p);
351 p->p_shutdownstate = setsdstate;
352 if (p->p_stat != SZOMB) {
353 proc_unlock(p);
b0d623f7
A
354 if (countproc != 0) {
355 proc_list_lock();
356 p->p_listflag |= P_LIST_EXITCOUNT;
357 proc_shutdown_exitcount++;
358 proc_list_unlock();
359 }
2d21ac55 360 psignal(p, signo);
b0d623f7
A
361 if (countproc != 0)
362 sd->activecount++;
2d21ac55
A
363 } else
364 proc_unlock(p);
365
366 return(PROC_RETURNED);
367
368}
369
370static int
371sd_callback3(proc_t p, void * args)
372{
373 struct sd_iterargs * sd = (struct sd_iterargs *)args;
b0d623f7
A
374 vfs_context_t ctx = vfs_context_current();
375
2d21ac55
A
376 int setsdstate = sd->setsdstate;
377
378 proc_lock(p);
379 p->p_shutdownstate = setsdstate;
380 if (p->p_stat != SZOMB) {
381 /*
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.
386 * XXX
387 */
388 if (p->exit_thread) { /* someone already doing it */
389 proc_unlock(p);
390 /* give him a chance */
391 thread_block(THREAD_CONTINUE_NULL);
392 } else {
393 p->exit_thread = current_thread();
394 printf(".");
b0d623f7
A
395
396 sd_log(ctx, "%s[%d] had to be forced closed with exit1().\n", p->p_comm, p->p_pid);
397
2d21ac55 398 proc_unlock(p);
b0d623f7
A
399 KERNEL_DEBUG_CONSTANT(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_FRCEXIT) | DBG_FUNC_NONE,
400 p->p_pid, 0, 1, 0, 0);
401 sd->activecount++;
2d21ac55
A
402 exit1(p, 1, (int *)NULL);
403 }
404 } else
405 proc_unlock(p);
406
407 return(PROC_RETURNED);
408}
409
410
1c79356b
A
411/*
412 * proc_shutdown()
413 *
414 * Shutdown down proc system (release references to current and root
415 * dirs for each process).
416 *
417 * POSIX modifications:
418 *
419 * For POSIX fcntl() file locking call vno_lockrelease() on
420 * the file to release all of its record locks, if any.
421 */
422
423static void
2d21ac55 424proc_shutdown(void)
1c79356b 425{
b0d623f7
A
426 vfs_context_t ctx = vfs_context_current();
427 struct proc *p, *self;
91447636 428 int delayterm = 0;
2d21ac55
A
429 struct sd_filterargs sfargs;
430 struct sd_iterargs sdargs;
b0d623f7
A
431 int error = 0;
432 struct timespec ts;
1c79356b
A
433
434 /*
435 * Kill as many procs as we can. (Except ourself...)
436 */
0b4e3aa0 437 self = (struct proc *)current_proc();
1c79356b
A
438
439 /*
91447636
A
440 * Signal the init with SIGTERM so that he does not launch
441 * new processes
1c79356b 442 */
2d21ac55 443 p = proc_find(1);
91447636
A
444 if (p && p != self) {
445 psignal(p, SIGTERM);
446 }
2d21ac55 447 proc_rele(p);
1c79356b 448
1c79356b
A
449 printf("Killing all processes ");
450
2d21ac55 451sigterm_loop:
1c79356b
A
452 /*
453 * send SIGTERM to those procs interested in catching one
454 */
2d21ac55
A
455 sfargs.delayterm = delayterm;
456 sfargs.shutdownstate = 0;
457 sdargs.signo = SIGTERM;
458 sdargs.setsdstate = 1;
b0d623f7
A
459 sdargs.countproc = 1;
460 sdargs.activecount = 0;
2d21ac55 461
b0d623f7 462 error = 0;
2d21ac55
A
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);
91447636 465
b0d623f7 466 if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) {
2d21ac55 467 proc_list_lock();
b0d623f7
A
468 if (proc_shutdown_exitcount != 0) {
469 /*
470 * now wait for up to 30 seconds to allow those procs catching SIGTERM
471 * to digest it
472 * as soon as these procs have exited, we'll continue on to the next step
473 */
474 ts.tv_sec = 30;
475 ts.tv_nsec = 0;
476 error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts);
477 if (error != 0) {
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;
481 }
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;
485 }
91447636 486 }
b0d623f7 487
1c79356b 488 }
2d21ac55 489 proc_list_unlock();
1c79356b 490 }
b0d623f7 491 if (error == ETIMEDOUT) {
91447636 492 /*
55e303ae
A
493 * log the names of the unresponsive tasks
494 */
91447636 495
2d21ac55
A
496
497 proc_list_lock();
498
b0d623f7 499 for (p = allproc.lh_first; p; p = p->p_list.le_next) {
91447636 500 if (p->p_shutdownstate == 1) {
b0d623f7
A
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);
91447636 503 }
55e303ae 504 }
2d21ac55
A
505
506 proc_list_unlock();
507
508 delay_for_interval(1000 * 5, 1000 * 1000);
55e303ae 509 }
1c79356b
A
510
511 /*
512 * send a SIGKILL to all the procs still hanging around
513 */
2d21ac55
A
514 sfargs.delayterm = delayterm;
515 sfargs.shutdownstate = 2;
516 sdargs.signo = SIGKILL;
517 sdargs.setsdstate = 2;
b0d623f7
A
518 sdargs.countproc = 1;
519 sdargs.activecount = 0;
2d21ac55 520
b0d623f7 521 /* post a SIGKILL to all that catch SIGTERM and not marked for delay */
2d21ac55 522 proc_rebootscan(sd_callback2, (void *)&sdargs, sd_filt2, (void *)&sfargs);
91447636 523
b0d623f7 524 if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) {
2d21ac55 525 proc_list_lock();
b0d623f7
A
526 if (proc_shutdown_exitcount != 0) {
527 /*
528 * wait for up to 60 seconds to allow these procs to exit normally
529 *
530 * History: The delay interval was changed from 100 to 200
531 * for NFS requests in particular.
532 */
533 ts.tv_sec = 60;
534 ts.tv_nsec = 0;
535 error = msleep(&proc_shutdown_exitcount, proc_list_mlock, PWAIT, "shutdownwait", &ts);
536 if (error != 0) {
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;
540 }
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;
544 }
545 }
1c79356b 546 }
2d21ac55 547 proc_list_unlock();
1c79356b
A
548 }
549
550 /*
551 * if we still have procs that haven't exited, then brute force 'em
552 */
2d21ac55
A
553 sfargs.delayterm = delayterm;
554 sfargs.shutdownstate = 3;
555 sdargs.signo = 0;
556 sdargs.setsdstate = 3;
b0d623f7
A
557 sdargs.countproc = 0;
558 sdargs.activecount = 0;
4a249263 559
2d21ac55
A
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);
562 printf("\n");
91447636
A
563
564 /* Now start the termination of processes that are marked for delayed termn */
565 if (delayterm == 0) {
566 delayterm = 1;
567 goto sigterm_loop;
1c79356b 568 }
b0d623f7
A
569
570 sd_closelog(ctx);
571
2d21ac55
A
572 /* drop the ref on initproc */
573 proc_rele(initproc);
1c79356b
A
574 printf("continuing\n");
575}
576