/*
 * Copyright (c) 1999 Ian Freislich
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      $Id: signals.c,v 1.9 2003/01/24 12:01:25 ianf Exp $
 */

#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <netinet/in.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>

#include "popd.h"

/* global variables */

extern struct config	config;
extern int	sigbuf[2];

static void dosigchld(int signo);
static void insertsignal(int signo);

void
opensigpipe(void)
{
	int	facility = LOG_NOTICE;

	if (pipe(sigbuf)) {
		syslog(facility, "Unable to setup signal pipeline: %s",
		    strerror(errno));
		exit(EX_OSFILE);
	}
	fcntl(sigbuf[0], F_SETFL, O_NONBLOCK);
}

void
closesigpipe(void)
{
	close(sigbuf[0]);
	close(sigbuf[1]);
}

void
setsignals(void)
{
	sigset_t	signals;
	struct sigaction	action;

	sigemptyset(&signals);
	sigaddset(&signals, SIGALRM);
	memcpy((char *)&action.sa_mask, (char *)&signals,
		sizeof(action.sa_mask));
	action.sa_flags = 0; 
	action.sa_handler = insertsignal;
	sigaction(SIGCHLD, &action, NULL);
}

void
blocksignals(void)
{
	signal(SIGALRM, SIG_IGN);
}

static void
insertsignal(int signo)
{
	write(sigbuf[1], (void *)&signo, sizeof(signo));
}

static void
dosigchld(int signo)
{
	pid_t	pid;
	int	facility = LOG_NOTICE;

	if (config.debug)
		syslog(LOG_DEBUG, "Handling signal %d", signo);
	while ((pid = waitpid((pid_t)-1, (int *)0, WNOHANG)) > 0) {
		if (config.debug)
			syslog(LOG_DEBUG, "Reaped child %d", pid);
	}
}

void
removesignal(void)
{
	int     signo, facility;

	facility = LOG_NOTICE;
	while (read(sigbuf[0], (void *)&signo, sizeof(signo)) > 0) {
		switch (signo) {
			case SIGCHLD:
				dosigchld(signo);
				break;
			default:
				syslog(facility, "Recieved unknown signal %d", signo);
				break;
		}
	}
}


syntax highlighted by Code2HTML, v. 0.9.1