summaryrefslogtreecommitdiff
path: root/regress/sys/kern/main-thread-exited/main-thread-exited.c
blob: 4ddd912aa92bc1a1db8be0302551c18966c47790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* $OpenBSD: main-thread-exited.c,v 1.2 2014/05/20 01:25:24 guenther Exp $ */
/* PUBLIC DOMAIN Mar 2012 <guenther@openbsd.org> */


#include <sys/types.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void *
tmain(void *arg)
{
	sleep(1);
	printf("sending SIGKILL\n");
	kill(getpid(), SIGKILL);
	sleep(1);
	printf("still running!\n");
	exit(1);
}

int
main(int argc, char **argv)
{
	pid_t pid;
	pthread_t t;
	int r;

	pid = fork();
	if (pid == -1)
		err(1, "fork");
	if (pid > 0) {
		int status;

		if (waitpid(pid, &status, 0) != pid)
			err(1, "waitpid");
		exit(! WIFSIGNALED(status) || WTERMSIG(status) != SIGKILL);
	}

	/* in child */
	if ((r = pthread_create(&t, NULL, tmain, NULL)))
		errc(1, r, "pthread_create");
	pthread_exit(NULL);
	abort();
}