File: add-foreground-option.patch

package info (click to toggle)
popa3d 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 452 kB
  • sloc: ansic: 2,334; makefile: 61; sh: 53
file content (147 lines) | stat: -rw-r--r-- 3,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
From: Andrew Bower <andrew@bower.uk>
Date: Tue, 19 Aug 2025 23:17:37 +0100
Bug: https://github.com/openwall/popa3d/issues/1
Forwarded: https://github.com/openwall/popa3d/pull/2
Last-Update: 2025-08-20
Subject: Add foreground server option

New '-F' option is like '-D' but does not fork. This enables improved init
system integration methods and can make up for the lack of a PID file writing
option.
---
 popa3d.8     |  8 ++++++++
 standalone.c | 29 +++++++++++++++++------------
 startup.c    | 12 ++++++++----
 3 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/popa3d.8 b/popa3d.8
index 2fcfca0..d7a0c3d 100644
--- a/popa3d.8
+++ b/popa3d.8
@@ -4,6 +4,7 @@ popa3d \- Post Office Protocol (POP3) server
 .SH SYNOPSIS
 .B popa3d
 .RB [ -D ]
+.RB [ -F ]
 .RB [ -V ]
 .SH DESCRIPTION
 .B popa3d
@@ -44,6 +45,13 @@ In this mode
 also does quite a few checks to significantly reduce the impact of
 connection flood attacks.
 .TP
+.B -F
+Foreground server mode.
+Like
+.B -D
+but staying in the foreground rather than becoming a daemon.
+This supports improved service integration with init systems.
+.TP
 .B -V
 Print version information and exit.
 .SH COMMANDS
diff --git a/standalone.c b/standalone.c
index 216d937..8fb8a2b 100644
--- a/standalone.c
+++ b/standalone.c
@@ -103,11 +103,13 @@ static void check_access(int sock)
 #endif
 
 #if POP_OPTIONS
-int do_standalone(void)
+int do_standalone(int foreground)
+{
 #else
 int main(void)
-#endif
 {
+	int foreground = 0;
+#endif
 	int true = 1;
 	int sock, new;
 	struct sockaddr_in addr;
@@ -137,20 +139,23 @@ int main(void)
 		return log_error("listen");
 
 	chdir("/");
-	setsid();
 
-	switch (fork()) {
-	case -1:
-		return log_error("fork");
+	if (!foreground) {
+		setsid();
 
-	case 0:
-		break;
+		switch (fork()) {
+		case -1:
+			return log_error("fork");
 
-	default:
-		return 0;
-	}
+		case 0:
+			break;
 
-	setsid();
+		default:
+			return 0;
+		}
+
+		setsid();
+	}
 
 #if defined(_SC_CLK_TCK) || !defined(CLK_TCK)
 	min_delay = MIN_DELAY * sysconf(_SC_CLK_TCK);
diff --git a/startup.c b/startup.c
index 25b2b9c..a298015 100644
--- a/startup.c
+++ b/startup.c
@@ -15,7 +15,7 @@ extern char popa3d_version[];
 extern char popa3d_date[];
 
 /* standalone.c */
-extern int do_standalone(void);
+extern int do_standalone(int foreground);
 
 /* pop_root.c */
 extern int do_pop_startup(void);
@@ -30,7 +30,7 @@ static char *progname;
 
 static void usage(void)
 {
-	fprintf(stderr, "Usage: %s [-D] [-V]\n", progname);
+	fprintf(stderr, "Usage: %s [-D] [-F] [-V]\n", progname);
 	exit(1);
 }
 
@@ -44,14 +44,18 @@ int main(int argc, char **argv)
 {
 	int c;
 	int standalone = 0;
+	int foreground = 0;
 
 #ifndef HAVE_PROGNAME
 	if (!(progname = argv[0]))
 		progname = POP_SERVER;
 #endif
 
-	while ((c = getopt(argc, argv, "DV")) != -1) {
+	while ((c = getopt(argc, argv, "DFV")) != -1) {
 		switch (c) {
+		case 'F':
+			foreground++;
+			/* fallthrough */
 		case 'D':
 			standalone++;
 			break;
@@ -68,7 +72,7 @@ int main(int argc, char **argv)
 		usage();
 
 	if (standalone)
-		return do_standalone();
+		return do_standalone(foreground);
 
 	if (do_pop_startup()) return 1;
 	return do_pop_session();