s/sliepen.warande.net/sliepen.eu.org/g
[oweals/tinc.git] / 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.10 2002/06/21 10:11:11 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 #include "fake-getnameinfo.c"
39
40 #ifndef HAVE_DAEMON
41 /*
42   Replacement for the daemon() function.
43   
44   The daemon() function is for programs wishing to detach themselves
45   from the controlling terminal and run in the background as system
46   daemons.
47
48   Unless the argument nochdir is non-zero, daemon() changes the
49   current working directory to the root (``/'').
50
51   Unless the argument noclose is non-zero, daemon() will redirect
52   standard input, standard output and standard error to /dev/null.
53 */
54 int daemon(int nochdir, int noclose)
55 {
56   pid_t pid;
57   int fd;
58   
59   pid = fork();
60   
61   /* Check if forking failed */
62   if(pid < 0)
63     {
64       perror("fork");
65       exit(-1);
66     }
67
68   /* If we are the parent, terminate */
69   if(pid)
70     exit(0);
71
72   /* Detach by becoming the new process group leader */
73   if(setsid() < 0)
74     {
75       perror("setsid");
76       return -1;
77     }
78   
79   /* Change working directory to the root (to avoid keeping mount
80      points busy) */
81   if(!nochdir)
82     {
83       chdir("/");
84     }
85     
86   /* Redirect stdin/out/err to /dev/null */
87   if(!noclose)
88     {
89       fd = open("/dev/null", O_RDWR);
90
91       if(fd < 0)
92         {
93           perror("opening /dev/null");
94           return -1;
95         }
96       else
97         {
98           dup2(fd, 0);
99           dup2(fd, 1);
100           dup2(fd, 2);
101         }
102     }
103
104   return 0;
105 }
106 #endif
107
108
109
110
111 #ifndef HAVE_GET_CURRENT_DIR_NAME
112 /*
113   Replacement for the GNU get_current_dir_name function:
114
115   get_current_dir_name will malloc(3) an array big enough to hold the
116   current directory name.  If the environment variable PWD is set, and
117   its value is correct, then that value will be returned.
118 */
119 char *get_current_dir_name(void)
120 {
121   size_t size;
122   char *buf;
123   char *r;
124
125   /* Start with 100 bytes.  If this turns out to be insufficient to
126      contain the working directory, double the size.  */
127   size = 100;
128   buf = xmalloc(size);
129
130   errno = 0; /* Success */
131   r = getcwd(buf, size);
132   /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
133      is insufficient to contain the entire working directory.  */
134   while(r == NULL && errno == ERANGE)
135     {
136       free(buf);
137       size <<= 1; /* double the size */
138       buf = xmalloc(size);
139       r = getcwd(buf, size);
140     }
141
142   return buf;
143 }
144 #endif
145
146 #ifndef HAVE_ASPRINTF
147 int asprintf(char **buf, const char *fmt, ...)
148 {
149   int status;
150   va_list ap;
151   int len;
152   
153   len = 4096;
154   *buf = xmalloc(len);
155
156   va_start(ap, fmt);
157   status = vsnprintf (*buf, len, fmt, ap);
158   va_end (ap);
159
160   if(status >= 0)
161     *buf = xrealloc(*buf, status);
162
163   if(status > len-1)
164     {
165       len = status;
166       va_start(ap, fmt);
167       status = vsnprintf (*buf, len, fmt, ap);
168       va_end (ap);
169     }
170
171   return status;
172 }
173 #endif