2dd091085656e24bfbbb778de1ae990b07863dad
[tinc] / lib / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2000,2001 Ivo Timmermans <ivo@o2w.nl>,
4                   2000,2001 Guus Sliepen <guus@sliepen.eu.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: dropin.c,v 1.1.2.13 2003/07/12 17:41:45 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32
33 #include <xalloc.h>
34
35 #include <system.h>
36 #include <errno.h>
37
38 #ifndef HAVE_DAEMON
39 /*
40   Replacement for the daemon() function.
41   
42   The daemon() function is for programs wishing to detach themselves
43   from the controlling terminal and run in the background as system
44   daemons.
45
46   Unless the argument nochdir is non-zero, daemon() changes the
47   current working directory to the root (``/'').
48
49   Unless the argument noclose is non-zero, daemon() will redirect
50   standard input, standard output and standard error to /dev/null.
51 */
52 int daemon(int nochdir, int noclose)
53 {
54         pid_t pid;
55         int fd;
56
57         pid = fork();
58
59         /* Check if forking failed */
60         if(pid < 0) {
61                 perror("fork");
62                 exit(-1);
63         }
64
65         /* If we are the parent, terminate */
66         if(pid)
67                 exit(0);
68
69         /* Detach by becoming the new process group leader */
70         if(setsid() < 0) {
71                 perror("setsid");
72                 return -1;
73         }
74
75         /* Change working directory to the root (to avoid keeping mount
76            points busy) */
77         if(!nochdir) {
78                 chdir("/");
79         }
80
81         /* Redirect stdin/out/err to /dev/null */
82         if(!noclose) {
83                 fd = open("/dev/null", O_RDWR);
84
85                 if(fd < 0) {
86                         perror("opening /dev/null");
87                         return -1;
88                 } else {
89                         dup2(fd, 0);
90                         dup2(fd, 1);
91                         dup2(fd, 2);
92                 }
93         }
94
95         return 0;
96 }
97 #endif
98
99 #ifndef HAVE_GET_CURRENT_DIR_NAME
100 /*
101   Replacement for the GNU get_current_dir_name function:
102
103   get_current_dir_name will malloc(3) an array big enough to hold the
104   current directory name.  If the environment variable PWD is set, and
105   its value is correct, then that value will be returned.
106 */
107 char *get_current_dir_name(void)
108 {
109         size_t size;
110         char *buf;
111         char *r;
112
113         /* Start with 100 bytes.  If this turns out to be insufficient to
114            contain the working directory, double the size.  */
115         size = 100;
116         buf = xmalloc(size);
117
118         errno = 0;                                      /* Success */
119         r = getcwd(buf, size);
120
121         /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
122            is insufficient to contain the entire working directory.  */
123         while(r == NULL && errno == ERANGE) {
124                 free(buf);
125                 size <<= 1;                             /* double the size */
126                 buf = xmalloc(size);
127                 r = getcwd(buf, size);
128         }
129
130         return buf;
131 }
132 #endif
133
134 #ifndef HAVE_ASPRINTF
135 int asprintf(char **buf, const char *fmt, ...)
136 {
137         int status;
138         va_list ap;
139         int len;
140
141         len = 4096;
142         *buf = xmalloc(len);
143
144         va_start(ap, fmt);
145         status = vsnprintf(*buf, len, fmt, ap);
146         va_end(ap);
147
148         if(status >= 0)
149                 *buf = xrealloc(*buf, status);
150
151         if(status > len - 1) {
152                 len = status;
153                 va_start(ap, fmt);
154                 status = vsnprintf(*buf, len, fmt, ap);
155                 va_end(ap);
156         }
157
158         return status;
159 }
160 #endif