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>
37 * ypserv startup function.
38 * We need out own main() since we have to do some additional work
39 * that rpcgen won't do for us. Most of this file was generated using
40 * rpcgen.new, and later modified.
43 #include <sys/types.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
55 #include <stdlib.h> /* getenv, exit */
56 #include <string.h> /* strcmp */
60 #include <sysent.h> /* getdtablesize, open */
61 #endif /* __cplusplus */
62 #include <netinet/in.h>
64 #include "yp_extern.h"
65 #include <netconfig.h>
67 #include <rpc/rpc_com.h>
70 #define SIG_PF void(*)(int)
73 #define _RPCSVC_CLOSEDOWN 120
74 int _rpcpmstart; /* Started by a port monitor ? */
75 static int _rpcfdtype; /* Whether Stream or Datagram? */
79 /* States a server can be in wrt request */
84 extern void ypprog_1(struct svc_req *, SVCXPRT *);
85 extern void ypprog_2(struct svc_req *, SVCXPRT *);
86 extern int _rpc_dtablesize(void);
87 extern int _rpcsvcstate; /* Set when a request is serviced */
88 char *progname = "ypserv";
89 char *yp_dir = _PATH_YP;
96 struct sockaddr_storage sle_ss;
97 SLIST_ENTRY(socklistent) sle_next;
99 static SLIST_HEAD(, socklistent) sle_head =
100 SLIST_HEAD_INITIALIZER(sle_head);
102 struct bindaddrlistent {
103 const char *ble_hostname;
104 SLIST_ENTRY(bindaddrlistent) ble_next;
106 static SLIST_HEAD(, bindaddrlistent) ble_head =
107 SLIST_HEAD_INITIALIZER(ble_head);
109 static char *servname = "0";
112 void _msgout(char* msg, ...)
119 vsyslog(LOG_ERR, msg, ap);
123 vsyslog(LOG_ERR, msg, ap);
136 #endif /* def FD_SETSIZE */
137 int fd_setsize = _rpc_dtablesize();
138 struct timeval timeout;
140 /* Establish the identity of the parent ypserv process. */
148 #endif /* def FD_SETSIZE */
150 FD_SET(resfd, &readfds);
152 timeout.tv_sec = RESOLVER_TIMEOUT;
154 switch (select(fd_setsize, &readfds, NULL, NULL,
157 if (errno == EINTR) {
160 warn("svc_run: - select failed");
163 if (getpid() == yp_pid)
167 if (getpid() == yp_pid) {
168 if (FD_ISSET(resfd, &readfds)) {
170 FD_CLR(resfd, &readfds);
172 svc_getreqset(&readfds);
175 if (yp_pid != getpid())
183 (void)svc_unreg(YPPROG, YPVERS);
184 (void)svc_unreg(YPPROG, YPOLDVERS);
204 if (sig == SIGCHLD) {
205 while (wait3(&status, WNOHANG, NULL) > 0)
217 fprintf(stderr, "usage: ypserv [-h addr] [-d] [-n] [-p path] [-P port]\n");
224 if (_rpcsvcstate == _IDLE) {
225 extern fd_set svc_fdset;
229 if (_rpcfdtype == SOCK_DGRAM) {
234 size = getdtablesize();
236 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
237 if (FD_ISSET(i, &svc_fdset))
244 if (_rpcsvcstate == _SERVED)
245 _rpcsvcstate = _IDLE;
247 (void) signal(SIGALRM, (SIG_PF) closedown);
248 (void) alarm(_RPCSVC_CLOSEDOWN/2);
252 create_service(const int sock, const struct netconfig *nconf,
253 const struct __rpc_sockinfo *si)
258 struct addrinfo hints, *res, *res0;
259 struct socklistent *slep;
260 struct bindaddrlistent *blep;
261 struct netbuf svcaddr;
263 SLIST_INIT(&sle_head);
264 memset(&hints, 0, sizeof(hints));
265 memset(&svcaddr, 0, sizeof(svcaddr));
267 hints.ai_family = si->si_af;
268 hints.ai_socktype = si->si_socktype;
269 hints.ai_protocol = si->si_proto;
272 * Build socketlist from bindaddrlist.
274 if (sock == RPC_ANYFD) {
275 SLIST_FOREACH(blep, &ble_head, ble_next) {
276 if (blep->ble_hostname == NULL)
277 hints.ai_flags = AI_PASSIVE;
280 error = getaddrinfo(blep->ble_hostname, servname,
283 _msgout("getaddrinfo(): %s",
284 gai_strerror(error));
287 for (res = res0; res; res = res->ai_next) {
290 s = __rpc_nconf2fd(nconf);
292 if (errno == EAFNOSUPPORT)
293 _msgout("unsupported"
297 _msgout("cannot create"
304 if (bindresvport_sa(s, res->ai_addr) == -1) {
305 if ((errno != EPERM) ||
306 (bind(s, res->ai_addr,
307 res->ai_addrlen) == -1)) {
308 _msgout("cannot bind "
317 if (nconf->nc_semantics != NC_TPI_CLTS)
318 listen(s, SOMAXCONN);
320 slep = malloc(sizeof(*slep));
322 _msgout("malloc failed: %s",
328 memset(slep, 0, sizeof(*slep));
329 memcpy(&slep->sle_ss, res->ai_addr,
332 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
335 * If servname == "0", redefine it by using
338 if (strncmp("0", servname, 1) == 0) {
339 struct sockaddr *sap;
343 sname = malloc(NI_MAXSERV);
345 _msgout("malloc(): %s",
351 memset(sname, 0, NI_MAXSERV);
353 sap = (struct sockaddr *)&slep->sle_ss;
355 error = getsockname(s, sap, &slen);
357 _msgout("getsockname(): %s",
364 error = getnameinfo(sap, slen,
367 NI_NUMERICHOST | NI_NUMERICSERV);
369 _msgout("getnameinfo(): %s",
382 slep = malloc(sizeof(*slep));
384 _msgout("malloc failed: %s", strerror(errno));
387 memset(slep, 0, sizeof(*slep));
388 slep->sle_sock = sock;
389 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
393 * Traverse socketlist and create rpc service handles for each socket.
395 SLIST_FOREACH(slep, &sle_head, sle_next) {
396 if (nconf->nc_semantics == NC_TPI_CLTS)
397 transp = svc_dg_create(slep->sle_sock, 0, 0);
399 transp = svc_vc_create(slep->sle_sock, RPC_MAXDATASIZE,
401 if (transp == NULL) {
402 _msgout("unable to create service: %s",
406 if (!svc_reg(transp, YPPROG, YPOLDVERS, ypprog_1, NULL)) {
408 close(slep->sle_sock);
409 _msgout("unable to register (YPPROG, YPOLDVERS, %s):"
410 " %s", nconf->nc_netid, strerror(errno));
413 if (!svc_reg(transp, YPPROG, YPVERS, ypprog_2, NULL)) {
415 close(slep->sle_sock);
416 _msgout("unable to register (YPPROG, YPVERS, %s): %s",
417 nconf->nc_netid, strerror(errno));
421 while(!(SLIST_EMPTY(&sle_head)))
422 SLIST_REMOVE_HEAD(&sle_head, sle_next);
425 * Register RPC service to rpcbind by using AI_PASSIVE address.
427 hints.ai_flags = AI_PASSIVE;
428 error = getaddrinfo(NULL, servname, &hints, &res0);
430 _msgout("getaddrinfo(): %s", gai_strerror(error));
433 svcaddr.buf = res0->ai_addr;
434 svcaddr.len = res0->ai_addrlen;
436 if (si->si_af == AF_INET) {
437 /* XXX: ignore error intentionally */
438 rpcb_set(YPPROG, YPOLDVERS, nconf, &svcaddr);
440 /* XXX: ignore error intentionally */
441 rpcb_set(YPPROG, YPVERS, nconf, &svcaddr);
447 main(int argc, char *argv[])
454 struct netconfig *nconf;
455 struct __rpc_sockinfo si;
456 struct bindaddrlistent *blep;
458 memset(&si, 0, sizeof(si));
459 SLIST_INIT(&ble_head);
461 while ((ch = getopt(argc, argv, "dh:np:P:")) != -1) {
464 debug = ypdb_debug = 1;
467 blep = malloc(sizeof(*blep));
469 err(1, "malloc() failed: -h %s", optarg);
470 blep->ble_hostname = optarg;
471 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
487 * Add "anyaddr" entry if no -h is specified.
489 if (SLIST_EMPTY(&ble_head)) {
490 blep = malloc(sizeof(*blep));
492 err(1, "malloc() failed");
493 memset(blep, 0, sizeof(*blep));
494 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
502 nc_handle = setnetconfig();
503 if (nc_handle == NULL)
504 err(1, "cannot read %s", NETCONFIG);
505 if (__rpc_fd2sockinfo(0, &si) != 0) {
506 /* invoked from inetd */
508 _rpcfdtype = si.si_socktype;
511 openlog("ypserv", LOG_PID, LOG_DAEMON);
513 /* standalone mode */
516 err(1,"cannot fork");
518 openlog("ypserv", LOG_PID, LOG_DAEMON);
526 if (madvise(NULL, 0, MADV_PROTECT) != 0)
527 _msgout("madvise(): %s", strerror(errno));
530 * Create RPC service for each transport.
533 while((nconf = getnetconfig(nc_handle))) {
534 if ((nconf->nc_flag & NC_VISIBLE)) {
535 if (__rpc_nconf2sockinfo(nconf, &si) == 0) {
536 _msgout("cannot get information for %s. "
537 "Ignored.", nconf->nc_netid);
541 if (si.si_socktype != _rpcfdtype ||
544 } else if (si.si_af != _rpcaf)
546 error = create_service(_rpcfd, nconf, &si);
548 endnetconfig(nc_handle);
554 endnetconfig(nc_handle);
555 while(!(SLIST_EMPTY(&ble_head)))
556 SLIST_REMOVE_HEAD(&ble_head, ble_next);
558 _msgout("no transport is available. Aborted.");
562 (void) signal(SIGALRM, (SIG_PF) closedown);
563 (void) alarm(_RPCSVC_CLOSEDOWN/2);
566 * Make sure SIGPIPE doesn't blow us away while servicing TCP
569 (void) signal(SIGPIPE, SIG_IGN);
570 (void) signal(SIGCHLD, (SIG_PF) reaper);
571 (void) signal(SIGTERM, (SIG_PF) reaper);
572 (void) signal(SIGINT, (SIG_PF) reaper);
573 (void) signal(SIGHUP, (SIG_PF) reaper);
575 _msgout("svc_run returned");