Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/ftp/ftpproxy/files/patch-src_daemon.c
16461 views
1
--- src/daemon.c.orig 2003-10-26 22:10:19 UTC
2
+++ src/daemon.c
3
@@ -33,6 +33,7 @@
4
#include <signal.h>
5
#include <sys/wait.h>
6
#include <pwd.h>
7
+#include <err.h>
8
9
#include <sys/types.h>
10
#include <sys/stat.h>
11
@@ -51,20 +52,35 @@
12
13
int acceptloop(int sock)
14
{
15
- int connect, pid, len;
16
+ int connect, pid, otherpid;
17
+ socklen_t len;
18
struct sockaddr_in client;
19
20
/*
21
* Go into background.
22
*/
23
24
- if (debug != 0)
25
- ; /* Do not fork in debug mode */
26
+ pfh = NULL;
27
28
- else if ((pid = fork()) > 0)
29
- exit (0);
30
+ if (debug == 0) {
31
+ pfh = pidfile_open(pidfile, 0600, &otherpid);
32
+ if (pfh == NULL) {
33
+ if (errno == EEXIST) {
34
+ errx(EXIT_FAILURE, "ftp.proxy already running, pid %jd.",
35
+ (intmax_t)otherpid);
36
+ }
37
+ warnx("Cannot open or create pid file %s", pidfile);
38
+ }
39
+ if (daemon(0, 0) == -1) {
40
+ warnx("Cannot daemonize");
41
+ pidfile_remove(pfh);
42
+ exit(EXIT_FAILURE);
43
+ }
44
+ pidfile_write(pfh);
45
+ }
46
47
fprintf (stderr, "\nstarting ftp.proxy %s in daemon mode ...\n", VERSION);
48
+
49
while (1) {
50
51
/*
52
@@ -82,7 +98,7 @@ int acceptloop(int sock)
53
54
if ((pid = fork()) < 0) {
55
fprintf (stderr, "%04X: can't fork process: %s\n", getpid(), strerror(errno));
56
- exit (1);
57
+ exit(EXIT_FAILURE);
58
}
59
else if (pid == 0) {
60
int optlen;
61
@@ -112,7 +128,9 @@ int acceptloop(int sock)
62
63
close (1);
64
fprintf (stderr, "%04X: terminating\n", getpid());
65
-
66
- exit (0);
67
+ if (pfh != NULL) {
68
+ pidfile_remove(pfh);
69
+ }
70
+ exit (EXIT_SUCCESS);
71
}
72
73
74