xref: /csrg-svn/sys/kern/kern_sig.c (revision 12750)
1*12750Ssam /*	kern_sig.c	5.17	83/05/27	*/
27421Sroot 
39755Ssam #include "../machine/reg.h"
49755Ssam #include "../machine/pte.h"
59755Ssam #include "../machine/psl.h"
69755Ssam 
77421Sroot #include "../h/param.h"
87421Sroot #include "../h/systm.h"
97421Sroot #include "../h/dir.h"
107421Sroot #include "../h/user.h"
117421Sroot #include "../h/inode.h"
127421Sroot #include "../h/proc.h"
137421Sroot #include "../h/timeb.h"
147421Sroot #include "../h/times.h"
157421Sroot #include "../h/conf.h"
167421Sroot #include "../h/buf.h"
177421Sroot #include "../h/mount.h"
187421Sroot #include "../h/text.h"
197421Sroot #include "../h/seg.h"
207421Sroot #include "../h/vm.h"
217421Sroot #include "../h/acct.h"
227818Sroot #include "../h/uio.h"
238117Sroot #include "../h/kernel.h"
249160Ssam #include "../h/nami.h"
257421Sroot 
267499Sroot /* KILL CODE SHOULDNT KNOW ABOUT PROCESS INTERNALS !?! */
277499Sroot 
287499Sroot sigvec()
297421Sroot {
307421Sroot 
317421Sroot }
327421Sroot 
337499Sroot sigblock()
347421Sroot {
357499Sroot 
367499Sroot }
377499Sroot 
387499Sroot sigsetmask()
397499Sroot {
407499Sroot 
417499Sroot }
427499Sroot 
437499Sroot sigpause()
447499Sroot {
457499Sroot 
467499Sroot }
477499Sroot 
487499Sroot sigstack()
497499Sroot {
507499Sroot 
517499Sroot }
527499Sroot 
538764Sroot #ifdef notdef
548032Sroot kill()
558032Sroot {
568032Sroot 
578032Sroot }
588764Sroot #endif
598032Sroot 
608032Sroot killpg()
618032Sroot {
629989Ssam 	register struct a {
639989Ssam 		int	pgrp;
649989Ssam 		int	signo;
659989Ssam 	} *uap = (struct a *)u.u_ap;
668032Sroot 
67*12750Ssam 	u.u_error = kill1(1, uap->signo, uap->pgrp);
688032Sroot }
698032Sroot 
70*12750Ssam kill1(ispgrp, signo, who)
719989Ssam 	int ispgrp, signo, who;
729989Ssam {
739989Ssam 	register struct proc *p;
749989Ssam 	int f, priv = 0;
759989Ssam 
769989Ssam 	if (signo == 0 || signo > NSIG)
779989Ssam 		return (EINVAL);
789989Ssam 	if (who > 0 && !ispgrp) {
799989Ssam 		p = pfind(who);
809989Ssam 		if (p == 0 || u.u_uid && u.u_uid != p->p_uid)
819989Ssam 			return (ESRCH);
829989Ssam 		psignal(p, signo);
839989Ssam 		return (0);
847421Sroot 	}
859989Ssam 	if (who == -1 && u.u_uid == 0)
869989Ssam 		priv++, who = 0, ispgrp = 1;	/* like sending to pgrp */
879989Ssam 	else if (who == 0) {
887421Sroot 		/*
897421Sroot 		 * Zero process id means send to my process group.
907421Sroot 		 */
919989Ssam 		ispgrp = 1;
929989Ssam 		who = u.u_procp->p_pgrp;
939989Ssam 		if (who == 0)
949989Ssam 			return (EINVAL);
957421Sroot 	}
969989Ssam 	for (f = 0, p = proc; p < procNPROC; p++) {
977421Sroot 		if (p->p_stat == NULL)
987421Sroot 			continue;
999989Ssam 		if (!ispgrp) {
1009989Ssam 			if (p->p_pid != who)
1017421Sroot 				continue;
1029989Ssam 		} else if (p->p_pgrp != who && priv == 0 || p->p_ppid == 0 ||
1039989Ssam 		    (p->p_flag&SSYS) || (priv && p == u.u_procp))
1047421Sroot 			continue;
1057421Sroot 		if (u.u_uid != 0 && u.u_uid != p->p_uid &&
1069989Ssam 		    (signo != SIGCONT || !inferior(p)))
1077421Sroot 			continue;
1087421Sroot 		f++;
1099989Ssam 		psignal(p, signo);
1107421Sroot 	}
111*12750Ssam 	return (f == 0 ? ESRCH : 0);
1127421Sroot }
1137421Sroot 
1147421Sroot /*
1157421Sroot  * Send the specified signal to
1167421Sroot  * all processes with 'pgrp' as
1177421Sroot  * process group.
1187421Sroot  * Called by tty.c for quits and
1197421Sroot  * interrupts.
1207421Sroot  */
1217421Sroot gsignal(pgrp, sig)
1227421Sroot 	register int pgrp;
1237421Sroot {
1247421Sroot 	register struct proc *p;
1257421Sroot 
1267421Sroot 	if (pgrp == 0)
1277421Sroot 		return;
1287421Sroot 	for(p = proc; p < procNPROC; p++)
1297421Sroot 		if (p->p_pgrp == pgrp)
1307421Sroot 			psignal(p, sig);
1317421Sroot }
1327421Sroot 
1337421Sroot /*
1347421Sroot  * Send the specified signal to
1357421Sroot  * the specified process.
1367421Sroot  */
1377421Sroot psignal(p, sig)
1387421Sroot 	register struct proc *p;
1397421Sroot 	register int sig;
1407421Sroot {
1417421Sroot 	register int s;
1427421Sroot 	register int (*action)();
1437421Sroot 	long sigmask;
1447421Sroot 
1457421Sroot 	if ((unsigned)sig >= NSIG)
1467421Sroot 		return;
1477421Sroot 	sigmask = (1L << (sig-1));
1487421Sroot 
1497421Sroot 	/*
1507421Sroot 	 * If proc is traced, always give parent a chance.
1517421Sroot 	 * Otherwise get the signal action from the bits in the proc table.
1527421Sroot 	 */
1537421Sroot 	if (p->p_flag & STRC)
1547421Sroot 		action = SIG_DFL;
1557421Sroot 	else {
1567421Sroot 		s = (p->p_siga1&sigmask) != 0;
1577421Sroot 		s <<= 1;
1587421Sroot 		s |= (p->p_siga0&sigmask) != 0;
1597421Sroot 		action = (int(*)())s;
1607421Sroot 		/*
1617421Sroot 		 * If the signal is ignored, we forget about it immediately.
1627421Sroot 		 */
1637421Sroot 		if (action == SIG_IGN)
1647421Sroot 			return;
1657421Sroot 	}
1667421Sroot #define mask(sig)	(1<<(sig-1))
1677421Sroot #define	stops	(mask(SIGSTOP)|mask(SIGTSTP)|mask(SIGTTIN)|mask(SIGTTOU))
1687421Sroot 	if (sig) {
1697421Sroot 		p->p_sig |= sigmask;
1707421Sroot 		switch (sig) {
1717421Sroot 
1727421Sroot 		case SIGTERM:
1737421Sroot 			if ((p->p_flag&STRC) != 0 || action != SIG_DFL)
1747421Sroot 				break;
1757421Sroot 			/* fall into ... */
1767421Sroot 
1777421Sroot 		case SIGKILL:
1787421Sroot 			if (p->p_nice > NZERO)
1797421Sroot 				p->p_nice = NZERO;
1807421Sroot 			break;
1817421Sroot 
1827421Sroot 		case SIGCONT:
1837421Sroot 			p->p_sig &= ~stops;
1847421Sroot 			break;
1857421Sroot 
1867421Sroot 		case SIGSTOP:
1877421Sroot 		case SIGTSTP:
1887421Sroot 		case SIGTTIN:
1897421Sroot 		case SIGTTOU:
1907421Sroot 			p->p_sig &= ~mask(SIGCONT);
1917421Sroot 			break;
1927421Sroot 		}
1937421Sroot 	}
1947421Sroot #undef mask
1957421Sroot #undef stops
1967421Sroot 	/*
1977421Sroot 	 * Defer further processing for signals which are held.
1987421Sroot 	 */
1997421Sroot 	if (action == SIG_HOLD)
2007421Sroot 		return;
2017421Sroot 	s = spl6();
2027421Sroot 	switch (p->p_stat) {
2037421Sroot 
2047421Sroot 	case SSLEEP:
2057421Sroot 		/*
2067421Sroot 		 * If process is sleeping at negative priority
2077421Sroot 		 * we can't interrupt the sleep... the signal will
2087421Sroot 		 * be noticed when the process returns through
2097421Sroot 		 * trap() or syscall().
2107421Sroot 		 */
2117421Sroot 		if (p->p_pri <= PZERO)
2127421Sroot 			goto out;
2137421Sroot 		/*
2147421Sroot 		 * Process is sleeping and traced... make it runnable
2157421Sroot 		 * so it can discover the signal in issig() and stop
2167421Sroot 		 * for the parent.
2177421Sroot 		 */
2187421Sroot 		if (p->p_flag&STRC)
2197421Sroot 			goto run;
2207421Sroot 		switch (sig) {
2217421Sroot 
2227421Sroot 		case SIGSTOP:
2237421Sroot 		case SIGTSTP:
2247421Sroot 		case SIGTTIN:
2257421Sroot 		case SIGTTOU:
2267421Sroot 			/*
2277421Sroot 			 * These are the signals which by default
2287421Sroot 			 * stop a process.
2297421Sroot 			 */
2307421Sroot 			if (action != SIG_DFL)
2317421Sroot 				goto run;
2327421Sroot 			/*
2337421Sroot 			 * Don't clog system with children of init
2347421Sroot 			 * stopped from the keyboard.
2357421Sroot 			 */
2367421Sroot 			if (sig != SIGSTOP && p->p_pptr == &proc[1]) {
2377421Sroot 				psignal(p, SIGKILL);
2387421Sroot 				p->p_sig &= ~sigmask;
2397421Sroot 				splx(s);
2407421Sroot 				return;
2417421Sroot 			}
2427421Sroot 			/*
2437421Sroot 			 * If a child in vfork(), stopping could
2447421Sroot 			 * cause deadlock.
2457421Sroot 			 */
2467421Sroot 			if (p->p_flag&SVFORK)
2477421Sroot 				goto out;
2487421Sroot 			p->p_sig &= ~sigmask;
2497421Sroot 			p->p_cursig = sig;
2507421Sroot 			stop(p);
2517421Sroot 			goto out;
2527421Sroot 
2537421Sroot 		case SIGIO:
2547421Sroot 		case SIGURG:
2557421Sroot 		case SIGCHLD:
2567421Sroot 			/*
2577421Sroot 			 * These signals are special in that they
2587421Sroot 			 * don't get propogated... if the process
2597421Sroot 			 * isn't interested, forget it.
2607421Sroot 			 */
2617421Sroot 			if (action != SIG_DFL)
2627421Sroot 				goto run;
2637421Sroot 			p->p_sig &= ~sigmask;		/* take it away */
2647421Sroot 			goto out;
2657421Sroot 
2667421Sroot 		default:
2677421Sroot 			/*
2687421Sroot 			 * All other signals cause the process to run
2697421Sroot 			 */
2707421Sroot 			goto run;
2717421Sroot 		}
2727421Sroot 		/*NOTREACHED*/
2737421Sroot 
2747421Sroot 	case SSTOP:
2757421Sroot 		/*
2767421Sroot 		 * If traced process is already stopped,
2777421Sroot 		 * then no further action is necessary.
2787421Sroot 		 */
2797421Sroot 		if (p->p_flag&STRC)
2807421Sroot 			goto out;
2817421Sroot 		switch (sig) {
2827421Sroot 
2837421Sroot 		case SIGKILL:
2847421Sroot 			/*
2857421Sroot 			 * Kill signal always sets processes running.
2867421Sroot 			 */
2877421Sroot 			goto run;
2887421Sroot 
2897421Sroot 		case SIGCONT:
2907421Sroot 			/*
2917421Sroot 			 * If the process catches SIGCONT, let it handle
2927421Sroot 			 * the signal itself.  If it isn't waiting on
2937421Sroot 			 * an event, then it goes back to run state.
2947421Sroot 			 * Otherwise, process goes back to sleep state.
2957421Sroot 			 */
2967421Sroot 			if (action != SIG_DFL || p->p_wchan == 0)
2977421Sroot 				goto run;
2987421Sroot 			p->p_stat = SSLEEP;
2997421Sroot 			goto out;
3007421Sroot 
3017421Sroot 		case SIGSTOP:
3027421Sroot 		case SIGTSTP:
3037421Sroot 		case SIGTTIN:
3047421Sroot 		case SIGTTOU:
3057421Sroot 			/*
3067421Sroot 			 * Already stopped, don't need to stop again.
3077421Sroot 			 * (If we did the shell could get confused.)
3087421Sroot 			 */
3097421Sroot 			p->p_sig &= ~sigmask;		/* take it away */
3107421Sroot 			goto out;
3117421Sroot 
3127421Sroot 		default:
3137421Sroot 			/*
3147421Sroot 			 * If process is sleeping interruptibly, then
3157421Sroot 			 * unstick it so that when it is continued
3167421Sroot 			 * it can look at the signal.
3177421Sroot 			 * But don't setrun the process as its not to
3187421Sroot 			 * be unstopped by the signal alone.
3197421Sroot 			 */
3207421Sroot 			if (p->p_wchan && p->p_pri > PZERO)
3217421Sroot 				unsleep(p);
3227421Sroot 			goto out;
3237421Sroot 		}
3247421Sroot 		/*NOTREACHED*/
3257421Sroot 
3267421Sroot 	default:
3277421Sroot 		/*
3287421Sroot 		 * SRUN, SIDL, SZOMB do nothing with the signal,
3297421Sroot 		 * other than kicking ourselves if we are running.
3307421Sroot 		 * It will either never be noticed, or noticed very soon.
3317421Sroot 		 */
3327421Sroot 		if (p == u.u_procp && !noproc)
3338444Sroot #include "../vax/mtpr.h"
3347421Sroot 			aston();
3357421Sroot 		goto out;
3367421Sroot 	}
3377421Sroot 	/*NOTREACHED*/
3387421Sroot run:
3397421Sroot 	/*
3407421Sroot 	 * Raise priority to at least PUSER.
3417421Sroot 	 */
3427421Sroot 	if (p->p_pri > PUSER)
3437421Sroot 		if ((p != u.u_procp || noproc) && p->p_stat == SRUN &&
3447421Sroot 		    (p->p_flag & SLOAD)) {
3457421Sroot 			remrq(p);
3467421Sroot 			p->p_pri = PUSER;
3477421Sroot 			setrq(p);
3487421Sroot 		} else
3497421Sroot 			p->p_pri = PUSER;
3507421Sroot 	setrun(p);
3517421Sroot out:
3527421Sroot 	splx(s);
3537421Sroot }
3547421Sroot 
3557421Sroot /*
3567421Sroot  * Returns true if the current
3577421Sroot  * process has a signal to process.
3587421Sroot  * The signal to process is put in p_cursig.
3597421Sroot  * This is asked at least once each time a process enters the
3607421Sroot  * system (though this can usually be done without actually
3617421Sroot  * calling issig by checking the pending signal masks.)
3627421Sroot  * A signal does not do anything
3637421Sroot  * directly to a process; it sets
3647421Sroot  * a flag that asks the process to
3657421Sroot  * do something to itself.
3667421Sroot  */
3677421Sroot issig()
3687421Sroot {
3697421Sroot 	register struct proc *p;
3707421Sroot 	register int sig;
3717421Sroot 	long sigbits;
3727421Sroot 	long sigmask;
3737421Sroot 
3747421Sroot 	p = u.u_procp;
3757421Sroot 	for (;;) {
3767421Sroot 		sigbits = p->p_sig;
3777421Sroot 		if ((p->p_flag&STRC) == 0)
3787421Sroot 			sigbits &= ~p->p_ignsig;
3797421Sroot 		if (p->p_flag&SVFORK)
3807421Sroot #define bit(a) (1<<(a-1))
3817421Sroot 			sigbits &= ~(bit(SIGSTOP)|bit(SIGTSTP)|bit(SIGTTIN)|bit(SIGTTOU));
3827421Sroot 		if (sigbits == 0)
3837421Sroot 			break;
3847421Sroot 		sig = ffs((int)sigbits);
3857421Sroot 		sigmask = 1L << (sig-1);
3867421Sroot 		p->p_sig &= ~sigmask;		/* take the signal! */
3877421Sroot 		p->p_cursig = sig;
3887421Sroot 		if (p->p_flag&STRC && (p->p_flag&SVFORK)==0) {
3897421Sroot 			/*
3907421Sroot 			 * If traced, always stop, and stay
3917421Sroot 			 * stopped until released by the parent.
3927421Sroot 			 */
3937421Sroot 			do {
3947421Sroot 				stop(p);
3957421Sroot 				swtch();
3967421Sroot 			} while (!procxmt() && p->p_flag&STRC);
3977421Sroot 
3987421Sroot 			/*
3997421Sroot 			 * If the traced bit got turned off,
4007421Sroot 			 * then put the signal taken above back into p_sig
4017421Sroot 			 * and go back up to the top to rescan signals.
4027421Sroot 			 * This ensures that siga0 and u_signal are consistent.
4037421Sroot 			 */
4047421Sroot 			if ((p->p_flag&STRC) == 0) {
4057421Sroot 				p->p_sig |= sigmask;
4067421Sroot 				continue;
4077421Sroot 			}
4087421Sroot 
4097421Sroot 			/*
4107421Sroot 			 * If parent wants us to take the signal,
4117421Sroot 			 * then it will leave it in p->p_cursig;
4127421Sroot 			 * otherwise we just look for signals again.
4137421Sroot 			 */
4147421Sroot 			sig = p->p_cursig;
4157421Sroot 			if (sig == 0)
4167421Sroot 				continue;
4177421Sroot 		}
4187421Sroot 		switch (u.u_signal[sig]) {
4197421Sroot 
4207421Sroot 		case SIG_DFL:
4217421Sroot 			/*
4227421Sroot 			 * Don't take default actions on system processes.
4237421Sroot 			 */
4247421Sroot 			if (p->p_ppid == 0)
4257421Sroot 				break;
4267421Sroot 			switch (sig) {
4277421Sroot 
4287421Sroot 			case SIGTSTP:
4297421Sroot 			case SIGTTIN:
4307421Sroot 			case SIGTTOU:
4317421Sroot 				/*
4327421Sroot 				 * Children of init aren't allowed to stop
4337421Sroot 				 * on signals from the keyboard.
4347421Sroot 				 */
4357421Sroot 				if (p->p_pptr == &proc[1]) {
4367421Sroot 					psignal(p, SIGKILL);
4377421Sroot 					continue;
4387421Sroot 				}
4397421Sroot 				/* fall into ... */
4407421Sroot 
4417421Sroot 			case SIGSTOP:
4427421Sroot 				if (p->p_flag&STRC)
4437421Sroot 					continue;
4447421Sroot 				stop(p);
4457421Sroot 				swtch();
4467421Sroot 				continue;
4477421Sroot 
4487421Sroot 			case SIGCONT:
4497421Sroot 			case SIGCHLD:
4507421Sroot 				/*
4517421Sroot 				 * These signals are normally not
4527421Sroot 				 * sent if the action is the default.
4537421Sroot 				 */
4547421Sroot 				continue;		/* == ignore */
4557421Sroot 
4567421Sroot 			default:
4577421Sroot 				goto send;
4587421Sroot 			}
4597421Sroot 			/*NOTREACHED*/
4607421Sroot 
4617421Sroot 		case SIG_HOLD:
4627421Sroot 		case SIG_IGN:
4637421Sroot 			/*
4647421Sroot 			 * Masking above should prevent us
4657421Sroot 			 * ever trying to take action on a held
4667421Sroot 			 * or ignored signal, unless process is traced.
4677421Sroot 			 */
4687421Sroot 			if ((p->p_flag&STRC) == 0)
4697421Sroot 				printf("issig\n");
4707421Sroot 			continue;
4717421Sroot 
4727421Sroot 		default:
4737421Sroot 			/*
4747421Sroot 			 * This signal has an action, let
4757421Sroot 			 * psig process it.
4767421Sroot 			 */
4777421Sroot 			goto send;
4787421Sroot 		}
4797421Sroot 		/*NOTREACHED*/
4807421Sroot 	}
4817421Sroot 	/*
4827421Sroot 	 * Didn't find a signal to send.
4837421Sroot 	 */
4847421Sroot 	p->p_cursig = 0;
4857421Sroot 	return (0);
4867421Sroot 
4877421Sroot send:
4887421Sroot 	/*
4897421Sroot 	 * Let psig process the signal.
4907421Sroot 	 */
4917421Sroot 	return (sig);
4927421Sroot }
4937421Sroot 
4947421Sroot /*
4957421Sroot  * Put the argument process into the stopped
4967421Sroot  * state and notify the parent via wakeup and/or signal.
4977421Sroot  */
4987421Sroot stop(p)
4997421Sroot 	register struct proc *p;
5007421Sroot {
5017421Sroot 
5027421Sroot 	p->p_stat = SSTOP;
5037421Sroot 	p->p_flag &= ~SWTED;
5047421Sroot 	wakeup((caddr_t)p->p_pptr);
5057421Sroot 	/*
5067421Sroot 	 * Avoid sending signal to parent if process is traced
5077421Sroot 	 */
5087421Sroot 	if (p->p_flag&STRC)
5097421Sroot 		return;
5107421Sroot 	psignal(p->p_pptr, SIGCHLD);
5117421Sroot }
5127421Sroot 
5137421Sroot /*
5147421Sroot  * Perform the action specified by
5157421Sroot  * the current signal.
5167421Sroot  * The usual sequence is:
5177421Sroot  *	if (issig())
5187421Sroot  *		psig();
5197421Sroot  * The signal bit has already been cleared by issig,
5207421Sroot  * and the current signal number stored in p->p_cursig.
5217421Sroot  */
5227421Sroot psig()
5237421Sroot {
5247421Sroot 	register struct proc *rp = u.u_procp;
5257421Sroot 	register int n = rp->p_cursig;
5267421Sroot 	long sigmask = 1L << (n-1);
5277421Sroot 	register int (*action)();
5287421Sroot 
5297421Sroot 	if (rp->p_cursig == 0)
5307421Sroot 		panic("psig");
5317421Sroot 	action = u.u_signal[n];
5327421Sroot 	if (action != SIG_DFL) {
5337421Sroot 		if (action == SIG_IGN || action == SIG_HOLD)
5347421Sroot 			panic("psig action");
5357421Sroot 		u.u_error = 0;
5367421Sroot 		if (n != SIGILL && n != SIGTRAP)
5377421Sroot 			u.u_signal[n] = 0;
5387421Sroot 		/*
5397421Sroot 		 * If this catch value indicates automatic holding of
5407421Sroot 		 * subsequent signals, set the hold value.
5417421Sroot 		 */
5427421Sroot 		if (SIGISDEFER(action)) {
5437421Sroot 			(void) spl6();
544*12750Ssam 			/* SIG_HOLD known to be 3 */
545*12750Ssam 			rp->p_siga0 |= sigmask;
546*12750Ssam 			rp->p_siga1 |= sigmask;
5477421Sroot 			u.u_signal[n] = SIG_HOLD;
5487421Sroot 			(void) spl0();
5497421Sroot 			action = SIGUNDEFER(action);
5507421Sroot 		}
5518032Sroot 		u.u_ru.ru_nsignals++;
5527421Sroot 		sendsig(action, n);
5537421Sroot 		rp->p_cursig = 0;
5547421Sroot 		return;
5557421Sroot 	}
5567421Sroot 	u.u_acflag |= AXSIG;
5577421Sroot 	switch (n) {
5587421Sroot 
5597421Sroot 	case SIGILL:
5607421Sroot 	case SIGIOT:
5617421Sroot 	case SIGBUS:
5627421Sroot 	case SIGQUIT:
5637421Sroot 	case SIGTRAP:
5647421Sroot 	case SIGEMT:
5657421Sroot 	case SIGFPE:
5667421Sroot 	case SIGSEGV:
5677421Sroot 	case SIGSYS:
5687421Sroot 		u.u_arg[0] = n;
5697421Sroot 		if (core())
5707421Sroot 			n += 0200;
5717421Sroot 	}
5727421Sroot 	exit(n);
5737421Sroot }
5747421Sroot 
5757421Sroot /*
5767421Sroot  * Create a core image on the file "core"
5777421Sroot  * If you are looking for protection glitches,
5787421Sroot  * there are probably a wealth of them here
5797421Sroot  * when this occurs to a suid command.
5807421Sroot  *
5817421Sroot  * It writes UPAGES block of the
5827421Sroot  * user.h area followed by the entire
5837421Sroot  * data+stack segments.
5847421Sroot  */
5857421Sroot core()
5867421Sroot {
5877421Sroot 	register struct inode *ip;
5887421Sroot 	extern schar();
5897421Sroot 
59012639Ssam 	if (u.u_uid != u.u_ruid || u.u_gid != u.u_rgid)
5917818Sroot 		return (0);
5928032Sroot 	if (ctob(UPAGES+u.u_dsize+u.u_ssize) >=
5938032Sroot 	    u.u_rlimit[RLIMIT_CORE].rlim_cur)
5947421Sroot 		return (0);
5957421Sroot 	u.u_error = 0;
5967421Sroot 	u.u_dirp = "core";
5979160Ssam 	ip = namei(schar, CREATE, 1);
5987421Sroot 	if (ip == NULL) {
5997421Sroot 		if (u.u_error)
6007421Sroot 			return (0);
60112639Ssam 		ip = maknode(0644);
6027421Sroot 		if (ip==NULL)
6037421Sroot 			return (0);
6047421Sroot 	}
6057818Sroot 	if (access(ip, IWRITE) ||
6067818Sroot 	   (ip->i_mode&IFMT) != IFREG ||
6077818Sroot 	   ip->i_nlink != 1) {
6087421Sroot 		u.u_error = EFAULT;
6097818Sroot 		goto out;
6107818Sroot 	}
6119160Ssam 	itrunc(ip, (u_long)0);
6127818Sroot 	u.u_acflag |= ACORE;
6138967Sroot 	/* if (u.u_error == 0) */
6148967Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
6158967Sroot 		    (caddr_t)&u,
6168967Sroot 		    ctob(UPAGES),
6178967Sroot 		    0, 1, (int *)0);
6188101Sroot 	if (u.u_error == 0)
6198644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
6208967Sroot 		    (caddr_t)ctob(dptov(u.u_procp, 0)),
6218967Sroot 		    ctob(u.u_dsize),
6228644Sroot 		    ctob(UPAGES), 0, (int *)0);
6238101Sroot 	if (u.u_error == 0)
6248644Sroot 		u.u_error = rdwri(UIO_WRITE, ip,
6258967Sroot 		    (caddr_t)ctob(sptov(u.u_procp, u.u_ssize - 1)),
6268967Sroot 		    ctob(u.u_ssize),
6278644Sroot 		    ctob(UPAGES)+ctob(u.u_dsize), 0, (int *)0);
6287818Sroot out:
6297421Sroot 	iput(ip);
6307818Sroot 	return (u.u_error == 0);
6317421Sroot }
632