2 dropin.c -- a set of drop-in replacements for libc functions
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 Replacement for the daemon() function.
28 The daemon() function is for programs wishing to detach themselves
29 from the controlling terminal and run in the background as system
32 Unless the argument nochdir is non-zero, daemon() changes the
33 current working directory to the root (``/'').
35 Unless the argument noclose is non-zero, daemon() will redirect
36 standard input, standard output and standard error to /dev/null.
38 int daemon(int nochdir, int noclose) {
45 /* Check if forking failed */
51 /* If we are the parent, terminate */
55 /* Detach by becoming the new process group leader */
61 /* Change working directory to the root (to avoid keeping mount
67 /* Redirect stdin/out/err to /dev/null */
69 fd = open("/dev/null", O_RDWR);
72 perror("opening /dev/null");
88 #ifndef HAVE_GET_CURRENT_DIR_NAME
90 Replacement for the GNU get_current_dir_name function:
92 get_current_dir_name will malloc(3) an array big enough to hold the
93 current directory name. If the environment variable PWD is set, and
94 its value is correct, then that value will be returned.
96 char *get_current_dir_name(void) {
101 /* Start with 100 bytes. If this turns out to be insufficient to
102 contain the working directory, double the size. */
106 errno = 0; /* Success */
107 r = getcwd(buf, size);
109 /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
110 is insufficient to contain the entire working directory. */
111 while(r == NULL && errno == ERANGE) {
113 size <<= 1; /* double the size */
115 r = getcwd(buf, size);
122 #ifndef HAVE_ASPRINTF
123 int asprintf(char **buf, const char *fmt, ...) {
127 result = vasprintf(buf, fmt, ap);
132 int vasprintf(char **buf, const char *fmt, va_list ap) {
141 status = vsnprintf(*buf, len, fmt, aq);
145 *buf = xrealloc(*buf, status + 1);
147 if(status > len - 1) {
150 status = vsnprintf(*buf, len, fmt, aq);
158 #ifndef HAVE_GETTIMEOFDAY
159 int gettimeofday(struct timeval *tv, void *tz) {
162 GetSystemTimeAsFileTime(&ft);
163 uint64_t lt = (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32);
164 lt -= 116444736000000000ULL;
165 tv->tv_sec = lt / 10000000;
166 tv->tv_usec = (lt / 10) % 1000000;
168 #warning No high resolution time source!
169 tv->tv_sec = time(NULL);
177 int usleep(long long usec) {
178 struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
179 select(0, NULL, NULL, NULL, &tv);