HAMMER - Stabilize and refactor volume removal
[dragonfly.git] / usr.sbin / ypserv / yp_main.c
blob3debd1cbdc21657f41e14553098616f1547a3a3e
1 /*
2 * Copyright (c) 1995
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * $FreeBSD: src/usr.sbin/ypserv/yp_main.c,v 1.29 2008/02/03 17:39:37 matteo Exp $
33 * $DragonFly: src/usr.sbin/ypserv/yp_main.c,v 1.5 2005/11/24 22:23:02 swildner Exp $
37 * ypserv startup function.
38 * We need out own main() since we have to do some additional work
39 * that rpcgen won't do for us. Most of this file was generated using
40 * rpcgen.new, and later modified.
43 #include "yp.h"
44 #include <err.h>
45 #include <errno.h>
46 #include <memory.h>
47 #include <stdio.h>
48 #include <signal.h>
49 #include <stdlib.h> /* getenv, exit */
50 #include <string.h> /* strcmp */
51 #include <syslog.h>
52 #include <unistd.h>
53 #include <rpc/pmap_clnt.h> /* for pmap_unset */
54 #ifdef __cplusplus
55 #include <sysent.h> /* getdtablesize, open */
56 #endif /* __cplusplus */
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <sys/wait.h>
60 #include "yp_extern.h"
61 #include <rpc/rpc.h>
63 #ifndef SIG_PF
64 #define SIG_PF void(*)(int)
65 #endif
67 #define _RPCSVC_CLOSEDOWN 120
68 int _rpcpmstart; /* Started by a port monitor ? */
69 static int _rpcfdtype;
70 /* Whether Stream or Datagram ? */
71 /* States a server can be in wrt request */
73 #define _IDLE 0
74 #define _SERVED 1
75 #define _SERVING 2
77 extern void ypprog_1(struct svc_req *, SVCXPRT *);
78 extern void ypprog_2(struct svc_req *, SVCXPRT *);
79 extern int _rpc_dtablesize(void);
80 extern int _rpcsvcstate; /* Set when a request is serviced */
81 char *progname = "ypserv";
82 const char *yp_dir = _PATH_YP;
83 /*int debug = 0;*/
84 int do_dns = 0;
85 int resfd;
87 struct socktype {
88 const char *st_name;
89 int st_type;
91 static struct socktype stlist[] = {
92 { "tcp", SOCK_STREAM },
93 { "udp", SOCK_DGRAM },
94 { NULL, 0 }
97 static void
98 _msgout(char *msg)
100 if (debug) {
101 if (_rpcpmstart)
102 syslog(LOG_ERR, "%s", msg);
103 else
104 warnx("%s", msg);
105 } else
106 syslog(LOG_ERR, "%s", msg);
109 pid_t yp_pid;
111 static void
112 yp_svc_run(void)
114 #ifdef FD_SETSIZE
115 fd_set readfds;
116 #else
117 int readfds;
118 #endif /* def FD_SETSIZE */
119 int fd_setsize = _rpc_dtablesize();
120 struct timeval timeout;
122 /* Establish the identity of the parent ypserv process. */
123 yp_pid = getpid();
125 for (;;) {
126 #ifdef FD_SETSIZE
127 readfds = svc_fdset;
128 #else
129 readfds = svc_fds;
130 #endif /* def FD_SETSIZE */
132 FD_SET(resfd, &readfds);
134 timeout.tv_sec = RESOLVER_TIMEOUT;
135 timeout.tv_usec = 0;
136 switch (select(fd_setsize, &readfds, NULL, NULL,
137 &timeout)) {
138 case -1:
139 if (errno == EINTR) {
140 continue;
142 warn("svc_run: - select failed");
143 return;
144 case 0:
145 if (getpid() == yp_pid)
146 yp_prune_dnsq();
147 break;
148 default:
149 if (getpid() == yp_pid) {
150 if (FD_ISSET(resfd, &readfds)) {
151 yp_run_dnsq();
152 FD_CLR(resfd, &readfds);
154 svc_getreqset(&readfds);
157 if (yp_pid != getpid())
158 _exit(0);
162 static void
163 unregister(void)
165 pmap_unset(YPPROG, YPVERS);
166 pmap_unset(YPPROG, YPOLDVERS);
169 static void
170 reaper(int sig)
172 int status;
173 int saved_errno;
175 saved_errno = errno;
177 if (sig == SIGHUP) {
178 load_securenets();
179 #ifdef DB_CACHE
180 yp_flush_all();
181 #endif
182 errno = saved_errno;
183 return;
186 if (sig == SIGCHLD) {
187 while (wait3(&status, WNOHANG, NULL) > 0)
188 children--;
189 } else {
190 unregister();
191 exit(0);
193 errno = saved_errno;
194 return;
197 static void
198 usage(void)
200 fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path] [-P port]\n");
201 exit(1);
204 static void
205 closedown(int sig)
207 if (_rpcsvcstate == _IDLE) {
208 extern fd_set svc_fdset;
209 static int size;
210 int i, openfd;
212 if (_rpcfdtype == SOCK_DGRAM) {
213 unregister();
214 exit(0);
216 if (size == 0) {
217 size = getdtablesize();
219 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
220 if (FD_ISSET(i, &svc_fdset))
221 openfd++;
222 if (openfd <= 1) {
223 unregister();
224 exit(0);
227 if (_rpcsvcstate == _SERVED)
228 _rpcsvcstate = _IDLE;
230 signal(SIGALRM, (SIG_PF)closedown);
231 alarm(_RPCSVC_CLOSEDOWN / 2);
235 main(int argc, char **argv)
237 SVCXPRT *transp = NULL;
238 int sock;
239 int proto = 0;
240 struct sockaddr_in saddr;
241 socklen_t asize = sizeof (saddr);
242 int ch;
243 in_port_t yp_port = 0;
244 char *errstr;
245 struct socktype *st;
247 while ((ch = getopt(argc, argv, "hdnp:P:")) != -1) {
248 switch (ch) {
249 case 'd':
250 debug = ypdb_debug = 1;
251 break;
252 case 'n':
253 do_dns = 1;
254 break;
255 case 'p':
256 yp_dir = optarg;
257 break;
258 case 'P':
259 yp_port = (in_port_t)strtonum(optarg, 1, 65535,
260 (const char **)&errstr);
261 if (yp_port == 0 && errstr != NULL) {
262 _msgout("invalid port number provided");
263 exit(1);
265 break;
266 case 'h':
267 default:
268 usage();
272 load_securenets();
273 yp_init_resolver();
274 #ifdef DB_CACHE
275 yp_init_dbs();
276 #endif
277 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
278 int ssize = sizeof (int);
280 if (saddr.sin_family != AF_INET)
281 exit(1);
282 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
283 (char *)&_rpcfdtype, &ssize) == -1)
284 exit(1);
285 sock = 0;
286 _rpcpmstart = 1;
287 proto = 0;
288 openlog("ypserv", LOG_PID, LOG_DAEMON);
289 } else {
290 if (!debug) {
291 if (daemon(0,0)) {
292 err(1,"cannot fork");
294 openlog("ypserv", LOG_PID, LOG_DAEMON);
296 sock = RPC_ANYSOCK;
297 pmap_unset(YPPROG, YPVERS);
298 pmap_unset(YPPROG, 1);
302 * Initialize TCP/UDP sockets.
304 memset((char *)&saddr, 0, sizeof(saddr));
305 saddr.sin_family = AF_INET;
306 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
307 saddr.sin_port = htons(yp_port);
308 for (st = stlist; st->st_name != NULL; st++) {
309 /* Do not bind the socket if the user didn't specify a port */
310 if (yp_port == 0)
311 break;
313 sock = socket(AF_INET, st->st_type, 0);
314 if (sock == -1) {
315 if ((asprintf(&errstr, "cannot create a %s socket",
316 st->st_name)) == -1)
317 err(1, "unexpected failure in asprintf()");
318 _msgout(errstr);
319 free((void *)errstr);
320 exit(1);
322 if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr))
323 == -1) {
324 if ((asprintf(&errstr, "cannot bind %s socket",
325 st->st_name)) == -1)
326 err(1, "unexpected failure in asprintf()");
327 _msgout(errstr);
328 free((void *)errstr);
329 exit(1);
331 errstr = NULL;
334 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
335 transp = svcudp_create(sock);
336 if (transp == NULL) {
337 _msgout("cannot create udp service");
338 exit(1);
340 if (!_rpcpmstart)
341 proto = IPPROTO_UDP;
342 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
343 _msgout("unable to register (YPPROG, YPOLDVERS, udp)");
344 exit(1);
346 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
347 _msgout("unable to register (YPPROG, YPVERS, udp)");
348 exit(1);
352 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
353 transp = svctcp_create(sock, 0, 0);
354 if (transp == NULL) {
355 _msgout("cannot create tcp service");
356 exit(1);
358 if (!_rpcpmstart)
359 proto = IPPROTO_TCP;
360 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
361 _msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
362 exit(1);
364 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
365 _msgout("unable to register (YPPROG, YPVERS, tcp)");
366 exit(1);
370 if (transp == NULL) {
371 _msgout("could not create a handle");
372 exit(1);
374 if (_rpcpmstart) {
375 signal(SIGALRM, (SIG_PF)closedown);
376 alarm(_RPCSVC_CLOSEDOWN / 2);
379 * Make sure SIGPIPE doesn't blow us away while servicing TCP
380 * connections.
382 signal(SIGPIPE, SIG_IGN);
383 signal(SIGCHLD, (SIG_PF) reaper);
384 signal(SIGTERM, (SIG_PF) reaper);
385 signal(SIGINT, (SIG_PF) reaper);
386 signal(SIGHUP, (SIG_PF) reaper);
387 yp_svc_run();
388 _msgout("svc_run returned");
389 exit(1);
390 /* NOTREACHED */