dhcpcd: update README.DRAGONFLY
[dragonfly.git] / usr.sbin / ypserv / yp_main.c
blob0911c7475dcb45b52f6c2d8fe6f25a82403e08cc
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 $
36 * ypserv startup function.
37 * We need out own main() since we have to do some additional work
38 * that rpcgen won't do for us. Most of this file was generated using
39 * rpcgen.new, and later modified.
42 #include "yp.h"
43 #include <err.h>
44 #include <errno.h>
45 #include <memory.h>
46 #include <stdio.h>
47 #include <signal.h>
48 #include <stdlib.h> /* getenv, exit */
49 #include <string.h> /* strcmp */
50 #include <syslog.h>
51 #include <unistd.h>
52 #include <rpc/pmap_clnt.h> /* for pmap_unset */
53 #ifdef __cplusplus
54 #include <sysent.h> /* getdtablesize, open */
55 #endif /* __cplusplus */
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <sys/wait.h>
59 #include "yp_extern.h"
60 #include <rpc/rpc.h>
62 #ifndef SIG_PF
63 #define SIG_PF void(*)(int)
64 #endif
66 #define _RPCSVC_CLOSEDOWN 120
67 int _rpcpmstart; /* Started by a port monitor ? */
68 static int _rpcfdtype;
69 /* Whether Stream or Datagram ? */
70 /* States a server can be in wrt request */
72 #define _IDLE 0
73 #define _SERVED 1
74 #define _SERVING 2
76 extern void ypprog_1(struct svc_req *, SVCXPRT *);
77 extern void ypprog_2(struct svc_req *, SVCXPRT *);
78 extern int _rpc_dtablesize(void);
79 extern int _rpcsvcstate; /* Set when a request is serviced */
80 char *progname = "ypserv";
81 const char *yp_dir = _PATH_YP;
82 int debug = 0;
83 int do_dns = 0;
84 int resfd;
86 struct socktype {
87 const char *st_name;
88 int st_type;
90 static struct socktype stlist[] = {
91 { "tcp", SOCK_STREAM },
92 { "udp", SOCK_DGRAM },
93 { NULL, 0 }
96 static void
97 _msgout(char *msg)
99 if (debug) {
100 if (_rpcpmstart)
101 syslog(LOG_ERR, "%s", msg);
102 else
103 warnx("%s", msg);
104 } else
105 syslog(LOG_ERR, "%s", msg);
108 pid_t yp_pid;
110 static void
111 yp_svc_run(void)
113 #ifdef FD_SETSIZE
114 fd_set readfds;
115 #else
116 int readfds;
117 #endif /* def FD_SETSIZE */
118 int fd_setsize = _rpc_dtablesize();
119 struct timeval timeout;
121 /* Establish the identity of the parent ypserv process. */
122 yp_pid = getpid();
124 for (;;) {
125 #ifdef FD_SETSIZE
126 readfds = svc_fdset;
127 #else
128 readfds = svc_fds;
129 #endif /* def FD_SETSIZE */
131 FD_SET(resfd, &readfds);
133 timeout.tv_sec = RESOLVER_TIMEOUT;
134 timeout.tv_usec = 0;
135 switch (select(fd_setsize, &readfds, NULL, NULL,
136 &timeout)) {
137 case -1:
138 if (errno == EINTR) {
139 continue;
141 warn("svc_run: - select failed");
142 return;
143 case 0:
144 if (getpid() == yp_pid)
145 yp_prune_dnsq();
146 break;
147 default:
148 if (getpid() == yp_pid) {
149 if (FD_ISSET(resfd, &readfds)) {
150 yp_run_dnsq();
151 FD_CLR(resfd, &readfds);
153 svc_getreqset(&readfds);
156 if (yp_pid != getpid())
157 _exit(0);
161 static void
162 unregister(void)
164 pmap_unset(YPPROG, YPVERS);
165 pmap_unset(YPPROG, YPOLDVERS);
168 static void
169 reaper(int sig)
171 int status;
172 int saved_errno;
174 saved_errno = errno;
176 if (sig == SIGHUP) {
177 load_securenets();
178 #ifdef DB_CACHE
179 yp_flush_all();
180 #endif
181 errno = saved_errno;
182 return;
185 if (sig == SIGCHLD) {
186 while (wait3(&status, WNOHANG, NULL) > 0)
187 children--;
188 } else {
189 unregister();
190 exit(0);
192 errno = saved_errno;
193 return;
196 static void
197 usage(void)
199 fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path] [-P port]\n");
200 exit(1);
203 static void
204 closedown(int sig)
206 if (_rpcsvcstate == _IDLE) {
207 extern fd_set svc_fdset;
208 static int size;
209 int i, openfd;
211 if (_rpcfdtype == SOCK_DGRAM) {
212 unregister();
213 exit(0);
215 if (size == 0) {
216 size = getdtablesize();
218 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
219 if (FD_ISSET(i, &svc_fdset))
220 openfd++;
221 if (openfd <= 1) {
222 unregister();
223 exit(0);
226 if (_rpcsvcstate == _SERVED)
227 _rpcsvcstate = _IDLE;
229 signal(SIGALRM, (SIG_PF)closedown);
230 alarm(_RPCSVC_CLOSEDOWN / 2);
234 main(int argc, char *argv[])
236 SVCXPRT *transp = NULL;
237 int sock;
238 int proto = 0;
239 struct sockaddr_in saddr;
240 socklen_t asize = sizeof (saddr);
241 int ch;
242 in_port_t yp_port = 0;
243 char *errstr;
244 struct socktype *st;
246 while ((ch = getopt(argc, argv, "hdnp:P:")) != -1) {
247 switch (ch) {
248 case 'd':
249 debug = ypdb_debug = 1;
250 break;
251 case 'n':
252 do_dns = 1;
253 break;
254 case 'p':
255 yp_dir = optarg;
256 break;
257 case 'P':
258 yp_port = (in_port_t)strtonum(optarg, 1, 65535,
259 (const char **)&errstr);
260 if (yp_port == 0 && errstr != NULL) {
261 _msgout("invalid port number provided");
262 exit(1);
264 break;
265 case 'h':
266 default:
267 usage();
271 load_securenets();
272 yp_init_resolver();
273 #ifdef DB_CACHE
274 yp_init_dbs();
275 #endif
276 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
277 int ssize = sizeof (int);
279 if (saddr.sin_family != AF_INET)
280 exit(1);
281 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
282 (char *)&_rpcfdtype, &ssize) == -1)
283 exit(1);
284 sock = 0;
285 _rpcpmstart = 1;
286 proto = 0;
287 openlog("ypserv", LOG_PID, LOG_DAEMON);
288 } else {
289 if (!debug) {
290 if (daemon(0,0)) {
291 err(1,"cannot fork");
293 openlog("ypserv", LOG_PID, LOG_DAEMON);
295 sock = RPC_ANYSOCK;
296 pmap_unset(YPPROG, YPVERS);
297 pmap_unset(YPPROG, 1);
301 * Initialize TCP/UDP sockets.
303 memset((char *)&saddr, 0, sizeof(saddr));
304 saddr.sin_family = AF_INET;
305 saddr.sin_addr.s_addr = htonl(INADDR_ANY);
306 saddr.sin_port = htons(yp_port);
307 for (st = stlist; st->st_name != NULL; st++) {
308 /* Do not bind the socket if the user didn't specify a port */
309 if (yp_port == 0)
310 break;
312 sock = socket(AF_INET, st->st_type, 0);
313 if (sock == -1) {
314 if ((asprintf(&errstr, "cannot create a %s socket",
315 st->st_name)) == -1)
316 err(1, "unexpected failure in asprintf()");
317 _msgout(errstr);
318 free((void *)errstr);
319 exit(1);
321 if (bind(sock, (struct sockaddr *) &saddr, sizeof(saddr))
322 == -1) {
323 if ((asprintf(&errstr, "cannot bind %s socket",
324 st->st_name)) == -1)
325 err(1, "unexpected failure in asprintf()");
326 _msgout(errstr);
327 free((void *)errstr);
328 exit(1);
330 errstr = NULL;
333 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
334 transp = svcudp_create(sock);
335 if (transp == NULL) {
336 _msgout("cannot create udp service");
337 exit(1);
339 if (!_rpcpmstart)
340 proto = IPPROTO_UDP;
341 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
342 _msgout("unable to register (YPPROG, YPOLDVERS, udp)");
343 exit(1);
345 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
346 _msgout("unable to register (YPPROG, YPVERS, udp)");
347 exit(1);
351 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
352 transp = svctcp_create(sock, 0, 0);
353 if (transp == NULL) {
354 _msgout("cannot create tcp service");
355 exit(1);
357 if (!_rpcpmstart)
358 proto = IPPROTO_TCP;
359 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
360 _msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
361 exit(1);
363 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
364 _msgout("unable to register (YPPROG, YPVERS, tcp)");
365 exit(1);
369 if (transp == NULL) {
370 _msgout("could not create a handle");
371 exit(1);
373 if (_rpcpmstart) {
374 signal(SIGALRM, (SIG_PF)closedown);
375 alarm(_RPCSVC_CLOSEDOWN / 2);
378 * Make sure SIGPIPE doesn't blow us away while servicing TCP
379 * connections.
381 signal(SIGPIPE, SIG_IGN);
382 signal(SIGCHLD, (SIG_PF) reaper);
383 signal(SIGTERM, (SIG_PF) reaper);
384 signal(SIGINT, (SIG_PF) reaper);
385 signal(SIGHUP, (SIG_PF) reaper);
386 yp_svc_run();
387 _msgout("svc_run returned");
388 exit(1);
389 /* NOTREACHED */