Delay sending the real ID request until after a proxy request is granted.
[oweals/tinc.git] / src / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2000-2005 Ivo Timmermans,
4                   2000-2016 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "xalloc.h"
24
25 #ifndef HAVE_DAEMON
26 /*
27   Replacement for the daemon() function.
28   
29   The daemon() function is for programs wishing to detach themselves
30   from the controlling terminal and run in the background as system
31   daemons.
32
33   Unless the argument nochdir is non-zero, daemon() changes the
34   current working directory to the root (``/'').
35
36   Unless the argument noclose is non-zero, daemon() will redirect
37   standard input, standard output and standard error to /dev/null.
38 */
39 int daemon(int nochdir, int noclose) {
40 #ifdef HAVE_FORK
41         pid_t pid;
42         int fd;
43
44         pid = fork();
45
46         /* Check if forking failed */
47         if(pid < 0) {
48                 perror("fork");
49                 exit(-1);
50         }
51
52         /* If we are the parent, terminate */
53         if(pid)
54                 exit(0);
55
56         /* Detach by becoming the new process group leader */
57         if(setsid() < 0) {
58                 perror("setsid");
59                 return -1;
60         }
61
62         /* Change working directory to the root (to avoid keeping mount
63            points busy) */
64         if(!nochdir) {
65                 chdir("/");
66         }
67
68         /* Redirect stdin/out/err to /dev/null */
69         if(!noclose) {
70                 fd = open("/dev/null", O_RDWR);
71
72                 if(fd < 0) {
73                         perror("opening /dev/null");
74                         return -1;
75                 } else {
76                         dup2(fd, 0);
77                         dup2(fd, 1);
78                         dup2(fd, 2);
79                 }
80         }
81
82         return 0;
83 #else
84         return -1;
85 #endif
86 }
87 #endif
88
89 #ifndef HAVE_GET_CURRENT_DIR_NAME
90 /*
91   Replacement for the GNU get_current_dir_name function:
92
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.
96 */
97 char *get_current_dir_name(void) {
98         size_t size;
99         char *buf;
100         char *r;
101
102         /* Start with 100 bytes.  If this turns out to be insufficient to
103            contain the working directory, double the size.  */
104         size = 100;
105         buf = xmalloc(size);
106
107         errno = 0;                                      /* Success */
108         r = getcwd(buf, size);
109
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) {
113                 free(buf);
114                 size <<= 1;                             /* double the size */
115                 buf = xmalloc(size);
116                 r = getcwd(buf, size);
117         }
118
119         return buf;
120 }
121 #endif
122
123 #ifndef HAVE_ASPRINTF
124 int asprintf(char **buf, const char *fmt, ...) {
125         int result;
126         va_list ap;
127         va_start(ap, fmt);
128         result = vasprintf(buf, fmt, ap);
129         va_end(ap);
130         return result;
131 }
132
133 int vasprintf(char **buf, const char *fmt, va_list ap) {
134         int status;
135         va_list aq;
136         int len;
137
138         len = 4096;
139         *buf = xmalloc(len);
140
141         va_copy(aq, ap);
142         status = vsnprintf(*buf, len, fmt, aq);
143         buf[len - 1] = 0;
144         va_end(aq);
145
146         if(status >= 0)
147                 *buf = xrealloc(*buf, status + 1);
148
149         if(status > len - 1) {
150                 len = status;
151                 va_copy(aq, ap);
152                 status = vsnprintf(*buf, len, fmt, aq);
153                 va_end(aq);
154         }
155
156         return status;
157 }
158 #endif
159
160 #ifndef HAVE_GETTIMEOFDAY
161 int gettimeofday(struct timeval *tv, void *tz) {
162         tv->tv_sec = time(NULL);
163         tv->tv_usec = 0;
164         return 0;
165 }
166 #endif
167
168 #ifndef HAVE_USLEEP
169 int usleep(long long usec) {
170         struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
171         select(0, NULL, NULL, NULL, &tv);
172         return 0;
173 }
174 #endif