Updating HEAD branch #4; Merging CABAL -> HEAD.
[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 <itimmermans@bigfoot.com>,
4                   2000,2001 Guus Sliepen <guus@sliepen.warande.net>
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.2 2002/04/09 15:26:00 zarq 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     {
62       perror("fork");
63       exit(-1);
64     }
65
66   /* If we are the parent, terminate */
67   if(pid)
68     exit(0);
69
70   /* Detach by becoming the new process group leader */
71   if(setsid() < 0)
72     {
73       perror("setsid");
74       return -1;
75     }
76   
77   /* Change working directory to the root (to avoid keeping mount
78      points busy) */
79   if(!nochdir)
80     {
81       chdir("/");
82     }
83     
84   /* Redirect stdin/out/err to /dev/null */
85   if(!noclose)
86     {
87       fd = open("/dev/null", O_RDWR);
88
89       if(fd < 0)
90         {
91           perror("opening /dev/null");
92           return -1;
93         }
94       else
95         {
96           dup2(fd, 0);
97           dup2(fd, 1);
98           dup2(fd, 2);
99         }
100     }
101
102   return 0;
103 }
104 #endif
105
106
107
108
109 #ifndef HAVE_GET_CURRENT_DIR_NAME
110 /*
111   Replacement for the GNU get_current_dir_name function:
112
113   get_current_dir_name will malloc(3) an array big enough to hold the
114   current directory name.  If the environment variable PWD is set, and
115   its value is correct, then that value will be returned.
116 */
117 char *get_current_dir_name(void)
118 {
119   size_t size;
120   char *buf;
121   char *r;
122
123   /* Start with 100 bytes.  If this turns out to be insufficient to
124      contain the working directory, double the size.  */
125   size = 100;
126   buf = xmalloc(size);
127
128   errno = 0; /* Success */
129   r = getcwd(buf, size);
130   /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
131      is insufficient to contain the entire working directory.  */
132   while(r == NULL && errno == ERANGE)
133     {
134       free(buf);
135       size <<= 1; /* double the size */
136       buf = xmalloc(size);
137       r = getcwd(buf, size);
138     }
139
140   return buf;
141 }
142 #endif
143
144 #ifndef HAVE_ASPRINTF
145 int asprintf(char **buf, const char *fmt, ...)
146 {
147   int status;
148   va_list ap;
149   int len;
150   
151   len = 4096;
152   *buf = xmalloc(len);
153
154   va_start(ap, fmt);
155   status = vsnprintf (*buf, len, fmt, ap);
156   va_end (ap);
157
158   if(status >= 0)
159     *buf = xrealloc(*buf, status);
160
161   if(status > len-1)
162     {
163       len = status;
164       va_start(ap, fmt);
165       status = vsnprintf (*buf, len, fmt, ap);
166       va_end (ap);
167     }
168
169   return status;
170 }
171 #endif