Update the address of the Free Software Foundation in all copyright headers.
[tinc] / lib / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2000-2005 Ivo Timmermans,
4                   2000-2009 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 {
41 #ifdef HAVE_FORK
42         pid_t pid;
43         int fd;
44
45         pid = fork();
46
47         /* Check if forking failed */
48         if(pid < 0) {
49                 perror("fork");
50                 exit(-1);
51         }
52
53         /* If we are the parent, terminate */
54         if(pid)
55                 exit(0);
56
57         /* Detach by becoming the new process group leader */
58         if(setsid() < 0) {
59                 perror("setsid");
60                 return -1;
61         }
62
63         /* Change working directory to the root (to avoid keeping mount
64            points busy) */
65         if(!nochdir) {
66                 chdir("/");
67         }
68
69         /* Redirect stdin/out/err to /dev/null */
70         if(!noclose) {
71                 fd = open("/dev/null", O_RDWR);
72
73                 if(fd < 0) {
74                         perror("opening /dev/null");
75                         return -1;
76                 } else {
77                         dup2(fd, 0);
78                         dup2(fd, 1);
79                         dup2(fd, 2);
80                 }
81         }
82
83         return 0;
84 #else
85         return -1;
86 #endif
87 }
88 #endif
89
90 #ifndef HAVE_GET_CURRENT_DIR_NAME
91 /*
92   Replacement for the GNU get_current_dir_name function:
93
94   get_current_dir_name will malloc(3) an array big enough to hold the
95   current directory name.  If the environment variable PWD is set, and
96   its value is correct, then that value will be returned.
97 */
98 char *get_current_dir_name(void)
99 {
100         size_t size;
101         char *buf;
102         char *r;
103
104         /* Start with 100 bytes.  If this turns out to be insufficient to
105            contain the working directory, double the size.  */
106         size = 100;
107         buf = xmalloc(size);
108
109         errno = 0;                                      /* Success */
110         r = getcwd(buf, size);
111
112         /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
113            is insufficient to contain the entire working directory.  */
114         while(r == NULL && errno == ERANGE) {
115                 free(buf);
116                 size <<= 1;                             /* double the size */
117                 buf = xmalloc(size);
118                 r = getcwd(buf, size);
119         }
120
121         return buf;
122 }
123 #endif
124
125 #ifndef HAVE_ASPRINTF
126 int asprintf(char **buf, const char *fmt, ...) {
127         int result;
128         va_list ap;
129         va_start(ap, fmt);
130         result = vasprintf(buf, fmt, ap);
131         va_end(ap);
132         return result;
133 }
134
135 int vasprintf(char **buf, const char *fmt, va_list ap) {
136         int status;
137         va_list aq;
138         int len;
139
140         len = 4096;
141         *buf = xmalloc(len);
142
143         va_copy(aq, ap);
144         status = vsnprintf(*buf, len, fmt, aq);
145         va_end(aq);
146
147         if(status >= 0)
148                 *buf = xrealloc(*buf, status + 1);
149
150         if(status > len - 1) {
151                 len = status;
152                 va_copy(aq, ap);
153                 status = vsnprintf(*buf, len, fmt, aq);
154                 va_end(aq);
155         }
156
157         return status;
158 }
159 #endif
160
161 #ifndef HAVE_GETTIMEOFDAY
162 int gettimeofday(struct timeval *tv, void *tz) {
163         tv->tv_sec = time(NULL);
164         tv->tv_usec = 0;
165         return 0;
166 }
167 #endif