198ca309dSBrad Fitzpatrick /* $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $ */
298ca309dSBrad Fitzpatrick /* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ */
398ca309dSBrad Fitzpatrick /*-
498ca309dSBrad Fitzpatrick * Copyright (c) 1990, 1993
598ca309dSBrad Fitzpatrick * The Regents of the University of California. All rights reserved.
698ca309dSBrad Fitzpatrick *
798ca309dSBrad Fitzpatrick * Redistribution and use in source and binary forms, with or without
898ca309dSBrad Fitzpatrick * modification, are permitted provided that the following conditions
998ca309dSBrad Fitzpatrick * are met:
1098ca309dSBrad Fitzpatrick * 1. Redistributions of source code must retain the above copyright
1198ca309dSBrad Fitzpatrick * notice, this list of conditions and the following disclaimer.
1298ca309dSBrad Fitzpatrick * 2. Redistributions in binary form must reproduce the above copyright
1398ca309dSBrad Fitzpatrick * notice, this list of conditions and the following disclaimer in the
1498ca309dSBrad Fitzpatrick * documentation and/or other materials provided with the distribution.
1598ca309dSBrad Fitzpatrick * 3. Neither the name of the University nor the names of its contributors
1698ca309dSBrad Fitzpatrick * may be used to endorse or promote products derived from this software
1798ca309dSBrad Fitzpatrick * without specific prior written permission.
1898ca309dSBrad Fitzpatrick *
1998ca309dSBrad Fitzpatrick * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2098ca309dSBrad Fitzpatrick * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2198ca309dSBrad Fitzpatrick * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2298ca309dSBrad Fitzpatrick * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2398ca309dSBrad Fitzpatrick * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2498ca309dSBrad Fitzpatrick * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2598ca309dSBrad Fitzpatrick * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2698ca309dSBrad Fitzpatrick * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2798ca309dSBrad Fitzpatrick * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2898ca309dSBrad Fitzpatrick * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2998ca309dSBrad Fitzpatrick * SUCH DAMAGE.
3098ca309dSBrad Fitzpatrick */
3186969ea4SBrad Fitzpatrick
3298ca309dSBrad Fitzpatrick #if defined __SUNPRO_C || defined __DECC || defined __HP_cc
3398ca309dSBrad Fitzpatrick # pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
3498ca309dSBrad Fitzpatrick # pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
3598ca309dSBrad Fitzpatrick #endif
3686969ea4SBrad Fitzpatrick
3798ca309dSBrad Fitzpatrick #include <fcntl.h>
38*2c196ed6SDustin Sallings #include <stdio.h>
3998ca309dSBrad Fitzpatrick #include <stdlib.h>
4098ca309dSBrad Fitzpatrick #include <unistd.h>
4186969ea4SBrad Fitzpatrick
42df1b7e42STrond Norbye #include "memcached.h"
43df1b7e42STrond Norbye
daemonize(int nochdir,int noclose)4408c14e4eSTrond Norbye int daemonize(int nochdir, int noclose)
4598ca309dSBrad Fitzpatrick {
4698ca309dSBrad Fitzpatrick int fd;
4786969ea4SBrad Fitzpatrick
4898ca309dSBrad Fitzpatrick switch (fork()) {
4998ca309dSBrad Fitzpatrick case -1:
5098ca309dSBrad Fitzpatrick return (-1);
5198ca309dSBrad Fitzpatrick case 0:
5298ca309dSBrad Fitzpatrick break;
5398ca309dSBrad Fitzpatrick default:
54c8425072SPaul Lindner _exit(EXIT_SUCCESS);
5598ca309dSBrad Fitzpatrick }
5686969ea4SBrad Fitzpatrick
5798ca309dSBrad Fitzpatrick if (setsid() == -1)
5898ca309dSBrad Fitzpatrick return (-1);
5986969ea4SBrad Fitzpatrick
60*2c196ed6SDustin Sallings if (nochdir == 0) {
61*2c196ed6SDustin Sallings if(chdir("/") != 0) {
62*2c196ed6SDustin Sallings perror("chdir");
63*2c196ed6SDustin Sallings return (-1);
64*2c196ed6SDustin Sallings }
65*2c196ed6SDustin Sallings }
6686969ea4SBrad Fitzpatrick
67e31164edSPaul Lindner if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
68*2c196ed6SDustin Sallings if(dup2(fd, STDIN_FILENO) < 0) {
69*2c196ed6SDustin Sallings perror("dup2 stdin");
70*2c196ed6SDustin Sallings return (-1);
71*2c196ed6SDustin Sallings }
72*2c196ed6SDustin Sallings if(dup2(fd, STDOUT_FILENO) < 0) {
73*2c196ed6SDustin Sallings perror("dup2 stdout");
74*2c196ed6SDustin Sallings return (-1);
75*2c196ed6SDustin Sallings }
76*2c196ed6SDustin Sallings if(dup2(fd, STDERR_FILENO) < 0) {
77*2c196ed6SDustin Sallings perror("dup2 stderr");
78*2c196ed6SDustin Sallings return (-1);
79*2c196ed6SDustin Sallings }
80*2c196ed6SDustin Sallings
81*2c196ed6SDustin Sallings if (fd > STDERR_FILENO) {
82*2c196ed6SDustin Sallings if(close(fd) < 0) {
83*2c196ed6SDustin Sallings perror("close");
84*2c196ed6SDustin Sallings return (-1);
85*2c196ed6SDustin Sallings }
86*2c196ed6SDustin Sallings }
8798ca309dSBrad Fitzpatrick }
8898ca309dSBrad Fitzpatrick return (0);
8998ca309dSBrad Fitzpatrick }
90