#include <sys/types.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <err.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void dohup(int signo)
{
struct utsname name;
uname(&name);
}
void *tmain(void *arg)
{
return (arg);
}
int
main()
{
pthread_t tid;
pid_t pid, rpid;
int r, status;
if (signal(SIGHUP, dohup) == SIG_ERR)
err(1, "signal");
if ((r = pthread_create(&tid, NULL, tmain, NULL)))
errc(1, r, "pthread_create");
pthread_join(tid, NULL);
kill(0, 0);
if ((pid = fork()) <= 0) {
if (pid == -1)
err(1, "fork");
_exit(0);
}
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
switch(pid = fork()) {
case -1:
err(1, "fork");
break;
case 0:
sleep(2);
_exit(0);
default:
kill(pid, SIGHUP);
sleep(3);
if ((rpid = waitpid(pid, &status, WNOHANG)) == -1)
err(1, "waitpid");
if (rpid == 0) {
kill(pid, SIGKILL);
if (waitpid(pid, &status, 0) == -1)
err(1, "waitpid");
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
exit(0);
else if (WIFEXITED(status))
errx(1, "child exited with status %d",
WEXITSTATUS(status));
else if (WTERMSIG(status) == SIGKILL)
errx(1, "failed: child hung");
errx(1, "child killed by signal %d", WTERMSIG(status));
}
}