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
34 static const char rcsid[] =
38 #include "ypupdate_prot.h"
40 #include <stdlib.h> /* getenv, exit */
41 #include <rpc/pmap_clnt.h> /* for pmap_unset */
42 #include <string.h> /* strcmp */
44 #include <sys/ttycom.h> /* TIOCNOTTY */
46 #include <sysent.h> /* getdtablesize, open */
47 #endif /* __cplusplus */
49 #include <sys/socket.h>
50 #include <netinet/in.h>
57 #include "ypupdated_extern.h"
58 #include "yp_extern.h"
61 #define SIG_PF void(*)(int)
68 #define _RPCSVC_CLOSEDOWN 120
69 int _rpcpmstart; /* Started by a port monitor ? */
70 static int _rpcfdtype;
71 /* Whether Stream or Datagram ? */
72 /* States a server can be in wrt request */
78 extern int _rpcsvcstate; /* Set when a request is serviced */
80 char *progname = "rpc.ypupdated";
81 char *yp_dir = "/var/yp/";
84 void _msgout(char* msg)
99 if (_rpcsvcstate == _IDLE) {
100 extern fd_set svc_fdset;
104 if (_rpcfdtype == SOCK_DGRAM)
107 size = getdtablesize();
109 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
110 if (FD_ISSET(i, &svc_fdset))
115 if (_rpcsvcstate == _SERVED)
116 _rpcsvcstate = _IDLE;
118 (void) signal(SIGALRM, (SIG_PF) closedown);
119 (void) alarm(_RPCSVC_CLOSEDOWN/2);
129 #endif /* def FD_SETSIZE */
132 int fd_setsize = _rpc_dtablesize();
134 /* Establish the identity of the parent ypupdated process. */
142 #endif /* def FD_SETSIZE */
143 switch (select(fd_setsize, &readfds, NULL, NULL,
144 (struct timeval *)0)) {
146 if (errno == EINTR) {
149 warn("svc_run: - select failed");
154 svc_getreqset(&readfds);
155 if (forked && pid != getpid())
161 static void reaper(sig)
173 if (sig == SIGCHLD) {
174 while (wait3(&status, WNOHANG, NULL) > 0)
177 (void) pmap_unset(YPU_PROG, YPU_VERS);
184 fprintf(stderr, "rpc.ypupdatedd [-p path]\n");
193 register SVCXPRT *transp = NULL;
196 struct sockaddr_in saddr;
197 int asize = sizeof (saddr);
200 while ((ch = getopt(argc, argv, "p:h")) != -1) {
214 if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) {
215 yp_error("failed to register AUTH_DES flavor");
219 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
220 int ssize = sizeof (int);
222 if (saddr.sin_family != AF_INET)
224 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
225 (char *)&_rpcfdtype, &ssize) == -1)
230 openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON);
234 err(1, "cannot fork");
236 openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON);
239 (void) pmap_unset(YPU_PROG, YPU_VERS);
242 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
243 transp = svcudp_create(sock);
244 if (transp == NULL) {
245 _msgout("cannot create udp service.");
250 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
251 _msgout("unable to register (YPU_PROG, YPU_VERS, udp).");
256 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
257 transp = svctcp_create(sock, 0, 0);
258 if (transp == NULL) {
259 _msgout("cannot create tcp service.");
264 if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) {
265 _msgout("unable to register (YPU_PROG, YPU_VERS, tcp).");
270 if (transp == (SVCXPRT *)NULL) {
271 _msgout("could not create a handle");
275 (void) signal(SIGALRM, (SIG_PF) closedown);
276 (void) alarm(_RPCSVC_CLOSEDOWN/2);
279 (void) signal(SIGPIPE, SIG_IGN);
280 (void) signal(SIGCHLD, (SIG_PF) reaper);
281 (void) signal(SIGTERM, (SIG_PF) reaper);
282 (void) signal(SIGINT, (SIG_PF) reaper);
283 (void) signal(SIGHUP, (SIG_PF) reaper);
286 _msgout("svc_run returned");