/*= -*- c-file-style: "bsd"; indent-tabs-mode: nil; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: wait.c,v 1.2 2001/06/20 13:28:44 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "includes.h"

#include <sys/wait.h>

n4err n4_reap_children(void)
{
    int stat, errno_save;
    pid_t pid;
	
    errno_save = errno;

    while ((pid = waitpid(-1, &stat, WNOHANG)) > 0) {
        if (WIFSIGNALED(stat)) {
            char const *signame = strsignal(WTERMSIG(stat));
			
            n4log_error("child %d exited abnormally: signal %d: %s%s",
                         pid, WTERMSIG(stat),
                         signame ? signame : "(unknown signal)",
                         WCOREDUMP(stat) ? ": core dumped" : "");
        } else if (WIFEXITED(stat)) {
            int rc = WEXITSTATUS(stat);
            if (rc) 
                n4trace("child %d exited with exit code %d",
                         pid, rc);
            else
                n4trace(
                         "child %d died happy",
                         pid);
        }
    }

    /* ECHILD just means that we have no more children alive or
       dead; we don't worry about that. */

    if (pid == -1  &&  errno != ECHILD) {
        n4log_error("waitpid error: %s", strerror(errno));
    }
		
    errno = errno_save;

    return N4OK;
}


