2 dropin.c -- a set of drop-in replacements for libc functions
3 Copyright (C) 2000-2005 Ivo Timmermans,
4 2000-2011 Guus Sliepen <guus@tinc-vpn.org>
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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 Replacement for the daemon() function.
29 The daemon() function is for programs wishing to detach themselves
30 from the controlling terminal and run in the background as system
33 Unless the argument nochdir is non-zero, daemon() changes the
34 current working directory to the root (``/'').
36 Unless the argument noclose is non-zero, daemon() will redirect
37 standard input, standard output and standard error to /dev/null.
39 int daemon(int nochdir, int noclose) {
46 /* Check if forking failed */
52 /* If we are the parent, terminate */
56 /* Detach by becoming the new process group leader */
62 /* Change working directory to the root (to avoid keeping mount
68 /* Redirect stdin/out/err to /dev/null */
70 fd = open("/dev/null", O_RDWR);
73 perror("opening /dev/null");
89 #ifndef HAVE_GET_CURRENT_DIR_NAME
91 Replacement for the GNU get_current_dir_name function:
93 get_current_dir_name will malloc(3) an array big enough to hold the
94 current directory name. If the environment variable PWD is set, and
95 its value is correct, then that value will be returned.
97 char *get_current_dir_name(void) {
102 /* Start with 100 bytes. If this turns out to be insufficient to
103 contain the working directory, double the size. */
107 errno = 0; /* Success */
108 r = getcwd(buf, size);
110 /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
111 is insufficient to contain the entire working directory. */
112 while(r == NULL && errno == ERANGE) {
114 size <<= 1; /* double the size */
116 r = getcwd(buf, size);
123 #ifndef HAVE_ASPRINTF
124 int asprintf(char **buf, const char *fmt, ...) {
128 result = vasprintf(buf, fmt, ap);
133 int vasprintf(char **buf, const char *fmt, va_list ap) {
142 status = vsnprintf(*buf, len, fmt, aq);
146 *buf = xrealloc(*buf, status + 1);
148 if(status > len - 1) {
151 status = vsnprintf(*buf, len, fmt, aq);
159 #ifndef HAVE_GETTIMEOFDAY
160 int gettimeofday(struct timeval *tv, void *tz) {
163 GetSystemTimeAsFileTime(&ft);
164 uint64_t lt = (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32);
165 lt -= 116444736000000000ULL;
166 tv->tv_sec = lt / 10000000;
167 tv->tv_usec = (lt / 10) % 1000000;
169 #warning No high resolution time source!
170 tv->tv_sec = time(NULL);
178 int usleep(long long usec) {
179 struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
180 select(0, NULL, NULL, NULL, &tv);