Blame daemon.c

Packit 4e8bc4
/*    $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $    */
Packit 4e8bc4
/*    $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $    */
Packit 4e8bc4
/*-
Packit 4e8bc4
 * Copyright (c) 1990, 1993
Packit 4e8bc4
 *    The Regents of the University of California.  All rights reserved.
Packit 4e8bc4
 *
Packit 4e8bc4
 * Redistribution and use in source and binary forms, with or without
Packit 4e8bc4
 * modification, are permitted provided that the following conditions
Packit 4e8bc4
 * are met:
Packit 4e8bc4
 * 1. Redistributions of source code must retain the above copyright
Packit 4e8bc4
 *    notice, this list of conditions and the following disclaimer.
Packit 4e8bc4
 * 2. Redistributions in binary form must reproduce the above copyright
Packit 4e8bc4
 *    notice, this list of conditions and the following disclaimer in the
Packit 4e8bc4
 *    documentation and/or other materials provided with the distribution.
Packit 4e8bc4
 * 3. Neither the name of the University nor the names of its contributors
Packit 4e8bc4
 *    may be used to endorse or promote products derived from this software
Packit 4e8bc4
 *    without specific prior written permission.
Packit 4e8bc4
 *
Packit 4e8bc4
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit 4e8bc4
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 4e8bc4
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 4e8bc4
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit 4e8bc4
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit 4e8bc4
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit 4e8bc4
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit 4e8bc4
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit 4e8bc4
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit 4e8bc4
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit 4e8bc4
 * SUCH DAMAGE.
Packit 4e8bc4
 */
Packit 4e8bc4
Packit 4e8bc4
#if defined __SUNPRO_C || defined __DECC || defined __HP_cc
Packit 4e8bc4
# pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
Packit 4e8bc4
# pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
Packit 4e8bc4
#endif
Packit 4e8bc4
Packit 4e8bc4
#include <fcntl.h>
Packit 4e8bc4
#include <stdio.h>
Packit 4e8bc4
#include <stdlib.h>
Packit 4e8bc4
#include <unistd.h>
Packit 4e8bc4
Packit 4e8bc4
#include "memcached.h"
Packit 4e8bc4
Packit 4e8bc4
int daemonize(int nochdir, int noclose)
Packit 4e8bc4
{
Packit 4e8bc4
    int fd;
Packit 4e8bc4
Packit 4e8bc4
    switch (fork()) {
Packit 4e8bc4
    case -1:
Packit 4e8bc4
        return (-1);
Packit 4e8bc4
    case 0:
Packit 4e8bc4
        break;
Packit 4e8bc4
    default:
Packit 4e8bc4
        _exit(EXIT_SUCCESS);
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    if (setsid() == -1)
Packit 4e8bc4
        return (-1);
Packit 4e8bc4
Packit 4e8bc4
    if (nochdir == 0) {
Packit 4e8bc4
        if(chdir("/") != 0) {
Packit 4e8bc4
            perror("chdir");
Packit 4e8bc4
            return (-1);
Packit 4e8bc4
        }
Packit 4e8bc4
    }
Packit 4e8bc4
Packit 4e8bc4
    if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
Packit 4e8bc4
        if(dup2(fd, STDIN_FILENO) < 0) {
Packit 4e8bc4
            perror("dup2 stdin");
Packit 4e8bc4
            return (-1);
Packit 4e8bc4
        }
Packit 4e8bc4
        if(dup2(fd, STDOUT_FILENO) < 0) {
Packit 4e8bc4
            perror("dup2 stdout");
Packit 4e8bc4
            return (-1);
Packit 4e8bc4
        }
Packit 4e8bc4
        if(dup2(fd, STDERR_FILENO) < 0) {
Packit 4e8bc4
            perror("dup2 stderr");
Packit 4e8bc4
            return (-1);
Packit 4e8bc4
        }
Packit 4e8bc4
Packit 4e8bc4
        if (fd > STDERR_FILENO) {
Packit 4e8bc4
            if(close(fd) < 0) {
Packit 4e8bc4
                perror("close");
Packit 4e8bc4
                return (-1);
Packit 4e8bc4
            }
Packit 4e8bc4
        }
Packit 4e8bc4
    }
Packit 4e8bc4
    return (0);
Packit 4e8bc4
}