Blame daemon.c

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