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