#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <unistd.h>

int main()
{
	sigset_t mask;
	int sfd;
	int epfd;
	int rc;
	struct epoll_event event;

	printf("blocking SIGURG...\n");
	sigemptyset(&mask);
	sigaddset(&mask, SIGURG);
	sigprocmask(SIG_BLOCK, &mask, NULL);

	printf("creating a signalfd to receive SIGURG...\n");
	sfd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
	if (sfd < 0)
	{
		perror("signalfd");
		return EXIT_FAILURE;
	}

	printf("creating an epoll set...\n");
	epfd = epoll_create1(EPOLL_CLOEXEC);
	if (epfd < 0)
	{
		perror("epoll_create1");
		return EXIT_FAILURE;
	}

	printf("adding signalfd to epoll set...\n");
	event.events = EPOLLIN | EPOLLOUT | EPOLLERR | EPOLLHUP;
	rc = epoll_ctl(epfd, EPOLL_CTL_ADD, sfd, &event);
	if (rc < 0)
	{
		perror("epoll_ctl");
		return EXIT_FAILURE;
	}

	printf("polling the epoll set... ");
	rc = epoll_wait(epfd, &event, 1, 0);
	printf("%d\n", rc);

	printf("sending a signal...\n");
	kill(getpid(), SIGURG);

	printf("polling the epoll set... ");
	rc = epoll_wait(epfd, &event, 1, 0);
	printf("%d\n", rc);

	return EXIT_SUCCESS;
}
