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$");
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>
44 #include <sys/queue.h>
45 #include <sys/socket.h>
54 #include <stdlib.h> /* getenv, exit */
55 #include <string.h> /* strcmp */
59 #include <sysent.h> /* getdtablesize, open */
60 #endif /* __cplusplus */
61 #include <netinet/in.h>
63 #include "yp_extern.h"
64 #include <netconfig.h>
66 #include <rpc/rpc_com.h>
69 #define SIG_PF void(*)(int)
72 #define _RPCSVC_CLOSEDOWN 120
73 int _rpcpmstart; /* Started by a port monitor ? */
74 static int _rpcfdtype;
75 /* Whether Stream or Datagram ? */
79 /* States a server can be in wrt request */
85 extern void ypprog_1(struct svc_req *, SVCXPRT *);
86 extern void ypprog_2(struct svc_req *, SVCXPRT *);
87 extern int _rpc_dtablesize(void);
88 extern int _rpcsvcstate; /* Set when a request is serviced */
89 char *progname = "ypserv";
90 char *yp_dir = _PATH_YP;
97 struct sockaddr_storage sle_ss;
98 SLIST_ENTRY(socklistent) sle_next;
100 static SLIST_HEAD(, socklistent) sle_head =
101 SLIST_HEAD_INITIALIZER(sle_head);
103 struct bindaddrlistent {
104 const char *ble_hostname;
105 SLIST_ENTRY(bindaddrlistent) ble_next;
107 static SLIST_HEAD(, bindaddrlistent) ble_head =
108 SLIST_HEAD_INITIALIZER(ble_head);
110 static char *servname = "0";
113 void _msgout(char* msg, ...)
120 vsyslog(LOG_ERR, msg, ap);
124 vsyslog(LOG_ERR, msg, ap);
137 #endif /* def FD_SETSIZE */
138 int fd_setsize = _rpc_dtablesize();
139 struct timeval timeout;
141 /* Establish the identity of the parent ypserv process. */
149 #endif /* def FD_SETSIZE */
151 FD_SET(resfd, &readfds);
153 timeout.tv_sec = RESOLVER_TIMEOUT;
155 switch (select(fd_setsize, &readfds, NULL, NULL,
158 if (errno == EINTR) {
161 warn("svc_run: - select failed");
164 if (getpid() == yp_pid)
168 if (getpid() == yp_pid) {
169 if (FD_ISSET(resfd, &readfds)) {
171 FD_CLR(resfd, &readfds);
173 svc_getreqset(&readfds);
176 if (yp_pid != getpid())
184 (void)svc_unreg(YPPROG, YPVERS);
185 (void)svc_unreg(YPPROG, YPOLDVERS);
205 if (sig == SIGCHLD) {
206 while (wait3(&status, WNOHANG, NULL) > 0)
219 fprintf(stderr, "usage: ypserv [-h addr] [-d] [-n] [-p path] [-P port]\n");
226 if (_rpcsvcstate == _IDLE) {
227 extern fd_set svc_fdset;
231 if (_rpcfdtype == SOCK_DGRAM) {
236 size = getdtablesize();
238 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
239 if (FD_ISSET(i, &svc_fdset))
246 if (_rpcsvcstate == _SERVED)
247 _rpcsvcstate = _IDLE;
249 (void) signal(SIGALRM, (SIG_PF) closedown);
250 (void) alarm(_RPCSVC_CLOSEDOWN/2);
254 create_service(const int sock, const struct netconfig *nconf,
255 const struct __rpc_sockinfo *si)
260 struct addrinfo hints, *res, *res0;
261 struct socklistent *slep;
262 struct bindaddrlistent *blep;
263 struct netbuf svcaddr;
265 SLIST_INIT(&sle_head);
266 memset(&hints, 0, sizeof(hints));
267 memset(&svcaddr, 0, sizeof(svcaddr));
269 hints.ai_family = si->si_af;
270 hints.ai_socktype = si->si_socktype;
271 hints.ai_protocol = si->si_proto;
274 * Build socketlist from bindaddrlist.
276 if (sock == RPC_ANYFD) {
277 SLIST_FOREACH(blep, &ble_head, ble_next) {
278 if (blep->ble_hostname == NULL)
279 hints.ai_flags = AI_PASSIVE;
282 error = getaddrinfo(blep->ble_hostname, servname,
285 _msgout("getaddrinfo(): %s",
286 gai_strerror(error));
289 for (res = res0; res; res = res->ai_next) {
292 s = __rpc_nconf2fd(nconf);
294 if (errno == EPROTONOSUPPORT)
295 _msgout("unsupported"
299 _msgout("cannot create"
306 if (bindresvport_sa(s, res->ai_addr) == -1) {
307 if ((errno != EPERM) ||
308 (bind(s, res->ai_addr,
309 res->ai_addrlen) == -1)) {
310 _msgout("cannot bind "
319 if (nconf->nc_semantics != NC_TPI_CLTS)
320 listen(s, SOMAXCONN);
322 slep = malloc(sizeof(*slep));
324 _msgout("malloc failed: %s",
330 memset(slep, 0, sizeof(*slep));
331 memcpy(&slep->sle_ss,
332 (struct sockaddr *)(res->ai_addr),
333 sizeof(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",
366 error = getnameinfo(sap, slen,
369 NI_NUMERICHOST | NI_NUMERICSERV);
371 _msgout("getnameinfo(): %s",
383 slep = malloc(sizeof(*slep));
385 _msgout("malloc failed: %s", strerror(errno));
388 memset(slep, 0, sizeof(*slep));
389 slep->sle_sock = sock;
390 SLIST_INSERT_HEAD(&sle_head, slep, sle_next);
394 * Traverse socketlist and create rpc service handles for each socket.
396 SLIST_FOREACH(slep, &sle_head, sle_next) {
397 if (nconf->nc_semantics == NC_TPI_CLTS)
398 transp = svc_dg_create(slep->sle_sock, 0, 0);
400 transp = svc_vc_create(slep->sle_sock, RPC_MAXDATASIZE,
402 if (transp == NULL) {
403 _msgout("unable to create service: %s",
407 if (!svc_reg(transp, YPPROG, YPOLDVERS, ypprog_1, NULL)) {
409 close(slep->sle_sock);
410 _msgout("unable to register (YPPROG, YPOLDVERS, %s):"
411 " %s", nconf->nc_netid, strerror(errno));
414 if (!svc_reg(transp, YPPROG, YPVERS, ypprog_2, NULL)) {
416 close(slep->sle_sock);
417 _msgout("unable to register (YPPROG, YPVERS, %s): %s",
418 nconf->nc_netid, strerror(errno));
422 while(!(SLIST_EMPTY(&sle_head)))
423 SLIST_REMOVE_HEAD(&sle_head, sle_next);
426 * Register RPC service to rpcbind by using AI_PASSIVE address.
428 hints.ai_flags = AI_PASSIVE;
429 error = getaddrinfo(NULL, servname, &hints, &res0);
431 _msgout("getaddrinfo(): %s", gai_strerror(error));
434 svcaddr.buf = res0->ai_addr;
435 svcaddr.len = res0->ai_addrlen;
437 if (si->si_af == AF_INET) {
438 /* XXX: ignore error intentionally */
439 rpcb_set(YPPROG, YPOLDVERS, nconf, &svcaddr);
441 /* XXX: ignore error intentionally */
442 rpcb_set(YPPROG, YPVERS, nconf, &svcaddr);
449 main(int argc, char *argv[])
456 struct netconfig *nconf;
457 struct __rpc_sockinfo si;
458 struct bindaddrlistent *blep;
460 memset(&si, 0, sizeof(si));
461 SLIST_INIT(&ble_head);
463 while ((ch = getopt(argc, argv, "dh:np:P:")) != -1) {
466 debug = ypdb_debug = 1;
469 blep = malloc(sizeof(*blep));
471 err(1, "malloc() failed: -h %s", optarg);
472 blep->ble_hostname = optarg;
473 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
489 * Add "anyaddr" entry if no -h is specified.
491 if (SLIST_EMPTY(&ble_head)) {
492 blep = malloc(sizeof(*blep));
494 err(1, "malloc() failed");
495 memset(blep, 0, sizeof(*blep));
496 SLIST_INSERT_HEAD(&ble_head, blep, ble_next);
504 nc_handle = setnetconfig();
505 if (nc_handle == NULL)
506 err(1, "cannot read %s", NETCONFIG);
507 if (__rpc_fd2sockinfo(0, &si) != 0) {
508 /* invoked from inetd */
510 _rpcfdtype = si.si_socktype;
513 openlog("ypserv", LOG_PID, LOG_DAEMON);
515 /* standalone mode */
518 err(1,"cannot fork");
520 openlog("ypserv", LOG_PID, LOG_DAEMON);
529 * Create RPC service for each transport.
532 while((nconf = getnetconfig(nc_handle))) {
533 if ((nconf->nc_flag & NC_VISIBLE)) {
534 if (__rpc_nconf2sockinfo(nconf, &si) == 0) {
535 _msgout("cannot get information for %s. "
536 "Ignored.", nconf->nc_netid);
540 if (si.si_socktype != _rpcfdtype ||
543 } else if (si.si_af != _rpcaf)
545 error = create_service(_rpcfd, nconf, &si);
547 endnetconfig(nc_handle);
553 endnetconfig(nc_handle);
554 while(!(SLIST_EMPTY(&ble_head)))
555 SLIST_REMOVE_HEAD(&ble_head, ble_next);
557 _msgout("no transport is available. Aborted.");
561 (void) signal(SIGALRM, (SIG_PF) closedown);
562 (void) alarm(_RPCSVC_CLOSEDOWN/2);
565 * Make sure SIGPIPE doesn't blow us away while servicing TCP
568 (void) signal(SIGPIPE, SIG_IGN);
569 (void) signal(SIGCHLD, (SIG_PF) reaper);
570 (void) signal(SIGTERM, (SIG_PF) reaper);
571 (void) signal(SIGINT, (SIG_PF) reaper);
572 (void) signal(SIGHUP, (SIG_PF) reaper);
574 _msgout("svc_run returned");