xref: /openbsd-src/regress/sys/kern/signal/earlysig/earlysig.c (revision e86519808349e779a0178fd3fc87d05e2b856666)
1*e8651980Smiod /*	$OpenBSD: earlysig.c,v 1.2 2007/08/01 21:32:53 miod Exp $	*/
29afa0e1fSotto 
39afa0e1fSotto /*
49afa0e1fSotto  * Public domain.  2005, Otto Moerbeek
59afa0e1fSotto  *
6*e8651980Smiod  * Try to create the case where a signal is delivered to a process before
79afa0e1fSotto  * fork() returns.
89afa0e1fSotto  */
99afa0e1fSotto 
109afa0e1fSotto #include <sys/types.h>
119afa0e1fSotto #include <sys/wait.h>
129afa0e1fSotto 
139afa0e1fSotto #include <err.h>
149afa0e1fSotto #include <stdio.h>
159afa0e1fSotto #include <stdlib.h>
169afa0e1fSotto #include <signal.h>
179afa0e1fSotto #include <unistd.h>
189afa0e1fSotto 
dohup(int signo)199afa0e1fSotto void dohup(int signo)
209afa0e1fSotto {
219afa0e1fSotto }
229afa0e1fSotto 
239afa0e1fSotto int
main()249afa0e1fSotto main()
259afa0e1fSotto {
269afa0e1fSotto 	pid_t pid;
279afa0e1fSotto 	int status;
289afa0e1fSotto 
299afa0e1fSotto 	signal(SIGHUP, dohup);
309afa0e1fSotto 
319afa0e1fSotto 	switch(pid = fork()) {
329afa0e1fSotto 	case -1:
339afa0e1fSotto 		err(1, "fork");
349afa0e1fSotto 		break;
359afa0e1fSotto 	case 0:
369afa0e1fSotto 		sleep(2);
379afa0e1fSotto 		exit(0);
389afa0e1fSotto 	default:
399afa0e1fSotto 		kill(pid, SIGHUP);
409afa0e1fSotto 		sleep(1);
419afa0e1fSotto 		if (waitpid(pid, &status, 0) == -1)
429afa0e1fSotto 			err(1, "waitpid");
439afa0e1fSotto 		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
449afa0e1fSotto 			exit(0);
459afa0e1fSotto 		else
469afa0e1fSotto 			errx(1, "child exited with status %d",
479afa0e1fSotto 			    WEXITSTATUS(status));
489afa0e1fSotto 	}
499afa0e1fSotto }
50