9de5c2c593daccdd4e515c60fb06cc455c1a395b
[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.4 2001/02/06 10:12:51 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
31 #include <xalloc.h>
32
33 #include <system.h>
34 #include <errno.h>
35
36 #ifndef HAVE_DAEMON
37 /*
38   Replacement for the daemon() function.
39   
40   The daemon() function is for programs wishing to detach themselves
41   from the controlling terminal and run in the background as system
42   daemons.
43
44   Unless the argument nochdir is non-zero, daemon() changes the
45   current working directory to the root (``/'').
46
47   Unless the argument noclose is non-zero, daemon() will redirect
48   standard input, standard output and standard error to /dev/null.
49 */
50 int daemon(int nochdir, int noclose)
51 {
52   pid_t pid;
53   int fd;
54   
55   pid = fork();
56   
57   /* Check if forking failed */
58   if(pid < 0)
59     {
60       perror("fork");
61       exit(-1);
62     }
63
64   /* If we are the parent, terminate */
65   if(pid)
66     exit(0);
67
68   /* Detach by becoming the new process group leader */
69   if(setsid() < 0)
70     {
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     {
79       chdir("/");
80     }
81     
82   /* Redirect stdin/out/err to /dev/null */
83   if(!noclose)
84     {
85       fd = open("/dev/null", O_RDWR);
86
87       if(fd < 0)
88         {
89           perror("opening /dev/null");
90           return -1;
91         }
92       else
93         {
94           dup2(fd, 0);
95           dup2(fd, 1);
96           dup2(fd, 2);
97         }
98     }
99 }
100 #endif
101
102
103
104
105 #ifndef HAVE_GET_CURRENT_DIR_NAME
106 /*
107   Replacement for the GNU get_current_dir_name function:
108
109   get_current_dir_name will malloc(3) an array big enough to hold the
110   current directory name.  If the environment variable PWD is set, and
111   its value is correct, then that value will be returned.
112 */
113 char *get_current_dir_name(void)
114 {
115   size_t size;
116   char *buf;
117   char *r;
118
119   /* Start with 100 bytes.  If this turns out to be insufficient to
120      contain the working directory, double the size.  */
121   size = 100;
122   buf = xmalloc(size);
123
124   errno = 0; /* Success */
125   r = getcwd(buf, size);
126   /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
127      is insufficient to contain the entire working directory.  */
128   while(r == NULL && errno == ERANGE)
129     {
130       free(buf);
131       size <<= 1; /* double the size */
132       buf = xmalloc(size);
133       r = getcwd(buf, size);
134     }
135
136   return buf;
137 }
138 #endif