xref: /netbsd-src/external/bsd/nsd/dist/contrib/patch_for_s6_startup_and_other_service_supervisors.diff (revision febe9f0745506a2002b7f9f000e4bd0f5b50cb88)
1diff --git nsd.c nsd.c
2index 98dec613..e9d7b2cc 100644
3--- nsd.c
4+++ nsd.c
5@@ -91,6 +91,9 @@ usage (void)
6 		"  -n tcp-count         The maximum number of TCP connections per server.\n"
7 		"  -P pidfile           Specify the PID file to write.\n"
8 		"  -p port              Specify the port to listen to.\n"
9+		"  -r                   Print a newline into the file descriptor index\n"
10+		"                          described in the READY_FD environment variable to\n"
11+		"                          indicate that nsd is ready to accept connections.\n"
12 		"  -s seconds           Dump statistics every SECONDS seconds.\n"
13 		"  -t chrootdir         Change root to specified directory on startup.\n"
14 		);
15@@ -323,6 +326,24 @@ sig_handler(int sig)
16 	}
17 }
18
19+/*
20+ * Parse envvar as a positive integer.
21+ *
22+ */
23+int
24+get_fd_from_env(const char* key)
25+{
26+	char *env = getenv(key);
27+	if (!env || env[0] == '\0')
28+		return -1;
29+	errno = 0;
30+	char *endptr;
31+	int fd = (int)strtol(env, &endptr, 10);
32+	if (errno != 0 || endptr[0] != '\0')
33+		return -1;
34+	return fd;
35+}
36+
37 /*
38  * Statistic output...
39  *
40@@ -450,7 +471,7 @@ main(int argc, char *argv[])
41 	}
42
43 	/* Parse the command line... */
44-	while ((c = getopt(argc, argv, "46a:c:df:hi:I:l:N:n:P:p:s:u:t:X:V:v"
45+	while ((c = getopt(argc, argv, "46a:c:df:hi:I:l:N:n:P:p:rs:u:t:X:V:v"
46 #ifndef NDEBUG /* <mattthijs> only when configured with --enable-checking */
47 		"F:L:"
48 #endif /* NDEBUG */
49@@ -533,6 +554,9 @@ main(int argc, char *argv[])
50 			tcp_port = optarg;
51 			udp_port = optarg;
52 			break;
53+		case 'r':
54+			nsd.readyfd = 1;
55+			break;
56 		case 's':
57 #ifdef BIND8_STATS
58 			nsd.st.period = atoi(optarg);
59@@ -965,6 +989,18 @@ main(int argc, char *argv[])
60 	}
61 #endif /* HAVE_SSL */
62
63+	/* When asked to notify readiness via REⒶDY_FD, do so. */
64+	if (nsd.readyfd) {
65+		int readyfd = get_fd_from_env("READY_FD");
66+		if (readyfd < 0)
67+			error("READY_FD unset or contains garbage");
68+		unsetenv("READY_FD");
69+		int ret = dprintf(readyfd, "\n");
70+		close(readyfd);
71+		if (ret < 0)
72+			error("could not write to READY_FD index");
73+	}
74+
75 	/* Unless we're debugging, fork... */
76 	if (!nsd.debug) {
77 		int fd;
78diff --git nsd.h nsd.h
79index de3ae8e1..ff27b798 100644
80--- nsd.h
81+++ nsd.h
82@@ -179,6 +179,7 @@ struct	nsd
83 	unsigned		server_kind;
84 	struct namedb	*db;
85 	int				debug;
86+	int readyfd;
87
88 	size_t            child_count;
89 	struct nsd_child *children;
90