2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1995, 1996
5 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
43 #include <stdlib.h> /* getenv, exit */
45 #include <rpc/pmap_clnt.h> /* for pmap_unset */
46 #include <rpc/rpc_com.h>
47 #include <string.h> /* strcmp */
49 #include <sys/ttycom.h> /* TIOCNOTTY */
51 #include <sysent.h> /* getdtablesize, open */
52 #endif /* __cplusplus */
54 #include <sys/socket.h>
55 #include <netinet/in.h>
57 #include "ypxfrd_extern.h"
62 #define SIG_PF void(*)(int)
69 #define _RPCSVC_CLOSEDOWN 120
70 int _rpcpmstart; /* Started by a port monitor ? */
71 static int _rpcfdtype;
72 /* Whether Stream or Datagram ? */
73 /* States a server can be in wrt request */
79 extern int _rpcsvcstate; /* Set when a request is serviced */
83 char *progname = "rpc.ypxfrd";
84 char *yp_dir = "/var/yp/";
91 syslog(LOG_ERR, "%s", msg);
95 syslog(LOG_ERR, "%s", msg);
102 if (_rpcsvcstate == _IDLE) {
103 extern fd_set svc_fdset;
107 if (_rpcfdtype == SOCK_DGRAM)
110 size = getdtablesize();
112 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
113 if (FD_ISSET(i, &svc_fdset))
118 if (_rpcsvcstate == _SERVED)
119 _rpcsvcstate = _IDLE;
121 (void) signal(SIGALRM, (SIG_PF) closedown);
122 (void) alarm(_RPCSVC_CLOSEDOWN/2);
133 #endif /* def FD_SETSIZE */
136 int fd_setsize = _rpc_dtablesize();
138 /* Establish the identity of the parent ypserv process. */
146 #endif /* def FD_SETSIZE */
147 switch (select(fd_setsize, &readfds, NULL, NULL,
148 (struct timeval *)0)) {
150 if (errno == EINTR) {
153 warn("svc_run: - select failed");
158 svc_getreqset(&readfds);
159 if (forked && pid != getpid())
165 static void reaper(int sig)
178 if (sig == SIGCHLD) {
179 while (wait3(&status, WNOHANG, NULL) > 0)
182 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
193 fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n");
198 main(int argc, char *argv[])
200 register SVCXPRT *transp = NULL;
203 struct sockaddr_in saddr;
204 int asize = sizeof (saddr);
207 while ((ch = getopt(argc, argv, "p:h")) != -1) {
220 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
221 int ssize = sizeof (int);
223 if (saddr.sin_family != AF_INET)
225 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
226 (char *)&_rpcfdtype, &ssize) == -1)
231 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
242 size = getdtablesize();
243 for (i = 0; i < size; i++)
245 i = open(_PATH_CONSOLE, 2);
248 i = open(_PATH_TTY, 2);
250 (void) ioctl(i, TIOCNOTTY, (char *)NULL);
253 openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
256 (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
259 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
260 transp = svcudp_create(sock);
261 if (transp == NULL) {
262 _msgout("cannot create udp service.");
267 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
268 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
273 if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
274 transp = svctcp_create(sock, 0, 0);
275 if (transp == NULL) {
276 _msgout("cannot create tcp service.");
281 if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
282 _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp).");
287 if (transp == (SVCXPRT *)NULL) {
288 _msgout("could not create a handle");
292 (void) signal(SIGALRM, (SIG_PF) closedown);
293 (void) alarm(_RPCSVC_CLOSEDOWN/2);
296 (void) signal(SIGPIPE, SIG_IGN);
297 (void) signal(SIGCHLD, (SIG_PF) reaper);
298 (void) signal(SIGTERM, (SIG_PF) reaper);
299 (void) signal(SIGINT, (SIG_PF) reaper);
300 (void) signal(SIGHUP, (SIG_PF) reaper);
303 _msgout("svc_run returned");