2 dropin.c -- a set of drop-in replacements for libc functions
3 Copyright (C) 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000,2001 Guus Sliepen <guus@sliepen.warande.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: dropin.c,v 1.1.2.6 2001/11/05 19:09:08 guus Exp $
25 #include <sys/types.h>
40 Replacement for the daemon() function.
42 The daemon() function is for programs wishing to detach themselves
43 from the controlling terminal and run in the background as system
46 Unless the argument nochdir is non-zero, daemon() changes the
47 current working directory to the root (``/'').
49 Unless the argument noclose is non-zero, daemon() will redirect
50 standard input, standard output and standard error to /dev/null.
52 int daemon(int nochdir, int noclose)
59 /* Check if forking failed */
66 /* If we are the parent, terminate */
70 /* Detach by becoming the new process group leader */
77 /* Change working directory to the root (to avoid keeping mount
84 /* Redirect stdin/out/err to /dev/null */
87 fd = open("/dev/null", O_RDWR);
91 perror("opening /dev/null");
109 #ifndef HAVE_GET_CURRENT_DIR_NAME
111 Replacement for the GNU get_current_dir_name function:
113 get_current_dir_name will malloc(3) an array big enough to hold the
114 current directory name. If the environment variable PWD is set, and
115 its value is correct, then that value will be returned.
117 char *get_current_dir_name(void)
123 /* Start with 100 bytes. If this turns out to be insufficient to
124 contain the working directory, double the size. */
128 errno = 0; /* Success */
129 r = getcwd(buf, size);
130 /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
131 is insufficient to contain the entire working directory. */
132 while(r == NULL && errno == ERANGE)
135 size <<= 1; /* double the size */
137 r = getcwd(buf, size);
144 #ifndef HAVE_ASPRINTF
145 int asprintf(char **buf, const char *fmt, ...)
155 status = vsnprintf (*buf, len, fmt, ap);
159 *buf = xrealloc(*buf, status);
165 status = vsnprintf (*buf, len, fmt, ap);