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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
41 #include <stdlib.h> /* getenv, exit */
43 #include <rpc/pmap_clnt.h> /* for pmap_unset */
44 #include <rpc/rpc_com.h>
45 #include <string.h> /* strcmp */
47 #include <sys/ttycom.h> /* TIOCNOTTY */
49 #include <sysent.h> /* getdtablesize, open */
50 #endif /* __cplusplus */
52 #include <sys/socket.h>
53 #include <netinet/in.h>
55 #include "ypxfrd_extern.h"
60 #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 int _rpcsvcstate
; /* Set when a request is serviced */
79 char *progname
= "rpc.ypxfrd";
80 char *yp_dir
= "/var/yp/";
87 syslog(LOG_ERR
, "%s", msg
);
91 syslog(LOG_ERR
, "%s", msg
);
98 if (_rpcsvcstate
== _IDLE
) {
99 extern fd_set svc_fdset
;
103 if (_rpcfdtype
== SOCK_DGRAM
)
106 size
= getdtablesize();
108 for (i
= 0, openfd
= 0; i
< size
&& openfd
< 2; i
++)
109 if (FD_ISSET(i
, &svc_fdset
))
114 if (_rpcsvcstate
== _SERVED
)
115 _rpcsvcstate
= _IDLE
;
117 (void) signal(SIGALRM
, (SIG_PF
) closedown
);
118 (void) alarm(_RPCSVC_CLOSEDOWN
/2);
129 #endif /* def FD_SETSIZE */
132 int fd_setsize
= _rpc_dtablesize();
134 /* Establish the identity of the parent ypserv 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(int sig
)
174 if (sig
== SIGCHLD
) {
175 while (wait3(&status
, WNOHANG
, NULL
) > 0)
178 (void) pmap_unset(YPXFRD_FREEBSD_PROG
, YPXFRD_FREEBSD_VERS
);
189 fprintf(stderr
, "usage: rpc.ypxfrd [-p path]\n");
194 main(int argc
, char *argv
[])
196 register SVCXPRT
*transp
= NULL
;
199 struct sockaddr_in saddr
;
200 int asize
= sizeof (saddr
);
203 while ((ch
= getopt(argc
, argv
, "p:h")) != -1) {
216 if (getsockname(0, (struct sockaddr
*)&saddr
, &asize
) == 0) {
217 int ssize
= sizeof (int);
219 if (saddr
.sin_family
!= AF_INET
)
221 if (getsockopt(0, SOL_SOCKET
, SO_TYPE
,
222 (char *)&_rpcfdtype
, &ssize
) == -1)
227 openlog("rpc.ypxfrd", LOG_PID
, LOG_DAEMON
);
238 size
= getdtablesize();
239 for (i
= 0; i
< size
; i
++)
241 i
= open(_PATH_CONSOLE
, 2);
244 i
= open(_PATH_TTY
, 2);
246 (void) ioctl(i
, TIOCNOTTY
, (char *)NULL
);
249 openlog("rpc.ypxfrd", LOG_PID
, LOG_DAEMON
);
252 (void) pmap_unset(YPXFRD_FREEBSD_PROG
, YPXFRD_FREEBSD_VERS
);
255 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_DGRAM
)) {
256 transp
= svcudp_create(sock
);
257 if (transp
== NULL
) {
258 _msgout("cannot create udp service.");
263 if (!svc_register(transp
, YPXFRD_FREEBSD_PROG
, YPXFRD_FREEBSD_VERS
, ypxfrd_freebsd_prog_1
, proto
)) {
264 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
269 if ((_rpcfdtype
== 0) || (_rpcfdtype
== SOCK_STREAM
)) {
270 transp
= svctcp_create(sock
, 0, 0);
271 if (transp
== NULL
) {
272 _msgout("cannot create tcp service.");
277 if (!svc_register(transp
, YPXFRD_FREEBSD_PROG
, YPXFRD_FREEBSD_VERS
, ypxfrd_freebsd_prog_1
, proto
)) {
278 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp).");
283 if (transp
== (SVCXPRT
*)NULL
) {
284 _msgout("could not create a handle");
288 (void) signal(SIGALRM
, (SIG_PF
) closedown
);
289 (void) alarm(_RPCSVC_CLOSEDOWN
/2);
292 (void) signal(SIGPIPE
, SIG_IGN
);
293 (void) signal(SIGCHLD
, (SIG_PF
) reaper
);
294 (void) signal(SIGTERM
, (SIG_PF
) reaper
);
295 (void) signal(SIGINT
, (SIG_PF
) reaper
);
296 (void) signal(SIGHUP
, (SIG_PF
) reaper
);
299 _msgout("svc_run returned");