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