Path: blob/main/crypto/krb5/src/lib/apputils/daemon.c
39562 views
/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */1/*2* Copyright (c) 1990 The Regents of the University of California.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13* 3. All advertising materials mentioning features or use of this software14* must display the following acknowledgement:15* This product includes software developed by the University of16* California, Berkeley and its contributors.17* 4. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include "k5-int.h"35#include <fcntl.h>36#include <sys/types.h>37#include <sys/file.h>38#include <unistd.h>39#ifdef HAVE_PATHS_H40#include <paths.h>41#endif4243#ifndef _PATH_DEVNULL44#define _PATH_DEVNULL "/dev/null"45#endif4647int48daemon(nochdir, noclose)49int nochdir, noclose;50{51int cpid;5253if ((cpid = fork()) == -1)54return (-1);55if (cpid)56exit(0);57#ifdef HAVE_SETSID58(void) setsid();59#else60#ifndef TIOCNOTTY61setpgrp();62#else63{64int n;6566/*67* The open below may hang on pseudo ttys if the person68* who starts named logs out before this point. Thus,69* the need for the timer.70*/71alarm(120);72n = open("/dev/tty", O_RDWR);73alarm(0);74if (n > 0) {75(void) ioctl(n, TIOCNOTTY, (char *)NULL);76(void) close(n);77}78}79#endif80#endif81if (!nochdir)82(void) chdir("/");83if (!noclose) {84int devnull = open(_PATH_DEVNULL, O_RDWR, 0);8586if (devnull != -1) {87(void) dup2(devnull, 0);88(void) dup2(devnull, 1);89(void) dup2(devnull, 2);90if (devnull > 2)91(void) close(devnull);92}93}94return (0);95}969798