Raise default crypto algorithms to AES256 and SHA256.
[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 {
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         int result;
130         va_list ap;
131         va_start(ap, fmt);
132         result = vasprintf(buf, fmt, ap);
133         va_end(ap);
134         return result;
135 }
136
137 int vasprintf(char **buf, const char *fmt, va_list ap) {
138 {
139         int status;
140         va_list aq;
141         int len;
142
143         len = 4096;
144         *buf = xmalloc(len);
145
146         va_copy(aq, ap);
147         status = vsnprintf(*buf, len, fmt, aq);
148         va_end(aq);
149
150         if(status >= 0)
151                 *buf = xrealloc(*buf, status + 1);
152
153         if(status > len - 1) {
154                 len = status;
155                 va_copy(aq, ap);
156                 status = vsnprintf(*buf, len, fmt, aq);
157                 va_end(aq);
158         }
159
160         return status;
161 }
162 #endif
163
164 #ifndef HAVE_GETTIMEOFDAY
165 int gettimeofday(struct timeval *tv, void *tz) {
166         tv->tv_sec = time(NULL);
167         tv->tv_usec = 0;
168         return 0;
169 }
170 #endif