xref: /original-bsd/sys/kern/kern_sig.c (revision 9a77813a)
1 /*
2  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  *	@(#)kern_sig.c	7.7 (Berkeley) 05/05/89
18  */
19 
20 #include "param.h"
21 #include "systm.h"
22 #include "user.h"
23 #include "vnode.h"
24 #include "proc.h"
25 #include "timeb.h"
26 #include "times.h"
27 #include "buf.h"
28 #include "mount.h"
29 #include "text.h"
30 #include "seg.h"
31 #include "vm.h"
32 #include "acct.h"
33 #include "uio.h"
34 #include "file.h"
35 #include "kernel.h"
36 
37 #include "machine/reg.h"
38 #include "machine/pte.h"
39 #include "machine/psl.h"
40 #include "machine/mtpr.h"
41 
42 #define	cantmask	(sigmask(SIGKILL)|sigmask(SIGCONT)|sigmask(SIGSTOP))
43 #define	stopsigmask	(sigmask(SIGSTOP)|sigmask(SIGTSTP)| \
44 			sigmask(SIGTTIN)|sigmask(SIGTTOU))
45 
46 /*
47  * Generalized interface signal handler.
48  */
49 sigvec()
50 {
51 	register struct a {
52 		int	signo;
53 		struct	sigvec *nsv;
54 		struct	sigvec *osv;
55 	} *uap = (struct a  *)u.u_ap;
56 	struct sigvec vec;
57 	register struct sigvec *sv;
58 	register int sig;
59 	int bit;
60 
61 	sig = uap->signo;
62 	if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP) {
63 		u.u_error = EINVAL;
64 		return;
65 	}
66 	sv = &vec;
67 	if (uap->osv) {
68 		sv->sv_handler = u.u_signal[sig];
69 		sv->sv_mask = u.u_sigmask[sig];
70 		bit = sigmask(sig);
71 		sv->sv_flags = 0;
72 		if ((u.u_sigonstack & bit) != 0)
73 			sv->sv_flags |= SV_ONSTACK;
74 		if ((u.u_sigintr & bit) != 0)
75 			sv->sv_flags |= SV_INTERRUPT;
76 		u.u_error =
77 		    copyout((caddr_t)sv, (caddr_t)uap->osv, sizeof (vec));
78 		if (u.u_error)
79 			return;
80 	}
81 	if (uap->nsv) {
82 		u.u_error =
83 		    copyin((caddr_t)uap->nsv, (caddr_t)sv, sizeof (vec));
84 		if (u.u_error)
85 			return;
86 		if (sig == SIGCONT && sv->sv_handler == SIG_IGN) {
87 			u.u_error = EINVAL;
88 			return;
89 		}
90 		setsigvec(sig, sv);
91 	}
92 }
93 
94 setsigvec(sig, sv)
95 	int sig;
96 	register struct sigvec *sv;
97 {
98 	register struct proc *p;
99 	register int bit;
100 
101 	bit = sigmask(sig);
102 	p = u.u_procp;
103 	/*
104 	 * Change setting atomically.
105 	 */
106 	(void) splhigh();
107 	u.u_signal[sig] = sv->sv_handler;
108 	u.u_sigmask[sig] = sv->sv_mask &~ cantmask;
109 	if (sv->sv_flags & SV_INTERRUPT)
110 		u.u_sigintr |= bit;
111 	else
112 		u.u_sigintr &= ~bit;
113 	if (sv->sv_flags & SV_ONSTACK)
114 		u.u_sigonstack |= bit;
115 	else
116 		u.u_sigonstack &= ~bit;
117 	if (sv->sv_handler == SIG_IGN) {
118 		p->p_sig &= ~bit;		/* never to be seen again */
119 		p->p_sigignore |= bit;
120 		p->p_sigcatch &= ~bit;
121 	} else {
122 		p->p_sigignore &= ~bit;
123 		if (sv->sv_handler == SIG_DFL)
124 			p->p_sigcatch &= ~bit;
125 		else
126 			p->p_sigcatch |= bit;
127 	}
128 	(void) spl0();
129 }
130 
131 sigblock()
132 {
133 	struct a {
134 		int	mask;
135 	} *uap = (struct a *)u.u_ap;
136 	register struct proc *p = u.u_procp;
137 
138 	(void) splhigh();
139 	u.u_r.r_val1 = p->p_sigmask;
140 	p->p_sigmask |= uap->mask &~ cantmask;
141 	(void) spl0();
142 }
143 
144 sigsetmask()
145 {
146 	struct a {
147 		int	mask;
148 	} *uap = (struct a *)u.u_ap;
149 	register struct proc *p = u.u_procp;
150 
151 	(void) splhigh();
152 	u.u_r.r_val1 = p->p_sigmask;
153 	p->p_sigmask = uap->mask &~ cantmask;
154 	(void) spl0();
155 }
156 
157 sigpause()
158 {
159 	struct a {
160 		int	mask;
161 	} *uap = (struct a *)u.u_ap;
162 	register struct proc *p = u.u_procp;
163 
164 	/*
165 	 * When returning from sigpause, we want
166 	 * the old mask to be restored after the
167 	 * signal handler has finished.  Thus, we
168 	 * save it here and mark the proc structure
169 	 * to indicate this (should be in u.).
170 	 */
171 	u.u_oldmask = p->p_sigmask;
172 	p->p_flag |= SOMASK;
173 	p->p_sigmask = uap->mask &~ cantmask;
174 	for (;;)
175 		sleep((caddr_t)&u, PSLEP);
176 	/*NOTREACHED*/
177 }
178 #undef cantmask
179 
180 sigstack()
181 {
182 	register struct a {
183 		struct	sigstack *nss;
184 		struct	sigstack *oss;
185 	} *uap = (struct a *)u.u_ap;
186 	struct sigstack ss;
187 
188 	if (uap->oss) {
189 		u.u_error = copyout((caddr_t)&u.u_sigstack, (caddr_t)uap->oss,
190 		    sizeof (struct sigstack));
191 		if (u.u_error)
192 			return;
193 	}
194 	if (uap->nss) {
195 		u.u_error =
196 		    copyin((caddr_t)uap->nss, (caddr_t)&ss, sizeof (ss));
197 		if (u.u_error == 0)
198 			u.u_sigstack = ss;
199 	}
200 }
201 
202 kill()
203 {
204 	register struct a {
205 		int	pid;
206 		int	signo;
207 	} *uap = (struct a *)u.u_ap;
208 	register struct proc *p;
209 
210 	if (uap->signo < 0 || uap->signo > NSIG) {
211 		u.u_error = EINVAL;
212 		return;
213 	}
214 	if (uap->pid > 0) {
215 		/* kill single process */
216 		p = pfind(uap->pid);
217 		if (p == 0) {
218 			u.u_error = ESRCH;
219 			return;
220 		}
221 		if (u.u_uid && u.u_uid != p->p_uid)
222 			u.u_error = EPERM;
223 		else if (uap->signo)
224 			psignal(p, uap->signo);
225 		return;
226 	}
227 	switch (uap->pid) {
228 	case -1:		/* broadcast signal */
229 		u.u_error = killpg1(uap->signo, 0, 1);
230 		break;
231 	case 0:			/* signal own process group */
232 		u.u_error = killpg1(uap->signo, 0, 0);
233 		break;
234 	default:		/* negative explicit process group */
235 		u.u_error = killpg1(uap->signo, -uap->pid, 0);
236 		break;
237 	}
238 	return;
239 }
240 
241 killpg()
242 {
243 	register struct a {
244 		int	pgid;
245 		int	signo;
246 	} *uap = (struct a *)u.u_ap;
247 
248 	if (uap->signo < 0 || uap->signo > NSIG) {
249 		u.u_error = EINVAL;
250 		return;
251 	}
252 	u.u_error = killpg1(uap->signo, uap->pgid, 0);
253 }
254 
255 /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
256 
257 killpg1(signo, pgid, all)
258 	int signo, pgid, all;
259 {
260 	register struct proc *p;
261 	struct pgrp *pgrp;
262 	int f = 0, error = 0;
263 
264 
265 	if (all)
266 		/*
267 		 * broadcast
268 		 */
269 		for (p = allproc; p != NULL; p = p->p_nxt) {
270 			if (p->p_ppid == 0 || p->p_flag&SSYS ||
271 			    p == u.u_procp ||
272 			   (u.u_uid && u.u_uid != p->p_uid &&
273 			   !(signo == SIGCONT && inferior(p))))
274 				continue;
275 			f++;
276 			if (signo)
277 				psignal(p, signo);
278 		}
279 	else {
280 		if (pgid == 0)
281 			/*
282 			 * zero pgid means send to my process group.
283 			 */
284 			pgrp = u.u_procp->p_pgrp;
285 		else {
286 			pgrp = pgfind(pgid);
287 			if (pgrp == NULL)
288 				return(ESRCH);
289 		}
290 		if (!(pgrp->pg_jobc) &&
291 		     (signo==SIGTTIN || signo==SIGTTOU || signo==SIGTSTP))
292 			return(EPERM);
293 		for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt) {
294 			if (p->p_ppid == 0 || p->p_flag&SSYS)
295 				continue;
296 			if (u.u_uid && u.u_uid != p->p_uid &&
297 			   !(signo == SIGCONT && inferior(p))) {
298 				error = EPERM;
299 				continue;
300 			}
301 			f++;
302 			if (signo)
303 				psignal(p, signo);
304 		}
305 	}
306 	return (error ? error : (f == 0 ? ESRCH : 0));
307 }
308 
309 /*
310  * Send the specified signal to
311  * all processes with 'pgid' as
312  * process group.
313  */
314 gsignal(pgid, sig)
315 {
316 	register struct pgrp *pgrp;
317 	register struct proc *p;
318 
319 	if (!pgid)
320 		return;
321 	if ((pgrp = pgfind(pgid)) == NULL)
322 		return;
323 	pgsignal(pgrp, sig);
324 }
325 
326 pgsignal(pgrp, sig)
327 	register struct pgrp *pgrp;
328 {
329 	register struct proc *p;
330 
331 	if (!(pgrp->pg_jobc) &&
332 	     (sig==SIGTTIN || sig==SIGTTOU || sig==SIGTSTP))
333 		return;
334 	for (p = pgrp->pg_mem; p != NULL; p = p->p_pgrpnxt)
335 		psignal(p, sig);
336 }
337 
338 /*
339  * Send the specified signal to
340  * the specified process.
341  */
342 psignal(p, sig)
343 	register struct proc *p;
344 	register int sig;
345 {
346 	register int s;
347 	register int (*action)();
348 	int mask;
349 
350 	if ((unsigned)sig >= NSIG)
351 		return;
352 	mask = sigmask(sig);
353 
354 	/*
355 	 * If proc is traced, always give parent a chance.
356 	 */
357 	if (p->p_flag & STRC)
358 		action = SIG_DFL;
359 	else {
360 		/*
361 		 * If the signal is being ignored,
362 		 * then we forget about it immediately.
363 		 */
364 		if (p->p_sigignore & mask)
365 			return;
366 		if (p->p_sigmask & mask)
367 			action = SIG_HOLD;
368 		else if (p->p_sigcatch & mask)
369 			action = SIG_CATCH;
370 		else
371 			action = SIG_DFL;
372 	}
373 	if (sig) {
374 		switch (sig) {
375 
376 		case SIGTERM:
377 			if ((p->p_flag&STRC) || action != SIG_DFL)
378 				break;
379 			/* fall into ... */
380 
381 		case SIGKILL:
382 			if (p->p_nice > NZERO)
383 				p->p_nice = NZERO;
384 			break;
385 
386 		case SIGCONT:
387 			p->p_sig &= ~stopsigmask;
388 			break;
389 
390 		case SIGTSTP:
391 		case SIGTTIN:
392 		case SIGTTOU:
393 			/*FALLTHROUGH*/
394 		case SIGSTOP:
395 			p->p_sig &= ~sigmask(SIGCONT);
396 			break;
397 		}
398 		p->p_sig |= mask;
399 	}
400 	/*
401 	 * Defer further processing for signals which are held.
402 	 */
403 	if (action == SIG_HOLD)
404 		return;
405 	s = splhigh();
406 	switch (p->p_stat) {
407 
408 	case SSLEEP:
409 		/*
410 		 * If process is sleeping at negative priority
411 		 * we can't interrupt the sleep... the signal will
412 		 * be noticed when the process returns through
413 		 * trap() or syscall().
414 		 */
415 		if (p->p_pri <= PZERO)
416 			goto out;
417 		/*
418 		 * Process is sleeping and traced... make it runnable
419 		 * so it can discover the signal in issig() and stop
420 		 * for the parent.
421 		 */
422 		if (p->p_flag&STRC)
423 			goto run;
424 		switch (sig) {
425 
426 		case SIGSTOP:
427 		case SIGTSTP:
428 		case SIGTTIN:
429 		case SIGTTOU:
430 			/*
431 			 * These are the signals which by default
432 			 * stop a process.
433 			 */
434 			if (action != SIG_DFL)
435 				goto run;
436 			/*
437 			 * If a child in vfork(), stopping could
438 			 * cause deadlock.
439 			 */
440 			if (p->p_flag&SVFORK)
441 				goto out;
442 			p->p_sig &= ~mask;
443 			p->p_cursig = sig;
444 			psignal(p->p_pptr, SIGCHLD);
445 			stop(p);
446 			goto out;
447 
448 		case SIGIO:
449 		case SIGURG:
450 		case SIGCHLD:
451 		case SIGWINCH:
452 			/*
453 			 * These signals are special in that they
454 			 * don't get propogated... if the process
455 			 * isn't interested, forget it.
456 			 */
457 			if (action != SIG_DFL)
458 				goto run;
459 			p->p_sig &= ~mask;		/* take it away */
460 			goto out;
461 
462 		default:
463 			/*
464 			 * All other signals cause the process to run
465 			 */
466 			goto run;
467 		}
468 		/*NOTREACHED*/
469 
470 	case SSTOP:
471 		/*
472 		 * If traced process is already stopped,
473 		 * then no further action is necessary.
474 		 */
475 		if (p->p_flag&STRC)
476 			goto out;
477 		switch (sig) {
478 
479 		case SIGKILL:
480 			/*
481 			 * Kill signal always sets processes running.
482 			 */
483 			goto run;
484 
485 		case SIGCONT:
486 			/*
487 			 * If the process catches SIGCONT, let it handle
488 			 * the signal itself.  If it isn't waiting on
489 			 * an event, then it goes back to run state.
490 			 * Otherwise, process goes back to sleep state.
491 			 */
492 			if (action != SIG_DFL || p->p_wchan == 0)
493 				goto run;
494 			p->p_stat = SSLEEP;
495 			goto out;
496 
497 		case SIGSTOP:
498 		case SIGTSTP:
499 		case SIGTTIN:
500 		case SIGTTOU:
501 			/*
502 			 * Already stopped, don't need to stop again.
503 			 * (If we did the shell could get confused.)
504 			 */
505 			p->p_sig &= ~mask;		/* take it away */
506 			goto out;
507 
508 		default:
509 			/*
510 			 * If process is sleeping interruptibly, then
511 			 * unstick it so that when it is continued
512 			 * it can look at the signal.
513 			 * But don't setrun the process as its not to
514 			 * be unstopped by the signal alone.
515 			 */
516 			if (p->p_wchan && p->p_pri > PZERO)
517 				unsleep(p);
518 			goto out;
519 		}
520 		/*NOTREACHED*/
521 
522 	default:
523 		/*
524 		 * SRUN, SIDL, SZOMB do nothing with the signal,
525 		 * other than kicking ourselves if we are running.
526 		 * It will either never be noticed, or noticed very soon.
527 		 */
528 		if (p == u.u_procp && !noproc)
529 			aston();
530 		goto out;
531 	}
532 	/*NOTREACHED*/
533 run:
534 	/*
535 	 * Raise priority to at least PUSER.
536 	 */
537 	if (p->p_pri > PUSER)
538 		p->p_pri = PUSER;
539 	setrun(p);
540 out:
541 	splx(s);
542 }
543 
544 /*
545  * Returns true if the current
546  * process has a signal to process.
547  * The signal to process is put in p_cursig.
548  * This is asked at least once each time a process enters the
549  * system (though this can usually be done without actually
550  * calling issig by checking the pending signal masks.)
551  * A signal does not do anything
552  * directly to a process; it sets
553  * a flag that asks the process to
554  * do something to itself.
555  */
556 issig()
557 {
558 	register struct proc *p;
559 	register int sig;
560 	int sigbits, mask;
561 
562 	p = u.u_procp;
563 	for (;;) {
564 		sigbits = p->p_sig &~ p->p_sigmask;
565 		if ((p->p_flag&STRC) == 0)
566 			sigbits &= ~p->p_sigignore;
567 		if (p->p_flag&SVFORK)
568 			sigbits &= ~stopsigmask;
569 		if (sigbits == 0)
570 			break;
571 		sig = ffs((long)sigbits);
572 		mask = sigmask(sig);
573 		p->p_sig &= ~mask;		/* take the signal! */
574 		p->p_cursig = sig;
575 		if (p->p_flag&STRC && (p->p_flag&SVFORK) == 0) {
576 			/*
577 			 * If traced, always stop, and stay
578 			 * stopped until released by the parent.
579 			 */
580 			psignal(p->p_pptr, SIGCHLD);
581 			do {
582 				stop(p);
583 				swtch();
584 			} while (!procxmt() && p->p_flag&STRC);
585 
586 			/*
587 			 * If the traced bit got turned off,
588 			 * then put the signal taken above back into p_sig
589 			 * and go back up to the top to rescan signals.
590 			 * This ensures that p_sig* and u_signal are consistent.
591 			 */
592 			if ((p->p_flag&STRC) == 0) {
593 				p->p_sig |= mask;
594 				continue;
595 			}
596 
597 			/*
598 			 * If parent wants us to take the signal,
599 			 * then it will leave it in p->p_cursig;
600 			 * otherwise we just look for signals again.
601 			 */
602 			sig = p->p_cursig;
603 			if (sig == 0)
604 				continue;
605 
606 			/*
607 			 * If signal is being masked put it back
608 			 * into p_sig and look for other signals.
609 			 */
610 			mask = sigmask(sig);
611 			if (p->p_sigmask & mask) {
612 				p->p_sig |= mask;
613 				continue;
614 			}
615 		}
616 		switch ((int)u.u_signal[sig]) {
617 
618 		case SIG_DFL:
619 			/*
620 			 * Don't take default actions on system processes.
621 			 */
622 			if (p->p_ppid == 0)
623 				break;
624 			switch (sig) {
625 
626 			case SIGTSTP:
627 			case SIGTTIN:
628 			case SIGTTOU:
629 			case SIGSTOP:
630 				if (p->p_flag&STRC)
631 					continue;
632 				psignal(p->p_pptr, SIGCHLD);
633 				stop(p);
634 				swtch();
635 				continue;
636 
637 			case SIGCONT:
638 			case SIGCHLD:
639 			case SIGURG:
640 			case SIGIO:
641 			case SIGWINCH:
642 				/*
643 				 * These signals are normally not
644 				 * sent if the action is the default.
645 				 */
646 				continue;		/* == ignore */
647 
648 			default:
649 				goto send;
650 			}
651 			/*NOTREACHED*/
652 
653 		case SIG_HOLD:
654 		case SIG_IGN:
655 			/*
656 			 * Masking above should prevent us
657 			 * ever trying to take action on a held
658 			 * or ignored signal, unless process is traced.
659 			 */
660 			if ((p->p_flag&STRC) == 0)
661 				printf("issig\n");
662 			continue;
663 
664 		default:
665 			/*
666 			 * This signal has an action, let
667 			 * psig process it.
668 			 */
669 			goto send;
670 		}
671 		/*NOTREACHED*/
672 	}
673 	/*
674 	 * Didn't find a signal to send.
675 	 */
676 	p->p_cursig = 0;
677 	return (0);
678 
679 send:
680 	/*
681 	 * Let psig process the signal.
682 	 */
683 	return (sig);
684 }
685 
686 /*
687  * Put the argument process into the stopped
688  * state and notify the parent via wakeup.
689  * Signals are handled elsewhere.
690  */
691 stop(p)
692 	register struct proc *p;
693 {
694 
695 	p->p_stat = SSTOP;
696 	p->p_flag &= ~SWTED;
697 	wakeup((caddr_t)p->p_pptr);
698 }
699 
700 /*
701  * Perform the action specified by
702  * the current signal.
703  * The usual sequence is:
704  *	if (issig())
705  *		psig();
706  * The signal bit has already been cleared by issig,
707  * and the current signal number stored in p->p_cursig.
708  */
709 psig()
710 {
711 	register struct proc *p = u.u_procp;
712 	register int sig = p->p_cursig;
713 	int mask = sigmask(sig), returnmask;
714 	register int (*action)();
715 
716 	if (sig == 0)
717 		panic("psig");
718 	action = u.u_signal[sig];
719 	if (action != SIG_DFL) {
720 		if (action == SIG_IGN || (p->p_sigmask & mask))
721 			panic("psig action");
722 		u.u_error = 0;
723 		/*
724 		 * Set the new mask value and also defer further
725 		 * occurences of this signal (unless we're simulating
726 		 * the old signal facilities).
727 		 *
728 		 * Special case: user has done a sigpause.  Here the
729 		 * current mask is not of interest, but rather the
730 		 * mask from before the sigpause is what we want restored
731 		 * after the signal processing is completed.
732 		 */
733 		(void) splhigh();
734 		if (p->p_flag & SOUSIG) {
735 			if (sig != SIGILL && sig != SIGTRAP) {
736 				u.u_signal[sig] = SIG_DFL;
737 				p->p_sigcatch &= ~mask;
738 			}
739 			mask = 0;
740 		}
741 		if (p->p_flag & SOMASK) {
742 			returnmask = u.u_oldmask;
743 			p->p_flag &= ~SOMASK;
744 		} else
745 			returnmask = p->p_sigmask;
746 		p->p_sigmask |= u.u_sigmask[sig] | mask;
747 		(void) spl0();
748 		u.u_ru.ru_nsignals++;
749 		sendsig(action, sig, returnmask);
750 		p->p_cursig = 0;
751 		return;
752 	}
753 	u.u_acflag |= AXSIG;
754 	switch (sig) {
755 
756 	case SIGILL:
757 	case SIGIOT:
758 	case SIGBUS:
759 	case SIGQUIT:
760 	case SIGTRAP:
761 	case SIGEMT:
762 	case SIGFPE:
763 	case SIGSEGV:
764 	case SIGSYS:
765 		u.u_arg[0] = sig;
766 		if (core() == 0)
767 			sig += 0200;
768 	}
769 	exit(sig);
770 }
771 
772 /*
773  * Create a core image on the file "core"
774  * If you are looking for protection glitches,
775  * there are probably a wealth of them here
776  * when this occurs to a suid command.
777  *
778  * It writes UPAGES block of the
779  * user.h area followed by the entire
780  * data+stack segments.
781  */
782 core()
783 {
784 	register struct vnode *vp, *dvp;
785 	register struct nameidata *ndp = &u.u_nd;
786 	struct vattr vattr;
787 	int error;
788 
789 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
790 		return (EFAULT);
791 	if (ctob(UPAGES + u.u_dsize + u.u_ssize) >=
792 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
793 		return (EFAULT);
794 	if (u.u_procp->p_textp) {
795 		vop_lock(u.u_procp->p_textp->x_vptr);
796 		error = vn_access(u.u_procp->p_textp->x_vptr, VREAD, u.u_cred);
797 		vop_unlock(u.u_procp->p_textp->x_vptr);
798 		if (error)
799 			return (EFAULT);
800 	}
801 	ndp->ni_segflg = UIO_SYSSPACE;
802 	ndp->ni_dirp = "core";
803 	if (error = vn_open(ndp, FCREAT|FWRITE, 0644))
804 		return (error);
805 	vp = ndp->ni_vp;
806 	if (vp->v_type != VREG ||
807 	    vop_getattr(vp, &vattr, u.u_cred) ||
808 	    vattr.va_nlink != 1) {
809 		error = EFAULT;
810 		goto out;
811 	}
812 #ifdef MMAP
813 	{ register int fd;
814 	/* unmasp funky devices in the user's address space */
815 	for (fd = 0; fd < u.u_lastfile; fd++)
816 		if (u.u_ofile[fd] && (u.u_pofile[fd] & UF_MAPPED))
817 			munmapfd(fd);
818 	}
819 #endif
820 	vattr_null(&vattr);
821 	vattr.va_size = 0;
822 	vop_setattr(vp, &vattr, u.u_cred);
823 	u.u_acflag |= ACORE;
824 	error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&u, ctob(UPAGES), (off_t)0,
825 	    UIO_SYSSPACE, IO_UNIT, (int *)0);
826 	if (error == 0)
827 		error = vn_rdwr(UIO_WRITE, vp,
828 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
829 		    (int)ctob(u.u_dsize), (off_t)ctob(UPAGES),
830 		    UIO_USERSPACE, IO_UNIT, (int *)0);
831 	if (error == 0)
832 		error = vn_rdwr(UIO_WRITE, vp,
833 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
834 		    (int)ctob(u.u_ssize),
835 		    (off_t)ctob(UPAGES) + ctob(u.u_dsize),
836 		    UIO_USERSPACE, IO_UNIT, (int *)0);
837 out:
838 	if (vp)
839 		vrele(vp);
840 	return (error);
841 }
842