K&R style braces
[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-2006 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 #ifdef HAVE_FORK
43         pid_t pid;
44         int fd;
45
46         pid = fork();
47
48         /* Check if forking failed */
49         if(pid < 0) {
50                 perror("fork");
51                 exit(-1);
52         }
53
54         /* If we are the parent, terminate */
55         if(pid)
56                 exit(0);
57
58         /* Detach by becoming the new process group leader */
59         if(setsid() < 0) {
60                 perror("setsid");
61                 return -1;
62         }
63
64         /* Change working directory to the root (to avoid keeping mount
65            points busy) */
66         if(!nochdir) {
67                 chdir("/");
68         }
69
70         /* Redirect stdin/out/err to /dev/null */
71         if(!noclose) {
72                 fd = open("/dev/null", O_RDWR);
73
74                 if(fd < 0) {
75                         perror("opening /dev/null");
76                         return -1;
77                 } else {
78                         dup2(fd, 0);
79                         dup2(fd, 1);
80                         dup2(fd, 2);
81                 }
82         }
83
84         return 0;
85 #else
86         return -1;
87 #endif
88 }
89 #endif
90
91 #ifndef HAVE_GET_CURRENT_DIR_NAME
92 /*
93   Replacement for the GNU get_current_dir_name function:
94
95   get_current_dir_name will malloc(3) an array big enough to hold the
96   current directory name.  If the environment variable PWD is set, and
97   its value is correct, then that value will be returned.
98 */
99 char *get_current_dir_name(void) {
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 status;
128         va_list ap;
129         int len;
130
131         len = 4096;
132         *buf = xmalloc(len);
133
134         va_start(ap, fmt);
135         status = vsnprintf(*buf, len, fmt, ap);
136         va_end(ap);
137
138         if(status >= 0)
139                 *buf = xrealloc(*buf, status + 1);
140
141         if(status > len - 1) {
142                 len = status;
143                 va_start(ap, fmt);
144                 status = vsnprintf(*buf, len, fmt, ap);
145                 va_end(ap);
146         }
147
148         return status;
149 }
150 #endif
151
152 #ifndef HAVE_GETTIMEOFDAY
153 int gettimeofday(struct timeval *tv, void *tz) {
154         tv->tv_sec = time(NULL);
155         tv->tv_usec = 0;
156         return 0;
157 }
158 #endif
159
160 #ifndef HAVE_RANDOM
161 #include <openssl/rand.h>
162
163 long int random(void) {
164         long int x;
165         
166         RAND_pseudo_bytes((unsigned char *)&x, sizeof(x));
167
168         return x;
169 }
170 #endif