2 * SPDX-License-Identifier: BSD-4-Clause
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$");
39 * ypserv startup function.
40 * We need out own main() since we have to do some additional work
41 * that rpcgen won't do for us. Most of this file was generated using
42 * rpcgen.new, and later modified.
45 #include <sys/types.h>
47 #include <sys/queue.h>
48 #include <sys/socket.h>
57 #include <stdlib.h> /* getenv, exit */
58 #include <string.h> /* strcmp */
62 #include <sysent.h> /* getdtablesize, open */
63 #endif /* __cplusplus */
64 #include <netinet/in.h>
66 #include "yp_extern.h"
67 #include <netconfig.h>
69 #include <rpc/rpc_com.h>
72 #define SIG_PF void(*)(int)
75 #define _RPCSVC_CLOSEDOWN 120
76 int _rpcpmstart; /* Started by a port monitor ? */
77 static int _rpcfdtype; /* Whether Stream or Datagram? */
81 /* States a server can be in wrt request */
86 extern void ypprog_1(struct svc_req *, SVCXPRT *);
87 extern void ypprog_2(struct svc_req *, SVCXPRT *);
88 extern int _rpc_dtablesize(void);
89 extern int _rpcsvcstate; /* Set when a request is serviced */
90 char *progname = "ypserv";
91 char *yp_dir = _PATH_YP;
98 struct sockaddr_storage sle_ss;
99 SLIST_ENTRY(socklistent) sle_next;
101 static SLIST_HEAD(, socklistent) sle_head =
102 SLIST_HEAD_INITIALIZER(sle_head);
104 struct bindaddrlistent {
105 const char *ble_hostname;
106 SLIST_ENTRY(bindaddrlistent) ble_next;
108 static SLIST_HEAD(, bindaddrlistent) ble_head =
109 SLIST_HEAD_INITIALIZER(ble_head);
111 static char *servname = "0";
114 void _msgout(char* msg, ...)
121 vsyslog(LOG_ERR, msg, ap);
125 vsyslog(LOG_ERR, msg, ap);
138 #endif /* def FD_SETSIZE */
139 int fd_setsize = _rpc_dtablesize();
140 struct timeval timeout;
142 /* Establish the identity of the parent ypserv process. */
150 #endif /* def FD_SETSIZE */
152 FD_SET(resfd, &readfds);
154 timeout.tv_sec = RESOLVER_TIMEOUT;
156 switch (select(fd_setsize, &readfds, NULL, NULL,
159 if (errno == EINTR) {
162 warn("svc_run: - select failed");
165 if (getpid() == yp_pid)
169 if (getpid() == yp_pid) {
170 if (FD_ISSET(resfd, &readfds)) {
172 FD_CLR(resfd, &readfds);
174 svc_getreqset(&readfds);
177 if (yp_pid != getpid())
185 (void)svc_unreg(YPPROG, YPVERS);
186 (void)svc_unreg(YPPROG, YPOLDVERS);
206 if (sig == SIGCHLD) {
207 while (wait3(&status, WNOHANG, NULL) > 0)
220 fprintf(stderr, "usage: ypserv [-h addr] [-d] [-n] [-p path] [-P port]\n");
227 if (_rpcsvcstate == _IDLE) {
228 extern fd_set svc_fdset;
232 if (_rpcfdtype == SOCK_DGRAM) {
237 size = getdtablesize();
239 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
240 if (FD_ISSET(i, &svc_fdset))
247 if (_rpcsvcstate == _SERVED)
248 _rpcsvcstate = _IDLE;
250 (void) signal(SIGALRM, (SIG_PF) closedown);
251 (void) alarm(_RPCSVC_CLOSEDOWN/2);
255 create_service(const int sock, const struct netconfig *nconf,
256 const struct __rpc_sockinfo *si)
261 struct addrinfo hints, *res, *res0;
262 struct socklistent *slep;
263 struct bindaddrlistent *blep;
264 struct netbuf svcaddr;
266 SLIST_INIT(&sle_head);
267 memset(&hints, 0, sizeof(hints));
268 memset(&svcaddr, 0, sizeof(svcaddr));
270 hints.ai_family = si->si_af;
271 hints.ai_socktype = si->si_socktype;
272 hints.ai_protocol = si->si_proto;
275 * Build socketlist from bindaddrlist.
277 if (sock == RPC_ANYFD) {
278 SLIST_FOREACH(blep, &ble_head, ble_next) {
279 if (blep->ble_hostname == NULL)
280 hints.ai_flags = AI_PASSIVE;
283 error = getaddrinfo(blep->ble_hostname, servname,
286 _msgout("getaddrinfo(): %s",
287 gai_strerror(error));
290 for (res = res0; res; res = res->ai_next) {
293 s = __rpc_nconf2fd(nconf);
295 if (errno == EAFNOSUPPORT)
296 _msgout("unsupported"
300 _msgout("cannot create"
307 if (bindresvport_sa(s, res->ai_addr) == -1) {
308 if ((errno != EPERM) ||
309 (bind(s, res->ai_addr,
310 res->ai_addrlen) == -1)) {
311 _msgout("cannot bind "
320 if (nconf->nc_semantics != NC_TPI_CLTS)
321 listen(s, SOMAXCONN);
323 slep = malloc(sizeof(*slep));
325 _msgout("malloc failed: %s",
331 memset(slep, 0, sizeof(*slep));
332 memcpy(&slep->sle_ss, res->ai_addr,
335 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
338 * If servname == "0", redefine it by using
341 if (strncmp("0", servname, 1) == 0) {
342 struct sockaddr *sap;
346 sname = malloc(NI_MAXSERV);
348 _msgout("malloc(): %s",
354 memset(sname, 0, NI_MAXSERV);
356 sap = (struct sockaddr *)&slep->sle_ss;
358 error = getsockname(s, sap, &slen);
360 _msgout("getsockname(): %s",
367 error = getnameinfo(sap, slen,
370 NI_NUMERICHOST | NI_NUMERICSERV);
372 _msgout("getnameinfo(): %s",
385 slep = malloc(sizeof(*slep));
387 _msgout("malloc failed: %s", strerror(errno));
390 memset(slep, 0, sizeof(*slep));
391 slep->sle_sock = sock;
392 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
396 * Traverse socketlist and create rpc service handles for each socket.
398 SLIST_FOREACH(slep, &sle_head, sle_next) {
399 if (nconf->nc_semantics == NC_TPI_CLTS)
400 transp = svc_dg_create(slep->sle_sock, 0, 0);
402 transp = svc_vc_create(slep->sle_sock, RPC_MAXDATASIZE,
404 if (transp == NULL) {
405 _msgout("unable to create service: %s",
409 if (!svc_reg(transp, YPPROG, YPOLDVERS, ypprog_1, NULL)) {
411 close(slep->sle_sock);
412 _msgout("unable to register (YPPROG, YPOLDVERS, %s):"
413 " %s", nconf->nc_netid, strerror(errno));
416 if (!svc_reg(transp, YPPROG, YPVERS, ypprog_2, NULL)) {
418 close(slep->sle_sock);
419 _msgout("unable to register (YPPROG, YPVERS, %s): %s",
420 nconf->nc_netid, strerror(errno));
424 while(!(SLIST_EMPTY(&sle_head)))
425 SLIST_REMOVE_HEAD(&sle_head, sle_next);
428 * Register RPC service to rpcbind by using AI_PASSIVE address.
430 hints.ai_flags = AI_PASSIVE;
431 error = getaddrinfo(NULL, servname, &hints, &res0);
433 _msgout("getaddrinfo(): %s", gai_strerror(error));
436 svcaddr.buf = res0->ai_addr;
437 svcaddr.len = res0->ai_addrlen;
439 if (si->si_af == AF_INET) {
440 /* XXX: ignore error intentionally */
441 rpcb_set(YPPROG, YPOLDVERS, nconf, &svcaddr);
443 /* XXX: ignore error intentionally */
444 rpcb_set(YPPROG, YPVERS, nconf, &svcaddr);
450 main(int argc, char *argv[])
457 struct netconfig *nconf;
458 struct __rpc_sockinfo si;
459 struct bindaddrlistent *blep;
461 memset(&si, 0, sizeof(si));
462 SLIST_INIT(&ble_head);
464 while ((ch = getopt(argc, argv, "dh:np:P:")) != -1) {
467 debug = ypdb_debug = 1;
470 blep = malloc(sizeof(*blep));
472 err(1, "malloc() failed: -h %s", optarg);
473 blep->ble_hostname = optarg;
474 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
490 * Add "anyaddr" entry if no -h is specified.
492 if (SLIST_EMPTY(&ble_head)) {
493 blep = malloc(sizeof(*blep));
495 err(1, "malloc() failed");
496 memset(blep, 0, sizeof(*blep));
497 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
505 nc_handle = setnetconfig();
506 if (nc_handle == NULL)
507 err(1, "cannot read %s", NETCONFIG);
508 if (__rpc_fd2sockinfo(0, &si) != 0) {
509 /* invoked from inetd */
511 _rpcfdtype = si.si_socktype;
514 openlog("ypserv", LOG_PID, LOG_DAEMON);
516 /* standalone mode */
519 err(1,"cannot fork");
521 openlog("ypserv", LOG_PID, LOG_DAEMON);
529 if (madvise(NULL, 0, MADV_PROTECT) != 0)
530 _msgout("madvise(): %s", strerror(errno));
533 * Create RPC service for each transport.
536 while((nconf = getnetconfig(nc_handle))) {
537 if ((nconf->nc_flag & NC_VISIBLE)) {
538 if (__rpc_nconf2sockinfo(nconf, &si) == 0) {
539 _msgout("cannot get information for %s. "
540 "Ignored.", nconf->nc_netid);
544 if (si.si_socktype != _rpcfdtype ||
547 } else if (si.si_af != _rpcaf)
549 error = create_service(_rpcfd, nconf, &si);
551 endnetconfig(nc_handle);
557 endnetconfig(nc_handle);
558 while(!(SLIST_EMPTY(&ble_head)))
559 SLIST_REMOVE_HEAD(&ble_head, ble_next);
561 _msgout("no transport is available. Aborted.");
565 (void) signal(SIGALRM, (SIG_PF) closedown);
566 (void) alarm(_RPCSVC_CLOSEDOWN/2);
569 * Make sure SIGPIPE doesn't blow us away while servicing TCP
572 (void) signal(SIGPIPE, SIG_IGN);
573 (void) signal(SIGCHLD, (SIG_PF) reaper);
574 (void) signal(SIGTERM, (SIG_PF) reaper);
575 (void) signal(SIGINT, (SIG_PF) reaper);
576 (void) signal(SIGHUP, (SIG_PF) reaper);
578 _msgout("svc_run returned");