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
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
32 * $FreeBSD: src/usr.sbin/ypserv/yp_main.c,v 1.21.2.3 2002/02/15 00:47:00 des Exp $
33 * $DragonFly: src/usr.sbin/ypserv/yp_main.c,v 1.4 2004/12/18 22:48:15 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.
49 #include <stdlib.h> /* getenv, exit */
50 #include <string.h> /* strcmp */
53 #include <rpc/pmap_clnt.h> /* for pmap_unset */
55 #include <sysent.h> /* getdtablesize, open */
56 #endif /* __cplusplus */
57 #include <sys/socket.h>
58 #include <netinet/in.h>
60 #include "yp_extern.h"
64 #define SIG_PF void(*)(int)
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 */
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 char *yp_dir = _PATH_YP;
92 syslog(LOG_ERR, "%s", msg);
96 syslog(LOG_ERR, "%s", msg);
108 #endif /* def FD_SETSIZE */
110 int fd_setsize = _rpc_dtablesize();
111 struct timeval timeout;
113 /* Establish the identity of the parent ypserv process. */
121 #endif /* def FD_SETSIZE */
123 FD_SET(resfd, &readfds);
125 timeout.tv_sec = RESOLVER_TIMEOUT;
127 switch (select(fd_setsize, &readfds, NULL, NULL,
130 if (errno == EINTR) {
133 warn("svc_run: - select failed");
136 if (getpid() == yp_pid)
140 if (getpid() == yp_pid) {
141 if (FD_ISSET(resfd, &readfds)) {
143 FD_CLR(resfd, &readfds);
145 svc_getreqset(&readfds);
148 if (yp_pid != getpid())
156 pmap_unset(YPPROG, YPVERS);
157 pmap_unset(YPPROG, YPOLDVERS);
160 static void reaper(int sig)
176 if (sig == SIGCHLD) {
177 while (wait3(&status, WNOHANG, NULL) > 0)
190 fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path]\n");
197 if (_rpcsvcstate == _IDLE) {
198 extern fd_set svc_fdset;
202 if (_rpcfdtype == SOCK_DGRAM) {
207 size = getdtablesize();
209 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
210 if (FD_ISSET(i, &svc_fdset))
217 if (_rpcsvcstate == _SERVED)
218 _rpcsvcstate = _IDLE;
220 signal(SIGALRM, (SIG_PF)closedown);
221 alarm(_RPCSVC_CLOSEDOWN / 2);
225 main(int argc, char **argv)
227 SVCXPRT *transp = NULL;
230 struct sockaddr_in saddr;
231 int asize = sizeof(saddr);
234 while ((ch = getopt(argc, argv, "hdnp:")) != -1) {
237 debug = ypdb_debug = 1;
256 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
257 int ssize = sizeof (int);
259 if (saddr.sin_family != AF_INET)
261 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
262 (char *)&_rpcfdtype, &ssize) == -1)
267 openlog("ypserv", LOG_PID, LOG_DAEMON);
271 err(1,"cannot fork");
273 openlog("ypserv", LOG_PID, LOG_DAEMON);
276 pmap_unset(YPPROG, YPVERS);
277 pmap_unset(YPPROG, 1);
280 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
281 transp = svcudp_create(sock);
282 if (transp == NULL) {
283 _msgout("cannot create udp service");
288 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
289 _msgout("unable to register (YPPROG, YPOLDVERS, udp)");
292 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
293 _msgout("unable to register (YPPROG, YPVERS, udp)");
298 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
299 transp = svctcp_create(sock, 0, 0);
300 if (transp == NULL) {
301 _msgout("cannot create tcp service");
306 if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
307 _msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
310 if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
311 _msgout("unable to register (YPPROG, YPVERS, tcp)");
316 if (transp == (SVCXPRT *)NULL) {
317 _msgout("could not create a handle");
321 signal(SIGALRM, (SIG_PF)closedown);
322 alarm(_RPCSVC_CLOSEDOWN / 2);
325 * Make sure SIGPIPE doesn't blow us away while servicing TCP
328 signal(SIGPIPE, SIG_IGN);
329 signal(SIGCHLD, (SIG_PF) reaper);
330 signal(SIGTERM, (SIG_PF) reaper);
331 signal(SIGINT, (SIG_PF) reaper);
332 signal(SIGHUP, (SIG_PF) reaper);
334 _msgout("svc_run returned");