]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/kern_shutdown.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / kern / kern_shutdown.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
39 #include <sys/proc_internal.h>
40 #include <sys/user.h>
41 #include <sys/reboot.h>
42 #include <sys/conf.h>
43 #include <sys/vnode_internal.h>
44 #include <sys/file_internal.h>
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>
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>
59
60 #include <security/audit/audit.h>
61
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
71 #include <sys/kdebug.h>
72
73 int system_inshutdown = 0;
74
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);
78
79 int waittime = -1;
80 unsigned int proc_shutdown_exitcount = 0;
81
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);
86
87 extern void IOSystemShutdownNotification(void);
88
89 struct sd_filterargs{
90 int delayterm;
91 int shutdownstate;
92 };
93
94
95 struct sd_iterargs {
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 */
100 };
101
102 static vnode_t sd_logvp = NULLVP;
103 static off_t sd_log_offset = 0;
104
105
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);
111
112 void
113 boot(int paniced, int howto, char *command)
114 {
115 struct proc *p = current_proc(); /* XXX */
116 int hostboot_option=0;
117 int funnel_state;
118
119 system_inshutdown = 1;
120
121 funnel_state = thread_funnel_set(kernel_flock, TRUE);
122
123 /*
124 * Temporary hack to notify the power management root domain
125 * that the system will shut down.
126 */
127 IOSystemShutdownNotification();
128
129 md_prepare_for_shutdown(paniced, howto, command);
130
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) {
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
152 #if CONFIG_AUDIT
153 audit_shutdown();
154 #endif
155
156 if (unmountroot_pre_hook != NULL)
157 unmountroot_pre_hook();
158
159 sync(p, (void *)NULL, (int *)NULL);
160
161 /*
162 * Now that all processes have been terminated and system is
163 * sync'ed up, suspend init
164 */
165
166 if (initproc && p != initproc)
167 task_suspend(initproc->task);
168
169 if (kdebug_enable)
170 kdbg_dump_trace_to_file("/var/log/shutdown/shutdown.trace");
171
172 /*
173 * Unmount filesystems
174 */
175 vfs_unmountall();
176
177 /* Wait for the buffer cache to clean remaining dirty buffers */
178 for (iter = 0; iter < 100; iter++) {
179 nbusy = count_busy_buffers();
180 if (nbusy == 0)
181 break;
182 printf("%d ", nbusy);
183 delay_for_interval( 1 * nbusy, 1000 * 1000);
184 }
185 if (nbusy)
186 printf("giving up\n");
187 else
188 printf("done\n");
189 }
190
191 #if NETWORKING
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();
198 #endif /* NETWORKING */
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
207 if (howto & RB_UPSDELAY) {
208 hostboot_option = HOST_REBOOT_UPSDELAY;
209 }
210
211 host_reboot(host_priv_self(), hostboot_option);
212
213 thread_funnel_set(kernel_flock, FALSE);
214 }
215
216 static int
217 sd_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
238 static int
239 sd_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
250 static void
251 sd_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
277 static int
278 sd_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
297 static int
298 sd_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;
303 int countproc = sd->countproc;
304
305 proc_lock(p);
306 p->p_shutdownstate = setsdstate;
307 if (p->p_stat != SZOMB) {
308 proc_unlock(p);
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
316 psignal(p, signo);
317 if (countproc != 0)
318 sd->activecount++;
319 } else
320 proc_unlock(p);
321 return(PROC_RETURNED);
322 }
323
324 static int
325 sd_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
342 static int
343 sd_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;
348 int countproc = sd->countproc;
349
350 proc_lock(p);
351 p->p_shutdownstate = setsdstate;
352 if (p->p_stat != SZOMB) {
353 proc_unlock(p);
354 if (countproc != 0) {
355 proc_list_lock();
356 p->p_listflag |= P_LIST_EXITCOUNT;
357 proc_shutdown_exitcount++;
358 proc_list_unlock();
359 }
360 psignal(p, signo);
361 if (countproc != 0)
362 sd->activecount++;
363 } else
364 proc_unlock(p);
365
366 return(PROC_RETURNED);
367
368 }
369
370 static int
371 sd_callback3(proc_t p, void * args)
372 {
373 struct sd_iterargs * sd = (struct sd_iterargs *)args;
374 vfs_context_t ctx = vfs_context_current();
375
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(".");
395
396 sd_log(ctx, "%s[%d] had to be forced closed with exit1().\n", p->p_comm, p->p_pid);
397
398 proc_unlock(p);
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++;
402 exit1(p, 1, (int *)NULL);
403 }
404 } else
405 proc_unlock(p);
406
407 return(PROC_RETURNED);
408 }
409
410
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
423 static void
424 proc_shutdown(void)
425 {
426 vfs_context_t ctx = vfs_context_current();
427 struct proc *p, *self;
428 int delayterm = 0;
429 struct sd_filterargs sfargs;
430 struct sd_iterargs sdargs;
431 int error = 0;
432 struct timespec ts;
433
434 /*
435 * Kill as many procs as we can. (Except ourself...)
436 */
437 self = (struct proc *)current_proc();
438
439 /*
440 * Signal the init with SIGTERM so that he does not launch
441 * new processes
442 */
443 p = proc_find(1);
444 if (p && p != self) {
445 psignal(p, SIGTERM);
446 }
447 proc_rele(p);
448
449 printf("Killing all processes ");
450
451 sigterm_loop:
452 /*
453 * send SIGTERM to those procs interested in catching one
454 */
455 sfargs.delayterm = delayterm;
456 sfargs.shutdownstate = 0;
457 sdargs.signo = SIGTERM;
458 sdargs.setsdstate = 1;
459 sdargs.countproc = 1;
460 sdargs.activecount = 0;
461
462 error = 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);
465
466 if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) {
467 proc_list_lock();
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 }
486 }
487
488 }
489 proc_list_unlock();
490 }
491 if (error == ETIMEDOUT) {
492 /*
493 * log the names of the unresponsive tasks
494 */
495
496
497 proc_list_lock();
498
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);
503 }
504 }
505
506 proc_list_unlock();
507
508 delay_for_interval(1000 * 5, 1000 * 1000);
509 }
510
511 /*
512 * send a SIGKILL to all the procs still hanging around
513 */
514 sfargs.delayterm = delayterm;
515 sfargs.shutdownstate = 2;
516 sdargs.signo = SIGKILL;
517 sdargs.setsdstate = 2;
518 sdargs.countproc = 1;
519 sdargs.activecount = 0;
520
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);
523
524 if (sdargs.activecount != 0 && proc_shutdown_exitcount!= 0) {
525 proc_list_lock();
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 }
546 }
547 proc_list_unlock();
548 }
549
550 /*
551 * if we still have procs that haven't exited, then brute force 'em
552 */
553 sfargs.delayterm = delayterm;
554 sfargs.shutdownstate = 3;
555 sdargs.signo = 0;
556 sdargs.setsdstate = 3;
557 sdargs.countproc = 0;
558 sdargs.activecount = 0;
559
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");
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;
568 }
569
570 sd_closelog(ctx);
571
572 /* drop the ref on initproc */
573 proc_rele(initproc);
574 printf("continuing\n");
575 }
576