2 * Copyright (c) 1995, 1996
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/rpc.ypupdated/ypupdated_main.c,v 1.4.2.3 2002/02/15 00:46:58 des Exp $
33 * $DragonFly: src/usr.sbin/rpc.ypupdated/ypupdated_main.c,v 1.5 2005/11/25 00:32:49 swildner Exp $
36 #include "ypupdate_prot.h"
38 #include <stdlib.h> /* getenv, exit */
39 #include <rpc/pmap_clnt.h> /* for pmap_unset */
40 #include <string.h> /* strcmp */
43 #include <sysent.h> /* getdtablesize, open */
44 #endif /* __cplusplus */
46 #include <sys/socket.h>
47 #include <netinet/in.h>
54 #include "ypupdated_extern.h"
55 #include "yp_extern.h"
58 #define SIG_PF void(*)(int)
65 #define _RPCSVC_CLOSEDOWN 120
66 int _rpcpmstart; /* Started by a port monitor ? */
67 static int _rpcfdtype;
68 /* Whether Stream or Datagram ? */
69 /* States a server can be in wrt request */
75 extern int _rpcsvcstate; /* Set when a request is serviced */
77 char *progname = "rpc.ypupdated";
78 char *yp_dir = "/var/yp/";
85 syslog(LOG_ERR, "%s", msg);
89 syslog(LOG_ERR, "%s", msg);
96 if (_rpcsvcstate == _IDLE) {
97 extern fd_set svc_fdset;
101 if (_rpcfdtype == SOCK_DGRAM)
104 size = getdtablesize();
106 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
107 if (FD_ISSET(i, &svc_fdset))
112 if (_rpcsvcstate == _SERVED)
113 _rpcsvcstate = _IDLE;
115 signal(SIGALRM, (SIG_PF) closedown);
116 alarm(_RPCSVC_CLOSEDOWN/2);
120 ypupdated_svc_run(void)
126 #endif /* def FD_SETSIZE */
129 int fd_setsize = _rpc_dtablesize();
131 /* Establish the identity of the parent ypupdated process. */
139 #endif /* def FD_SETSIZE */
140 switch (select(fd_setsize, &readfds, NULL, NULL,
141 (struct timeval *)0)) {
143 if (errno == EINTR) {
146 warn("svc_run: - select failed");
151 svc_getreqset(&readfds);
152 if (forked && pid != getpid())
170 if (sig == SIGCHLD) {
171 while (wait3(&status, WNOHANG, NULL) > 0)
174 pmap_unset(YPU_PROG, YPU_VERS);
182 fprintf(stderr, "rpc.ypupdatedd [-p path]\n");
187 main(int argc, char *argv[])
189 SVCXPRT *transp = NULL;
192 struct sockaddr_in saddr;
193 int asize = sizeof (saddr);
196 while ((ch = getopt(argc, argv, "p:h")) != -1) {
210 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) {
211 yp_error("failed to register AUTH_DES flavor");
215 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
216 int ssize = sizeof (int);
218 if (saddr.sin_family != AF_INET)
220 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
221 (char *)&_rpcfdtype, &ssize) == -1)
226 openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON);
230 err(1, "cannot fork");
232 openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON);
235 pmap_unset(YPU_PROG, YPU_VERS);
238 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
239 transp = svcudp_create(sock);
240 if (transp == NULL) {
241 _msgout("cannot create udp service.");
246 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
247 _msgout("unable to register (YPU_PROG, YPU_VERS, udp).");
252 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
253 transp = svctcp_create(sock, 0, 0);
254 if (transp == NULL) {
255 _msgout("cannot create tcp service.");
260 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
261 _msgout("unable to register (YPU_PROG, YPU_VERS, tcp).");
266 if (transp == (SVCXPRT *)NULL) {
267 _msgout("could not create a handle");
271 signal(SIGALRM, (SIG_PF) closedown);
272 alarm(_RPCSVC_CLOSEDOWN/2);
275 signal(SIGPIPE, SIG_IGN);
276 signal(SIGCHLD, (SIG_PF) reaper);
277 signal(SIGTERM, (SIG_PF) reaper);
278 signal(SIGINT, (SIG_PF) reaper);
279 signal(SIGHUP, (SIG_PF) reaper);
282 _msgout("svc_run returned");