Update copyrights, links, email addresses and let Subversion update $Id$ keywords.
[oweals/tinc.git] / lib / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4                   2000-2004 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
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #include "system.h"
24
25 #include "xalloc.h"
26
27 #ifndef HAVE_DAEMON
28 /*
29   Replacement for the daemon() function.
30   
31   The daemon() function is for programs wishing to detach themselves
32   from the controlling terminal and run in the background as system
33   daemons.
34
35   Unless the argument nochdir is non-zero, daemon() changes the
36   current working directory to the root (``/'').
37
38   Unless the argument noclose is non-zero, daemon() will redirect
39   standard input, standard output and standard error to /dev/null.
40 */
41 int daemon(int nochdir, int noclose)
42 {
43 #ifdef HAVE_FORK
44         pid_t pid;
45         int fd;
46
47         pid = fork();
48
49         /* Check if forking failed */
50         if(pid < 0) {
51                 perror("fork");
52                 exit(-1);
53         }
54
55         /* If we are the parent, terminate */
56         if(pid)
57                 exit(0);
58
59         /* Detach by becoming the new process group leader */
60         if(setsid() < 0) {
61                 perror("setsid");
62                 return -1;
63         }
64
65         /* Change working directory to the root (to avoid keeping mount
66            points busy) */
67         if(!nochdir) {
68                 chdir("/");
69         }
70
71         /* Redirect stdin/out/err to /dev/null */
72         if(!noclose) {
73                 fd = open("/dev/null", O_RDWR);
74
75                 if(fd < 0) {
76                         perror("opening /dev/null");
77                         return -1;
78                 } else {
79                         dup2(fd, 0);
80                         dup2(fd, 1);
81                         dup2(fd, 2);
82                 }
83         }
84
85         return 0;
86 #else
87         return -1;
88 #endif
89 }
90 #endif
91
92 #ifndef HAVE_GET_CURRENT_DIR_NAME
93 /*
94   Replacement for the GNU get_current_dir_name function:
95
96   get_current_dir_name will malloc(3) an array big enough to hold the
97   current directory name.  If the environment variable PWD is set, and
98   its value is correct, then that value will be returned.
99 */
100 char *get_current_dir_name(void)
101 {
102         size_t size;
103         char *buf;
104         char *r;
105
106         /* Start with 100 bytes.  If this turns out to be insufficient to
107            contain the working directory, double the size.  */
108         size = 100;
109         buf = xmalloc(size);
110
111         errno = 0;                                      /* Success */
112         r = getcwd(buf, size);
113
114         /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
115            is insufficient to contain the entire working directory.  */
116         while(r == NULL && errno == ERANGE) {
117                 free(buf);
118                 size <<= 1;                             /* double the size */
119                 buf = xmalloc(size);
120                 r = getcwd(buf, size);
121         }
122
123         return buf;
124 }
125 #endif
126
127 #ifndef HAVE_ASPRINTF
128 int asprintf(char **buf, const char *fmt, ...)
129 {
130         int status;
131         va_list ap;
132         int len;
133
134         len = 4096;
135         *buf = xmalloc(len);
136
137         va_start(ap, fmt);
138         status = vsnprintf(*buf, len, fmt, ap);
139         va_end(ap);
140
141         if(status >= 0)
142                 *buf = xrealloc(*buf, status + 1);
143
144         if(status > len - 1) {
145                 len = status;
146                 va_start(ap, fmt);
147                 status = vsnprintf(*buf, len, fmt, ap);
148                 va_end(ap);
149         }
150
151         return status;
152 }
153 #endif
154
155 #ifndef HAVE_GETTIMEOFDAY
156 int gettimeofday(struct timeval *tv, void *tz) {
157         tv->tv_sec = time(NULL);
158         tv->tv_usec = 0;
159         return 0;
160 }
161 #endif
162
163 #ifndef HAVE_RANDOM
164 #include <openssl/rand.h>
165
166 long int random(void) {
167         long int x;
168         
169         RAND_pseudo_bytes((unsigned char *)&x, sizeof(x));
170
171         return x;
172 }
173 #endif