Browse code

build: move daemon() emulation into compat

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Acked-by: Samuli Seppänen <samuli@openvpn.net>
Acked-by: David Sommerseth <davids@redhat.com>
Signed-off-by: David Sommerseth <davids@redhat.com>

Alon Bar-Lev authored on 2012/03/01 05:12:16
Showing 7 changed files
... ...
@@ -21,4 +21,5 @@ libcompat_la_SOURCES = \
21 21
 	compat.h \
22 22
 	compat-dirname.c \
23 23
 	compat-basename.c \
24
-	compat-gettimeofday.c
24
+	compat-gettimeofday.c \
25
+	compat-daemon.c
25 26
new file mode 100644
... ...
@@ -0,0 +1,100 @@
0
+/*
1
+ *  OpenVPN -- An application to securely tunnel IP networks
2
+ *             over a single UDP port, with support for SSL/TLS-based
3
+ *             session authentication and key exchange,
4
+ *             packet encryption, packet authentication, and
5
+ *             packet compression.
6
+ *
7
+ *  Copyright (C) 2011 - David Sommerseth <davids@redhat.com>
8
+ *
9
+ *  This program is free software; you can redistribute it and/or modify
10
+ *  it under the terms of the GNU General Public License version 2
11
+ *  as published by the Free Software Foundation.
12
+ *
13
+ *  This program is distributed in the hope that it will be useful,
14
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ *  GNU General Public License for more details.
17
+ *
18
+ *  You should have received a copy of the GNU General Public License
19
+ *  along with this program (see the file COPYING included with this
20
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
21
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
+ */
23
+
24
+#ifdef HAVE_CONFIG_H
25
+#include "config.h"
26
+#elif defined(_MSC_VER)
27
+#include "config-msvc.h"
28
+#endif
29
+
30
+#ifndef HAVE_DAEMON
31
+
32
+#ifdef HAVE_UNISTD_H
33
+#include <unistd.h>
34
+#endif
35
+
36
+#ifdef HAVE_STDLIB_H
37
+#include <stdlib.h>
38
+#endif
39
+
40
+#ifdef HAVE_SYS_TYPES_H
41
+#include <sys/types.h>
42
+#endif
43
+
44
+#ifdef HAVE_SYS_STAT_H
45
+#include <sys/stat.h>
46
+#endif
47
+
48
+#ifdef HAVE_FCNTL_H
49
+#include <fcntl.h>
50
+#endif
51
+
52
+#ifdef HAVE_ERRNO_H
53
+#include <errno.h>
54
+#endif
55
+
56
+int
57
+daemon(int nochdir, int noclose)
58
+{
59
+#if defined(HAVE_FORK) && defined(HAVE_SETSID)
60
+	switch (fork()) {
61
+		case -1:
62
+			return (-1);
63
+		case 0:
64
+		break;
65
+		default:
66
+			exit(0);
67
+	}
68
+
69
+	if (setsid() == -1)
70
+		return (-1);
71
+
72
+	if (!nochdir)
73
+		chdir("/");
74
+
75
+	if (!noclose) {
76
+#if defined(HAVE_DUP) && defined(HAVE_DUP2)
77
+		int fd;
78
+		if ((fd = open ("/dev/null", O_RDWR, 0)) != -1) {
79
+			dup2 (fd, 0);
80
+			dup2 (fd, 1);
81
+			dup2 (fd, 2);
82
+			if (fd > 2) {
83
+				close (fd);
84
+			}
85
+		}
86
+#endif
87
+	}
88
+
89
+	return 0;
90
+#else
91
+	(void)nochdir;
92
+	(void)noclose;
93
+	errno = EFAULT;
94
+	return -1;
95
+#endif
96
+}
97
+
98
+#endif
99
+
... ...
@@ -46,4 +46,8 @@ char * basename(char *str);
46 46
 int gettimeofday (struct timeval *tv, void *tz);
47 47
 #endif
48 48
 
49
+#ifndef HAVE_DAEMON
50
+int daemon(int nochdir, int noclose);
51
+#endif
52
+
49 53
 #endif /* COMPAT_H */
... ...
@@ -162,6 +162,10 @@
162 162
 				RelativePath=".\compat-gettimeofday.c"
163 163
 				>
164 164
 			</File>
165
+			<File
166
+				RelativePath=".\compat-daemon.c"
167
+				>
168
+			</File>
165 169
 		</Filter>
166 170
 		<Filter
167 171
 			Name="Header Files"
... ...
@@ -993,7 +993,7 @@ possibly_become_daemon (const struct options *options, const bool first_time)
993 993
     {
994 994
       ASSERT (!options->inetd);
995 995
       if (daemon (options->cd_dir != NULL, options->log) < 0)
996
-	msg (M_ERR, "daemon() failed");
996
+	msg (M_ERR, "daemon() failed or unsupported");
997 997
       restore_signal_state ();
998 998
       if (options->log)
999 999
 	set_std_files_to_null (true);
... ...
@@ -301,38 +301,6 @@ do_mlockall(bool print_msg)
301 301
 #endif
302 302
 }
303 303
 
304
-#ifndef HAVE_DAEMON
305
-
306
-int
307
-daemon(int nochdir, int noclose)
308
-{
309
-#if defined(HAVE_FORK) && defined(HAVE_SETSID)
310
-  switch (fork())
311
-    {
312
-    case -1:
313
-      return (-1);
314
-    case 0:
315
-      break;
316
-    default:
317
-      openvpn_exit (OPENVPN_EXIT_STATUS_GOOD); /* exit point */
318
-    }
319
-
320
-  if (setsid() == -1)
321
-    return (-1);
322
-
323
-  if (!nochdir)
324
-    openvpn_chdir ("/");
325
-
326
-  if (!noclose)
327
-    set_std_files_to_null (false);
328
-#else
329
-  msg (M_FATAL, "Sorry but I can't become a daemon because this operating system doesn't appear to support either the daemon() or fork() system calls");
330
-#endif
331
-  return (0);
332
-}
333
-
334
-#endif
335
-
336 304
 /*
337 305
  * Set standard file descriptors to /dev/null
338 306
  */
... ...
@@ -115,10 +115,6 @@ unsigned int openvpn_getpid (void);
115 115
 
116 116
 void do_mlockall (bool print_msg); /* Disable paging */
117 117
 
118
-#ifndef HAVE_DAEMON
119
-int daemon (int nochdir, int noclose);
120
-#endif
121
-
122 118
 /* check file protections */
123 119
 void warn_if_group_others_accessible(const char* filename);
124 120